message.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. BizMsgMenuID int64 `xml:"bizmsgmenuid"`
  116. // 事件相关
  117. Event EventType `xml:"Event" json:"Event"`
  118. EventKey string `xml:"EventKey"`
  119. Ticket string `xml:"Ticket"`
  120. Latitude string `xml:"Latitude"`
  121. Longitude string `xml:"Longitude"`
  122. Precision string `xml:"Precision"`
  123. MenuID string `xml:"MenuId"`
  124. Status string `xml:"Status"`
  125. SessionFrom string `xml:"SessionFrom"`
  126. TotalCount int64 `xml:"TotalCount"`
  127. FilterCount int64 `xml:"FilterCount"`
  128. SentCount int64 `xml:"SentCount"`
  129. ErrorCount int64 `xml:"ErrorCount"`
  130. ScanCodeInfo struct {
  131. ScanType string `xml:"ScanType"`
  132. ScanResult string `xml:"ScanResult"`
  133. } `xml:"ScanCodeInfo"`
  134. SendPicsInfo struct {
  135. Count int32 `xml:"Count"`
  136. PicList []EventPic `xml:"PicList>item"`
  137. } `xml:"SendPicsInfo"`
  138. SendLocationInfo struct {
  139. LocationX float64 `xml:"Location_X"`
  140. LocationY float64 `xml:"Location_Y"`
  141. Scale float64 `xml:"Scale"`
  142. Label string `xml:"Label"`
  143. Poiname string `xml:"Poiname"`
  144. }
  145. subscribeMsgPopupEventList []SubscribeMsgPopupEvent `json:"-"`
  146. SubscribeMsgPopupEvent []struct {
  147. List SubscribeMsgPopupEvent `xml:"List"`
  148. } `xml:"SubscribeMsgPopupEvent"`
  149. // 事件相关:发布能力
  150. PublishEventInfo struct {
  151. PublishID int64 `xml:"publish_id"` // 发布任务id
  152. PublishStatus freepublish.PublishStatus `xml:"publish_status"` // 发布状态
  153. ArticleID string `xml:"article_id"` // 当发布状态为0时(即成功)时,返回图文的 article_id,可用于“客服消息”场景
  154. ArticleDetail struct {
  155. Count uint `xml:"count"` // 文章数量
  156. Item []struct {
  157. Index uint `xml:"idx"` // 文章对应的编号
  158. ArticleURL string `xml:"article_url"` // 图文的永久链接
  159. } `xml:"item"`
  160. } `xml:"article_detail"` // 当发布状态为0时(即成功)时,返回内容
  161. FailIndex []uint `xml:"fail_idx"` // 当发布状态为2或4时,返回不通过的文章编号,第一篇为 1;其他发布状态则为空
  162. } `xml:"PublishEventInfo"`
  163. // 第三方平台相关
  164. InfoType InfoType `xml:"InfoType"`
  165. AppID string `xml:"AppId"`
  166. ComponentVerifyTicket string `xml:"ComponentVerifyTicket"`
  167. AuthorizerAppid string `xml:"AuthorizerAppid"`
  168. AuthorizationCode string `xml:"AuthorizationCode"`
  169. AuthorizationCodeExpiredTime int64 `xml:"AuthorizationCodeExpiredTime"`
  170. PreAuthCode string `xml:"PreAuthCode"`
  171. AuthCode string `xml:"auth_code"`
  172. Info struct {
  173. Name string `xml:"name"`
  174. Code string `xml:"code"`
  175. CodeType int `xml:"code_type"`
  176. LegalPersonaWechat string `xml:"legal_persona_wechat"`
  177. LegalPersonaName string `xml:"legal_persona_name"`
  178. ComponentPhone string `xml:"component_phone"`
  179. } `xml:"info"`
  180. ResultInfo struct {
  181. APIName string `xml:"api_name"`
  182. ApplyTime string `xml:"apply_time"`
  183. AuditID string `xml:"audit_id"`
  184. AuditTime string `xml:"audit_time"`
  185. Reason string `xml:"reason"`
  186. Status string `xml:"status"`
  187. } `xml:"result_info"`
  188. // 卡券相关
  189. CardID string `xml:"CardId"`
  190. RefuseReason string `xml:"RefuseReason"`
  191. IsGiveByFriend int32 `xml:"IsGiveByFriend"`
  192. FriendUserName string `xml:"FriendUserName"`
  193. UserCardCode string `xml:"UserCardCode"`
  194. OldUserCardCode string `xml:"OldUserCardCode"`
  195. OuterStr string `xml:"OuterStr"`
  196. IsRestoreMemberCard int32 `xml:"IsRestoreMemberCard"`
  197. UnionID string `xml:"UnionId"`
  198. // 内容审核相关
  199. IsRisky bool `xml:"isrisky"`
  200. ExtraInfoJSON string `xml:"extra_info_json"`
  201. TraceID string `xml:"trace_id"`
  202. StatusCode int `xml:"status_code"`
  203. //小程序名称审核结果事件推送
  204. Ret int32 `xml:"ret"` //审核结果 2:失败,3:成功
  205. NickName string `xml:"nickname"` //小程序昵称
  206. // 设备相关
  207. device.MsgDevice
  208. //小程序审核通知
  209. SuccTime int `xml:"SuccTime"` //审核成功时的时间戳
  210. FailTime int `xml:"FailTime"` //审核不通过的时间戳
  211. DelayTime int `xml:"DelayTime"` //审核延后时的时间戳
  212. Reason string `xml:"Reason"` //审核不通过的原因
  213. ScreenShot string `xml:"ScreenShot"` //审核不通过的截图示例。用 | 分隔的 media_id 的列表,可通过获取永久素材接口拉取截图内容
  214. }
  215. // SubscribeMsgPopupEvent 订阅通知事件推送的消息体
  216. type SubscribeMsgPopupEvent struct {
  217. TemplateID string `xml:"TemplateId" json:"TemplateId"`
  218. SubscribeStatusString string `xml:"SubscribeStatusString" json:"SubscribeStatusString"`
  219. PopupScene int `xml:"PopupScene" json:"PopupScene,string"`
  220. }
  221. // SetSubscribeMsgPopupEvents 设置订阅消息事件
  222. func (s *MixMessage) SetSubscribeMsgPopupEvents(list []SubscribeMsgPopupEvent) {
  223. s.subscribeMsgPopupEventList = list
  224. }
  225. // GetSubscribeMsgPopupEvents 获取订阅消息事件数据
  226. func (s *MixMessage) GetSubscribeMsgPopupEvents() []SubscribeMsgPopupEvent {
  227. if s.subscribeMsgPopupEventList != nil {
  228. return s.subscribeMsgPopupEventList
  229. }
  230. list := make([]SubscribeMsgPopupEvent, len(s.SubscribeMsgPopupEvent))
  231. for i, item := range s.SubscribeMsgPopupEvent {
  232. list[i] = item.List
  233. }
  234. return list
  235. }
  236. // EventPic 发图事件推送
  237. type EventPic struct {
  238. PicMd5Sum string `xml:"PicMd5Sum"`
  239. }
  240. // EncryptedXMLMsg 安全模式下的消息体
  241. type EncryptedXMLMsg struct {
  242. XMLName struct{} `xml:"xml" json:"-"`
  243. ToUserName string `xml:"ToUserName" json:"ToUserName"`
  244. EncryptedMsg string `xml:"Encrypt" json:"Encrypt"`
  245. }
  246. // ResponseEncryptedXMLMsg 需要返回的消息体
  247. type ResponseEncryptedXMLMsg struct {
  248. XMLName struct{} `xml:"xml" json:"-"`
  249. EncryptedMsg string `xml:"Encrypt" json:"Encrypt"`
  250. MsgSignature string `xml:"MsgSignature" json:"MsgSignature"`
  251. Timestamp int64 `xml:"TimeStamp" json:"TimeStamp"`
  252. Nonce string `xml:"Nonce" json:"Nonce"`
  253. }
  254. // CDATA 使用该类型,在序列化为 xml 文本时文本会被解析器忽略
  255. type CDATA string
  256. // MarshalXML 实现自己的序列化方法
  257. func (c CDATA) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  258. return e.EncodeElement(struct {
  259. string `xml:",cdata"`
  260. }{string(c)}, start)
  261. }
  262. // CommonToken 消息中通用的结构
  263. type CommonToken struct {
  264. XMLName xml.Name `xml:"xml"`
  265. ToUserName CDATA `xml:"ToUserName" json:"ToUserName"`
  266. FromUserName CDATA `xml:"FromUserName" json:"FromUserName"`
  267. CreateTime int64 `xml:"CreateTime" json:"CreateTime"`
  268. MsgType MsgType `xml:"MsgType" json:"MsgType"`
  269. }
  270. // SetToUserName set ToUserName
  271. func (msg *CommonToken) SetToUserName(toUserName CDATA) {
  272. msg.ToUserName = toUserName
  273. }
  274. // SetFromUserName set FromUserName
  275. func (msg *CommonToken) SetFromUserName(fromUserName CDATA) {
  276. msg.FromUserName = fromUserName
  277. }
  278. // SetCreateTime set createTime
  279. func (msg *CommonToken) SetCreateTime(createTime int64) {
  280. msg.CreateTime = createTime
  281. }
  282. // SetMsgType set MsgType
  283. func (msg *CommonToken) SetMsgType(msgType MsgType) {
  284. msg.MsgType = msgType
  285. }
  286. // GetOpenID get the FromUserName value
  287. func (msg *CommonToken) GetOpenID() string {
  288. return string(msg.FromUserName)
  289. }