40 lines
729 B
Go
40 lines
729 B
Go
package message
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"strconv"
|
|
)
|
|
|
|
// 回复消息结构体
|
|
type ReplyMessage struct {
|
|
Type string `json:"type"`
|
|
Data ReplyData `json:"data"`
|
|
}
|
|
|
|
// 回复消息数据结构体
|
|
type ReplyData struct {
|
|
ID int `json:"id"`
|
|
}
|
|
|
|
func (msg *ReplyMessage) ToCQString() string {
|
|
return fmt.Sprintf(`\[CQ:reply,id=%d\]`, msg.Data.ID)
|
|
}
|
|
|
|
func (msg *ReplyMessage) ParseMessage(data string) error {
|
|
// 使用正则表达式提取ID
|
|
re := regexp.MustCompile(`\[CQ:reply,id=(\d+)\]`)
|
|
matches := re.FindStringSubmatch(data)
|
|
if len(matches) < 2 {
|
|
return fmt.Errorf("数据格式不正确")
|
|
}
|
|
|
|
id, _ := strconv.Atoi(matches[1])
|
|
|
|
// 返回解析后的结构体
|
|
msg.Data = ReplyData{
|
|
ID: id,
|
|
}
|
|
return nil
|
|
}
|