feat: 完善消息模块,新增多种消息类型和统一消息解析机制
This commit is contained in:
54
message/video.go
Normal file
54
message/video.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package message
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
// VideoMessage 视频消息
|
||||
type VideoMessage struct {
|
||||
Type string `json:"type"`
|
||||
Data VideoMessageData `json:"data"`
|
||||
}
|
||||
|
||||
type VideoMessageData struct {
|
||||
File string `json:"file"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Thumb string `json:"thumb,omitempty"`
|
||||
URL string `json:"url,omitempty"`
|
||||
Path string `json:"path,omitempty"`
|
||||
FileID string `json:"file_id,omitempty"`
|
||||
FileSize string `json:"file_size,omitempty"`
|
||||
FileUnique string `json:"file_unique,omitempty"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterMessageType(TypeVideo, func() CQMessage {
|
||||
return &VideoMessage{Type: TypeVideo}
|
||||
})
|
||||
}
|
||||
|
||||
func (msg *VideoMessage) SetData(data json.RawMessage) error {
|
||||
return json.Unmarshal(data, &msg.Data)
|
||||
}
|
||||
|
||||
func (msg *VideoMessage) ToCQString() string {
|
||||
if msg.Data.Thumb != "" {
|
||||
return fmt.Sprintf("[CQ:video,file=%s,thumb=%s]", msg.Data.File, msg.Data.Thumb)
|
||||
}
|
||||
return fmt.Sprintf("[CQ:video,file=%s]", msg.Data.File)
|
||||
}
|
||||
|
||||
func (msg *VideoMessage) ParseMessage(data string) error {
|
||||
re := regexp.MustCompile(`\[CQ:video,file=(.*?)(?:,thumb=(.*?))?\]`)
|
||||
matches := re.FindStringSubmatch(data)
|
||||
if len(matches) < 2 {
|
||||
return fmt.Errorf("视频消息格式不正确")
|
||||
}
|
||||
msg.Data.File = matches[1]
|
||||
if len(matches) > 2 {
|
||||
msg.Data.Thumb = matches[2]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user