46 lines
1.0 KiB
Go
46 lines
1.0 KiB
Go
package message
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"regexp"
|
|
)
|
|
|
|
// LightappMessage 小程序卡片消息
|
|
type LightappMessage struct {
|
|
Type string `json:"type"`
|
|
Data LightappMessageData `json:"data"`
|
|
}
|
|
|
|
type LightappMessageData struct {
|
|
Content string `json:"content"` // 小程序卡片内容
|
|
}
|
|
|
|
func init() {
|
|
RegisterMessageType(TypeLightapp, func() CQMessage {
|
|
return NewLightappMessage()
|
|
})
|
|
}
|
|
|
|
func NewLightappMessage() *LightappMessage {
|
|
return &LightappMessage{Type: TypeLightapp}
|
|
}
|
|
|
|
func (msg *LightappMessage) SetData(data json.RawMessage) error {
|
|
return json.Unmarshal(data, &msg.Data)
|
|
}
|
|
|
|
func (msg *LightappMessage) ToCQString() string {
|
|
return fmt.Sprintf("[CQ:lightapp,content=%s]", msg.Data.Content)
|
|
}
|
|
|
|
func (msg *LightappMessage) ParseMessage(data string) error {
|
|
re := regexp.MustCompile(`\[CQ:lightapp,content=(.*?)\]`)
|
|
matches := re.FindStringSubmatch(data)
|
|
if len(matches) < 2 {
|
|
return fmt.Errorf("小程序卡片消息格式不正确")
|
|
}
|
|
msg.Data.Content = matches[1]
|
|
return nil
|
|
}
|