智能广播接口

This commit is contained in:
Jerry 2025-05-15 12:51:13 +08:00
parent 865f606399
commit daf5d53d46
3 changed files with 49 additions and 1 deletions

View File

@ -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<void> => {
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<void> => {
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<void> => {
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();

View File

@ -120,6 +120,11 @@ class BotService {
return false;
}
/**
* 广
* @param message 广
*/
// TODO 添加群聊信誉分机制低于30分的群聊不播报等..
public async broadcastToAllGroups(message: string): Promise<void> {
const userPath = paths.get('userData');
const botsPath = path.join(userPath, '/crystelfBots');

View File

@ -2,6 +2,11 @@ import RetryOptions from '../../types/retry';
import logger from './logger';
let tools = {
/**
*
* @param operation
* @param options
*/
async retry(operation: () => Promise<any>, options: RetryOptions): Promise<any> {
let attempt = 0;
let lastError: any;
@ -22,10 +27,19 @@ let tools = {
logger.error(lastError);
},
/**
*
* @param list
*/
getRandomItem<T>(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;
},