Skip to content

Commit

Permalink
Beta10 (#12)
Browse files Browse the repository at this point in the history
* beta1

* beta2

* beta3

* beta4

* beta5

* beta6

* beta7

* beta8

* beta9

* beta10
  • Loading branch information
Hoshinonyaruko authored Jan 13, 2024
1 parent bb248c7 commit b0c7a32
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
36 changes: 36 additions & 0 deletions handlers/send_group_msg.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package handlers

import (
"encoding/base64"
"io"
"net/http"
"strconv"
"time"

Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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
}
10 changes: 3 additions & 7 deletions wsclient/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit b0c7a32

Please sign in to comment.