From 8443f5f5a0455a0dfb845d68d9b866d1d2535cf5 Mon Sep 17 00:00:00 2001 From: liyp Date: Sun, 1 Sep 2024 14:28:52 +0800 Subject: [PATCH] =?UTF-8?q?feat(ai):=E4=BF=AE=E5=A4=8Dai=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E5=A4=B1=E8=B4=A5=E7=9A=84=E5=A4=84=E7=90=86?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/router.go | 2 +- workers/ai.go | 49 ++++++++++++++++++++++++++++++++----------------- workers/core.go | 35 ++++++++++++++++++++++++----------- 3 files changed, 57 insertions(+), 29 deletions(-) diff --git a/utils/router.go b/utils/router.go index 7389854..726ef1d 100644 --- a/utils/router.go +++ b/utils/router.go @@ -35,7 +35,7 @@ func Router(data map[string]interface{}) { } prefix, err := config.GetConfig()["Prefix"].(string) if !err { - panic("Prefix设置异常!") + panic("Prefix参数设置异常!") } // fmt.Println("raw_msg:", string(raw_msg)) diff --git a/workers/ai.go b/workers/ai.go index 5d46d0f..72c5086 100644 --- a/workers/ai.go +++ b/workers/ai.go @@ -5,6 +5,7 @@ import ( "encoding/base64" "fmt" "io" + "net/http" "os" "log" @@ -94,29 +95,33 @@ func (a *AI) GetMsg() string { log.Println("未找到回复消息") return "未找到回复消息" } - message := a.GetHisMsg(msgId) + file, picUrl, fileSizeStr := a.GetHisMsg(msgId) // 正则表达式匹配 file 和 file_size 的值 - re = regexp.MustCompile(`file=([^,]+),.*file_size=(\d+)`) - matches = re.FindStringSubmatch(message) - var file string - var fileSizeStr string - if len(matches) > 2 { - file = matches[1] - fileSizeStr = matches[2] - } else { + // re = regexp.MustCompile(`file=([^,]+),.*file_size=(\d+)`) + // matches = re.FindStringSubmatch(message) + // var file string + // var fileSizeStr string + // if len(matches) > 2 { + // file = matches[1] + // fileSizeStr = matches[2] + // } else { + // log.Println("未找到文件信息") + // return "未找到文件信息" + // } + // 将 fileSizeStr 转换为整数 + if picUrl == "" { log.Println("未找到文件信息") return "未找到文件信息" } - // 将 fileSizeStr 转换为整数 fileSize, err := strconv.ParseFloat(fileSizeStr, 64) if err != nil { fmt.Println("获取图片大小失败:", err) return "获取图片大小失败" } - if fileSize/1024/1024 > 2.0 { - log.Println("文件大小超过2M") - return "文件大小超过2M" + if fileSize/1024/1024 > 5.0 { + log.Println("文件大小超过5M") + return "文件大小超过5M" } filePath := a.GetImage(file) // println("filePath:", filePath) @@ -124,7 +129,7 @@ func (a *AI) GetMsg() string { log.Println("获取图片失败") return "获取图片失败" } - base64Img := Image2Base64(filePath) + base64Img := Image2Base64(filePath, picUrl) if base64Img == "" { log.Println("图片转换base64失败") return "图片转换base64失败" @@ -211,14 +216,24 @@ func getConfig() (string, string, string) { return OPENAI_API_KEY, OPENAI_BaseURL, MODEL } -func Image2Base64(path string) string { +func Image2Base64(path string, picUrl string) string { + // 尝试从文件路径读取图片 file, err := os.Open(path) + if err == nil { + defer file.Close() + if data, err := io.ReadAll(file); err == nil { + return "data:image/jpeg;base64," + base64.StdEncoding.EncodeToString(data) + } + } + + // 如果文件路径不可用,则尝试从 URL 下载图片 + resp, err := http.Get(picUrl) if err != nil { return "" } - defer file.Close() + defer resp.Body.Close() - if data, err := io.ReadAll(file); err == nil { + if data, err := io.ReadAll(resp.Body); err == nil { return "data:image/jpeg;base64," + base64.StdEncoding.EncodeToString(data) } return "" diff --git a/workers/core.go b/workers/core.go index b05295c..717814c 100644 --- a/workers/core.go +++ b/workers/core.go @@ -13,7 +13,7 @@ type Worker interface { GetMsg() string SendMsg(msg string) bool GetImage(file string) string - GetHisMsg(id string) string + GetHisMsg(id string) (string, string, string) } type StdAns struct { AllowGroup []interface{} @@ -117,9 +117,9 @@ func (s *StdAns) GetMsg() string { // } } -func (s *StdAns) GetHisMsg(id string) string { +func (s *StdAns) GetHisMsg(id string) (string, string, string) { if id == "" { - return "" + return "", "", "" } url := cfg["POSTURL"].(string) + "/get_msg?message_id=" + id @@ -128,22 +128,35 @@ func (s *StdAns) GetHisMsg(id string) string { if len(errs) > 0 { fmt.Println("Error sending request:", errs) - return "" + return "", "", "" } defer resp.Body.Close() data := make(map[string]interface{}) err := json.Unmarshal([]byte(body), &data) if err != nil { fmt.Println("解析JSON失败:", err) - return "" + return "", "", "" } - - // fmt.Println("响应返回:", body) - raw_message, ok := data["data"].(map[string]interface{})["raw_message"].(string) - if !ok { - return "" + if data["status"] == "ok" { + // "message": [ + // { + // "type": "image", + // "data": { + // "file": "NapCatOneBot-MsgFile-2-874769998-7483754018991870377-7409536985154972756", + // "sub_type": 1, + // "file_id": "NapCatOneBot-MsgFile-2-874769998-7483754018991870377-7409536985154972756", + // "url": "https://multimedia.nt.qq.com.cn/download?appid=1407&fileid=Cgk3OTQ1MDg5ODYSFL7bvc7L2hg8RHGXJQcDvMBVdYnBGOvXAyD_CiiUi5zw_KCIA1CAvaMB&spec=0&rkey=CAQSKAB6JWENi5LMzvLUe0XM_1cgokyjgML0etpRKmPhrFq1UBxRN8XOeWQ", + // "file_size": "60395" + // } + // } + // ], + message := data["data"].(map[string]interface{})["message"].(map[string]interface{})["data"].(map[string]interface{}) + return message["file"].(string), message["url"].(string), message["file_id"].(string) } else { - return raw_message + // fmt.Println("响应返回:", body) + return "", "", "" + // println("raw_message:", raw_message) + } }