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 {
return
}
key := fmt.Sprintf("group_message:%f:%f", gid, uid)
exists, err := tools.CheckKeyExists(fmt.Sprintf("group_message:%d:%d", int64(gid), int64(uid)))
key := fmt.Sprintf("group_message:%d:%d", int64(gid), int64(uid))
exists, err := tools.CheckKeyExists(key)
if err != nil || !exists {
redisClient.RPush(context.Background(), key, jsonString, 0)
// tools.SetValue(key, string(jsonString), 10*time.Second)

View file

@ -6,15 +6,15 @@ import (
"encoding/json"
"fmt"
"go-bot/tools"
"os"
"io"
"log"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"github.com/imroc/req/v3"
openai "github.com/sashabaranov/go-openai"
)
@ -159,7 +159,7 @@ func (a *AI) GetMsg() string {
if len(matches) > 0 {
msgId = matches[1]
println("msgId:", msgId)
// println("msgId:", msgId)
} else {
msgId = ""
log.Println("未找到回复消息")
@ -185,7 +185,9 @@ func (a *AI) GetMsg() string {
// println("filePath:", filePath)
if filePath == "" {
log.Println("获取图片失败")
return "获取图片失败"
// return "获取图片失败"
// 下载picUrl文件
}
base64Img := Image2Base64(filePath, picUrl)
if base64Img == "" {
@ -223,7 +225,7 @@ func (a *AI) GetMsg() string {
)
if err != nil {
log.Println("ChatCompletion error: ", err)
return "请求失败"
return "请求失败,api可能不支持图片上传"
}
msg += resp.Choices[0].Message.Content
@ -315,10 +317,16 @@ func Image2Base64(path string, picUrl string) string {
}
// 如果文件路径不可用,则尝试从 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 {
return ""
}
defer resp.Body.Close()
if data, err := io.ReadAll(resp.Body); err == nil {

View file

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