feat: 完善消息模块,新增多种消息类型和统一消息解析机制
This commit is contained in:
41
message/lightapp.go
Normal file
41
message/lightapp.go
Normal file
@@ -0,0 +1,41 @@
|
||||
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 &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
|
||||
}
|
||||
Reference in New Issue
Block a user