From d26007d56340ee422d2a0fe66dab75ac83967acb Mon Sep 17 00:00:00 2001 From: lixiangwuxian Date: Sun, 20 Oct 2024 01:25:28 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=9B=BE=E7=89=87cq=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/cq_message.go | 81 +++++++++++++++++++ .../{gocq_message.go => upstream_message.go} | 42 ---------- 2 files changed, 81 insertions(+), 42 deletions(-) create mode 100644 model/cq_message.go rename model/{gocq_message.go => upstream_message.go} (79%) diff --git a/model/cq_message.go b/model/cq_message.go new file mode 100644 index 0000000..c9a0e55 --- /dev/null +++ b/model/cq_message.go @@ -0,0 +1,81 @@ +package model + +import ( + "fmt" + "net/url" + "regexp" + "strconv" +) + +// @某个QQ用户的结构体 +type AtMessage struct { + Type string `json:"type"` + Data AtData `json:"data"` +} + +// @消息数据结构体 +type AtData struct { + QQ string `json:"qq"` +} + +// 回复消息结构体 +type ReplyMessage struct { + Type string `json:"type"` + Data ReplyData `json:"data"` +} + +// 回复消息数据结构体 +type ReplyData struct { + ID string `json:"id"` +} + +type CQImageMessage struct { + File string + SubType string + FileID string + URL string + FileSize int + FileUnique string +} + +func (msg *CQImageMessage) ToCQString() (string, error) { + // URL 转义 + escapedURL := url.QueryEscape(msg.URL) + + // 构造 CQ:image 字符串 + cqString := fmt.Sprintf("[CQ:image,file=%s,sub_type=%s,file_id=%s,url=%s,file_size=%d,file_unique=%s]", + msg.File, msg.SubType, msg.FileID, escapedURL, msg.FileSize, msg.FileUnique) + + return cqString, nil +} + +func ParseCQImageMessage(data string) (*CQImageMessage, error) { + // 使用正则表达式提取各个字段 + re := regexp.MustCompile(`CQ:image,file=(.*?),sub_type=(.*?),file_id=(.*?),url=(.*?),file_size=(\d+),file_unique=(.*?)\]`) + matches := re.FindStringSubmatch(data) + if len(matches) < 7 { + return nil, fmt.Errorf("数据格式不正确") + } + + // 转换file_size为整数 + fileSize, err := strconv.Atoi(matches[5]) + if err != nil { + return nil, fmt.Errorf("解析 file_size 出错: %v", err) + } + + // 处理URL转义 + decodedURL, err := url.QueryUnescape(matches[4]) + if err != nil { + return nil, fmt.Errorf("URL 转义失败: %v", err) + } + + // 返回解析后的结构体 + return &CQImageMessage{ + File: matches[1], + SubType: matches[2], + FileID: matches[3], + URL: decodedURL, + FileSize: fileSize, + FileUnique: matches[6], + }, nil +} diff --git a/model/gocq_message.go b/model/upstream_message.go similarity index 79% rename from model/gocq_message.go rename to model/upstream_message.go index df86f13..d8eccb1 100644 --- a/model/gocq_message.go +++ b/model/upstream_message.go @@ -1,53 +1,11 @@ package model import ( - "fmt" - "git.lxtend.com/qqbot/constants" ) // 消息接口 -// @某个QQ用户的结构体 -type AtMessage struct { - Type string `json:"type"` - Data AtData `json:"data"` -} - -// @消息数据结构体 -type AtData struct { - QQ string `json:"qq"` -} - -// 回复消息结构体 -type ReplyMessage struct { - Type string `json:"type"` - Data ReplyData `json:"data"` -} - -// 回复消息数据结构体 -type ReplyData struct { - ID string `json:"id"` -} - -func GenReplyMsg(msgID int64) ReplyMessage { - return ReplyMessage{ - Type: "reply", - Data: ReplyData{ - ID: fmt.Sprint(msgID), - }, - } -} - -func GenAtMsg(qqId string) AtMessage { - return AtMessage{ - Type: "at", - Data: AtData{ - QQ: qqId, - }, - } -} - // 上报的消息结构体 type SendPkg struct { Action string `json:"action"`