import TgBaseService from './tgbase'; import * as _ from 'lodash'; import { In } from 'typeorm'; import TgGroupType from './tggrouptype'; export default class TggroupService extends TgBaseService { /** * group list */ async list() { return await this.getRepo().admin.tg.TgGroup.find(); } /** * bot count */ async count() { return await this.getRepo().admin.tg.TgGroup.count(); } /** * 分页加载bot */ async page(page: number, count: number) { return await this.getRepo().admin.tg.TgGroup.find({ relations: [ 'tgGroupBots' ], order: { id: 'ASC', }, take: count, skip: page * count, }); } /** * bot add */ async add(param:any) { const { name, username, groupId, description, type, defaultBotId, status, tgGroupBots } = param; const bots = tgGroupBots; await this.getRepo().admin.tg.TgGroup.insert({ name, username, groupId, description, type, defaultBotId, status }); if (bots && bots.length > 0 && groupId != null) { // 关联 机器人 const insertRows = bots.map((bid: any) => { return { groupId, botId: bid, }; }); await this.getRepo().admin.tg.TgGroupBot.insert(insertRows); } return { groupId }; } async update(param:any) { const { id, name, username, groupId, description, type, defaultBotId, status, tgGroupBots } = param; const bots = tgGroupBots; const group = await this.getRepo().admin.tg.TgGroup.save({ id, name, username, groupId, description, type, defaultBotId, status }); const oribots = await this.getRepo().admin.tg.TgGroupBot.find({ groupId }); const oribotIds = oribots.map(e => { return e.botId; }); // 开始对比差异 const insertIds = _.difference(bots, oribotIds); const deleteIds = _.difference(oribotIds, bots); // using transaction if (insertIds.length || deleteIds.length) { await this.ctx.ormManager.transaction(async manager => { if (insertIds.length > 0) { // 有条目更新 const insertRows = insertIds.map(bid => { return { groupId, botId: bid, }; }); await manager.insert(this.getEntity().admin.tg.TgGroupBot, insertRows as any); } if (oribots.length > 0) { // 有条目需要删除 const realDeleteRowIds = _.filter(oribots, e => { return _.includes(deleteIds, e.botId); }).map(e => { return (e as any).id; }); await manager.delete(this.getEntity().admin.tg.TgGroupBot, realDeleteRowIds); } }); } return group; } /** * 根据数组删除 */ async delete(ids: number[]) { await this.getRepo().admin.tg.TgGroup.delete(ids); return ids; } /** * 获取消息内的所有group信息,因为无法直接获取groupid 所以需要先拉机器人进去监听chatId */ async msg_groupIds() { return await this.getRepo().admin.tg.TgMessage.createQueryBuilder('msg') .select([ 'msg.chatId AS msg_chatId', 'ANY_VALUE(msg.chatTitle) AS msg_chatTitle', // 使用 ANY_VALUE() 函数 ]) .where('msg.chatType = :type', { type: 'group' }) .groupBy('msg.chatId') // 只需要按 chatId 分组 .getRawMany(); } /** * 发送多组消息 */ async send_groups_msg(groupIds:any[], message:string) { const groups = await this.getRepo().admin.tg.TgGroup.find({ where: { groupId: In(groupIds), }, }); const success:any[] = []; const sendList:any[] = []; if (groups != null) { for (const group of groups) { const { groupId, defaultBotId, name , type} = group; if (defaultBotId != null && type !== TgGroupType.LISTEN_ONLY) { sendList.push({ groupId, botId: defaultBotId, name, text: message }); } } } const res = await this.reqTgbotApi('sendMessage', sendList); if (res) { sendList.forEach(value => { success.push({ groupId: value.groupId, botId: value.botId, name: value.name, success: true, }); }); } return success; } /** * 单群单机器人 */ async send_msg(groupId:any, botId:any, message:string) { const group = await this.getRepo().admin.tg.TgGroup.findOne({ groupId }); let success:any = false; if (group != null && group.type !== TgGroupType.LISTEN_ONLY) { success = await this.reqTgbotApi('sendMessage', [{ groupId, botId, text: message }]); } return { groupId, botId, name: group?.name, success }; } async all_botlist() { return await this.getRepo().admin.tg.TgBot.find({ select: [ 'id', 'botId', 'name' ], where: { status: 1 }, order: { id: 'ASC', }, }); } }