mirror of
https://github.com/crystelf/crystelf-core.git
synced 2025-07-04 06:39:18 +00:00
发送消息接口
This commit is contained in:
parent
ebeba44f9d
commit
f66118bc12
@ -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();
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user