Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

168 linhas
3.0KB

  1. //analyze xml message
  2. package main
  3. import (
  4. "encoding/xml"
  5. )
  6. //EncryptedMsg encrypted message from Wechat
  7. type EncryptedMsg struct {
  8. ToUserName string
  9. Encrypt string
  10. }
  11. //all xml message has these headers
  12. type CommonHeader struct {
  13. ToUserName string
  14. FromUserName string
  15. CreateTime int64
  16. MsgType string
  17. }
  18. //text message
  19. type TextMsg struct {
  20. Content string
  21. MsgId int64
  22. }
  23. //picture
  24. type PicMsg struct {
  25. PicUrl string
  26. MediaId string
  27. MsgId int64
  28. }
  29. //voice
  30. type VoiceMsg struct {
  31. MediaId string
  32. Format string
  33. MsgId int64
  34. Recognition string
  35. }
  36. //video
  37. type VideoMsg struct {
  38. MediaId string
  39. ThumbMediaId string
  40. MsgId int64
  41. }
  42. //short video
  43. type ShortVideoMsg struct {
  44. MediaId string
  45. ThumbMediaId string
  46. MsgId int64
  47. }
  48. //Location Incoming Msg
  49. type LocationMsg struct {
  50. Location_X float64
  51. Location_Y float64
  52. Scale int
  53. Label string
  54. MsgId int64
  55. }
  56. type LinkMsg struct {
  57. Title string
  58. Description string
  59. Url string
  60. MsgId int64
  61. }
  62. //EventMsg all type of event
  63. type EventMsg struct {
  64. Event string //subscribe, unsubscribe, SCAN, LOCATION, CLICK, VIEW
  65. EventKey string //for CLICK, VIEW, SCAN, subscribe
  66. Ticket string //for SCAN, subscribe
  67. Latitude float64 //for LOCATION
  68. Longitude float64 //for LOCATION
  69. Precision float64 //for LOCATION
  70. }
  71. //ReadCommonHeader parse xml of common field of wechat post message
  72. func ReadCommonHeader(s string) CommonHeader {
  73. var r = CommonHeader{}
  74. xml.Unmarshal([]byte(s), &r)
  75. return r
  76. }
  77. //ReadTextMsg extract text message
  78. func ReadTextMsg(s string) TextMsg {
  79. var r = TextMsg{}
  80. xml.Unmarshal([]byte(s), &r)
  81. return r
  82. }
  83. //ReadPicMsg extract text message
  84. func ReadPicMsg(s string) PicMsg {
  85. var r = PicMsg{}
  86. xml.Unmarshal([]byte(s), &r)
  87. return r
  88. }
  89. //ReadVoiceMsg extract text message
  90. func ReadVoiceMsg(s string) VoiceMsg {
  91. var r = VoiceMsg{}
  92. xml.Unmarshal([]byte(s), &r)
  93. return r
  94. }
  95. //ReadVideoMsg extract text message
  96. func ReadVideoMsg(s string) VideoMsg {
  97. var r = VideoMsg{}
  98. xml.Unmarshal([]byte(s), &r)
  99. return r
  100. }
  101. //ReadShortVideoMsg extract text message
  102. func ReadShortVideoMsg(s string) ShortVideoMsg {
  103. var r = ShortVideoMsg{}
  104. xml.Unmarshal([]byte(s), &r)
  105. return r
  106. }
  107. //ReadLocationMsg extract text message
  108. func ReadLocationMsg(s string) LocationMsg {
  109. var r = LocationMsg{}
  110. xml.Unmarshal([]byte(s), &r)
  111. return r
  112. }
  113. //ReadLinkMsg extract text message
  114. func ReadLinkMsg(s string) LinkMsg {
  115. var r = LinkMsg{}
  116. xml.Unmarshal([]byte(s), &r)
  117. return r
  118. }
  119. //ReadEncryptedMsg get Encrypted Msg
  120. func ReadEncryptedMsg(s string) EncryptedMsg {
  121. var r = EncryptedMsg{}
  122. xml.Unmarshal([]byte(s), &r)
  123. return r
  124. }
  125. //ReadEventMsg get event msg details
  126. func ReadEventMsg(s string) EventMsg {
  127. var r = EventMsg{}
  128. xml.Unmarshal([]byte(s), &r)
  129. return r
  130. }
  131. //ReadEventType tells event type in string
  132. // subscribe
  133. // unsubscribe
  134. // scan
  135. func ReadEventType(s string) string {
  136. var r = EventMsg{}
  137. xml.Unmarshal([]byte(s), &r)
  138. return r.Event
  139. }
  140. func decryptToXML(raw string) string {
  141. e := ReadEncryptedMsg(raw)
  142. sd := Decode(e.Encrypt)
  143. return sd
  144. }