Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

230 lines
5.9KB

  1. package main
  2. import (
  3. "crypto/sha1"
  4. "errors"
  5. "fmt"
  6. "log"
  7. "math/rand"
  8. "sort"
  9. "strings"
  10. "time"
  11. )
  12. //Article is one item in News Message
  13. // one such message can contains upto 10 Articles
  14. type Article struct {
  15. title, description, picURL, url string
  16. }
  17. //BuildTextMsg Given a text message send it to wechat client
  18. func BuildTextMsg(txt string, ToUserName string) (xml string, err error) {
  19. if txt == "" || ToUserName == "" {
  20. err = errors.New("Empty text body or Empty destination")
  21. xml = ""
  22. return
  23. }
  24. msg := fmt.Sprintf(txtMsgTemplate(), ToUserName, APIConfig.PublicAccountID, int32(time.Now().Unix()), txt)
  25. e := Encode(msg)
  26. xml, _, _, _ = signMsg(e)
  27. return
  28. }
  29. //BuildLocationMsg doesn't work for build location message
  30. func BuildLocationMsg(long, lat, precision float64, ToUserName string) (xml string) {
  31. msg := buildLocationMsg()
  32. e := Encode(msg)
  33. xml, _, _, _ = signMsg(e)
  34. return
  35. }
  36. func signMsg(content string) (xml string, timestamp int32, nonce int32, signature string) {
  37. timestamp = int32(time.Now().Unix())
  38. nonce = rand.Int31()
  39. strTimestamp := fmt.Sprintf("%d", timestamp)
  40. strNonce := fmt.Sprintf("%d", nonce)
  41. signature = getSignature(APIConfig.Token, strTimestamp, strNonce, content)
  42. xml = "<xml>" +
  43. "<Encrypt>" + content + "</Encrypt>" +
  44. "<MsgSignature>" + signature + "</MsgSignature>" +
  45. "<TimeStamp>" + strTimestamp + "</TimeStamp>" +
  46. "<Nonce>" + strNonce + "</Nonce>" +
  47. "</xml>"
  48. return
  49. }
  50. func getSignature(token string, timestamp string, nonce string, content string) (signature string) {
  51. strs := []string{token, timestamp, nonce, content}
  52. sort.Strings(strs)
  53. s := strings.Join(strs, "")
  54. h := sha1.New()
  55. h.Write([]byte(s))
  56. signature = fmt.Sprintf("%x", h.Sum(nil))
  57. return
  58. }
  59. func buildTxtMsg(ToUserName, content string) (msg string) {
  60. msg = fmt.Sprintf(txtMsgTemplate(), ToUserName, APIConfig.PublicAccountID, int32(time.Now().Unix()), content)
  61. return
  62. }
  63. func txtMsgTemplate() string {
  64. return `<xml>
  65. <ToUserName><![CDATA[%s]]></ToUserName>
  66. <FromUserName><![CDATA[%s]]></FromUserName>
  67. <CreateTime>%d</CreateTime>
  68. <MsgType><![CDATA[text]]></MsgType>
  69. <Content><![CDATA[%s]]></Content>
  70. </xml>`
  71. }
  72. //upload image to media as temp data, get mediaid and send it out
  73. func buildUploadPicMsg(ToUserName, path string) (msg string) {
  74. mediaID := uploadImage(path)
  75. msg = buildPicMsg(ToUserName, mediaID)
  76. return
  77. }
  78. func buildPicMsg(ToUserName, mediaID string) (msg string) {
  79. msg = fmt.Sprintf(picMsgTemplate(), ToUserName, APIConfig.PublicAccountID, int32(time.Now().Unix()), mediaID)
  80. return
  81. }
  82. func picMsgTemplate() string {
  83. return `<xml>
  84. <ToUserName><![CDATA[%s]]></ToUserName>
  85. <FromUserName><![CDATA[%s]]></FromUserName>
  86. <CreateTime>%d</CreateTime>
  87. <MsgType><![CDATA[image]]></MsgType>
  88. <Image>
  89. <MediaId><![CDATA[%s]]></MediaId>
  90. </Image>
  91. </xml>`
  92. }
  93. func buildVoiceMsg(ToUserName, mediaID string) (msg string) {
  94. msg = fmt.Sprintf(voiceMsgTemplate(), ToUserName, APIConfig.PublicAccountID, int32(time.Now().Unix()), mediaID)
  95. return
  96. }
  97. func voiceMsgTemplate() string {
  98. return `<xml>
  99. <ToUserName><![CDATA[%s]]></ToUserName>
  100. <FromUserName><![CDATA[%s]]></FromUserName>
  101. <CreateTime>%d</CreateTime>
  102. <MsgType><![CDATA[voice]]></MsgType>
  103. <Voice>
  104. <MediaId><![CDATA[%s]]></MediaId>
  105. </Voice>
  106. </xml>`
  107. }
  108. func buildVideoMsg(ToUserName, mediaID, title, description string) (msg string) {
  109. msg = fmt.Sprintf(videoMsgTemplate(), ToUserName, APIConfig.PublicAccountID, int32(time.Now().Unix()), mediaID, title, description)
  110. return
  111. }
  112. func videoMsgTemplate() string {
  113. return `<xml>
  114. <ToUserName><![CDATA[%s]]></ToUserName>
  115. <FromUserName><![CDATA[%s]]></FromUserName>
  116. <CreateTime>%d</CreateTime>
  117. <MsgType><![CDATA[video]]></MsgType>
  118. <Video>
  119. <MediaId><![CDATA[%s]]></MediaId>
  120. <Title><![CDATA[%s]]></Title>
  121. <Description><![CDATA[%s]]></Description>
  122. </Video>
  123. </xml>`
  124. }
  125. func bulidMusicMsg(ToUserName, mediaID, title, description, url, hqURL string) (msg string) {
  126. msg = fmt.Sprintf(musicMsgTemplate(),
  127. ToUserName,
  128. APIConfig.PublicAccountID,
  129. int32(time.Now().Unix()),
  130. title,
  131. description,
  132. url,
  133. hqURL,
  134. mediaID)
  135. return
  136. }
  137. func musicMsgTemplate() string {
  138. return `<xml>
  139. <ToUserName><![CDATA[toUser]]></ToUserName>
  140. <FromUserName><![CDATA[fromUser]]></FromUserName>
  141. <CreateTime>12345678</CreateTime>
  142. <MsgType><![CDATA[music]]></MsgType>
  143. <Music>
  144. <Title><![CDATA[TITLE]]></Title>
  145. <Description><![CDATA[DESCRIPTION]]></Description>
  146. <MusicUrl><![CDATA[MUSIC_Url]]></MusicUrl>
  147. <HQMusicUrl><![CDATA[HQ_MUSIC_Url]]></HQMusicUrl>
  148. <ThumbMediaId><![CDATA[media_id]]></ThumbMediaId>
  149. </Music>
  150. </xml>`
  151. }
  152. func buildNewsMsg(ToUserName, title, description string, articles []Article) (msg string) {
  153. count := 0
  154. items := []string{}
  155. for _, a := range articles {
  156. if count >= 8 {
  157. err := errors.New("too many articles, only take first 8")
  158. log.Fatal(err)
  159. break
  160. }
  161. s := buildArticleItem(a)
  162. items = append(items, s)
  163. count++
  164. }
  165. strItems := strings.Join(items, "")
  166. msg = fmt.Sprintf(newsMsgTemplate(),
  167. ToUserName,
  168. APIConfig.PublicAccountID,
  169. int32(time.Now().Unix()),
  170. count,
  171. strItems)
  172. return
  173. }
  174. func buildArticleItem(item Article) (article string) {
  175. template := `<item>
  176. <Title><![CDATA[%s]]></Title>
  177. <Description><![CDATA[%s]]></Description>
  178. <PicUrl><![CDATA[%s]]></PicUrl>
  179. <Url><![CDATA[%s]]></Url>
  180. </item>
  181. `
  182. article = fmt.Sprintf(template,
  183. item.title, item.description, item.picURL, item.url)
  184. return article
  185. }
  186. func newsMsgTemplate() string {
  187. return `<xml>
  188. <ToUserName><![CDATA[%s]]></ToUserName>
  189. <FromUserName><![CDATA[%s]]></FromUserName>
  190. <CreateTime>%d</CreateTime>
  191. <MsgType><![CDATA[news]]></MsgType>
  192. <ArticleCount>%d</ArticleCount>
  193. <Articles>
  194. %s
  195. </Articles>
  196. </xml>`
  197. }
  198. func buildLocationMsg() string {
  199. template := `<xml>
  200. <ToUserName><![CDATA[oUN420bxqFqlx0ZQHciUOesZO3PE]]></ToUserName>
  201. <FromUserName><![CDATA[gh_f09231355c68]]></FromUserName>
  202. <CreateTime>1494124221</CreateTime>
  203. <MsgType><![CDATA[location]]></MsgType>
  204. <Location_X>23.134521</Location_X>
  205. <Location_Y>113.358803</Location_Y>
  206. <Scale>20</Scale>
  207. <Label><![CDATA[位置信息]]></Label>
  208. </xml> `
  209. return template
  210. }