1
0

tggroup.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import TgBaseService from './tgbase';
  2. import * as _ from 'lodash';
  3. import { In } from 'typeorm';
  4. import TgGroupType from './tggrouptype';
  5. export default class TggroupService extends TgBaseService {
  6. /**
  7. * group list
  8. */
  9. async list() {
  10. return await this.getRepo().admin.tg.TgGroup.find();
  11. }
  12. /**
  13. * bot count
  14. */
  15. async count() {
  16. return await this.getRepo().admin.tg.TgGroup.count();
  17. }
  18. /**
  19. * 分页加载bot
  20. */
  21. async page(page: number, count: number) {
  22. return await this.getRepo().admin.tg.TgGroup.find({
  23. relations: [ 'tgGroupBots' ],
  24. order: {
  25. id: 'ASC',
  26. },
  27. take: count,
  28. skip: page * count,
  29. });
  30. }
  31. /**
  32. * bot add
  33. */
  34. async add(param:any) {
  35. const { name, username, groupId, description, type, defaultBotId, status, tgGroupBots } = param;
  36. const bots = tgGroupBots;
  37. await this.getRepo().admin.tg.TgGroup.insert({ name, username, groupId, description, type, defaultBotId, status });
  38. if (bots && bots.length > 0 && groupId != null) {
  39. // 关联 机器人
  40. const insertRows = bots.map((bid: any) => {
  41. return {
  42. groupId,
  43. botId: bid,
  44. };
  45. });
  46. await this.getRepo().admin.tg.TgGroupBot.insert(insertRows);
  47. }
  48. return { groupId };
  49. }
  50. async update(param:any) {
  51. const { id, name, username, groupId, description, type, defaultBotId, status, tgGroupBots } = param;
  52. const bots = tgGroupBots;
  53. const group = await this.getRepo().admin.tg.TgGroup.save({ id, name, username, groupId, description, type, defaultBotId, status });
  54. const oribots = await this.getRepo().admin.tg.TgGroupBot.find({ groupId });
  55. const oribotIds = oribots.map(e => { return e.botId; });
  56. // 开始对比差异
  57. const insertIds = _.difference(bots, oribotIds);
  58. const deleteIds = _.difference(oribotIds, bots);
  59. // using transaction
  60. if (insertIds.length || deleteIds.length) {
  61. await this.ctx.ormManager.transaction(async manager => {
  62. if (insertIds.length > 0) {
  63. // 有条目更新
  64. const insertRows = insertIds.map(bid => {
  65. return {
  66. groupId,
  67. botId: bid,
  68. };
  69. });
  70. await manager.insert(this.getEntity().admin.tg.TgGroupBot, insertRows as any);
  71. }
  72. if (oribots.length > 0) {
  73. // 有条目需要删除
  74. const realDeleteRowIds = _.filter(oribots, e => {
  75. return _.includes(deleteIds, e.botId);
  76. }).map(e => { return (e as any).id; });
  77. await manager.delete(this.getEntity().admin.tg.TgGroupBot, realDeleteRowIds);
  78. }
  79. });
  80. }
  81. return group;
  82. }
  83. /**
  84. * 根据数组删除
  85. */
  86. async delete(ids: number[]) {
  87. await this.getRepo().admin.tg.TgGroup.delete(ids);
  88. return ids;
  89. }
  90. /**
  91. * 获取消息内的所有group信息,因为无法直接获取groupid 所以需要先拉机器人进去监听chatId
  92. */
  93. async msg_groupIds() {
  94. return await this.getRepo().admin.tg.TgMessage.createQueryBuilder('msg')
  95. .select([
  96. 'msg.chatId AS msg_chatId',
  97. 'ANY_VALUE(msg.chatTitle) AS msg_chatTitle', // 使用 ANY_VALUE() 函数
  98. ])
  99. .where('msg.chatType = :type', { type: 'group' })
  100. .groupBy('msg.chatId') // 只需要按 chatId 分组
  101. .getRawMany();
  102. }
  103. /**
  104. * 发送多组消息
  105. */
  106. async send_groups_msg(groupIds:any[], message:string) {
  107. const groups = await this.getRepo().admin.tg.TgGroup.find({
  108. where: {
  109. groupId: In(groupIds),
  110. },
  111. });
  112. const success:any[] = [];
  113. const sendList:any[] = [];
  114. if (groups != null) {
  115. for (const group of groups) {
  116. const { groupId, defaultBotId, name , type} = group;
  117. if (defaultBotId != null && type !== TgGroupType.LISTEN_ONLY) {
  118. sendList.push({ groupId, botId: defaultBotId, name, text: message });
  119. }
  120. }
  121. }
  122. const res = await this.reqTgbotApi('sendMessage', sendList);
  123. if (res) {
  124. sendList.forEach(value => {
  125. success.push({
  126. groupId: value.groupId,
  127. botId: value.botId,
  128. name: value.name,
  129. success: true,
  130. });
  131. });
  132. }
  133. return success;
  134. }
  135. /**
  136. * 单群单机器人
  137. */
  138. async send_msg(groupId:any, botId:any, message:string) {
  139. const group = await this.getRepo().admin.tg.TgGroup.findOne({ groupId });
  140. let success:any = false;
  141. if (group != null && group.type !== TgGroupType.LISTEN_ONLY) {
  142. success = await this.reqTgbotApi('sendMessage', [{ groupId, botId, text: message }]);
  143. }
  144. return { groupId, botId, name: group?.name, success };
  145. }
  146. async all_botlist() {
  147. return await this.getRepo().admin.tg.TgBot.find({
  148. select: [ 'id', 'botId', 'name' ],
  149. where: { status: 1 },
  150. order: {
  151. id: 'ASC',
  152. },
  153. });
  154. }
  155. }