refactor(router): 修复Redis键格式错误并优化键生成逻辑

refactor(ai): 优化图片下载逻辑,移除不必要的日志输出
refactor(pkg): 简化网络错误提示信息
This commit is contained in:
liyp 2024-10-20 20:58:25 +08:00
parent 79a41bc5ad
commit b33e44e07c
3 changed files with 17 additions and 9 deletions

View file

@ -48,8 +48,8 @@ func Router(data map[string]interface{}) {
if err != nil { if err != nil {
return return
} }
key := fmt.Sprintf("group_message:%f:%f", gid, uid) key := fmt.Sprintf("group_message:%d:%d", int64(gid), int64(uid))
exists, err := tools.CheckKeyExists(fmt.Sprintf("group_message:%d:%d", int64(gid), int64(uid))) exists, err := tools.CheckKeyExists(key)
if err != nil || !exists { if err != nil || !exists {
redisClient.RPush(context.Background(), key, jsonString, 0) redisClient.RPush(context.Background(), key, jsonString, 0)
// tools.SetValue(key, string(jsonString), 10*time.Second) // tools.SetValue(key, string(jsonString), 10*time.Second)

View file

@ -6,15 +6,15 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"go-bot/tools" "go-bot/tools"
"os"
"io" "io"
"log" "log"
"net/http"
"os"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"github.com/imroc/req/v3"
openai "github.com/sashabaranov/go-openai" openai "github.com/sashabaranov/go-openai"
) )
@ -159,7 +159,7 @@ func (a *AI) GetMsg() string {
if len(matches) > 0 { if len(matches) > 0 {
msgId = matches[1] msgId = matches[1]
println("msgId:", msgId) // println("msgId:", msgId)
} else { } else {
msgId = "" msgId = ""
log.Println("未找到回复消息") log.Println("未找到回复消息")
@ -185,7 +185,9 @@ func (a *AI) GetMsg() string {
// println("filePath:", filePath) // println("filePath:", filePath)
if filePath == "" { if filePath == "" {
log.Println("获取图片失败") log.Println("获取图片失败")
return "获取图片失败" // return "获取图片失败"
// 下载picUrl文件
} }
base64Img := Image2Base64(filePath, picUrl) base64Img := Image2Base64(filePath, picUrl)
if base64Img == "" { if base64Img == "" {
@ -223,7 +225,7 @@ func (a *AI) GetMsg() string {
) )
if err != nil { if err != nil {
log.Println("ChatCompletion error: ", err) log.Println("ChatCompletion error: ", err)
return "请求失败" return "请求失败,api可能不支持图片上传"
} }
msg += resp.Choices[0].Message.Content msg += resp.Choices[0].Message.Content
@ -315,10 +317,16 @@ func Image2Base64(path string, picUrl string) string {
} }
// 如果文件路径不可用,则尝试从 URL 下载图片 // 如果文件路径不可用,则尝试从 URL 下载图片
resp, err := http.Get(picUrl)
client := req.C().
SetUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36").
SetTLSFingerprintChrome() // 模拟 Chrome 浏览器的 TLS 握手指纹,让网站相信这是 Chrome 浏览器在访问,予以通行。
resp, err := client.R().Get(picUrl)
if err != nil { if err != nil {
return "" return ""
} }
defer resp.Body.Close() defer resp.Body.Close()
if data, err := io.ReadAll(resp.Body); err == nil { if data, err := io.ReadAll(resp.Body); err == nil {

View file

@ -46,7 +46,7 @@ func (a *Pkg) GetMsg() string {
SetSuccessResult(&pkg). SetSuccessResult(&pkg).
Get(url) Get(url)
if err != nil { if err != nil {
return "服务器网络错误!" return "网络错误!"
} }
defer resp.Body.Close() defer resp.Body.Close()