mirror of
https://github.com/crystelf/crystelf-core.git
synced 2025-07-04 14:49:19 +00:00
智能广播接口
This commit is contained in:
parent
865f606399
commit
daf5d53d46
@ -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();
|
||||
|
@ -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');
|
||||
|
@ -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;
|
||||
},
|
||||
|
Loading…
x
Reference in New Issue
Block a user