Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

182 lines
3.4KB

  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. KfAccount string //for kf_close_session kf_create_session
  71. CloseType string //for kf_close_session
  72. ScanCodeInfo struct {
  73. ScanType string //for scancode_push scancode_waitmsg
  74. ScanResult string
  75. }
  76. SendPicsInfo struct { //for pic_photo_or_album pic_weixin pic_sysphoto
  77. Count int
  78. PicList struct {
  79. Item []struct {
  80. PicMd5Sum string
  81. } `xml:"item"`
  82. }
  83. }
  84. }
  85. //ReadCommonHeader parse xml of common field of wechat post message
  86. func ReadCommonHeader(s string) CommonHeader {
  87. var r = CommonHeader{}
  88. xml.Unmarshal([]byte(s), &r)
  89. return r
  90. }
  91. //ReadTextMsg extract text message
  92. func ReadTextMsg(s string) TextMsg {
  93. var r = TextMsg{}
  94. xml.Unmarshal([]byte(s), &r)
  95. return r
  96. }
  97. //ReadPicMsg extract text message
  98. func ReadPicMsg(s string) PicMsg {
  99. var r = PicMsg{}
  100. xml.Unmarshal([]byte(s), &r)
  101. return r
  102. }
  103. //ReadVoiceMsg extract text message
  104. func ReadVoiceMsg(s string) VoiceMsg {
  105. var r = VoiceMsg{}
  106. xml.Unmarshal([]byte(s), &r)
  107. return r
  108. }
  109. //ReadVideoMsg extract text message
  110. func ReadVideoMsg(s string) VideoMsg {
  111. var r = VideoMsg{}
  112. xml.Unmarshal([]byte(s), &r)
  113. return r
  114. }
  115. //ReadShortVideoMsg extract text message
  116. func ReadShortVideoMsg(s string) ShortVideoMsg {
  117. var r = ShortVideoMsg{}
  118. xml.Unmarshal([]byte(s), &r)
  119. return r
  120. }
  121. //ReadLocationMsg extract text message
  122. func ReadLocationMsg(s string) LocationMsg {
  123. var r = LocationMsg{}
  124. xml.Unmarshal([]byte(s), &r)
  125. return r
  126. }
  127. //ReadLinkMsg extract text message
  128. func ReadLinkMsg(s string) LinkMsg {
  129. var r = LinkMsg{}
  130. xml.Unmarshal([]byte(s), &r)
  131. return r
  132. }
  133. //ReadEncryptedMsg get Encrypted Msg
  134. func ReadEncryptedMsg(s string) EncryptedMsg {
  135. var r = EncryptedMsg{}
  136. xml.Unmarshal([]byte(s), &r)
  137. return r
  138. }
  139. //ReadEventMsg get event msg details
  140. func ReadEventMsg(s string) EventMsg {
  141. var r = EventMsg{}
  142. xml.Unmarshal([]byte(s), &r)
  143. return r
  144. }
  145. //ReadEventType tells event type in string
  146. // subscribe
  147. // unsubscribe
  148. // scan
  149. func ReadEventType(s string) string {
  150. var r = EventMsg{}
  151. xml.Unmarshal([]byte(s), &r)
  152. return r.Event
  153. }
  154. func decryptToXML(raw string) string {
  155. e := ReadEncryptedMsg(raw)
  156. sd := Decode(e.Encrypt)
  157. return sd
  158. }