From e3c9470b4d1ea10c836e281f534208c9a077b6f0 Mon Sep 17 00:00:00 2001 From: Cool <747682928@qq.com> Date: Tue, 5 Nov 2024 18:38:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=99=BB=E5=BD=95=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wechat_client.go | 108 +++++++++++++++++++++++------------------------ 1 file changed, 54 insertions(+), 54 deletions(-) diff --git a/wechat_client.go b/wechat_client.go index e0b2be0..e7eb51f 100644 --- a/wechat_client.go +++ b/wechat_client.go @@ -1,79 +1,80 @@ package main + import ( - "fmt" - "strings" - "net/http" - "io/ioutil" - "time" - "os" "bytes" "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "os" + "strings" + "time" + "github.com/eatmoreapple/openwechat" ) func Use(vals ...interface{}) { - for _, val := range vals { - _ = val - } + for _, val := range vals { + _ = val + } } type SendTextRequest struct { - InGroup bool `json:"in_group"` //本来想用于区分在群聊和非群聊时的上下文记忆规则,但是最终没有实现... - UserID string `json:"user_id"` - Text string `json:"text"` + InGroup bool `json:"in_group"` //本来想用于区分在群聊和非群聊时的上下文记忆规则,但是最终没有实现... + UserID string `json:"user_id"` + Text string `json:"text"` } type SendTextResponse struct { - UserID string `json:"user_id"` - Text string `json:"text"` - HasError bool `json:"error"` + UserID string `json:"user_id"` + Text string `json:"text"` + HasError bool `json:"error"` ErrorMessage string `json:"error_msg"` } type SendImageRequest struct { - UserName string `json:"user_name"` - FileNames []string `json:"filenames"` - HasError bool `json:"error"` - ErrorMessage string `json:"error_msg"` + UserName string `json:"user_name"` + FileNames []string `json:"filenames"` + HasError bool `json:"error"` + ErrorMessage string `json:"error_msg"` } type GenerateImageRequest struct { - UserName string `json:"user_name"` - Prompt string `json:"prompt"` + UserName string `json:"user_name"` + Prompt string `json:"prompt"` } func HttpPost(url string, data interface{}, timelim int) []byte { - // 超时时间 - timeout, _ := time.ParseDuration(fmt.Sprintf("%ss", timelim)) //是的,这里有个bug,但是这里就是靠这个bug正常运行的!!!??? + // 超时时间 + 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() + 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 + result, _ := ioutil.ReadAll(resp.Body) + return result -// ——————————————— -// 版权声明:本文为CSDN博主「gaoluhua」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 -// 原文链接:https://blog.csdn.net/gaoluhua/article/details/124855716 + // ——————————————— + // 版权声明:本文为CSDN博主「gaoluhua」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 + // 原文链接:https://blog.csdn.net/gaoluhua/article/details/124855716 } func main() { - bot := openwechat.DefaultBot(openwechat.Desktop) // 桌面模式,上面登录不上的可以尝试切换这种模式 - reloadStorage := openwechat.NewJsonFileHotReloadStorage("storage.json") - defer reloadStorage.Close() + bot := openwechat.DefaultBot(openwechat.Desktop) // 桌面模式 - err := bot.PushLogin(reloadStorage, openwechat.NewRetryLoginOption()) - if err != nil { + // 注册登陆二维码回调 + bot.UUIDCallback = openwechat.PrintlnQrcodeUrl + if err := bot.Login(); err != nil { fmt.Println(err) return } - + // 获取登陆的用户 self, err := bot.GetCurrentUser() if err != nil { @@ -94,7 +95,7 @@ func main() { return } - // fmt.Println(msg.Content) + // fmt.Println(msg.Content) content := msg.Content if msg.IsSendByGroup() && !msg.IsAt() { @@ -109,20 +110,19 @@ func main() { } } //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 + // 调用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) + resp_raw := HttpPost("http://localhost:11111/draw", GenerateImageRequest{UserName: sender.ID(), Prompt: content}, 120) if len(resp_raw) == 0 { msg.ReplyText("生成图片出错啦QwQ,或许可以再试一次") return @@ -132,7 +132,7 @@ func main() { json.Unmarshal(resp_raw, &resp) //fmt.Println(resp.FileName) if resp.HasError { - msg.ReplyText( fmt.Sprintf("生成图片出错啦QwQ,错误信息是:%s", resp.ErrorMessage) ) + msg.ReplyText(fmt.Sprintf("生成图片出错啦QwQ,错误信息是:%s", resp.ErrorMessage)) } else { for i := 0; i < len(resp.FileNames); i++ { img, _ := os.Open(resp.FileNames[i]) @@ -149,14 +149,14 @@ func main() { var group *openwechat.Group = nil if msg.IsSendByGroup() { - group = &openwechat.Group{User : sender} + group = &openwechat.Group{User: sender} } if content == "重置上下文" { if !msg.IsSendByGroup() { - HttpPost("http://localhost:11111/chat_clear", SendTextRequest{InGroup : msg.IsSendByGroup(), UserID : sender.ID(), Text : ""}, 60) + HttpPost("http://localhost:11111/chat_clear", SendTextRequest{InGroup: msg.IsSendByGroup(), UserID: sender.ID(), Text: ""}, 60) } else { - HttpPost("http://localhost:11111/chat_clear", SendTextRequest{InGroup : msg.IsSendByGroup(), UserID : group.ID(), Text : ""}, 60) + HttpPost("http://localhost:11111/chat_clear", SendTextRequest{InGroup: msg.IsSendByGroup(), UserID: group.ID(), Text: ""}, 60) } msg.ReplyText("OK,我忘掉了之前的上下文。") return @@ -166,9 +166,9 @@ func main() { resp_raw := []byte("") if !msg.IsSendByGroup() { - resp_raw = HttpPost("http://localhost:11111/chat", SendTextRequest{InGroup : false, UserID : sender.ID(), Text : msg.Content}, 60) + 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) + 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,或许可以再试一次。") @@ -201,6 +201,6 @@ func main() { } } - + bot.Block() -} \ No newline at end of file +}