message.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. package message
  2. import (
  3. "encoding/xml"
  4. "github.com/silenceper/wechat/v2/officialaccount/device"
  5. "github.com/silenceper/wechat/v2/officialaccount/freepublish"
  6. )
  7. // MsgType 基本消息类型
  8. type MsgType string
  9. // EventType 事件类型
  10. type EventType string
  11. // InfoType 第三方平台授权事件类型
  12. type InfoType string
  13. const (
  14. // MsgTypeText 表示文本消息
  15. MsgTypeText MsgType = "text"
  16. // MsgTypeImage 表示图片消息
  17. MsgTypeImage MsgType = "image"
  18. // MsgTypeVoice 表示语音消息
  19. MsgTypeVoice MsgType = "voice"
  20. // MsgTypeVideo 表示视频消息
  21. MsgTypeVideo MsgType = "video"
  22. // MsgTypeMiniprogrampage 表示小程序卡片消息
  23. MsgTypeMiniprogrampage MsgType = "miniprogrampage"
  24. // MsgTypeShortVideo 表示短视频消息[限接收]
  25. MsgTypeShortVideo MsgType = "shortvideo"
  26. // MsgTypeLocation 表示坐标消息[限接收]
  27. MsgTypeLocation MsgType = "location"
  28. // MsgTypeLink 表示链接消息[限接收]
  29. MsgTypeLink MsgType = "link"
  30. // MsgTypeMusic 表示音乐消息[限回复]
  31. MsgTypeMusic MsgType = "music"
  32. // MsgTypeNews 表示图文消息[限回复]
  33. MsgTypeNews MsgType = "news"
  34. // MsgTypeTransfer 表示消息消息转发到客服
  35. MsgTypeTransfer MsgType = "transfer_customer_service"
  36. // MsgTypeEvent 表示事件推送消息
  37. MsgTypeEvent MsgType = "event"
  38. )
  39. const (
  40. // EventSubscribe 订阅
  41. EventSubscribe EventType = "subscribe"
  42. // EventUnsubscribe 取消订阅
  43. EventUnsubscribe EventType = "unsubscribe"
  44. // EventScan 用户已经关注公众号,则微信会将带场景值扫描事件推送给开发者
  45. EventScan EventType = "SCAN"
  46. // EventLocation 上报地理位置事件
  47. EventLocation EventType = "LOCATION"
  48. // EventClick 点击菜单拉取消息时的事件推送
  49. EventClick EventType = "CLICK"
  50. // EventView 点击菜单跳转链接时的事件推送
  51. EventView EventType = "VIEW"
  52. // EventScancodePush 扫码推事件的事件推送
  53. EventScancodePush EventType = "scancode_push"
  54. // EventScancodeWaitmsg 扫码推事件且弹出“消息接收中”提示框的事件推送
  55. EventScancodeWaitmsg EventType = "scancode_waitmsg"
  56. // EventPicSysphoto 弹出系统拍照发图的事件推送
  57. EventPicSysphoto EventType = "pic_sysphoto"
  58. // EventPicPhotoOrAlbum 弹出拍照或者相册发图的事件推送
  59. EventPicPhotoOrAlbum EventType = "pic_photo_or_album"
  60. // EventPicWeixin 弹出微信相册发图器的事件推送
  61. EventPicWeixin EventType = "pic_weixin"
  62. // EventLocationSelect 弹出地理位置选择器的事件推送
  63. EventLocationSelect EventType = "location_select"
  64. // EventTemplateSendJobFinish 发送模板消息推送通知
  65. EventTemplateSendJobFinish EventType = "TEMPLATESENDJOBFINISH"
  66. // EventMassSendJobFinish 群发消息推送通知
  67. EventMassSendJobFinish EventType = "MASSSENDJOBFINISH"
  68. // EventWxaMediaCheck 异步校验图片/音频是否含有违法违规内容推送事件
  69. EventWxaMediaCheck EventType = "wxa_media_check"
  70. // EventSubscribeMsgPopupEvent 订阅通知事件推送
  71. EventSubscribeMsgPopupEvent EventType = "subscribe_msg_popup_event"
  72. // EventPublishJobFinish 发布任务完成
  73. EventPublishJobFinish EventType = "PUBLISHJOBFINISH"
  74. )
  75. const (
  76. // 微信开放平台需要用到
  77. // InfoTypeVerifyTicket 返回ticket
  78. InfoTypeVerifyTicket InfoType = "component_verify_ticket"
  79. // InfoTypeAuthorized 授权
  80. InfoTypeAuthorized InfoType = "authorized"
  81. // InfoTypeUnauthorized 取消授权
  82. InfoTypeUnauthorized InfoType = "unauthorized"
  83. // InfoTypeUpdateAuthorized 更新授权
  84. InfoTypeUpdateAuthorized InfoType = "updateauthorized"
  85. // InfoTypeNotifyThirdFasterRegister 注册审核事件推送
  86. InfoTypeNotifyThirdFasterRegister InfoType = "notify_third_fasteregister"
  87. )
  88. // MixMessage 存放所有微信发送过来的消息和事件
  89. type MixMessage struct {
  90. CommonToken
  91. // 基本消息
  92. MsgID int64 `xml:"MsgId"` // 其他消息推送过来是MsgId
  93. TemplateMsgID int64 `xml:"MsgID"` // 模板消息推送成功的消息是MsgID
  94. Content string `xml:"Content"`
  95. Recognition string `xml:"Recognition"`
  96. PicURL string `xml:"PicUrl"`
  97. MediaID string `xml:"MediaId"`
  98. Format string `xml:"Format"`
  99. ThumbMediaID string `xml:"ThumbMediaId"`
  100. LocationX float64 `xml:"Location_X"`
  101. LocationY float64 `xml:"Location_Y"`
  102. Scale float64 `xml:"Scale"`
  103. Label string `xml:"Label"`
  104. Title string `xml:"Title"`
  105. Description string `xml:"Description"`
  106. URL string `xml:"Url"`
  107. // 事件相关
  108. Event EventType `xml:"Event" json:"Event"`
  109. EventKey string `xml:"EventKey"`
  110. Ticket string `xml:"Ticket"`
  111. Latitude string `xml:"Latitude"`
  112. Longitude string `xml:"Longitude"`
  113. Precision string `xml:"Precision"`
  114. MenuID string `xml:"MenuId"`
  115. Status string `xml:"Status"`
  116. SessionFrom string `xml:"SessionFrom"`
  117. TotalCount int64 `xml:"TotalCount"`
  118. FilterCount int64 `xml:"FilterCount"`
  119. SentCount int64 `xml:"SentCount"`
  120. ErrorCount int64 `xml:"ErrorCount"`
  121. ScanCodeInfo struct {
  122. ScanType string `xml:"ScanType"`
  123. ScanResult string `xml:"ScanResult"`
  124. } `xml:"ScanCodeInfo"`
  125. SendPicsInfo struct {
  126. Count int32 `xml:"Count"`
  127. PicList []EventPic `xml:"PicList>item"`
  128. } `xml:"SendPicsInfo"`
  129. SendLocationInfo struct {
  130. LocationX float64 `xml:"Location_X"`
  131. LocationY float64 `xml:"Location_Y"`
  132. Scale float64 `xml:"Scale"`
  133. Label string `xml:"Label"`
  134. Poiname string `xml:"Poiname"`
  135. }
  136. subscribeMsgPopupEventList []SubscribeMsgPopupEvent `json:"-"`
  137. SubscribeMsgPopupEvent []struct {
  138. List SubscribeMsgPopupEvent `xml:"List"`
  139. } `xml:"SubscribeMsgPopupEvent"`
  140. // 事件相关:发布能力
  141. PublishEventInfo struct {
  142. PublishID int64 `xml:"publish_id"` // 发布任务id
  143. PublishStatus freepublish.PublishStatus `xml:"publish_status"` // 发布状态
  144. ArticleID string `xml:"article_id"` // 当发布状态为0时(即成功)时,返回图文的 article_id,可用于“客服消息”场景
  145. ArticleDetail struct {
  146. Count uint `xml:"count"` // 文章数量
  147. Item []struct {
  148. Index uint `xml:"idx"` // 文章对应的编号
  149. ArticleURL string `xml:"article_url"` // 图文的永久链接
  150. } `xml:"item"`
  151. } `xml:"article_detail"` // 当发布状态为0时(即成功)时,返回内容
  152. FailIndex []uint `xml:"fail_idx"` // 当发布状态为2或4时,返回不通过的文章编号,第一篇为 1;其他发布状态则为空
  153. } `xml:"PublishEventInfo"`
  154. // 第三方平台相关
  155. InfoType InfoType `xml:"InfoType"`
  156. AppID string `xml:"AppId"`
  157. ComponentVerifyTicket string `xml:"ComponentVerifyTicket"`
  158. AuthorizerAppid string `xml:"AuthorizerAppid"`
  159. AuthorizationCode string `xml:"AuthorizationCode"`
  160. AuthorizationCodeExpiredTime int64 `xml:"AuthorizationCodeExpiredTime"`
  161. PreAuthCode string `xml:"PreAuthCode"`
  162. AuthCode string `xml:"auth_code"`
  163. Info struct {
  164. Name string `xml:"name"`
  165. Code string `xml:"code"`
  166. CodeType int `xml:"code_type"`
  167. LegalPersonaWechat string `xml:"legal_persona_wechat"`
  168. LegalPersonaName string `xml:"legal_persona_name"`
  169. ComponentPhone string `xml:"component_phone"`
  170. } `xml:"info"`
  171. // 卡券相关
  172. CardID string `xml:"CardId"`
  173. RefuseReason string `xml:"RefuseReason"`
  174. IsGiveByFriend int32 `xml:"IsGiveByFriend"`
  175. FriendUserName string `xml:"FriendUserName"`
  176. UserCardCode string `xml:"UserCardCode"`
  177. OldUserCardCode string `xml:"OldUserCardCode"`
  178. OuterStr string `xml:"OuterStr"`
  179. IsRestoreMemberCard int32 `xml:"IsRestoreMemberCard"`
  180. UnionID string `xml:"UnionId"`
  181. // 内容审核相关
  182. IsRisky bool `xml:"isrisky"`
  183. ExtraInfoJSON string `xml:"extra_info_json"`
  184. TraceID string `xml:"trace_id"`
  185. StatusCode int `xml:"status_code"`
  186. // 设备相关
  187. device.MsgDevice
  188. }
  189. // SubscribeMsgPopupEvent 订阅通知事件推送的消息体
  190. type SubscribeMsgPopupEvent struct {
  191. TemplateID string `xml:"TemplateId" json:"TemplateId"`
  192. SubscribeStatusString string `xml:"SubscribeStatusString" json:"SubscribeStatusString"`
  193. PopupScene int `xml:"PopupScene" json:"PopupScene,string"`
  194. }
  195. // SetSubscribeMsgPopupEvents 设置订阅消息事件
  196. func (s *MixMessage) SetSubscribeMsgPopupEvents(list []SubscribeMsgPopupEvent) {
  197. s.subscribeMsgPopupEventList = list
  198. }
  199. // GetSubscribeMsgPopupEvents 获取订阅消息事件数据
  200. func (s *MixMessage) GetSubscribeMsgPopupEvents() []SubscribeMsgPopupEvent {
  201. if s.subscribeMsgPopupEventList != nil {
  202. return s.subscribeMsgPopupEventList
  203. }
  204. list := make([]SubscribeMsgPopupEvent, len(s.SubscribeMsgPopupEvent))
  205. for i, item := range s.SubscribeMsgPopupEvent {
  206. list[i] = item.List
  207. }
  208. return list
  209. }
  210. // EventPic 发图事件推送
  211. type EventPic struct {
  212. PicMd5Sum string `xml:"PicMd5Sum"`
  213. }
  214. // EncryptedXMLMsg 安全模式下的消息体
  215. type EncryptedXMLMsg struct {
  216. XMLName struct{} `xml:"xml" json:"-"`
  217. ToUserName string `xml:"ToUserName" json:"ToUserName"`
  218. EncryptedMsg string `xml:"Encrypt" json:"Encrypt"`
  219. }
  220. // ResponseEncryptedXMLMsg 需要返回的消息体
  221. type ResponseEncryptedXMLMsg struct {
  222. XMLName struct{} `xml:"xml" json:"-"`
  223. EncryptedMsg string `xml:"Encrypt" json:"Encrypt"`
  224. MsgSignature string `xml:"MsgSignature" json:"MsgSignature"`
  225. Timestamp int64 `xml:"TimeStamp" json:"TimeStamp"`
  226. Nonce string `xml:"Nonce" json:"Nonce"`
  227. }
  228. // CDATA 使用该类型,在序列化为 xml 文本时文本会被解析器忽略
  229. type CDATA string
  230. // MarshalXML 实现自己的序列化方法
  231. func (c CDATA) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  232. return e.EncodeElement(struct {
  233. string `xml:",cdata"`
  234. }{string(c)}, start)
  235. }
  236. // CommonToken 消息中通用的结构
  237. type CommonToken struct {
  238. XMLName xml.Name `xml:"xml"`
  239. ToUserName CDATA `xml:"ToUserName" json:"ToUserName"`
  240. FromUserName CDATA `xml:"FromUserName" json:"FromUserName"`
  241. CreateTime int64 `xml:"CreateTime" json:"CreateTime"`
  242. MsgType MsgType `xml:"MsgType" json:"MsgType"`
  243. }
  244. // SetToUserName set ToUserName
  245. func (msg *CommonToken) SetToUserName(toUserName CDATA) {
  246. msg.ToUserName = toUserName
  247. }
  248. // SetFromUserName set FromUserName
  249. func (msg *CommonToken) SetFromUserName(fromUserName CDATA) {
  250. msg.FromUserName = fromUserName
  251. }
  252. // SetCreateTime set createTime
  253. func (msg *CommonToken) SetCreateTime(createTime int64) {
  254. msg.CreateTime = createTime
  255. }
  256. // SetMsgType set MsgType
  257. func (msg *CommonToken) SetMsgType(msgType MsgType) {
  258. msg.MsgType = msgType
  259. }
  260. // GetOpenID get the FromUserName value
  261. func (msg *CommonToken) GetOpenID() string {
  262. return string(msg.FromUserName)
  263. }