Skip to content

Commit

Permalink
feat: trigger split into independent private chat trigger and group c…
Browse files Browse the repository at this point in the history
…hat trigger.
  • Loading branch information
lcjqyml committed May 23, 2023
1 parent c249d5f commit 6908711
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 39 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@

| config.yaml | env | 说明 | 必须 | 默认 |
|----------|------|------|------|------|
| chatbotProxy | CHATBOT_PROXY | chatbot请求地址,参考[lss233/chatgpt-mirai-qq-bot][1] | YES | |
| autoAcceptFriendShip | AUTO_ACCEPT_FRIEND_SHIP | 自动通过好友请求 | NO | false |
| autoAcceptRoomInvite | AUTO_ACCEPT_ROOM_INVITE | 自动通过群聊邀请 | NO | false |
| chatbotTriggerKeyword | CHATBOT_TRIGGER_KEYWORD | 机器人聊天触发器,默认@触发 | NO | "" |
| responseQuote | RESPONSE_QUOTE | 群聊中回复时是否引用触发的消息 | NO | false |
| chatbotProxy | CHATBOT_PROXY | chatbot请求地址,参考[lss233/chatgpt-mirai-qq-bot][1] | YES | |
| autoAcceptFriendShip | AUTO_ACCEPT_FRIEND_SHIP | 自动通过好友请求 | NO | false |
| autoAcceptRoomInvite | AUTO_ACCEPT_ROOM_INVITE | 自动通过群聊邀请 | NO | false |
| privateChatTrigger | PRIVATE_CHAT_TRIGGER | 机器人私聊触发器,空则都触发 | NO | "" |
| groupChatTrigger | GROUP_CHAT_TRIGGER | 机器人群聊触发器,空则@触发,否则:trigger 或 @bot trigger 触发 | NO | "" |
| responseQuote | RESPONSE_QUOTE | 群聊中回复时是否引用触发的消息 | NO | false |

### lss233/chatgpt-mirai-qq-bot 项目配置说明
本项目与lss233项目部署在同一服务器时:
Expand Down
54 changes: 23 additions & 31 deletions src/chatbot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,18 @@ const postData = async (url: string, requestBody: RequestBody): Promise<string>

export class ChatBot {
// chatbot name (WeChat account name)
botName: string = "";
botName: string = "bot";

// self-chat may cause some issue for some WeChat Account
// please set to true if self-chat cause some errors
disableSelfChat: boolean = false;

// chatbot trigger keyword
chatbotTriggerKeyword: string = Config.chatbotTriggerKeyword;
privateChatTrigger: string = Config.privateChatTrigger;
groupChatTrigger: string = Config.groupChatTrigger;
groupChatTriggerRegex: RegExp = this.groupChatTrigger ?
new RegExp(`^(@${this.botName}\\s+)?${this.groupChatTrigger}`):
new RegExp(`^@${this.botName}`);

// message size for a single reply by the bot
SINGLE_MESSAGE_MAX_SIZE: number = 500;
Expand All @@ -74,6 +78,9 @@ export class ChatBot {
// set bot name during login stage
setBotName(botName: string) {
this.botName = botName;
this.groupChatTriggerRegex = this.groupChatTrigger ?
new RegExp(`^(@${this.botName}\\s+)?${this.groupChatTrigger}`):
new RegExp(`^@${this.botName}`);
}

private constructResponseData(result: string, message: string): ResponseData {
Expand All @@ -85,48 +92,33 @@ export class ChatBot {
}
}

// get trigger keyword in group chat: (@Name <keyword>)
// in group chat, replace the special character after "@username" to space
// to prevent cross-platfrom mention issue
private get chatGroupTriggerKeyword(): string {
return `@${this.botName} ${this.chatbotTriggerKeyword || ""}`;
}

private cleanMessage(
rawText: string,
isPrivateChat: boolean = false
): string {
private cleanMessage(rawText: string, isPrivateChat: boolean): string {
let text = rawText;
const item = rawText.split("- - - - - - - - - - - - - - -");
const item = rawText.split("----------");
if (item.length > 1) {
text = item[item.length - 1];
text = item[item.length - 1].trim();
}
return text.slice(
isPrivateChat
? this.chatbotTriggerKeyword.length
: this.chatGroupTriggerKeyword.length
);
if (isPrivateChat && this.privateChatTrigger){
return text.slice(this.privateChatTrigger.length).trim();
}
if (!isPrivateChat) {
return text.replace(this.groupChatTriggerRegex, "").trim();
}
return text.trim();
}

// check whether chat bot can be triggered
private triggerGPTMessage(
text: string,
isPrivateChat: boolean = false
): boolean {
const chatbotTriggerKeyword = this.chatbotTriggerKeyword;
let triggered = false;
let triggered;
if (isPrivateChat) {
triggered = chatbotTriggerKeyword
? text.startsWith(chatbotTriggerKeyword)
triggered = this.privateChatTrigger
? text.startsWith(this.privateChatTrigger)
: true;
} else {
const textMention = `@${this.botName}`;
const startsWithMention = text.startsWith(textMention);
const textWithoutMention = text.slice(textMention.length + 1);
const followByTriggerKeyword = textWithoutMention.startsWith(
this.chatbotTriggerKeyword
);
triggered = startsWithMention && followByTriggerKeyword;
triggered = this.groupChatTriggerRegex.test(text);
}
if (triggered) {
Logger.log(`🎯 ChatGPT triggered: ${text}`);
Expand Down
6 changes: 4 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ else {
chatbotProxy: process.env.CHATBOT_PROXY,
autoAcceptFriendShip: process.env.AUTO_ACCEPT_FRIEND_SHIP,
autoAcceptRoomInvite: process.env.AUTO_ACCEPT_ROOM_INVITE,
chatbotTriggerKeyword: process.env.CHATBOT_TRIGGER_KEYWORD,
privateChatTrigger: process.env.PRIVATE_CHAT_TRIGGER,
groupChatTrigger: process.env.GROUP_CHAT_TRIGGER,
responseQuote: process.env.RESPONSE_QUOTE,
};
}
Expand All @@ -42,7 +43,8 @@ export const Config: IConfig = {
chatbotProxy: configFile.chatbotProxy || "",
autoAcceptFriendShip: configFile.autoAcceptFriendShip == "true",
autoAcceptRoomInvite: configFile.autoAcceptRoomInvite == "true",
chatbotTriggerKeyword: configFile.chatbotTriggerKeyword || "",
privateChatTrigger: configFile.privateChatTrigger || "",
groupChatTrigger: configFile.groupChatTrigger || "",
responseQuote: configFile.responseQuote == "true"
};

Expand Down
3 changes: 2 additions & 1 deletion src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export interface IConfig {
chatbotProxy: string,
autoAcceptFriendShip: boolean,
autoAcceptRoomInvite: boolean,
chatbotTriggerKeyword: string,
privateChatTrigger: string,
groupChatTrigger: string,
responseQuote: boolean,
}

0 comments on commit 6908711

Please sign in to comment.