Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(QQ): 针对NCqq的图像格式的兼容性改进 #1066

Merged
merged 6 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 50 additions & 18 deletions dice/platform_adapter_gocq.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dice

import (
"encoding/json"
"errors"
"fmt"
"math/rand"
"os"
Expand All @@ -18,6 +19,8 @@ import (
"github.com/labstack/echo/v4"
"github.com/sacOO7/gowebsocket"
"github.com/samber/lo"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
"gopkg.in/yaml.v3"

"sealdice-core/message"
Expand Down Expand Up @@ -273,38 +276,61 @@ func FormatDiceIDQQChGroup(guildID, channelID string) string {
return fmt.Sprintf("QQ-CH-Group:%s-%s", guildID, channelID)
}

func tryParseOneBot11ArrayMessage(log *log.Helper, message string, writeTo *MessageQQ) error {
msgQQType2 := new(MessageQQArray)
err := json.Unmarshal([]byte(message), msgQQType2)
func hasURLScheme(text string) bool {
// 正则表达式匹配三种情况:file URI、http(s) URL 和 base64 URI
regex := `^[a-z]+://`
match, _ := regexp.MatchString(regex, text)
return match
}

if err != nil {
func tryParseOneBot11ArrayMessage(log *log.Helper, message string, writeTo *MessageQQ) error {
// 不合法的信息体
if !gjson.Valid(message) {
log.Warn("无法解析 onebot11 字段:", message)
return err
return errors.New("解析失败")
}

// 原版本转换为gjson对象
parseContent := gjson.Parse(message)
arrayContent := parseContent.Get("message").Array()
cqMessage := strings.Builder{}

for _, i := range msgQQType2.Message {
switch i.Type {
for _, i := range arrayContent {
// 使用String()方法,如果为空,会自动产生空字符串
typeStr := i.Get("type").String()
dataObj := i.Get("data")
switch typeStr {
case "text":
cqMessage.WriteString(i.Data["text"].(string))
cqMessage.WriteString(dataObj.Get("text").String())
case "image":
cqMessage.WriteString(fmt.Sprintf("[CQ:image,file=%v]", i.Data["file"]))
// 兼容NC情况, 此时file字段只有文件名, 完整URL在url字段
if !hasURLScheme(dataObj.Get("file").String()) && hasURLScheme(dataObj.Get("url").String()) {
cqMessage.WriteString(fmt.Sprintf("[CQ:image,file=%v]", dataObj.Get("url").String()))
} else {
cqMessage.WriteString(fmt.Sprintf("[CQ:image,file=%v]", dataObj.Get("file").String()))
}
case "face":
// 兼容四叶草,移除 .(string)。自动获取的信息表示此类型为 float64,这是go解析的问题
cqMessage.WriteString(fmt.Sprintf("[CQ:face,id=%v]", i.Data["id"]))
cqMessage.WriteString(fmt.Sprintf("[CQ:face,id=%v]", dataObj.Get("id").String()))
case "record":
cqMessage.WriteString(fmt.Sprintf("[CQ:record,file=%v]", i.Data["file"]))
cqMessage.WriteString(fmt.Sprintf("[CQ:record,file=%v]", dataObj.Get("file").String()))
case "at":
cqMessage.WriteString(fmt.Sprintf("[CQ:at,qq=%v]", i.Data["qq"]))
cqMessage.WriteString(fmt.Sprintf("[CQ:at,qq=%v]", dataObj.Get("qq").String()))
case "poke":
cqMessage.WriteString("[CQ:poke]")
case "reply":
cqMessage.WriteString(fmt.Sprintf("[CQ:reply,id=%v]", i.Data["id"]))
cqMessage.WriteString(fmt.Sprintf("[CQ:reply,id=%v]", dataObj.Get("id").String()))
}
}
writeTo.MessageQQBase = msgQQType2.MessageQQBase
writeTo.Message = cqMessage.String()
// 赋值对应的Message
tempStr, err := sjson.Set(parseContent.String(), "message", cqMessage.String())
if err != nil {
return err
}
// 返回被转换成结构体的结果
err = json.Unmarshal([]byte(tempStr), &writeTo)
if err != nil {
return err
}
return nil
}

Expand All @@ -329,8 +355,14 @@ func OneBot11CqMessageToArrayMessage(longText string) []interface{} {
// 将 CQ 拼入数组
switch cq.Type {
case "image":
i := OneBotV11ArrMsgItem[OneBotV11MsgItemImageType]{Type: "image", Data: OneBotV11MsgItemImageType{File: cq.Args["file"]}}
arr = append(arr, i)
// 兼容NC情况, 此时file字段只有文件名, 完整URL在url字段
if !hasURLScheme(cq.Args["file"]) && hasURLScheme(cq.Args["url"]) {
i := OneBotV11ArrMsgItem[OneBotV11MsgItemImageType]{Type: "image", Data: OneBotV11MsgItemImageType{File: cq.Args["url"]}}
arr = append(arr, i)
} else {
i := OneBotV11ArrMsgItem[OneBotV11MsgItemImageType]{Type: "image", Data: OneBotV11MsgItemImageType{File: cq.Args["file"]}}
arr = append(arr, i)
}
case "record":
i := OneBotV11ArrMsgItem[OneBotV11MsgItemRecordType]{Type: "record", Data: OneBotV11MsgItemRecordType{File: cq.Args["file"]}}
arr = append(arr, i)
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,12 @@ require (
github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af // indirect
github.com/tdewolff/parse/v2 v2.7.15 // indirect
github.com/tidwall/btree v1.7.0 // indirect
github.com/tidwall/gjson v1.17.0 // indirect
github.com/tidwall/gjson v1.18.0 // indirect
github.com/tidwall/grect v0.1.4 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/tidwall/rtred v0.1.2 // indirect
github.com/tidwall/sjson v1.2.5 // indirect
github.com/tidwall/tinyqueue v0.1.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,11 @@ github.com/tidwall/buntdb v1.3.1 h1:HKoDF01/aBhl9RjYtbaLnvX9/OuenwvQiC3OP1CcL4o=
github.com/tidwall/buntdb v1.3.1/go.mod h1:lZZrZUWzlyDJKlLQ6DKAy53LnG7m5kHyrEHvvcDmBpU=
github.com/tidwall/gjson v1.9.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.12.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.17.0 h1:/Jocvlh98kcTfpN2+JzGQWQcqrPQwDrVEMApx/M5ZwM=
github.com/tidwall/gjson v1.17.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/grect v0.1.4 h1:dA3oIgNgWdSspFzn1kS4S/RDpZFLrIxAZOdJKjYapOg=
github.com/tidwall/grect v0.1.4/go.mod h1:9FBsaYRaR0Tcy4UwefBX/UDcDcDy9V5jUcxHzv2jd5Q=
github.com/tidwall/lotsa v1.0.2 h1:dNVBH5MErdaQ/xd9s769R31/n2dXavsQ0Yf4TMEHHw8=
Expand All @@ -412,6 +415,8 @@ github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/rtred v0.1.2 h1:exmoQtOLvDoO8ud++6LwVsAMTu0KPzLTUrMln8u1yu8=
github.com/tidwall/rtred v0.1.2/go.mod h1:hd69WNXQ5RP9vHd7dqekAz+RIdtfBogmglkZSRxCHFQ=
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
github.com/tidwall/tinyqueue v0.1.1 h1:SpNEvEggbpyN5DIReaJ2/1ndroY8iyEGxPYxoSaymYE=
github.com/tidwall/tinyqueue v0.1.1/go.mod h1:O/QNHwrnjqr6IHItYrzoHAKYhBkLI67Q096fQP5zMYw=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
Expand Down
Loading