message.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. // EventViewMiniprogram 点击菜单跳转小程序的事件推送
  65. EventViewMiniprogram EventType = "view_miniprogram"
  66. // EventTemplateSendJobFinish 发送模板消息推送通知
  67. EventTemplateSendJobFinish EventType = "TEMPLATESENDJOBFINISH"
  68. // EventMassSendJobFinish 群发消息推送通知
  69. EventMassSendJobFinish EventType = "MASSSENDJOBFINISH"
  70. // EventWxaMediaCheck 异步校验图片/音频是否含有违法违规内容推送事件
  71. EventWxaMediaCheck EventType = "wxa_media_check"
  72. // EventSubscribeMsgPopupEvent 订阅通知事件推送
  73. EventSubscribeMsgPopupEvent EventType = "subscribe_msg_popup_event"
  74. // EventPublishJobFinish 发布任务完成
  75. EventPublishJobFinish EventType = "PUBLISHJOBFINISH"
  76. // EventWeappAuditSuccess 审核通过
  77. EventWeappAuditSuccess EventType = "weapp_audit_success"
  78. // EventWeappAuditFail 审核不通过
  79. EventWeappAuditFail EventType = "weapp_audit_fail"
  80. // EventWeappAuditDelay 审核延后
  81. EventWeappAuditDelay EventType = "weapp_audit_delay"
  82. )
  83. const (
  84. // 微信开放平台需要用到
  85. // InfoTypeVerifyTicket 返回ticket
  86. InfoTypeVerifyTicket InfoType = "component_verify_ticket"
  87. // InfoTypeAuthorized 授权
  88. InfoTypeAuthorized InfoType = "authorized"
  89. // InfoTypeUnauthorized 取消授权
  90. InfoTypeUnauthorized InfoType = "unauthorized"
  91. // InfoTypeUpdateAuthorized 更新授权
  92. InfoTypeUpdateAuthorized InfoType = "updateauthorized"
  93. // InfoTypeNotifyThirdFasterRegister 注册审核事件推送
  94. InfoTypeNotifyThirdFasterRegister InfoType = "notify_third_fasteregister"
  95. )
  96. // MixMessage 存放所有微信发送过来的消息和事件
  97. type MixMessage struct {
  98. CommonToken
  99. // 基本消息
  100. MsgID int64 `xml:"MsgId"` // 其他消息推送过来是MsgId
  101. TemplateMsgID int64 `xml:"MsgID"` // 模板消息推送成功的消息是MsgID
  102. Content string `xml:"Content"`
  103. Recognition string `xml:"Recognition"`
  104. PicURL string `xml:"PicUrl"`
  105. MediaID string `xml:"MediaId"`
  106. Format string `xml:"Format"`
  107. ThumbMediaID string `xml:"ThumbMediaId"`
  108. LocationX float64 `xml:"Location_X"`
  109. LocationY float64 `xml:"Location_Y"`
  110. Scale float64 `xml:"Scale"`
  111. Label string `xml:"Label"`
  112. Title string `xml:"Title"`
  113. Description string `xml:"Description"`
  114. URL string `xml:"Url"`
  115. // 事件相关
  116. Event EventType `xml:"Event" json:"Event"`
  117. EventKey string `xml:"EventKey"`
  118. Ticket string `xml:"Ticket"`
  119. Latitude string `xml:"Latitude"`
  120. Longitude string `xml:"Longitude"`
  121. Precision string `xml:"Precision"`
  122. MenuID string `xml:"MenuId"`
  123. Status string `xml:"Status"`
  124. SessionFrom string `xml:"SessionFrom"`
  125. TotalCount int64 `xml:"TotalCount"`
  126. FilterCount int64 `xml:"FilterCount"`
  127. SentCount int64 `xml:"SentCount"`
  128. ErrorCount int64 `xml:"ErrorCount"`
  129. ScanCodeInfo struct {
  130. ScanType string `xml:"ScanType"`
  131. ScanResult string `xml:"ScanResult"`
  132. } `xml:"ScanCodeInfo"`
  133. SendPicsInfo struct {
  134. Count int32 `xml:"Count"`
  135. PicList []EventPic `xml:"PicList>item"`
  136. } `xml:"SendPicsInfo"`
  137. SendLocationInfo struct {
  138. LocationX float64 `xml:"Location_X"`
  139. LocationY float64 `xml:"Location_Y"`
  140. Scale float64 `xml:"Scale"`
  141. Label string `xml:"Label"`
  142. Poiname string `xml:"Poiname"`
  143. }
  144. subscribeMsgPopupEventList []SubscribeMsgPopupEvent `json:"-"`
  145. SubscribeMsgPopupEvent []struct {
  146. List SubscribeMsgPopupEvent `xml:"List"`
  147. } `xml:"SubscribeMsgPopupEvent"`
  148. // 事件相关:发布能力
  149. PublishEventInfo struct {
  150. PublishID int64 `xml:"publish_id"` // 发布任务id
  151. PublishStatus freepublish.PublishStatus `xml:"publish_status"` // 发布状态
  152. ArticleID string `xml:"article_id"` // 当发布状态为0时(即成功)时,返回图文的 article_id,可用于“客服消息”场景
  153. ArticleDetail struct {
  154. Count uint `xml:"count"` // 文章数量
  155. Item []struct {
  156. Index uint `xml:"idx"` // 文章对应的编号
  157. ArticleURL string `xml:"article_url"` // 图文的永久链接
  158. } `xml:"item"`
  159. } `xml:"article_detail"` // 当发布状态为0时(即成功)时,返回内容
  160. FailIndex []uint `xml:"fail_idx"` // 当发布状态为2或4时,返回不通过的文章编号,第一篇为 1;其他发布状态则为空
  161. } `xml:"PublishEventInfo"`
  162. // 第三方平台相关
  163. InfoType InfoType `xml:"InfoType"`
  164. AppID string `xml:"AppId"`
  165. ComponentVerifyTicket string `xml:"ComponentVerifyTicket"`
  166. AuthorizerAppid string `xml:"AuthorizerAppid"`
  167. AuthorizationCode string `xml:"AuthorizationCode"`
  168. AuthorizationCodeExpiredTime int64 `xml:"AuthorizationCodeExpiredTime"`
  169. PreAuthCode string `xml:"PreAuthCode"`
  170. AuthCode string `xml:"auth_code"`
  171. Info struct {
  172. Name string `xml:"name"`
  173. Code string `xml:"code"`
  174. CodeType int `xml:"code_type"`
  175. LegalPersonaWechat string `xml:"legal_persona_wechat"`
  176. LegalPersonaName string `xml:"legal_persona_name"`
  177. ComponentPhone string `xml:"component_phone"`
  178. } `xml:"info"`
  179. // 卡券相关
  180. CardID string `xml:"CardId"`
  181. RefuseReason string `xml:"RefuseReason"`
  182. IsGiveByFriend int32 `xml:"IsGiveByFriend"`
  183. FriendUserName string `xml:"FriendUserName"`
  184. UserCardCode string `xml:"UserCardCode"`
  185. OldUserCardCode string `xml:"OldUserCardCode"`
  186. OuterStr string `xml:"OuterStr"`
  187. IsRestoreMemberCard int32 `xml:"IsRestoreMemberCard"`
  188. UnionID string `xml:"UnionId"`
  189. // 内容审核相关
  190. IsRisky bool `xml:"isrisky"`
  191. ExtraInfoJSON string `xml:"extra_info_json"`
  192. TraceID string `xml:"trace_id"`
  193. StatusCode int `xml:"status_code"`
  194. // 设备相关
  195. device.MsgDevice
  196. //小程序审核通知
  197. SuccTime int `xml:"SuccTime"` //审核成功时的时间戳
  198. FailTime int `xml:"FailTime"` //审核不通过的时间戳
  199. DelayTime int `xml:"DelayTime"` //审核延后时的时间戳
  200. Reason string `xml:"Reason"` //审核不通过的原因
  201. ScreenShot string `xml:"ScreenShot"` //审核不通过的截图示例。用 | 分隔的 media_id 的列表,可通过获取永久素材接口拉取截图内容
  202. }
  203. // SubscribeMsgPopupEvent 订阅通知事件推送的消息体
  204. type SubscribeMsgPopupEvent struct {
  205. TemplateID string `xml:"TemplateId" json:"TemplateId"`
  206. SubscribeStatusString string `xml:"SubscribeStatusString" json:"SubscribeStatusString"`
  207. PopupScene int `xml:"PopupScene" json:"PopupScene,string"`
  208. }
  209. // SetSubscribeMsgPopupEvents 设置订阅消息事件
  210. func (s *MixMessage) SetSubscribeMsgPopupEvents(list []SubscribeMsgPopupEvent) {
  211. s.subscribeMsgPopupEventList = list
  212. }
  213. // GetSubscribeMsgPopupEvents 获取订阅消息事件数据
  214. func (s *MixMessage) GetSubscribeMsgPopupEvents() []SubscribeMsgPopupEvent {
  215. if s.subscribeMsgPopupEventList != nil {
  216. return s.subscribeMsgPopupEventList
  217. }
  218. list := make([]SubscribeMsgPopupEvent, len(s.SubscribeMsgPopupEvent))
  219. for i, item := range s.SubscribeMsgPopupEvent {
  220. list[i] = item.List
  221. }
  222. return list
  223. }
  224. // EventPic 发图事件推送
  225. type EventPic struct {
  226. PicMd5Sum string `xml:"PicMd5Sum"`
  227. }
  228. // EncryptedXMLMsg 安全模式下的消息体
  229. type EncryptedXMLMsg struct {
  230. XMLName struct{} `xml:"xml" json:"-"`
  231. ToUserName string `xml:"ToUserName" json:"ToUserName"`
  232. EncryptedMsg string `xml:"Encrypt" json:"Encrypt"`
  233. }
  234. // ResponseEncryptedXMLMsg 需要返回的消息体
  235. type ResponseEncryptedXMLMsg struct {
  236. XMLName struct{} `xml:"xml" json:"-"`
  237. EncryptedMsg string `xml:"Encrypt" json:"Encrypt"`
  238. MsgSignature string `xml:"MsgSignature" json:"MsgSignature"`
  239. Timestamp int64 `xml:"TimeStamp" json:"TimeStamp"`
  240. Nonce string `xml:"Nonce" json:"Nonce"`
  241. }
  242. // CDATA 使用该类型,在序列化为 xml 文本时文本会被解析器忽略
  243. type CDATA string
  244. // MarshalXML 实现自己的序列化方法
  245. func (c CDATA) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  246. return e.EncodeElement(struct {
  247. string `xml:",cdata"`
  248. }{string(c)}, start)
  249. }
  250. // CommonToken 消息中通用的结构
  251. type CommonToken struct {
  252. XMLName xml.Name `xml:"xml"`
  253. ToUserName CDATA `xml:"ToUserName" json:"ToUserName"`
  254. FromUserName CDATA `xml:"FromUserName" json:"FromUserName"`
  255. CreateTime int64 `xml:"CreateTime" json:"CreateTime"`
  256. MsgType MsgType `xml:"MsgType" json:"MsgType"`
  257. }
  258. // SetToUserName set ToUserName
  259. func (msg *CommonToken) SetToUserName(toUserName CDATA) {
  260. msg.ToUserName = toUserName
  261. }
  262. // SetFromUserName set FromUserName
  263. func (msg *CommonToken) SetFromUserName(fromUserName CDATA) {
  264. msg.FromUserName = fromUserName
  265. }
  266. // SetCreateTime set createTime
  267. func (msg *CommonToken) SetCreateTime(createTime int64) {
  268. msg.CreateTime = createTime
  269. }
  270. // SetMsgType set MsgType
  271. func (msg *CommonToken) SetMsgType(msgType MsgType) {
  272. msg.MsgType = msgType
  273. }
  274. // GetOpenID get the FromUserName value
  275. func (msg *CommonToken) GetOpenID() string {
  276. return string(msg.FromUserName)
  277. }