From 324a8b4c5f190fe0da2727c080a68cda49f9eab4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=89=BE=E8=BF=AA?= <62269186+AnotiaWang@users.noreply.github.com> Date: Mon, 13 Mar 2023 13:32:36 +0800 Subject: [PATCH] chore: add README; improve reply logic --- README.md | 15 +++++++++++++++ build.sh | 6 ++++++ handler/message.go | 29 +++++++++++++---------------- 3 files changed, 34 insertions(+), 16 deletions(-) create mode 100644 README.md create mode 100644 build.sh diff --git a/README.md b/README.md new file mode 100644 index 0000000..4ae8c46 --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +# WeeChatGPT + +将 ChatGPT 集成到微信个人号。基于 [openwechat](https://github.com/eatmoreapple/openwechat)。 + +## 运行方法 + +1. 前往 [Releases](https://github.com/AnotiaWang/WeeChatGPT/releases/latest) ,根据你使用的平台,下载最新版本的 WeeChatGPT。 +2. 先运行一次 WeeChatGPT,程序会生成配置文件 `config.yml`,然后根据文件中的提示,填写配置。 +3. 再次运行 WeeChatGPT,会显示微信的登录二维码链接,在浏览器中打开它,扫码登录微信即可。 + +### 提示 + +- 程序支持热登录,如果两次登录之间的间隔较短,可以自动登录。 +- 建议将群组添加到通讯录,否则机器人可能无法获取到群组信息,导致回复失败。 +- 目前仅测试过 gpt-3.5-turbo 模型的支持。 diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..93a8716 --- /dev/null +++ b/build.sh @@ -0,0 +1,6 @@ +echo Building for linux_amd64... +CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ./dist/weechatbot_linux_amd64 . +echo Building for darwin_amd64... +CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o ./dist/weechatbot_darwin_amd64 . +echo Building for windows_amd64... +CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o ./dist/weechatbot_windows_amd64 . diff --git a/handler/message.go b/handler/message.go index 4882b66..c858af2 100644 --- a/handler/message.go +++ b/handler/message.go @@ -10,6 +10,7 @@ import ( func Default(ctx context.Context) openwechat.MessageHandler { self := ctx.Value(model.SelfKey).(*openwechat.Self) + config := ctx.Value(model.ConfigKey).(*model.Config) dispatcher := openwechat.NewMessageMatchDispatcher() dispatcher.OnText(func(msgCtx *openwechat.MessageContext) { @@ -18,62 +19,58 @@ func Default(ctx context.Context) openwechat.MessageHandler { if len(msg.Content) <= 4 { return } - if strings.Index(msg.Content, " ") == 0 { - } - if msg.Content[:4] == ":bot" { + if strings.Index(msg.Content, config.OpenAI.Prefix) == 0 { + log.Println("found message match: " + msg.Content) query := msg.Content[4:] - response, err := model.ChatCompletion(ctx, model.MakeMessage(query)) if err != nil { log.Println("ChatCompletion error: " + err.Error()) return } - log.Println("回复消息: " + response) receiver, err := msg.Receiver() if err != nil { - log.Println("Receiver error: " + err.Error()) + log.Println("get receiver failed: " + err.Error()) return } log.Printf("receiver: %v, isSendBySelf: %v", receiver, msg.IsSendBySelf()) sender, err := msg.Sender() if err != nil { - log.Println("Sender error: " + err.Error()) + log.Println("get sender failed: " + err.Error()) return } log.Printf("sender: %v, isSendBySelf: %v", sender, msg.IsSendBySelf()) + // 和文件传输助手的消息 if receiver != nil && receiver.UserName == "filehelper" { - log.Println("Reply to filehelper") fh := self.FileHelper() _, err := self.SendTextToFriend(fh, response) if err != nil { - log.Println("SendTextToFriend error: " + err.Error()) + log.Println("reply to filehelper failed: " + err.Error()) return } } else if receiver != nil && msg.IsSendBySelf() { - log.Println("Is send by self, isGroup:", receiver.IsGroup(), ", isSelf:", receiver.IsSelf(), ", isFriend:", receiver.IsFriend()) if receiver.IsGroup() { group, _ := receiver.AsGroup() - log.Println("Reply to group") + log.Println("replying to group", group.NickName) _, err := self.SendTextToGroup(group, response) if err != nil { - log.Println("SendTextToGroup error: " + err.Error()) + log.Println("SendTextToGroup failed: " + err.Error()) } } else { user, _ := receiver.AsFriend() - log.Println("Reply to user") + log.Println("replying to user", user.NickName) _, err := self.SendTextToFriend(user, response) if err != nil { - log.Println("SendTextToFriend error: " + err.Error()) + log.Println("SendTextToFriend failed: " + err.Error()) } } } else { - log.Println("Fallback reply to message") + log.Println("defaulting to replyText") _, err := msgCtx.ReplyText(response) if err != nil { - log.Println("ReplyText error: " + err.Error()) + log.Println("ReplyText failed: " + err.Error()) } } }