package message import ( "encoding/json" "fmt" "regexp" ) // ContactMessage 推荐好友/群消息 type ContactMessage struct { Type string `json:"type"` Data ContactMessageData `json:"data"` } type ContactMessageData struct { Type string `json:"type"` // qq或group ID string `json:"id"` // QQ号或群号 } func init() { RegisterMessageType(TypeContact, func() CQMessage { return &ContactMessage{Type: TypeContact} }) } func (msg *ContactMessage) SetData(data json.RawMessage) error { return json.Unmarshal(data, &msg.Data) } func (msg *ContactMessage) ToCQString() string { return fmt.Sprintf("[CQ:contact,type=%s,id=%s]", msg.Data.Type, msg.Data.ID) } func (msg *ContactMessage) ParseMessage(data string) error { re := regexp.MustCompile(`\[CQ:contact,type=(qq|group),id=(\d+)\]`) matches := re.FindStringSubmatch(data) if len(matches) < 3 { return fmt.Errorf("推荐好友/群消息格式不正确") } msg.Data.Type = matches[1] msg.Data.ID = matches[2] return nil }