You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 line
2.0KB

  1. //analyze xml message
  2. package main
  3. import (
  4. "encoding/xml"
  5. )
  6. //all xml message has these headers
  7. type CommonHeader struct {
  8. ToUserName string
  9. FromUserName string
  10. CreateTime int64
  11. MsgType string
  12. }
  13. //text message
  14. type TextMsg struct {
  15. Content string
  16. MsgId int64
  17. }
  18. //picture
  19. type PicMsg struct {
  20. PicUrl string
  21. MediaId string
  22. MsgId int64
  23. }
  24. //voice
  25. type VoiceMsg struct {
  26. MediaId string
  27. Format string
  28. MsgId int64
  29. Recognition string
  30. }
  31. //video
  32. type VideoMsg struct {
  33. MediaId string
  34. ThumbMediaId string
  35. MsgId int64
  36. }
  37. //short video
  38. type ShortVideoMsg struct {
  39. MediaId string
  40. ThumbMediaId string
  41. MsgId int64
  42. }
  43. //Location Info
  44. type LocationMsg struct {
  45. Location_X float64
  46. Location_Y float64
  47. Scale int
  48. Label string
  49. MsgId int64
  50. }
  51. type LinkMsg struct {
  52. Title string
  53. Description string
  54. Url string
  55. MsgId int64
  56. }
  57. //ReadCommonHeader parse xml of common field of wechat post message
  58. func ReadCommonHeader(s string) CommonHeader {
  59. var r = CommonHeader{}
  60. xml.Unmarshal([]byte(s), &r)
  61. return r
  62. }
  63. //ReadTextMsg extract text message
  64. func ReadTextMsg(s string) TextMsg {
  65. var r = TextMsg{}
  66. xml.Unmarshal([]byte(s), &r)
  67. return r
  68. }
  69. //ReadPicMsg extract text message
  70. func ReadPicMsg(s string) PicMsg {
  71. var r = PicMsg{}
  72. xml.Unmarshal([]byte(s), &r)
  73. return r
  74. }
  75. //ReadVoiceMsg extract text message
  76. func ReadVoiceMsg(s string) VoiceMsg {
  77. var r = VoiceMsg{}
  78. xml.Unmarshal([]byte(s), &r)
  79. return r
  80. }
  81. //ReadVideoMsg extract text message
  82. func ReadVideoMsg(s string) VideoMsg {
  83. var r = VideoMsg{}
  84. xml.Unmarshal([]byte(s), &r)
  85. return r
  86. }
  87. //ReadShortVideoMsg extract text message
  88. func ReadShortVideoMsg(s string) ShortVideoMsg {
  89. var r = ShortVideoMsg{}
  90. xml.Unmarshal([]byte(s), &r)
  91. return r
  92. }
  93. //ReadLocationMsg extract text message
  94. func ReadLocationMsg(s string) LocationMsg {
  95. var r = LocationMsg{}
  96. xml.Unmarshal([]byte(s), &r)
  97. return r
  98. }
  99. //ReadLinkMsg extract text message
  100. func ReadLinkMsg(s string) LinkMsg {
  101. var r = LinkMsg{}
  102. xml.Unmarshal([]byte(s), &r)
  103. return r
  104. }