diff --git a/src/modules/bot/bot.controller.ts b/src/modules/bot/bot.controller.ts index a974a29..778ddbe 100644 --- a/src/modules/bot/bot.controller.ts +++ b/src/modules/bot/bot.controller.ts @@ -19,6 +19,7 @@ class BotController { private init(): void { this.router.post(`/getBotId`, this.postBotsId); this.router.post('/getGroupInfo', this.postGroupInfo); + this.router.post('/sendMessage', this.sendMessage); } /** @@ -71,6 +72,31 @@ class BotController { await response.error(res); } }; + + /** + * 发送消息到群聊 + * @param req + * @param res + */ + private sendMessage = async (req: express.Request, res: express.Response): Promise => { + try { + const token = req.body.token; + if (tools.checkToken(token.toString())) { + const groupId: number = req.body.groupId; + const message: string = req.body.message; + const flag: boolean = await BotService.sendMessage(groupId, message); + if (flag) { + await response.success(res, {}); + } else { + await response.error(res); + } + } else { + await tools.tokenCheckFailed(res, token); + } + } catch (e) { + await response.error(res); + } + }; } export default new BotController(); diff --git a/src/modules/bot/bot.service.ts b/src/modules/bot/bot.service.ts index 35479e9..652ef15 100644 --- a/src/modules/bot/bot.service.ts +++ b/src/modules/bot/bot.service.ts @@ -86,6 +86,38 @@ class BotService { return undefined; } + /** + * 发送消息到群聊 + * @param groupId 群号 + * @param message 消息 + */ + public async sendMessage(groupId: number, message: string): Promise { + logger.debug('sendGroupMessage..'); + const sendBot: number | undefined = await this.getGroupBot(groupId); + if (!sendBot) { + logger.warn(`不存在能向群聊${groupId}发送消息的Bot!`); + return false; + } + const client = await this.getBotClient(sendBot); + if (!client) { + logger.warn(`不存${sendBot}对应的client!`); + return false; + } + const sendData = { + type: 'sendMessage', + data: { + botId: sendBot, + groupId: groupId, + clientId: client, + }, + }; + if (client) { + await wsClientManager.send(sendData.data.clientId, sendData); + return true; + } + return false; + } + /** * 获取`botId`对应的`client` * @param botId