No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

112 líneas
2.2KB

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. func getKfSendURL() (url string) {
  7. atk, _ := GetAccessToken()
  8. url = fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=%s", atk)
  9. return
  10. }
  11. type sendTxtMsg struct {
  12. ToUser string `json:"touser"`
  13. MsgType string `json:"msgtype"`
  14. Text struct {
  15. Content string `json:"content"`
  16. } `json:"text"`
  17. }
  18. func kfSendTxt(user, txt string) {
  19. u := getKfSendURL()
  20. s := sendTxtMsg{}
  21. s.MsgType = "text"
  22. s.Text.Content = txt
  23. s.ToUser = user
  24. j, _ := json.Marshal(s)
  25. postJSON(j, u)
  26. }
  27. type sendPicMsg struct {
  28. ToUser string `json:"touser"`
  29. MsgType string `json:"msgtype"`
  30. Image struct {
  31. MediaID string `json:"media_id"`
  32. } `json:"image"`
  33. }
  34. func kfSendPic(user, pic string) {
  35. mID := uploadImage(pic)
  36. kfSendPicByMediaID(user, mID)
  37. }
  38. func kfSendPicByMediaID(user, mediaID string) {
  39. u := getKfSendURL()
  40. s := sendPicMsg{}
  41. s.ToUser = user
  42. s.MsgType = "image"
  43. s.Image.MediaID = mediaID
  44. j, _ := json.Marshal(s)
  45. postJSON(j, u)
  46. }
  47. type sendVoiceMsg struct {
  48. ToUser string `json:"touser"`
  49. MsgType string `json:"msgtype"`
  50. Voice struct {
  51. MediaID string `json:"media_id"`
  52. } `json:"voice"`
  53. }
  54. func kfSendVoice(user, path string) {
  55. mID := uploadVoice(path)
  56. kfSendVoiceByMediaID(user, mID)
  57. }
  58. func kfSendVoiceByMediaID(user, mediaID string) {
  59. u := getKfSendURL()
  60. s := sendVoiceMsg{}
  61. s.ToUser = user
  62. s.MsgType = "voice"
  63. s.Voice.MediaID = mediaID
  64. j, _ := json.Marshal(s)
  65. postJSON(j, u)
  66. }
  67. type sendVideoMsg struct {
  68. ToUser string `json:"touser"`
  69. MsgType string `json:"msgtype"`
  70. Video struct {
  71. MediaID string `json:"media_id"`
  72. ThumbMediaID string `json:"thumb_media_id"`
  73. Title string `json:"title"`
  74. Description string `json:"description"`
  75. } `json:"video"`
  76. }
  77. func kfSendVideo(user, path, title, description, thumb string) {
  78. mID := uploadVideo(path)
  79. tID := uploadThumb(thumb)
  80. kfSendVideoByMediaID(user, mID, title, description, tID)
  81. }
  82. func kfSendVideoByMediaID(user, mediaID, title, description, tID string) {
  83. u := getKfSendURL()
  84. s := sendVideoMsg{}
  85. s.ToUser = user
  86. s.MsgType = "video"
  87. s.Video.MediaID = mediaID
  88. s.Video.Description = description
  89. s.Video.Title = title
  90. s.Video.ThumbMediaID = tID
  91. j, _ := json.Marshal(s)
  92. postJSON(j, u)
  93. }