diff --git a/handlers/send_group_msg.go b/handlers/send_group_msg.go index e13df62..44f4985 100644 --- a/handlers/send_group_msg.go +++ b/handlers/send_group_msg.go @@ -1,6 +1,9 @@ package handlers import ( + "encoding/base64" + "io" + "net/http" "strconv" "time" @@ -155,6 +158,8 @@ func generateKaiheilaMessage(foundItems map[string][]string, messageText string, } } else if imageURLs, ok := foundItems["url_image"]; ok && len(imageURLs) > 0 { newpiclink := "http://" + imageURLs[0] + //转换开黑啦img url + newpiclink, _ = uploadImageAndGetNewLink(newpiclink, Token, BaseUrl) // 发链接图片 return &Card{ Type: "card", @@ -174,6 +179,8 @@ func generateKaiheilaMessage(foundItems map[string][]string, messageText string, } } else if imageURLs, ok := foundItems["url_images"]; ok && len(imageURLs) > 0 { newpiclink := "https://" + imageURLs[0] + //转换开黑啦img url + newpiclink, _ = uploadImageAndGetNewLink(newpiclink, Token, BaseUrl) // 发链接图片 return &Card{ Type: "card", @@ -329,3 +336,32 @@ func GetMessageTypeByGroupidV2(GroupID interface{}) string { // } return msgtype } + +func downloadImage(url string) ([]byte, error) { + resp, err := http.Get(url) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + return io.ReadAll(resp.Body) +} + +func uploadImageAndGetNewLink(imageURL string, token string, baseUrl string) (string, error) { + // 下载图片 + imageData, err := downloadImage(imageURL) + if err != nil { + return "", err + } + + // 将图片数据转换为base64 + base64Image := base64.StdEncoding.EncodeToString(imageData) + + // 上传图片并获取新链接 + newURL, err := images.UploadImageBase64(base64Image, token, baseUrl) + if err != nil { + return "", err + } + + return newURL, nil +} diff --git a/wsclient/ws.go b/wsclient/ws.go index d4e74fd..419f0c3 100644 --- a/wsclient/ws.go +++ b/wsclient/ws.go @@ -41,9 +41,6 @@ func (c *WebSocketClient) SendMessage(message map[string]interface{}) error { err = c.conn.WriteMessage(websocket.TextMessage, msgBytes) if err != nil { mylog.Println("Error sending message:", err) - // if !c.isReconnecting { - // go c.Reconnect() - // } // 发送失败,将消息放入channel go func() { c.sendFailures <- message @@ -93,16 +90,15 @@ func (client *WebSocketClient) Reconnect() { reconnecttimes := config.GetReconnecTimes() newClient, err := NewWebSocketClient(client.urlStr, client.botID, client.Token, client.BaseUrl, reconnecttimes) if err == nil && newClient != nil { - client.mutex.Lock() // 在替换连接之前锁定 - oldCancel := client.cancel // 保存旧的取消函数 + client.mutex.Lock() // 在替换连接之前锁定 + client.cancel() client.conn = newClient.conn client.Token = newClient.Token client.BaseUrl = newClient.BaseUrl - oldCancel() // 停止所有相关的旧协程 client.cancel = newClient.cancel // 更新取消函数 client.mutex.Unlock() // 重发失败的消息 - go newClient.processFailedMessages(oldSendFailures) + newClient.processFailedMessages(oldSendFailures) mylog.Println("Successfully reconnected to WebSocket.") return }