发送消息接口

This commit is contained in:
Jerry 2025-05-14 13:51:36 +08:00
parent ebeba44f9d
commit f66118bc12
2 changed files with 58 additions and 0 deletions

View File

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

View File

@ -86,6 +86,38 @@ class BotService {
return undefined;
}
/**
*
* @param groupId
* @param message
*/
public async sendMessage(groupId: number, message: string): Promise<boolean> {
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