feat(ai):修复ai获取图片失败的处理逻辑

This commit is contained in:
liyp 2024-09-01 14:28:52 +08:00
parent 55479c5869
commit 8443f5f5a0
3 changed files with 57 additions and 29 deletions

View file

@ -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))

View file

@ -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 ""

View file

@ -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)
}
}