From 60bfa0e8a0d93998d5b41487ac2c23c7f39f1577 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Mon, 13 Nov 2023 13:32:42 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=96=B0=E5=A2=9E=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E7=AE=80=E6=98=93=E7=9A=84AI=E6=9C=BA=E5=99=A8?= =?UTF-8?q?=E4=BA=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.yaml | 11 ++++++++++- config/ai.go | 9 +++++++++ config/config.go | 1 + go.mod | 1 + go.sum | 2 ++ handler/at_message.go | 40 ++++++++++++++++++++++++++++++++++++++++ handler/parse.go | 5 +++++ utils/send.go | 14 +++++++++++++- 8 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 config/ai.go create mode 100644 handler/at_message.go diff --git a/config.yaml b/config.yaml index c463825c..a64ada6c 100644 --- a/config.yaml +++ b/config.yaml @@ -29,4 +29,13 @@ task: - '49448748645@chatroom' # 不计入统计范围的用户Id blacklist: - - 'wxid_7788687886912' \ No newline at end of file + - 'wxid_7788687886912' + +# AI回复 +ai: + # 是否启用 + enable: false + # OpenAI Api key + apiKey: sk-xxxx + # 接口代理域名,不填默认ChatGPT官方地址 + baseUrl: https://sxxx \ No newline at end of file diff --git a/config/ai.go b/config/ai.go new file mode 100644 index 00000000..5726dddb --- /dev/null +++ b/config/ai.go @@ -0,0 +1,9 @@ +package config + +// ai +// @description: AI配置 +type ai struct { + Enable bool `json:"enable" yaml:"enable"` // 是否启用AI + ApiKey string `json:"apiKey" yaml:"apiKey"` // API Key + BaseUrl string `json:"baseUrl" yaml:"baseUrl"` // API地址 +} diff --git a/config/config.go b/config/config.go index 4c1963f5..b8fa229a 100644 --- a/config/config.go +++ b/config/config.go @@ -8,6 +8,7 @@ type Config struct { Task task `json:"task" yaml:"task"` // 定时任务配置 MySQL mysql `json:"mysql" yaml:"mysql"` // MySQL 配置 Wechat wechat `json:"wechat" yaml:"wechat"` // 微信助手 + Ai ai `json:"ai" yaml:"ai"` // AI配置 } // task diff --git a/go.mod b/go.mod index b710f05a..c2c7651a 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/fsnotify/fsnotify v1.6.0 github.com/go-co-op/gocron v1.34.1 github.com/go-resty/resty/v2 v2.8.0 + github.com/sashabaranov/go-openai v1.17.5 github.com/spf13/viper v1.17.0 gorm.io/driver/mysql v1.5.1 gorm.io/gorm v1.25.4 diff --git a/go.sum b/go.sum index c0f33f62..9ede1c71 100644 --- a/go.sum +++ b/go.sum @@ -177,6 +177,8 @@ github.com/sagikazarmark/locafero v0.3.0 h1:zT7VEGWC2DTflmccN/5T1etyKvxSxpHsjb9c github.com/sagikazarmark/locafero v0.3.0/go.mod h1:w+v7UsPNFwzF1cHuOajOOzoq4U7v/ig1mpRjqV+Bu1U= github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= +github.com/sashabaranov/go-openai v1.17.5 h1:ItBzlrrfTtkFWOFlgfOhk3y/xRBC4PJol4gdbiK7hgg= +github.com/sashabaranov/go-openai v1.17.5/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg= github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= github.com/spf13/afero v1.10.0 h1:EaGW2JJh15aKOejeuJ+wpFSHnbd7GE6Wvp3TsNhb6LY= diff --git a/handler/at_message.go b/handler/at_message.go new file mode 100644 index 00000000..4ce8d68d --- /dev/null +++ b/handler/at_message.go @@ -0,0 +1,40 @@ +package handler + +import ( + "context" + "github.com/sashabaranov/go-openai" + "go-wechat/config" + "go-wechat/entity" + "go-wechat/utils" +) + +// handleAtMessage +// @description: 处理At机器人的消息 +// @param m +func handleAtMessage(m entity.Message) { + if !config.Conf.Ai.Enable { + return + } + // 默认使用AI回复 + client := openai.NewClient(config.Conf.Ai.ApiKey) + resp, err := client.CreateChatCompletion( + context.Background(), + openai.ChatCompletionRequest{ + Model: openai.GPT3Dot5Turbo0613, + Messages: []openai.ChatCompletionMessage{ + { + Role: openai.ChatMessageRoleUser, + Content: m.Content, + }, + }, + }, + ) + + if err != nil { + utils.SendMessage(m.FromUser, m.GroupUser, "AI炸啦~", 0) + return + } + + // 发送消息 + utils.SendMessage(m.FromUser, m.GroupUser, resp.Choices[0].Message.Content, 0) +} diff --git a/handler/parse.go b/handler/parse.go index 115858fc..98966e78 100644 --- a/handler/parse.go +++ b/handler/parse.go @@ -56,5 +56,10 @@ func Parse(remoteAddr net.Addr, msg []byte) { ent.DisplayFullContent = m.DisplayFullContent ent.Raw = string(msg) + // 处理At机器人的消息 + if strings.HasSuffix(m.DisplayFullContent, "在群聊中@了你") { + go handleAtMessage(ent) + } + go service.SaveMessage(ent) } diff --git a/utils/send.go b/utils/send.go index 732f97ca..11d13d57 100644 --- a/utils/send.go +++ b/utils/send.go @@ -18,18 +18,30 @@ func SendMessage(toId, atId, msg string, retryCount int) { log.Printf("重试五次失败,停止发送") return } + // 组装参数 param := map[string]any{ "wxid": toId, // 群或好友Id "msg": msg, // 消息 } + + // 接口地址 + apiUrl := config.Conf.Wechat.GetURL("/api/sendTextMsg") + if atId != "" { + apiUrl = config.Conf.Wechat.GetURL("/api/sendAtText") + param = map[string]any{ + "chatRoomId": toId, + "wxids": atId, + "msg": msg, // 消息 + } + } pbs, _ := json.Marshal(param) res := resty.New() resp, err := res.R(). SetHeader("Content-Type", "application/json;chartset=utf-8"). SetBody(string(pbs)). - Post(config.Conf.Wechat.GetURL("/api/sendTextMsg")) + Post(apiUrl) if err != nil { log.Printf("发送文本消息失败: %s", err.Error()) // 休眠五秒后重新发送