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.

183 lines
3.7KB

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "log"
  6. )
  7. func getKfSendURL() (url string) {
  8. atk, _ := GetAccessToken()
  9. url = fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=%s", atk)
  10. return
  11. }
  12. type sendTxtMsg struct {
  13. ToUser string `json:"touser"`
  14. MsgType string `json:"msgtype"`
  15. Text struct {
  16. Content string `json:"content"`
  17. } `json:"text"`
  18. }
  19. func kfSendTxt(user, txt string) {
  20. u := getKfSendURL()
  21. s := sendTxtMsg{}
  22. s.MsgType = "text"
  23. s.Text.Content = txt
  24. s.ToUser = user
  25. j, _ := json.Marshal(s)
  26. postJSON(j, u)
  27. }
  28. type sendPicMsg struct {
  29. ToUser string `json:"touser"`
  30. MsgType string `json:"msgtype"`
  31. Image struct {
  32. MediaID string `json:"media_id"`
  33. } `json:"image"`
  34. }
  35. func kfSendPic(user, pic string) {
  36. mID := uploadImage(pic)
  37. kfSendPicByMediaID(user, mID)
  38. }
  39. func kfSendPicByMediaID(user, mediaID string) {
  40. u := getKfSendURL()
  41. s := sendPicMsg{}
  42. s.ToUser = user
  43. s.MsgType = "image"
  44. s.Image.MediaID = mediaID
  45. j, _ := json.Marshal(s)
  46. postJSON(j, u)
  47. }
  48. type sendVoiceMsg struct {
  49. ToUser string `json:"touser"`
  50. MsgType string `json:"msgtype"`
  51. Voice struct {
  52. MediaID string `json:"media_id"`
  53. } `json:"voice"`
  54. }
  55. func kfSendVoice(user, path string) {
  56. mID := uploadVoice(path)
  57. kfSendVoiceByMediaID(user, mID)
  58. }
  59. func kfSendVoiceByMediaID(user, mediaID string) {
  60. u := getKfSendURL()
  61. s := sendVoiceMsg{}
  62. s.ToUser = user
  63. s.MsgType = "voice"
  64. s.Voice.MediaID = mediaID
  65. j, _ := json.Marshal(s)
  66. postJSON(j, u)
  67. }
  68. type sendVideoMsg struct {
  69. ToUser string `json:"touser"`
  70. MsgType string `json:"msgtype"`
  71. Video struct {
  72. MediaID string `json:"media_id"`
  73. ThumbMediaID string `json:"thumb_media_id"`
  74. Title string `json:"title"`
  75. Description string `json:"description"`
  76. } `json:"video"`
  77. }
  78. func kfSendVideo(user, path, title, description, thumb string) {
  79. mID := uploadVideo(path)
  80. tID := uploadThumb(thumb)
  81. kfSendVideoByMediaID(user, mID, title, description, tID)
  82. }
  83. func kfSendVideoByMediaID(user, mediaID, title, description, tID string) {
  84. u := getKfSendURL()
  85. s := sendVideoMsg{}
  86. s.ToUser = user
  87. s.MsgType = "video"
  88. s.Video.MediaID = mediaID
  89. s.Video.Description = description
  90. s.Video.Title = title
  91. s.Video.ThumbMediaID = tID
  92. j, _ := json.Marshal(s)
  93. postJSON(j, u)
  94. }
  95. type sendMusicMsg struct {
  96. ToUser string `json:"touser"`
  97. MsgType string `json:"msgtype"`
  98. Music struct {
  99. MediaID string `json:"media_id"`
  100. ThumbMediaID string `json:"thumb_media_id"`
  101. Title string `json:"title"`
  102. Description string `json:"description"`
  103. URL string `json:"musicurl"`
  104. HqURL string `json:"hqmusicurl"`
  105. } `json:"music"`
  106. }
  107. func kfSendMusic(user, title, description, thumbID, url, hqurl string) {
  108. u := getKfSendURL()
  109. s := sendMusicMsg{}
  110. s.ToUser = user
  111. s.MsgType = "music"
  112. s.Music.Title = title
  113. s.Music.Description = description
  114. s.Music.ThumbMediaID = thumbID
  115. s.Music.URL = url
  116. s.Music.HqURL = hqurl
  117. j, _ := json.Marshal(s)
  118. log.Println(string(j))
  119. postJSON(j, u)
  120. }
  121. type sendNewsArticle struct {
  122. Title string `json:"title"`
  123. Description string `json:"description"`
  124. URL string `json:"url"`
  125. PicURL string `json:"picurl"`
  126. }
  127. type sendNewsMsg struct {
  128. ToUser string `json:"touser"`
  129. MsgType string `json:"msgtype"`
  130. News struct {
  131. Articles []sendNewsArticle `json:"articles"`
  132. } `json:"news"`
  133. }
  134. func kfSendNews(toUser, title, description string, articles []sendNewsArticle) {
  135. count := 0
  136. s := sendNewsMsg{}
  137. s.ToUser = toUser
  138. s.MsgType = "news"
  139. //build article
  140. a := []sendNewsArticle{}
  141. for _, v := range articles {
  142. a = append(a, v)
  143. count++
  144. if count >= 10 {
  145. log.Printf("too many article, only take 10")
  146. break
  147. }
  148. }
  149. s.News.Articles = a
  150. //marshal
  151. j, _ := json.Marshal(s)
  152. log.Println(string(j))
  153. //send
  154. u := getKfSendURL()
  155. postJSON(j, u)
  156. }