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.

170 linhas
3.1KB

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