Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
lcjqyml committed Apr 28, 2023
2 parents 32e0117 + af70c5b commit e5dde83
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@
| autoAcceptFriendShip | AUTO_ACCEPT_FRIEND_SHIP | 自动通过好友请求 | NO | false |
| autoAcceptRoomInvite | AUTO_ACCEPT_ROOM_INVITE | 自动通过群聊邀请 | NO | false |
| chatbotTriggerKeyword | CHATBOT_TRIGGER_KEYWORD | 机器人聊天触发器,默认@触发 | NO | "" |
| responseQuote | RESPONSE_QUOTE | 群聊中回复时是否引用触发的消息 | NO | yes |

## 运行说明

执行以下命令启动:
```bash
docker run -e CHATBOT_PROXY="<your-proxy>" lcjqyml/wechatbot:latest
```
* `<your-proxy>` 需要替换为搭建的http服务,参考:http://127.0.0.1:8080
* `<your-proxy>` 需要替换为你自己搭建的http服务,例如:
```bash
docker run -e CHATBOT_PROXY="http://127.0.0.1:8080" lcjqyml/wechatbot:latest
```

扫码登陆即可。
PS:
Expand Down
1 change: 1 addition & 0 deletions config.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ chatbotProxy: "http://13.213.43.103:8080"
autoAcceptFriendShip: "true"
autoAcceptRoomInvite: "true"
chatbotTriggerKeyword: ""
responseQuote: "true"
24 changes: 21 additions & 3 deletions src/chatbot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ export class ChatBot {
const voiceType = this.getFirstMatchTextFromRegex(voice, Constants.voiceTypeReg);
if (voiceType === "mpeg") {
Logger.log(`Reply voice.mp3 -> ${voice.substr(0, 50)}`);
await talker.say(FileBox.fromDataURL(voice, "voice.mp3"));
const tmpName = await this.generateTmpName(talker) + ".mp3";
await talker.say(FileBox.fromDataURL(voice, tmpName));
}
}

Expand All @@ -234,14 +235,15 @@ export class ChatBot {
const imageType = this.getFirstMatchTextFromRegex(image, Constants.imageTypeReg);
if (imageType) {
Logger.log(`Reply image.${imageType} -> ${image.substr(0, 50)}`);
await talker.say(FileBox.fromDataURL(image, `image.${imageType}`));
const tmpName = await this.generateTmpName(talker) + `.${imageType}`;
await talker.say(FileBox.fromDataURL(image, tmpName));
}
}

private async reply(talker: ContactInterface|RoomInterface, prompt: string, responseData: ResponseData) {
if (responseData.message && responseData.message.length > 0) {
for (let m of responseData.message) {
if ("topic" in talker) {
if (Config.responseQuote && "topic" in talker) {
m = `${prompt}\n----------\n${m}`;
}
await this.replyText(talker, m);
Expand Down Expand Up @@ -310,4 +312,20 @@ export class ChatBot {
return await this.onGroupMessage(talker, room, text);
}
}

private async generateTmpName(talker: RoomInterface | ContactInterface) {
let tmpName = "";
if ("topic" in talker) {
tmpName = await talker.topic();
} else {
tmpName = talker.name();
}
tmpName = pinyin(tmpName, {style: pinyin.STYLE_FIRST_LETTER}).join("");
const now = new Date();
const hour = now.getHours().toString().padStart(2, "0");
const minute = now.getMinutes().toString().padStart(2, "0");
const second = now.getSeconds().toString().padStart(2, "0");
tmpName += `-${hour}${minute}${second}`;
return tmpName;
}
}
11 changes: 9 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ else {
autoAcceptFriendShip: process.env.AUTO_ACCEPT_FRIEND_SHIP,
autoAcceptRoomInvite: process.env.AUTO_ACCEPT_ROOM_INVITE,
chatbotTriggerKeyword: process.env.CHATBOT_TRIGGER_KEYWORD,
responseQuote: process.env.RESPONSE_QUOTE,
};
}

Expand All @@ -39,11 +40,17 @@ if (configFile.chatbotProxy === undefined) {

export const Config: IConfig = {
chatbotProxy: configFile.chatbotProxy || "",
autoAcceptFriendShip: Boolean(configFile.autoAcceptFriendShip),
autoAcceptRoomInvite: Boolean(configFile.autoAcceptRoomInvite),
autoAcceptFriendShip: configFile.autoAcceptFriendShip == "true",
autoAcceptRoomInvite: configFile.autoAcceptRoomInvite == "true",
chatbotTriggerKeyword: configFile.chatbotTriggerKeyword || "",
responseQuote: configFile.responseQuote == "true"
};

Logger.log("Config ->")
Object.entries(Config).forEach(([key, value]) => {
Logger.log(`${key}: ${value}`);
});

export const Constants = {
imageTypeReg: /data:image\/(\w+);base64,/g,
voiceTypeReg: /data:audio\/(\w+);base64,/g,
Expand Down
5 changes: 3 additions & 2 deletions src/interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export interface IConfig {
chatbotProxy: string;
chatbotProxy: string,
autoAcceptFriendShip: boolean,
autoAcceptRoomInvite: boolean,
chatbotTriggerKeyword: string;
chatbotTriggerKeyword: string,
responseQuote: boolean,
}

0 comments on commit e5dde83

Please sign in to comment.