diff --git a/config.yaml b/config.yaml index c463825..a64ada6 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 0000000..5726ddd --- /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 4c1963f..b8fa229 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 b710f05..c2c7651 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 c0f33f6..9ede1c7 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 0000000..4ce8d68 --- /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 115858f..98966e7 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 732f97c..11d13d5 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()) // 休眠五秒后重新发送