Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

219 lines
4.6KB

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