2023-02-05 21:27:35 +08:00
|
|
|
|
package main
|
2024-11-05 18:38:23 +08:00
|
|
|
|
|
2023-02-05 21:27:35 +08:00
|
|
|
|
import (
|
2024-11-05 18:38:23 +08:00
|
|
|
|
"bytes"
|
|
|
|
|
"encoding/json"
|
2023-02-05 21:27:35 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
"io/ioutil"
|
2024-11-05 18:38:23 +08:00
|
|
|
|
"net/http"
|
2023-02-05 21:27:35 +08:00
|
|
|
|
"os"
|
2024-11-05 18:38:23 +08:00
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
2023-02-05 21:27:35 +08:00
|
|
|
|
"github.com/eatmoreapple/openwechat"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func Use(vals ...interface{}) {
|
2024-11-05 18:38:23 +08:00
|
|
|
|
for _, val := range vals {
|
|
|
|
|
_ = val
|
|
|
|
|
}
|
2023-02-05 21:27:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SendTextRequest struct {
|
2024-11-05 18:38:23 +08:00
|
|
|
|
InGroup bool `json:"in_group"` //本来想用于区分在群聊和非群聊时的上下文记忆规则,但是最终没有实现...
|
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
|
Text string `json:"text"`
|
2023-02-05 21:27:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-02 16:43:10 +08:00
|
|
|
|
type SendTextResponse struct {
|
2024-11-05 18:38:23 +08:00
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
|
Text string `json:"text"`
|
|
|
|
|
HasError bool `json:"error"`
|
2023-07-02 16:43:10 +08:00
|
|
|
|
ErrorMessage string `json:"error_msg"`
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-05 21:27:35 +08:00
|
|
|
|
type SendImageRequest struct {
|
2024-11-05 18:38:23 +08:00
|
|
|
|
UserName string `json:"user_name"`
|
|
|
|
|
FileNames []string `json:"filenames"`
|
|
|
|
|
HasError bool `json:"error"`
|
|
|
|
|
ErrorMessage string `json:"error_msg"`
|
2023-02-05 21:27:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type GenerateImageRequest struct {
|
2024-11-05 18:38:23 +08:00
|
|
|
|
UserName string `json:"user_name"`
|
|
|
|
|
Prompt string `json:"prompt"`
|
2023-02-05 21:27:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func HttpPost(url string, data interface{}, timelim int) []byte {
|
2024-11-05 18:38:23 +08:00
|
|
|
|
// 超时时间
|
|
|
|
|
timeout, _ := time.ParseDuration(fmt.Sprintf("%ss", timelim)) //是的,这里有个bug,但是这里就是靠这个bug正常运行的!!!???
|
|
|
|
|
|
|
|
|
|
client := &http.Client{Timeout: timeout}
|
|
|
|
|
jsonStr, _ := json.Marshal(data)
|
|
|
|
|
resp, err := client.Post(url, "application/json", bytes.NewBuffer(jsonStr))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return []byte("")
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
result, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
// ———————————————
|
|
|
|
|
// 版权声明:本文为CSDN博主「gaoluhua」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
|
|
|
|
|
// 原文链接:https://blog.csdn.net/gaoluhua/article/details/124855716
|
2023-02-05 21:27:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
2024-11-05 18:38:23 +08:00
|
|
|
|
bot := openwechat.DefaultBot(openwechat.Desktop) // 桌面模式
|
2023-02-05 21:27:35 +08:00
|
|
|
|
|
2024-11-05 18:38:23 +08:00
|
|
|
|
// 注册登陆二维码回调
|
|
|
|
|
bot.UUIDCallback = openwechat.PrintlnQrcodeUrl
|
|
|
|
|
if err := bot.Login(); err != nil {
|
2023-02-05 21:27:35 +08:00
|
|
|
|
fmt.Println(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-11-05 18:38:23 +08:00
|
|
|
|
|
2023-02-05 21:27:35 +08:00
|
|
|
|
// 获取登陆的用户
|
|
|
|
|
self, err := bot.GetCurrentUser()
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Use(self)
|
|
|
|
|
|
|
|
|
|
// 注册消息处理函数
|
|
|
|
|
bot.MessageHandler = func(msg *openwechat.Message) {
|
2024-11-08 18:15:08 +08:00
|
|
|
|
go sendMessage(msg, self)
|
|
|
|
|
}
|
2023-02-05 21:27:35 +08:00
|
|
|
|
|
2024-11-08 18:15:08 +08:00
|
|
|
|
bot.Block()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func sendMessage(msg *openwechat.Message, self *openwechat.Self) {
|
|
|
|
|
if msg.IsTickledMe() {
|
|
|
|
|
msg.ReplyText("别拍了,机器人是会被拍坏掉的。")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !msg.IsText() {
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-02-05 21:27:35 +08:00
|
|
|
|
|
2024-11-08 18:15:08 +08:00
|
|
|
|
// fmt.Println(msg.Content)
|
2023-02-05 23:32:33 +08:00
|
|
|
|
|
2024-11-08 18:15:08 +08:00
|
|
|
|
content := msg.Content
|
|
|
|
|
if msg.IsSendByGroup() && !msg.IsAt() {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if msg.IsSendByGroup() && msg.IsAt() {
|
|
|
|
|
atheader := fmt.Sprintf("@%s", self.NickName)
|
|
|
|
|
//fmt.Println(atheader)
|
|
|
|
|
if strings.HasPrefix(content, atheader) {
|
|
|
|
|
content = strings.TrimLeft(content[len(atheader):], " \t\n")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//fmt.Println(content)
|
|
|
|
|
content = strings.TrimRight(content, " \t\n")
|
|
|
|
|
if content == "查看机器人信息" {
|
|
|
|
|
info := HttpPost("http://localhost:11111/info", nil, 20)
|
|
|
|
|
msg.ReplyText(string(info))
|
|
|
|
|
|
|
|
|
|
} else if strings.HasPrefix(content, "生成图片") {
|
|
|
|
|
// 调用Stable Diffusion
|
|
|
|
|
// msg.ReplyText("这个功能还没有实现,可以先期待一下~")
|
|
|
|
|
sender, _ := msg.Sender()
|
|
|
|
|
content = strings.TrimLeft(content[len("生成图片"):], " \t\n")
|
|
|
|
|
|
|
|
|
|
resp_raw := HttpPost("http://localhost:11111/draw", GenerateImageRequest{UserName: sender.ID(), Prompt: content}, 120)
|
|
|
|
|
if len(resp_raw) == 0 {
|
|
|
|
|
msg.ReplyText("生成图片出错啦QwQ,或许可以再试一次")
|
2023-02-05 21:27:35 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-08 18:15:08 +08:00
|
|
|
|
resp := SendImageRequest{}
|
|
|
|
|
json.Unmarshal(resp_raw, &resp)
|
|
|
|
|
//fmt.Println(resp.FileName)
|
|
|
|
|
if resp.HasError {
|
|
|
|
|
msg.ReplyText(fmt.Sprintf("生成图片出错啦QwQ,错误信息是:%s", resp.ErrorMessage))
|
|
|
|
|
} else {
|
|
|
|
|
for i := 0; i < len(resp.FileNames); i++ {
|
|
|
|
|
img, _ := os.Open(resp.FileNames[i])
|
|
|
|
|
defer img.Close()
|
|
|
|
|
msg.ReplyImage(img)
|
2023-02-05 21:27:35 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-08 18:15:08 +08:00
|
|
|
|
} else {
|
|
|
|
|
// 调用GPT
|
2023-02-05 21:27:35 +08:00
|
|
|
|
|
2024-11-08 18:15:08 +08:00
|
|
|
|
sender, _ := msg.Sender()
|
|
|
|
|
//var group openwechat.Group{} = nil
|
|
|
|
|
var group *openwechat.Group = nil
|
2023-02-05 21:27:35 +08:00
|
|
|
|
|
2024-11-08 18:15:08 +08:00
|
|
|
|
if msg.IsSendByGroup() {
|
|
|
|
|
group = &openwechat.Group{User: sender}
|
|
|
|
|
}
|
2023-02-05 21:27:35 +08:00
|
|
|
|
|
2024-11-08 18:15:08 +08:00
|
|
|
|
if content == "重置上下文" {
|
2023-02-05 21:27:35 +08:00
|
|
|
|
if !msg.IsSendByGroup() {
|
2024-11-08 18:15:08 +08:00
|
|
|
|
HttpPost("http://localhost:11111/chat_clear", SendTextRequest{InGroup: msg.IsSendByGroup(), UserID: sender.ID(), Text: ""}, 60)
|
2023-02-05 21:27:35 +08:00
|
|
|
|
} else {
|
2024-11-08 18:15:08 +08:00
|
|
|
|
HttpPost("http://localhost:11111/chat_clear", SendTextRequest{InGroup: msg.IsSendByGroup(), UserID: group.ID(), Text: ""}, 60)
|
2023-02-05 21:27:35 +08:00
|
|
|
|
}
|
2024-11-08 18:15:08 +08:00
|
|
|
|
msg.ReplyText("OK,我忘掉了之前的上下文。")
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-02-05 21:27:35 +08:00
|
|
|
|
|
2024-11-08 18:15:08 +08:00
|
|
|
|
resp := SendTextResponse{}
|
|
|
|
|
resp_raw := []byte("")
|
2023-02-05 21:27:35 +08:00
|
|
|
|
|
2024-11-08 18:15:08 +08:00
|
|
|
|
if !msg.IsSendByGroup() {
|
|
|
|
|
resp_raw = HttpPost("http://localhost:11111/chat", SendTextRequest{InGroup: false, UserID: sender.ID(), Text: msg.Content}, 60)
|
|
|
|
|
} else {
|
|
|
|
|
resp_raw = HttpPost("http://localhost:11111/chat", SendTextRequest{InGroup: false, UserID: group.ID(), Text: msg.Content}, 60)
|
|
|
|
|
}
|
|
|
|
|
if len(resp_raw) == 0 {
|
|
|
|
|
msg.ReplyText("运算超时了QAQ,或许可以再试一次。")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
json.Unmarshal(resp_raw, &resp)
|
|
|
|
|
|
|
|
|
|
if len(resp.Text) == 0 {
|
|
|
|
|
msg.ReplyText("GPT对此没有什么想说的,换个话题吧。")
|
|
|
|
|
} else {
|
|
|
|
|
if resp.HasError {
|
|
|
|
|
if msg.IsSendByGroup() {
|
|
|
|
|
sender_in_group, _ := msg.SenderInGroup()
|
|
|
|
|
nickname := sender_in_group.NickName
|
|
|
|
|
msg.ReplyText(fmt.Sprintf("@%s\n%s\n-------------------\n%s", nickname, content, resp.ErrorMessage))
|
|
|
|
|
} else {
|
|
|
|
|
msg.ReplyText(resp.ErrorMessage)
|
|
|
|
|
}
|
2023-02-05 21:27:35 +08:00
|
|
|
|
} else {
|
2024-11-08 18:15:08 +08:00
|
|
|
|
if msg.IsSendByGroup() {
|
|
|
|
|
sender_in_group, _ := msg.SenderInGroup()
|
|
|
|
|
nickname := sender_in_group.NickName
|
|
|
|
|
msg.ReplyText(fmt.Sprintf("@%s\n%s\n-------------------\n%s", nickname, content, resp.Text))
|
2023-02-05 21:27:35 +08:00
|
|
|
|
} else {
|
2024-11-08 18:15:08 +08:00
|
|
|
|
msg.ReplyText(resp.Text)
|
2023-02-05 21:27:35 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-05 18:38:23 +08:00
|
|
|
|
|
2024-11-08 18:15:08 +08:00
|
|
|
|
}
|
2024-11-05 18:38:23 +08:00
|
|
|
|
}
|