Skip to content

Commit

Permalink
feat: trigger rule
Browse files Browse the repository at this point in the history
  • Loading branch information
chenzn1 committed Feb 14, 2023
1 parent 9080276 commit a40f796
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 13 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ npm run dev

- **CHAT_PRIVATE_TRIGGER_KEYWORD** :如果您希望只有一些关键字才能在私人聊天中触发 ChatGPT,则可以设置它。

- **CHAT_TRIGGER_RULE** :如果您希望正则校验通过的聊天信息才能触发 ChatGPT,则可以设置它。

点击“部署”按钮,您的服务将立即开始部署。以下界面出现表示部署已经开始:

![railway-deploying](docs/images/railway-deploying.png)
Expand Down
1 change: 1 addition & 0 deletions config.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ chatGPTAccountPool:
password: password
isGoogleLogin: false
chatPrivateTiggerKeyword: ""
chatTiggerRule: ""
openAIProxy: ""
43 changes: 30 additions & 13 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`);
Expand All @@ -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<string> {
return await this.chatGPTPool.sendMessage(text, talkerId);
Expand All @@ -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}`);
Expand All @@ -103,7 +120,7 @@ export class ChatGPTBot {
talker.self() ||
// TODO: add doc support
messageType !== MessageType.Text ||
talker.name() == "微信团队" ||
talker.name() === "微信团队" ||
// 语音(视频)消息
text.includes("收到一条视频/语音聊天消息,请在手机上查看") ||
// 红包消息
Expand Down
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -37,6 +38,7 @@ export const config: IConfig = {
""
) ||
"",
chatTiggerRule: configFile.chatTiggerRule,
// Support openai-js use this proxy
openAIProxy: configFile.openAIProxy,
clearanceToken: configFile.clearanceToken,
Expand Down
1 change: 1 addition & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface IConfig {
chatGPTAccountPool: IAccount[];
chatGptRetryTimes: number;
chatPrivateTiggerKeyword: string;
chatTiggerRule?: string;
openAIProxy?: string;
clearanceToken: string;
userAgent: string;
Expand Down

0 comments on commit a40f796

Please sign in to comment.