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.

249 line
6.7KB

  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 buildSampleMusicMsg(ToUserName string) (msg string) {
  132. thumbID := "AxEDlSNwJcs_0KnyiGnpAYiB1-sjITosWkU3VFsj62KuCyTQO-Fh1UH8d-pBmY1K"
  133. thumbID = "JZoNKZr9gzaI5ELBnFUljujiBfIwr4CtXHkZtQMjm-sLVD5PGOU_uvsaF3oNNFjp"
  134. url := "http://www.youtubeinmp3.com/download/get/?i=vNIuJKoAE46uz2RggRaKGFqQUVAqd0Td&e=92&progressType=button&color=008000"
  135. url = "http://agobe.yt-downloader.org/download.php?id=d7be28d3a1fafa69d7e464edb8186226"
  136. url = "http://www.sample-videos.com/audio/mp3/india-national-anthem.mp3"
  137. url = "http://google.com/"
  138. msg = buildMusicMsg(ToUserName, thumbID, "音乐标杆出", "fkd;ajf;daf说明动卡及另附;dasjfsda", url, url)
  139. return
  140. }
  141. func buildMusicMsg(ToUserName, ThumbMediaID, title, description, url, hqURL string) (msg string) {
  142. msg = fmt.Sprintf(musicMsgTemplate(),
  143. ToUserName,
  144. APIConfig.PublicAccountID,
  145. int32(time.Now().Unix()),
  146. title,
  147. description,
  148. url,
  149. hqURL,
  150. ThumbMediaID)
  151. return
  152. }
  153. func musicMsgTemplate() string {
  154. return `<xml>
  155. <ToUserName><![CDATA[%s]]></ToUserName>
  156. <FromUserName><![CDATA[%s]]></FromUserName>
  157. <CreateTime>%d</CreateTime>
  158. <MsgType><![CDATA[music]]></MsgType>
  159. <Music>
  160. <Title><![CDATA[%s]]></Title>
  161. <Description><![CDATA[%s]]></Description>
  162. <MusicUrl><![CDATA%s]]></MusicUrl>
  163. <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
  164. <ThumbMediaId><![CDATA[%s]]></ThumbMediaId>
  165. </Music>
  166. </xml>`
  167. }
  168. func buildNewsMsg(ToUserName, title, description string, articles []Article) (msg string) {
  169. count := 0
  170. items := []string{}
  171. for _, a := range articles {
  172. if count >= 8 {
  173. err := errors.New("too many articles, only take first 8")
  174. log.Println(err)
  175. break
  176. }
  177. s := buildArticleItem(a)
  178. items = append(items, s)
  179. count++
  180. }
  181. strItems := strings.Join(items, "")
  182. msg = fmt.Sprintf(newsMsgTemplate(),
  183. ToUserName,
  184. APIConfig.PublicAccountID,
  185. int32(time.Now().Unix()),
  186. count,
  187. strItems)
  188. return
  189. }
  190. func buildArticleItem(item Article) (article string) {
  191. template := `<item>
  192. <Title><![CDATA[%s]]></Title>
  193. <Description><![CDATA[%s]]></Description>
  194. <PicUrl><![CDATA[%s]]></PicUrl>
  195. <Url><![CDATA[%s]]></Url>
  196. </item>
  197. `
  198. article = fmt.Sprintf(template,
  199. item.title, item.description, item.picURL, item.url)
  200. return article
  201. }
  202. func newsMsgTemplate() string {
  203. return `<xml>
  204. <ToUserName><![CDATA[%s]]></ToUserName>
  205. <FromUserName><![CDATA[%s]]></FromUserName>
  206. <CreateTime>%d</CreateTime>
  207. <MsgType><![CDATA[news]]></MsgType>
  208. <ArticleCount>%d</ArticleCount>
  209. <Articles>
  210. %s
  211. </Articles>
  212. </xml>`
  213. }
  214. func buildLocationMsg() string {
  215. template := `<xml>
  216. <ToUserName><![CDATA[oUN420bxqFqlx0ZQHciUOesZO3PE]]></ToUserName>
  217. <FromUserName><![CDATA[gh_f09231355c68]]></FromUserName>
  218. <CreateTime>1494124221</CreateTime>
  219. <MsgType><![CDATA[location]]></MsgType>
  220. <Location_X>23.134521</Location_X>
  221. <Location_Y>113.358803</Location_Y>
  222. <Scale>20</Scale>
  223. <Label><![CDATA[位置信息]]></Label>
  224. </xml> `
  225. return template
  226. }