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) prefix, err := config.GetConfig()["Prefix"].(string)
if !err { if !err {
panic("Prefix设置异常!") panic("Prefix参数设置异常!")
} }
// fmt.Println("raw_msg:", string(raw_msg)) // fmt.Println("raw_msg:", string(raw_msg))

View file

@ -5,6 +5,7 @@ import (
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"io" "io"
"net/http"
"os" "os"
"log" "log"
@ -94,29 +95,33 @@ func (a *AI) GetMsg() string {
log.Println("未找到回复消息") log.Println("未找到回复消息")
return "未找到回复消息" return "未找到回复消息"
} }
message := a.GetHisMsg(msgId) file, picUrl, fileSizeStr := a.GetHisMsg(msgId)
// 正则表达式匹配 file 和 file_size 的值 // 正则表达式匹配 file 和 file_size 的值
re = regexp.MustCompile(`file=([^,]+),.*file_size=(\d+)`) // re = regexp.MustCompile(`file=([^,]+),.*file_size=(\d+)`)
matches = re.FindStringSubmatch(message) // matches = re.FindStringSubmatch(message)
var file string // var file string
var fileSizeStr string // var fileSizeStr string
if len(matches) > 2 { // if len(matches) > 2 {
file = matches[1] // file = matches[1]
fileSizeStr = matches[2] // fileSizeStr = matches[2]
} else { // } else {
// log.Println("未找到文件信息")
// return "未找到文件信息"
// }
// 将 fileSizeStr 转换为整数
if picUrl == "" {
log.Println("未找到文件信息") log.Println("未找到文件信息")
return "未找到文件信息" return "未找到文件信息"
} }
// 将 fileSizeStr 转换为整数
fileSize, err := strconv.ParseFloat(fileSizeStr, 64) fileSize, err := strconv.ParseFloat(fileSizeStr, 64)
if err != nil { if err != nil {
fmt.Println("获取图片大小失败:", err) fmt.Println("获取图片大小失败:", err)
return "获取图片大小失败" return "获取图片大小失败"
} }
if fileSize/1024/1024 > 2.0 { if fileSize/1024/1024 > 5.0 {
log.Println("文件大小超过2M") log.Println("文件大小超过5M")
return "文件大小超过2M" return "文件大小超过5M"
} }
filePath := a.GetImage(file) filePath := a.GetImage(file)
// println("filePath:", filePath) // println("filePath:", filePath)
@ -124,7 +129,7 @@ func (a *AI) GetMsg() string {
log.Println("获取图片失败") log.Println("获取图片失败")
return "获取图片失败" return "获取图片失败"
} }
base64Img := Image2Base64(filePath) base64Img := Image2Base64(filePath, picUrl)
if base64Img == "" { if base64Img == "" {
log.Println("图片转换base64失败") log.Println("图片转换base64失败")
return "图片转换base64失败" return "图片转换base64失败"
@ -211,14 +216,24 @@ func getConfig() (string, string, string) {
return OPENAI_API_KEY, OPENAI_BaseURL, MODEL return OPENAI_API_KEY, OPENAI_BaseURL, MODEL
} }
func Image2Base64(path string) string { func Image2Base64(path string, picUrl string) string {
// 尝试从文件路径读取图片
file, err := os.Open(path) 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 { if err != nil {
return "" 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 "data:image/jpeg;base64," + base64.StdEncoding.EncodeToString(data)
} }
return "" return ""

View file

@ -13,7 +13,7 @@ type Worker interface {
GetMsg() string GetMsg() string
SendMsg(msg string) bool SendMsg(msg string) bool
GetImage(file string) string GetImage(file string) string
GetHisMsg(id string) string GetHisMsg(id string) (string, string, string)
} }
type StdAns struct { type StdAns struct {
AllowGroup []interface{} 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 == "" { if id == "" {
return "" return "", "", ""
} }
url := cfg["POSTURL"].(string) + "/get_msg?message_id=" + id url := cfg["POSTURL"].(string) + "/get_msg?message_id=" + id
@ -128,22 +128,35 @@ func (s *StdAns) GetHisMsg(id string) string {
if len(errs) > 0 { if len(errs) > 0 {
fmt.Println("Error sending request:", errs) fmt.Println("Error sending request:", errs)
return "" return "", "", ""
} }
defer resp.Body.Close() defer resp.Body.Close()
data := make(map[string]interface{}) data := make(map[string]interface{})
err := json.Unmarshal([]byte(body), &data) err := json.Unmarshal([]byte(body), &data)
if err != nil { if err != nil {
fmt.Println("解析JSON失败:", err) fmt.Println("解析JSON失败:", err)
return "" return "", "", ""
} }
if data["status"] == "ok" {
// fmt.Println("响应返回:", body) // "message": [
raw_message, ok := data["data"].(map[string]interface{})["raw_message"].(string) // {
if !ok { // "type": "image",
return "" // "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 { } else {
return raw_message // fmt.Println("响应返回:", body)
return "", "", ""
// println("raw_message", raw_message)
} }
} }