message.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package message
  2. import "encoding/xml"
  3. //MsgType 基本消息类型
  4. type MsgType string
  5. //EventType 事件类型
  6. type EventType string
  7. const (
  8. //MsgTypeText 表示文本消息
  9. MsgTypeText MsgType = "text"
  10. //MsgTypeImage 表示图片消息
  11. MsgTypeImage = "image"
  12. //MsgTypeVoice 表示语音消息
  13. MsgTypeVoice = "voice"
  14. //MsgTypeVideo 表示视频消息
  15. MsgTypeVideo = "video"
  16. //MsgTypeShortVideo 表示短视频消息[限接收]
  17. MsgTypeShortVideo = "shortvideo"
  18. //MsgTypeLocation 表示坐标消息[限接收]
  19. MsgTypeLocation = "location"
  20. //MsgTypeLink 表示链接消息[限接收]
  21. MsgTypeLink = "link"
  22. //MsgTypeMusic 表示音乐消息[限回复]
  23. MsgTypeMusic = "music"
  24. //MsgTypeNews 表示图文消息[限回复]
  25. MsgTypeNews = "news"
  26. //MsgTypeTransfer 表示消息消息转发到客服
  27. MsgTypeTransfer = "transfer_customer_service"
  28. )
  29. const (
  30. //EventSubscribe 订阅
  31. EventSubscribe EventType = "subscribe"
  32. //EventUnsubscribe 取消订阅
  33. EventUnsubscribe EventType = "unsubscribe"
  34. //EventScan 用户已经关注公众号,则微信会将带场景值扫描事件推送给开发者
  35. EventScan EventType = "SCAN"
  36. //EventLocation 上报地理位置事件
  37. EventLocation EventType = "LOCATION"
  38. //EventClick 点击菜单拉取消息时的事件推送
  39. EventClick EventType = "CLICK"
  40. //EventView 点击菜单跳转链接时的事件推送
  41. EventView EventType = "VIEW"
  42. )
  43. //MixMessage 存放所有微信发送过来的消息和事件
  44. type MixMessage struct {
  45. CommonToken
  46. //基本消息
  47. MsgID int64 `xml:"MsgId"`
  48. Content string `xml:"Content"`
  49. PicURL string `xml:"PicUrl"`
  50. MediaID string `xml:"MediaId"`
  51. Format string `xml:"Format"`
  52. ThumbMediaID string `xml:"ThumbMediaId"`
  53. LocationX float64 `xml:"Location_X"`
  54. LocationY float64 `xml:"Location_Y"`
  55. Scale float64 `xml:"Scale"`
  56. Label string `xml:"Label"`
  57. Title string `xml:"Title"`
  58. Description string `xml:"Description"`
  59. URL string `xml:"Url"`
  60. //事件相关
  61. Event string `xml:"Event"`
  62. EventKey string `xml:"EventKey"`
  63. Ticket string `xml:"Ticket"`
  64. Latitude string `xml:"Latitude"`
  65. Longitude string `xml:"Longitude"`
  66. Precision string `xml:"Precision"`
  67. }
  68. //EncryptedXMLMsg 安全模式下的消息体
  69. type EncryptedXMLMsg struct {
  70. XMLName struct{} `xml:"xml" json:"-"`
  71. ToUserName string `xml:"ToUserName" json:"ToUserName"`
  72. EncryptedMsg string `xml:"Encrypt" json:"Encrypt"`
  73. }
  74. //ResponseEncryptedXMLMsg 需要返回的消息体
  75. type ResponseEncryptedXMLMsg struct {
  76. XMLName struct{} `xml:"xml" json:"-"`
  77. EncryptedMsg string `xml:"Encrypt" json:"Encrypt"`
  78. MsgSignature string `xml:"MsgSignature" json:"MsgSignature"`
  79. Timestamp int64 `xml:"TimeStamp" json:"TimeStamp"`
  80. Nonce string `xml:"Nonce" json:"Nonce"`
  81. }
  82. // CommonToken 消息中通用的结构
  83. type CommonToken struct {
  84. XMLName xml.Name `xml:"xml"`
  85. ToUserName string `xml:"ToUserName"`
  86. FromUserName string `xml:"FromUserName"`
  87. CreateTime int64 `xml:"CreateTime"`
  88. MsgType MsgType `xml:"MsgType"`
  89. }
  90. //SetToUserName set ToUserName
  91. func (msg *CommonToken) SetToUserName(toUserName string) {
  92. msg.ToUserName = toUserName
  93. }
  94. //SetFromUserName set FromUserName
  95. func (msg *CommonToken) SetFromUserName(fromUserName string) {
  96. msg.FromUserName = fromUserName
  97. }
  98. //SetCreateTime set createTime
  99. func (msg *CommonToken) SetCreateTime(createTime int64) {
  100. msg.CreateTime = createTime
  101. }
  102. //SetMsgType set MsgType
  103. func (msg *CommonToken) SetMsgType(msgType MsgType) {
  104. msg.MsgType = msgType
  105. }