Skip to content

Commit

Permalink
Add a trigger: send a message periodically to check whether it is nor…
Browse files Browse the repository at this point in the history
…mal.
  • Loading branch information
lcjqyml committed Nov 16, 2024
1 parent 1d0242c commit 45f72ae
Show file tree
Hide file tree
Showing 5 changed files with 10,609 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
| privateChatTrigger | PRIVATE_CHAT_TRIGGER | 机器人私聊触发器,空则都触发 | NO | "" |
| groupChatTrigger | GROUP_CHAT_TRIGGER | 机器人群聊触发器,空则@触发,否则:trigger 或 @bot trigger 触发 | NO | "" |
| responseQuote | RESPONSE_QUOTE | 群聊中回复时是否引用触发的消息 | NO | false |
| checkOnlineTrigger | CHECK_ONLINE_TRIGGER | 向发送此trigger的联系人定时反馈执行指令的结果 | NO | \_\_check\_\_ |
| checkOnlineCommand | CHECK_ONLINE_COMMAND | 定时执行的指令 | NO | ping |
| checkOnlineInterval | CHECK_ONLINE_INTERVAL | 定时间隔 | NO | 60 * 60 * 1000 |

### lss233/chatgpt-mirai-qq-bot 项目配置说明
本项目与lss233项目部署在同一服务器时:
Expand Down
15 changes: 15 additions & 0 deletions src/chatbot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,23 @@ export class ChatBot {
}
}

private triggerActions(talker: ContactInterface, text: string) {
if (text !== Config.checkOnlineTrigger) {
return false;
}
setInterval(async () => {
await this.onPrivateMessage(talker, Config.checkOnlineCommand);
}, Config.checkOnlineInterval);
return true;
}

// reply to private message
private async onPrivateMessage(talker: ContactInterface, text: string) {
const sessionId = pinyin(talker.name(), {style: pinyin.STYLE_TONE2}).join("");
const chatbot = this;
if (this.triggerActions(talker, text)) {
text = Config.checkOnlineCommand;
}
await this.onChat(text, `friend-${sessionId}`, sessionId,
async function (responseData: ResponseData) {
await chatbot.reply(talker, text, responseData);
Expand Down Expand Up @@ -297,6 +310,7 @@ export class ChatBot {
const room = message.room();
const messageType = message.type();
const isPrivateChat = !room;
Logger.log(`收到${isPrivateChat ? "" : room.topic()} > ${talker.name()}(${talker.id})的消息`)
if (
this.isNonsense(talker, messageType, rawText) ||
!this.triggerGPTMessage(rawText, isPrivateChat)
Expand Down Expand Up @@ -328,3 +342,4 @@ export class ChatBot {
return tmpName;
}
}

8 changes: 7 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ else {
privateChatTrigger: process.env.PRIVATE_CHAT_TRIGGER,
groupChatTrigger: process.env.GROUP_CHAT_TRIGGER,
responseQuote: process.env.RESPONSE_QUOTE,
checkOnlineTrigger: process.env.CHECK_ONLINE_TRIGGER,
checkOnlineCommand: process.env.CHECK_ONLINE_COMMAND,
checkOnlineInterval: process.env.CHECK_ONLINE_INTERVAL,
};
}

Expand All @@ -45,7 +48,10 @@ export const Config: IConfig = {
autoAcceptRoomInvite: configFile.autoAcceptRoomInvite == "true",
privateChatTrigger: configFile.privateChatTrigger || "",
groupChatTrigger: configFile.groupChatTrigger || "",
responseQuote: configFile.responseQuote == "true"
responseQuote: configFile.responseQuote == "true",
checkOnlineTrigger: configFile.checkOnlineTrigger || "__check__",
checkOnlineCommand: configFile.checkOnlineCommand || "ping",
checkOnlineInterval: configFile.checkOnlineInterval || 60 * 60 * 1000,
};

Logger.log("Config ->")
Expand Down
3 changes: 3 additions & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ export interface IConfig {
privateChatTrigger: string,
groupChatTrigger: string,
responseQuote: boolean,
checkOnlineTrigger: string,
checkOnlineCommand: string,
checkOnlineInterval: number,
}
Loading

0 comments on commit 45f72ae

Please sign in to comment.