| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- import BaseService from '../../base';
- import TelegramBot = require('node-telegram-bot-api');
- import TgGroupType from './tggrouptype';
- /**
- * tg机器人 api 服务 只有一个进程可以用
- */
- const botListMap = new Map<number, TelegramBot>();
- export default class TgbotApiService extends BaseService {
- /**
- * 机器人进程初始化
- */
- async initTgbot() {
- await this.initTgBotList();
- return true;
- }
- /**
- * 进程通讯
- * @param cmd
- * @param data
- */
- async handleReq(cmd:string, data:any) {
- switch (cmd) {
- case 'addTgBot':
- await this.addTgBot(data);
- break;
- case 'removeTgBot':
- await this.removeTgBot(data);
- break;
- case 'sendMessage':
- await this.sendMessage(data);
- break;
- default:
- break;
- }
- return true;
- }
- /**
- * 初始化机器人监听
- */
- async initTgBotList() {
- const list = await this.getRepo().admin.tg.TgBot.find({ status: 1 });
- list.forEach(tgbot => {
- this.addTgBot(tgbot.botId, tgbot);
- });
- }
- /**
- * 添加机器人监听
- * @param botId
- * @param botInfo
- */
- async addTgBot(botId:number, botInfo:any = null) {
- if (botListMap.has(botId)) {
- return false;
- }
- if (botInfo == null) {
- botInfo = await this.getRepo().admin.tg.TgBot.findOne({ botId });
- }
- if (botInfo == null || botInfo.status === 0) return false;
- const bot = new TelegramBot(botInfo.token, { polling: true });
- botListMap.set(botId, bot);
- bot.on('message', msg => {
- this.onReciveMessage(botId, msg);
- });
- return true;
- }
- /**
- * 移除机器人监听
- * @param botId
- */
- async removeTgBot(botId:number) {
- if (!botListMap.has(botId)) {
- return false;
- }
- const bot = botListMap.get(botId);
- await bot.stopPolling();
- botListMap.delete(botId);
- return true;
- }
- /**
- * 解析msg
- * @param botId
- * @param msg
- */
- // {"message_id":83,"from":{"id":5034446407,"is_bot":false,"first_name":"xdh","username":"hdx234","language_code":"zh-hans"},"chat":{"id":-4870165498,"title":"z & xdh","type":"group","all_members_are_administrators":true,"accepted_gift_types":{"unlimited_gifts":false,"limited_gifts":false,"unique_gifts":false,"premium_subscription":false}},"date":1752997603,"text":"123452"}
- // {"message_id":85,"from":{"id":5034446407,"is_bot":false,"first_name":"xdh","username":"hdx234","language_code":"zh-hans"},"chat":{"id":5034446407,"first_name":"xdh","username":"hdx234","type":"private"},"date":1753021095,"text":"11111"}
- parseMessage(msg:any) {
- if (msg == null) return null;
- const { message_id, from, chat, date, text } = msg;
- return {
- messageId: message_id,
- chatId: chat?.id,
- chatType: chat?.type,
- chatTitle: chat?.title,
- chatName: chat?.first_name,
- chatUsername: chat?.username,
- fromUserId: from?.id,
- fromName: from?.first_name,
- fromUsername: from?.username,
- isBot: from && from.is_bot ? 1 : 0,
- sentTime: date,
- text,
- };
- }
- async recordMessage(botId:number, message:any) {
- try {
- const data = Object.assign({}, message, { botId });
- await this.getRepo().admin.tg.TgMessage.save(data);
- } catch (error) {
- console.error(error);
- }
- }
- async recordSendMessage(groupId:any, botId:any, text:any) {
- try {
- await this.getRepo().admin.tg.TgBotSendLog.save({ groupId, botId, text });
- } catch (error) {
- console.error(error);
- }
- }
- /**
- * 监听消息处理
- * @param botId
- * @param msg
- */
- async onReciveMessage(botId:number, msg:any) {
- const message = this.parseMessage(msg);
- const group = await this.getRepo().admin.tg.TgGroup.findOne({ groupId: message?.chatId });
- const groupType = group ? group.type : TgGroupType.LISTEN_ONLY;
- // 是否记录
- if (groupType === TgGroupType.LISTEN_ONLY || groupType === TgGroupType.BOTH) {
- await this.recordMessage(botId, message);
- await this.service.admin.tg.tgmsghandler.handleMessage(group, message);
- }
- }
- async sendMessage(message:any[]) {
- if (message == null) return false;
- try {
- for (const value of message) {
- const { groupId, botId, text } = value;
- if (botListMap.has(botId)) {
- const bot = botListMap.get(botId);
- bot.sendMessage(groupId, text);
- await this.recordSendMessage(groupId, botId, text);
- }
- }
- } catch (error) {
- console.error(error);
- return false;
- }
- return true;
- }
- }
|