Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

238 Zeilen
6.1KB

  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 buildUploadVoiceMsg(ToUserName, path string) (msg string) {
  94. mediaID := uploadVoice(path)
  95. log.Println("get media id " + mediaID)
  96. msg = buildVoiceMsg(ToUserName, mediaID)
  97. return
  98. }
  99. func buildVoiceMsg(ToUserName, mediaID string) (msg string) {
  100. msg = fmt.Sprintf(voiceMsgTemplate(), ToUserName, APIConfig.PublicAccountID, int32(time.Now().Unix()), mediaID)
  101. return
  102. }
  103. func voiceMsgTemplate() string {
  104. return `<xml>
  105. <ToUserName><![CDATA[%s]]></ToUserName>
  106. <FromUserName><![CDATA[%s]]></FromUserName>
  107. <CreateTime>%d</CreateTime>
  108. <MsgType><![CDATA[voice]]></MsgType>
  109. <Voice>
  110. <MediaId><![CDATA[%s]]></MediaId>
  111. </Voice>
  112. </xml>`
  113. }
  114. func buildVideoMsg(ToUserName, mediaID, title, description string) (msg string) {
  115. msg = fmt.Sprintf(videoMsgTemplate(), ToUserName, APIConfig.PublicAccountID, int32(time.Now().Unix()), mediaID, title, description)
  116. return
  117. }
  118. func videoMsgTemplate() string {
  119. return `<xml>
  120. <ToUserName><![CDATA[%s]]></ToUserName>
  121. <FromUserName><![CDATA[%s]]></FromUserName>
  122. <CreateTime>%d</CreateTime>
  123. <MsgType><![CDATA[video]]></MsgType>
  124. <Video>
  125. <MediaId><![CDATA[%s]]></MediaId>
  126. <Title><![CDATA[%s]]></Title>
  127. <Description><![CDATA[%s]]></Description>
  128. </Video>
  129. </xml>`
  130. }
  131. func bulidMusicMsg(ToUserName, mediaID, title, description, url, hqURL string) (msg string) {
  132. msg = fmt.Sprintf(musicMsgTemplate(),
  133. ToUserName,
  134. APIConfig.PublicAccountID,
  135. int32(time.Now().Unix()),
  136. title,
  137. description,
  138. url,
  139. hqURL,
  140. mediaID)
  141. return
  142. }
  143. func musicMsgTemplate() string {
  144. return `<xml>
  145. <ToUserName><![CDATA[toUser]]></ToUserName>
  146. <FromUserName><![CDATA[fromUser]]></FromUserName>
  147. <CreateTime>12345678</CreateTime>
  148. <MsgType><![CDATA[music]]></MsgType>
  149. <Music>
  150. <Title><![CDATA[TITLE]]></Title>
  151. <Description><![CDATA[DESCRIPTION]]></Description>
  152. <MusicUrl><![CDATA[MUSIC_Url]]></MusicUrl>
  153. <HQMusicUrl><![CDATA[HQ_MUSIC_Url]]></HQMusicUrl>
  154. <ThumbMediaId><![CDATA[media_id]]></ThumbMediaId>
  155. </Music>
  156. </xml>`
  157. }
  158. func buildNewsMsg(ToUserName, title, description string, articles []Article) (msg string) {
  159. count := 0
  160. items := []string{}
  161. for _, a := range articles {
  162. if count >= 8 {
  163. err := errors.New("too many articles, only take first 8")
  164. log.Fatal(err)
  165. break
  166. }
  167. s := buildArticleItem(a)
  168. items = append(items, s)
  169. count++
  170. }
  171. strItems := strings.Join(items, "")
  172. msg = fmt.Sprintf(newsMsgTemplate(),
  173. ToUserName,
  174. APIConfig.PublicAccountID,
  175. int32(time.Now().Unix()),
  176. count,
  177. strItems)
  178. return
  179. }
  180. func buildArticleItem(item Article) (article string) {
  181. template := `<item>
  182. <Title><![CDATA[%s]]></Title>
  183. <Description><![CDATA[%s]]></Description>
  184. <PicUrl><![CDATA[%s]]></PicUrl>
  185. <Url><![CDATA[%s]]></Url>
  186. </item>
  187. `
  188. article = fmt.Sprintf(template,
  189. item.title, item.description, item.picURL, item.url)
  190. return article
  191. }
  192. func newsMsgTemplate() string {
  193. return `<xml>
  194. <ToUserName><![CDATA[%s]]></ToUserName>
  195. <FromUserName><![CDATA[%s]]></FromUserName>
  196. <CreateTime>%d</CreateTime>
  197. <MsgType><![CDATA[news]]></MsgType>
  198. <ArticleCount>%d</ArticleCount>
  199. <Articles>
  200. %s
  201. </Articles>
  202. </xml>`
  203. }
  204. func buildLocationMsg() string {
  205. template := `<xml>
  206. <ToUserName><![CDATA[oUN420bxqFqlx0ZQHciUOesZO3PE]]></ToUserName>
  207. <FromUserName><![CDATA[gh_f09231355c68]]></FromUserName>
  208. <CreateTime>1494124221</CreateTime>
  209. <MsgType><![CDATA[location]]></MsgType>
  210. <Location_X>23.134521</Location_X>
  211. <Location_Y>113.358803</Location_Y>
  212. <Scale>20</Scale>
  213. <Label><![CDATA[位置信息]]></Label>
  214. </xml> `
  215. return template
  216. }