1
0
forked from lxh/go-wxhelper

新增一个简易的AI机器人

This commit is contained in:
李寻欢 2023-11-13 13:32:42 +08:00
parent d08937563a
commit 60bfa0e8a0
8 changed files with 81 additions and 2 deletions

View File

@ -29,4 +29,13 @@ task:
- '49448748645@chatroom' - '49448748645@chatroom'
# 不计入统计范围的用户Id # 不计入统计范围的用户Id
blacklist: blacklist:
- 'wxid_7788687886912' - 'wxid_7788687886912'
# AI回复
ai:
# 是否启用
enable: false
# OpenAI Api key
apiKey: sk-xxxx
# 接口代理域名不填默认ChatGPT官方地址
baseUrl: https://sxxx

9
config/ai.go Normal file
View File

@ -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地址
}

View File

@ -8,6 +8,7 @@ type Config struct {
Task task `json:"task" yaml:"task"` // 定时任务配置 Task task `json:"task" yaml:"task"` // 定时任务配置
MySQL mysql `json:"mysql" yaml:"mysql"` // MySQL 配置 MySQL mysql `json:"mysql" yaml:"mysql"` // MySQL 配置
Wechat wechat `json:"wechat" yaml:"wechat"` // 微信助手 Wechat wechat `json:"wechat" yaml:"wechat"` // 微信助手
Ai ai `json:"ai" yaml:"ai"` // AI配置
} }
// task // task

1
go.mod
View File

@ -7,6 +7,7 @@ require (
github.com/fsnotify/fsnotify v1.6.0 github.com/fsnotify/fsnotify v1.6.0
github.com/go-co-op/gocron v1.34.1 github.com/go-co-op/gocron v1.34.1
github.com/go-resty/resty/v2 v2.8.0 github.com/go-resty/resty/v2 v2.8.0
github.com/sashabaranov/go-openai v1.17.5
github.com/spf13/viper v1.17.0 github.com/spf13/viper v1.17.0
gorm.io/driver/mysql v1.5.1 gorm.io/driver/mysql v1.5.1
gorm.io/gorm v1.25.4 gorm.io/gorm v1.25.4

2
go.sum
View File

@ -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/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 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= 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 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spf13/afero v1.10.0 h1:EaGW2JJh15aKOejeuJ+wpFSHnbd7GE6Wvp3TsNhb6LY= github.com/spf13/afero v1.10.0 h1:EaGW2JJh15aKOejeuJ+wpFSHnbd7GE6Wvp3TsNhb6LY=

40
handler/at_message.go Normal file
View File

@ -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)
}

View File

@ -56,5 +56,10 @@ func Parse(remoteAddr net.Addr, msg []byte) {
ent.DisplayFullContent = m.DisplayFullContent ent.DisplayFullContent = m.DisplayFullContent
ent.Raw = string(msg) ent.Raw = string(msg)
// 处理At机器人的消息
if strings.HasSuffix(m.DisplayFullContent, "在群聊中@了你") {
go handleAtMessage(ent)
}
go service.SaveMessage(ent) go service.SaveMessage(ent)
} }

View File

@ -18,18 +18,30 @@ func SendMessage(toId, atId, msg string, retryCount int) {
log.Printf("重试五次失败,停止发送") log.Printf("重试五次失败,停止发送")
return return
} }
// 组装参数 // 组装参数
param := map[string]any{ param := map[string]any{
"wxid": toId, // 群或好友Id "wxid": toId, // 群或好友Id
"msg": msg, // 消息 "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) pbs, _ := json.Marshal(param)
res := resty.New() res := resty.New()
resp, err := res.R(). resp, err := res.R().
SetHeader("Content-Type", "application/json;chartset=utf-8"). SetHeader("Content-Type", "application/json;chartset=utf-8").
SetBody(string(pbs)). SetBody(string(pbs)).
Post(config.Conf.Wechat.GetURL("/api/sendTextMsg")) Post(apiUrl)
if err != nil { if err != nil {
log.Printf("发送文本消息失败: %s", err.Error()) log.Printf("发送文本消息失败: %s", err.Error())
// 休眠五秒后重新发送 // 休眠五秒后重新发送