Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

236 lines
5.0KB

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