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 NewRecordMessage() }) } func NewRecordMessage() *RecordMessage { 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 }