fix: 更新 ImageMessageData 结构体,修复文件大小类型并添加缺失字段;优化 ParseMessage 函数以支持动态属性解析
This commit is contained in:
parent
853f0bf603
commit
9e70cb733b
@ -6,7 +6,6 @@ import (
|
||||
"log"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -16,17 +15,24 @@ type ImageMessage struct {
|
||||
}
|
||||
|
||||
type ImageMessageData struct {
|
||||
URL string `json:"url"`
|
||||
Summary string `json:"summary"`
|
||||
File string `json:"file"`
|
||||
Key string `json:"key"`
|
||||
EmojiID string `json:"emoji_id"`
|
||||
PackageID string `json:"emoji_package_id"`
|
||||
SubType string `json:"sub_type"`
|
||||
FileID string `json:"file_id"`
|
||||
URL string `json:"url"`
|
||||
FileSize int `json:"file_size"`
|
||||
FileSize string `json:"file_size"`
|
||||
FileUnique string `json:"file_unique"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterMessageType(TypeImage, func() CQMessage {
|
||||
return &ImageMessage{Type: TypeImage}
|
||||
return &ImageMessage{
|
||||
Type: TypeImage,
|
||||
Data: ImageMessageData{},
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@ -42,7 +48,7 @@ func (msg *ImageMessage) ToCQString() string {
|
||||
escapedURL := url.QueryEscape(msg.Data.URL)
|
||||
|
||||
// 构造 CQ:image 字符串
|
||||
cqString := fmt.Sprintf("[CQ:image,file=%s,sub_type=%s,file_id=%s,url=%s,file_size=%d,file_unique=%s]",
|
||||
cqString := fmt.Sprintf("[CQ:image,file=%s,sub_type=%s,file_id=%s,url=%s,file_size=%s,file_unique=%s]",
|
||||
msg.Data.File, msg.Data.SubType, msg.Data.FileID, escapedURL, msg.Data.FileSize, msg.Data.FileUnique)
|
||||
|
||||
return cqString
|
||||
@ -51,35 +57,35 @@ func (msg *ImageMessage) ToCQString() string {
|
||||
func (msg *ImageMessage) ParseMessage(data string) error {
|
||||
log.Println("ParseMessage", data)
|
||||
// 使用正则表达式提取各个字段
|
||||
re := regexp.MustCompile(`\[CQ:image,file=(.*?),sub_type=(.*?),file_id=(.*?),url=(.*?),file_size=(\d+),file_unique=(.*?)\]`)
|
||||
re := regexp.MustCompile(`\[CQ:image,(.*?)\]`)
|
||||
matches := re.FindStringSubmatch(data)
|
||||
if len(matches) < 7 {
|
||||
return fmt.Errorf("数据格式不正确")
|
||||
if len(matches) != 2 {
|
||||
return fmt.Errorf("invalid image message format")
|
||||
}
|
||||
|
||||
// 转换file_size为整数
|
||||
fileSize, err := strconv.Atoi(matches[5])
|
||||
if err != nil {
|
||||
return fmt.Errorf("解析 file_size 出错: %v", err)
|
||||
attrs := make(map[string]string)
|
||||
pairs := strings.Split(matches[1], ",")
|
||||
for _, pair := range pairs {
|
||||
if kv := strings.SplitN(pair, "=", 2); len(kv) == 2 {
|
||||
attrs[kv[0]] = kv[1]
|
||||
}
|
||||
}
|
||||
|
||||
// 处理URL转义
|
||||
decodedURL, err := url.QueryUnescape(matches[4])
|
||||
msg.Data.URL = attrs["url"]
|
||||
msg.Data.Summary = attrs["summary"]
|
||||
msg.Data.File = attrs["file"]
|
||||
msg.Data.Key = attrs["key"]
|
||||
msg.Data.EmojiID = attrs["emoji_id"]
|
||||
msg.Data.PackageID = attrs["emoji_package_id"]
|
||||
msg.Data.SubType = attrs["sub_type"]
|
||||
msg.Data.FileID = attrs["file_id"]
|
||||
msg.Data.FileSize = attrs["file_size"]
|
||||
msg.Data.FileUnique = attrs["file_unique"]
|
||||
|
||||
decodedURL = strings.ReplaceAll(decodedURL, ",", ",")
|
||||
decodedURL = strings.ReplaceAll(decodedURL, "[", "[")
|
||||
decodedURL = strings.ReplaceAll(decodedURL, "]", "]")
|
||||
decodedURL = strings.ReplaceAll(decodedURL, "&", "&")
|
||||
if err != nil {
|
||||
return fmt.Errorf("URL 转义失败: %v", err)
|
||||
}
|
||||
msg.Data = ImageMessageData{
|
||||
File: matches[1],
|
||||
SubType: matches[2],
|
||||
FileID: matches[3],
|
||||
URL: decodedURL,
|
||||
FileSize: fileSize,
|
||||
FileUnique: matches[6],
|
||||
// URL是必需的字段(接收图片时)
|
||||
if msg.Data.URL == "" {
|
||||
return fmt.Errorf("missing required url field")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user