feat: 完善消息模块,新增多种消息类型和统一消息解析机制
This commit is contained in:
47
message/record.go
Normal file
47
message/record.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package message
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
// RecordMessage 语音消息
|
||||
type RecordMessage struct {
|
||||
Type string `json:"type"`
|
||||
Data RecordMessageData `json:"data"`
|
||||
}
|
||||
|
||||
type RecordMessageData struct {
|
||||
File string `json:"file"`
|
||||
Name string `json:"name,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(TypeRecord, func() CQMessage {
|
||||
return &RecordMessage{Type: TypeRecord}
|
||||
})
|
||||
}
|
||||
|
||||
func (msg *RecordMessage) SetData(data json.RawMessage) error {
|
||||
return json.Unmarshal(data, &msg.Data)
|
||||
}
|
||||
|
||||
func (msg *RecordMessage) ToCQString() string {
|
||||
return fmt.Sprintf("[CQ:record,file=%s]", msg.Data.File)
|
||||
}
|
||||
|
||||
func (msg *RecordMessage) ParseMessage(data string) error {
|
||||
re := regexp.MustCompile(`\[CQ:record,file=(.*?)\]`)
|
||||
matches := re.FindStringSubmatch(data)
|
||||
if len(matches) < 2 {
|
||||
return fmt.Errorf("语音消息格式不正确")
|
||||
}
|
||||
msg.Data.File = matches[1]
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user