From 0d72f37d315beed0a9594db1d2486cb3d6da7cd5 Mon Sep 17 00:00:00 2001 From: eatmoreapple <15055461510@163.com> Date: Tue, 3 Aug 2021 23:13:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0app=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E7=9A=84=E6=AD=A3=E6=96=87=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- message.go | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/message.go b/message.go index 7738730..c6025de 100644 --- a/message.go +++ b/message.go @@ -299,6 +299,23 @@ func (m *Message) AsRead() error { return m.Bot.Caller.WebWxStatusAsRead(m.Bot.Storage.Request, m.Bot.Storage.LoginInfo, m) } +// IsArticle 判断当前的消息类型是否为文章 +func (m *Message) IsArticle() bool { + return m.AppMsgType == AppMsgTypeUrl +} + +// MediaData 获取当前App Message的具体内容 +func (m *Message) MediaData() (*AppMessageData, error) { + if !m.IsMedia() { + return nil, errors.New("media message required") + } + var data AppMessageData + if err := xml.Unmarshal(stringToByte(m.Content), &data); err != nil { + return nil, err + } + return &data, nil +} + // Set 往消息上下文中设置值 // goroutine safe func (m *Message) Set(key string, value interface{}) { @@ -546,3 +563,72 @@ func NewFileAppMessage(stat os.FileInfo, attachId string) *appmsg { m.AppAttach.FileExt = getFileExt(stat.Name()) return m } + +// AppMessageData 获取APP消息的正文 +// See https://github.com/eatmoreapple/openwechat/issues/62 +type AppMessageData struct { + XMLName xml.Name `xml:"msg"` + AppMsg struct { + Appid string `xml:"appid,attr"` + SdkVer string `xml:"sdkver,attr"` + Title string `xml:"title"` + Des string `xml:"des"` + Action string `xml:"action"` + Type AppMessageType `xml:"type"` + ShowType string `xml:"showtype"` + Content string `xml:"content"` + URL string `xml:"url"` + DataUrl string `xml:"dataurl"` + LowUrl string `xml:"lowurl"` + LowDataUrl string `xml:"lowdataurl"` + RecordItem string `xml:"recorditem"` + ThumbUrl string `xml:"thumburl"` + MessageAction string `xml:"messageaction"` + Md5 string `xml:"md5"` + ExtInfo string `xml:"extinfo"` + SourceUsername string `xml:"sourceusername"` + SourceDisplayName string `xml:"sourcedisplayname"` + CommentUrl string `xml:"commenturl"` + AppAttach struct { + TotalLen string `xml:"totallen"` + AttachId string `xml:"attachid"` + EmoticonMd5 string `xml:"emoticonmd5"` + FileExt string `xml:"fileext"` + FileUploadToken string `xml:"fileuploadtoken"` + OverwriteNewMsgId string `xml:"overwrite_newmsgid"` + FileKey string `xml:"filekey"` + CdnAttachUrl string `xml:"cdnattachurl"` + AesKey string `xml:"aeskey"` + EncryVer string `xml:"encryver"` + } `xml:"appattach"` + WeAppInfo struct { + PagePath string `xml:"pagepath"` + Username string `xml:"username"` + Appid string `xml:"appid"` + AppServiceType string `xml:"appservicetype"` + } `xml:"weappinfo"` + WebSearch string `xml:"websearch"` + } `xml:"appmsg"` + FromUsername string `xml:"fromusername"` + Scene string `xml:"scene"` + AppInfo struct { + Version string `xml:"version"` + AppName string `xml:"appname"` + } `xml:"appinfo"` + CommentUrl string `xml:"commenturl"` +} + +// IsFromApplet 判断当前的消息类型是否来自小程序 +func (a *AppMessageData) IsFromApplet() bool { + return a.AppMsg.Appid != "" +} + +// IsArticle 判断当前的消息类型是否为文章 +func (a *AppMessageData) IsArticle() bool { + return a.AppMsg.Type == AppMsgTypeUrl +} + +// IsFile 判断当前的消息类型是否为文件 +func (a AppMessageData) IsFile() bool { + return a.AppMsg.Type == AppMsgTypeAttach +}