From a40f79656095421bdaf147b9d69a11376861e9d6 Mon Sep 17 00:00:00 2001 From: zorn <312073339@qq.com> Date: Tue, 14 Feb 2023 14:51:07 +0800 Subject: [PATCH] feat: trigger rule --- README.md | 2 ++ README_ZH.md | 2 ++ config.yaml.example | 1 + src/bot.ts | 43 ++++++++++++++++++++++++++++++------------- src/config.ts | 2 ++ src/interface.ts | 1 + 6 files changed, 38 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 65f1a51..30a808c 100644 --- a/README.md +++ b/README.md @@ -168,6 +168,8 @@ Some environment variables need to be configured: - **CHAT_PRIVATE_TRIGGER_KEYWORD** : If you hope only some keywords can trigger chatgpt on private chat, you can set it. +- **CHAT_TRIGGER_RULE** :If you hope only pass by a regular expression check can trigger chatgpt, you can set it + Click the Deploy button and your service will start deploying shortly.The following interface appears to indicate that the deployment has begun: ![railway-deploying](docs/images/railway-deploying.png) diff --git a/README_ZH.md b/README_ZH.md index 93f5ffe..e0cfefb 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -165,6 +165,8 @@ npm run dev - **CHAT_PRIVATE_TRIGGER_KEYWORD** :如果您希望只有一些关键字才能在私人聊天中触发 ChatGPT,则可以设置它。 +- **CHAT_TRIGGER_RULE** :如果您希望正则校验通过的聊天信息才能触发 ChatGPT,则可以设置它。 + 点击“部署”按钮,您的服务将立即开始部署。以下界面出现表示部署已经开始: ![railway-deploying](docs/images/railway-deploying.png) diff --git a/config.yaml.example b/config.yaml.example index e089136..459e5f4 100644 --- a/config.yaml.example +++ b/config.yaml.example @@ -3,4 +3,5 @@ chatGPTAccountPool: password: password isGoogleLogin: false chatPrivateTiggerKeyword: "" +chatTiggerRule: "" openAIProxy: "" \ No newline at end of file diff --git a/src/bot.ts b/src/bot.ts index 844c091..440b870 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -28,13 +28,22 @@ export class ChatGPTBot { // Record talkid with conversation id chatGPTPool = new ChatGPTPool(); chatPrivateTiggerKeyword = config.chatPrivateTiggerKeyword; + chatTiggerRule = config.chatTiggerRule? new RegExp(config.chatTiggerRule): undefined; botName: string = ""; ready = false; setBotName(botName: string) { this.botName = botName; } - get chatGroupTiggerKeyword(): string { - return `@${this.botName}`; + get chatGroupTiggerRegEx(): RegExp { + return new RegExp(`^@${this.botName}\\s`); + } + get chatPrivateTiggerRule(): RegExp | undefined { + const { chatPrivateTiggerKeyword, chatTiggerRule } = this; + let regEx = chatTiggerRule + if (!regEx && chatPrivateTiggerKeyword) { + regEx = new RegExp(chatPrivateTiggerKeyword) + } + return regEx } async startGPTBot() { console.debug(`Start GPT Bot Config is:${JSON.stringify(config)}`); @@ -51,12 +60,17 @@ export class ChatGPTBot { if (item.length > 1) { text = item[item.length - 1]; } - text = text.replace( - privateChat ? this.chatPrivateTiggerKeyword : this.chatGroupTiggerKeyword, - "" - ); + + const { chatTiggerRule, chatPrivateTiggerRule } = this; + + if (privateChat && chatPrivateTiggerRule) { + text = text.replace(chatPrivateTiggerRule, "") + } else if (!privateChat) { + text = text.replace(this.chatGroupTiggerRegEx, "") + text = chatTiggerRule? text.replace(chatTiggerRule, ""): text + } // remove more text via - - - - - - - - - - - - - - - - return text; + return text } async getGPTMessage(text: string, talkerId: string): Promise { return await this.chatGPTPool.sendMessage(text, talkerId); @@ -79,14 +93,17 @@ export class ChatGPTBot { } // Check whether the ChatGPT processing can be triggered tiggerGPTMessage(text: string, privateChat: boolean = false): boolean { - const chatPrivateTiggerKeyword = this.chatPrivateTiggerKeyword; + const { chatTiggerRule } = this; let triggered = false; if (privateChat) { - triggered = chatPrivateTiggerKeyword - ? text.includes(chatPrivateTiggerKeyword) - : true; + const regEx = this.chatPrivateTiggerRule + triggered = regEx? regEx.test(text): true; } else { - triggered = text.includes(this.chatGroupTiggerKeyword); + triggered = this.chatGroupTiggerRegEx.test(text); + // group message support `chatTiggerRule` + if (triggered && chatTiggerRule) { + triggered = chatTiggerRule.test(text.replace(this.chatGroupTiggerRegEx, "")) + } } if (triggered) { console.log(`🎯 Triggered ChatGPT: ${text}`); @@ -103,7 +120,7 @@ export class ChatGPTBot { talker.self() || // TODO: add doc support messageType !== MessageType.Text || - talker.name() == "微信团队" || + talker.name() === "微信团队" || // 语音(视频)消息 text.includes("收到一条视频/语音聊天消息,请在手机上查看") || // 红包消息 diff --git a/src/config.ts b/src/config.ts index df810aa..149ff68 100644 --- a/src/config.ts +++ b/src/config.ts @@ -18,6 +18,7 @@ if (fs.existsSync("./config.yaml")) { ], chatGptRetryTimes: Number(process.env.CHAT_GPT_RETRY_TIMES), chatPrivateTiggerKeyword: process.env.CHAT_PRIVATE_TRIGGER_KEYWORD, + chatTiggerRule: process.env.CHAT_TRIGGER_RULE, openAIProxy: process.env.OPENAI_PROXY, clearanceToken: process.env.CF_CLEARANCE, userAgent: process.env.USER_AGENT, @@ -37,6 +38,7 @@ export const config: IConfig = { "" ) || "", + chatTiggerRule: configFile.chatTiggerRule, // Support openai-js use this proxy openAIProxy: configFile.openAIProxy, clearanceToken: configFile.clearanceToken, diff --git a/src/interface.ts b/src/interface.ts index 0087f04..630b861 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -23,6 +23,7 @@ export interface IConfig { chatGPTAccountPool: IAccount[]; chatGptRetryTimes: number; chatPrivateTiggerKeyword: string; + chatTiggerRule?: string; openAIProxy?: string; clearanceToken: string; userAgent: string;