qq_bot/message/video.go

59 lines
1.4 KiB
Go

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 NewVideoMessage()
})
}
func NewVideoMessage() *VideoMessage {
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
}