新增一个简易的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

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.Raw = string(msg)
// 处理At机器人的消息
if strings.HasSuffix(m.DisplayFullContent, "在群聊中@了你") {
go handleAtMessage(ent)
}
go service.SaveMessage(ent)
}