diff --git a/src/modules/bot/bot.controller.ts b/src/modules/bot/bot.controller.ts index aa7aa48..9577a19 100644 --- a/src/modules/bot/bot.controller.ts +++ b/src/modules/bot/bot.controller.ts @@ -22,6 +22,7 @@ class BotController { this.router.post('/getGroupInfo', this.postGroupInfo); this.router.post('/sendMessage', this.sendMessage); this.router.post('/reportBots', this.reportBots); + this.router.post('/broadcast', this.smartBroadcast); } /** @@ -80,6 +81,7 @@ class BotController { * @param req * @param res */ + // TODO 测试接口可用性 private reportBots = async (req: express.Request, res: express.Response): Promise => { try { const token = req.body.token; @@ -88,8 +90,9 @@ class BotController { type: 'reportBots', data: {}, }; - await wsClientManager.broadcast(sendMessage); + logger.info(`正在请求同步bot数据..`); await response.success(res, {}); + await wsClientManager.broadcast(sendMessage); } else { await tools.tokenCheckFailed(res, token); } @@ -103,6 +106,7 @@ class BotController { * @param req * @param res */ + // TODO 测试接口可用性 private sendMessage = async (req: express.Request, res: express.Response): Promise => { try { const token = req.body.token; @@ -122,6 +126,31 @@ class BotController { await response.error(res); } }; + + /** + * 智能广播消息到全部群聊 + * @param req + * @param res + */ + // TODO 测试接口可用性 + private smartBroadcast = async (req: express.Request, res: express.Response): Promise => { + try { + const token = req.body.token; + const message = req.body.message; + if (!message || typeof message !== 'string') { + return await response.error(res, '缺少 message 字段', 400); + } + if (tools.checkToken(token.toString())) { + logger.info(`广播任务已开始,正在后台异步执行`); + await response.success(res, {}); + await BotService.broadcastToAllGroups(message); + } 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 b0e1c33..dc9dff6 100644 --- a/src/modules/bot/bot.service.ts +++ b/src/modules/bot/bot.service.ts @@ -120,6 +120,11 @@ class BotService { return false; } + /** + * 智能投放广播消息实现 + * @param message 要广播的消息 + */ + // TODO 添加群聊信誉分机制,低于30分的群聊不播报等.. public async broadcastToAllGroups(message: string): Promise { const userPath = paths.get('userData'); const botsPath = path.join(userPath, '/crystelfBots'); diff --git a/src/utils/core/tool.ts b/src/utils/core/tool.ts index 8cc740b..a49bbab 100644 --- a/src/utils/core/tool.ts +++ b/src/utils/core/tool.ts @@ -2,6 +2,11 @@ import RetryOptions from '../../types/retry'; import logger from './logger'; let tools = { + /** + * 异步重试机制 + * @param operation + * @param options + */ async retry(operation: () => Promise, options: RetryOptions): Promise { let attempt = 0; let lastError: any; @@ -22,10 +27,19 @@ let tools = { logger.error(lastError); }, + /** + * 从一个可迭代列表中随机选择一个对象 + * @param list 可迭代数据 + */ getRandomItem(list: T[]): T { return list[Math.floor(Math.random() * list.length)]; }, + /** + * 获取随机数 + * @param min 最小值 + * @param max 最大值 + */ getRandomDelay(min: number, max: number): number { return Math.floor(Math.random() * (max - min + 1)) + min; },