Skip to content

Commit

Permalink
chore: add README; improve reply logic
Browse files Browse the repository at this point in the history
  • Loading branch information
AnotiaWang committed Mar 13, 2023
1 parent c819d18 commit 324a8b4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 模型的支持。
6 changes: 6 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -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 .
29 changes: 13 additions & 16 deletions handler/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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())
}
}
}
Expand Down

0 comments on commit 324a8b4

Please sign in to comment.