subscribe.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package subscribe
  2. import (
  3. "fmt"
  4. "github.com/silenceper/wechat/v2/miniprogram/context"
  5. "github.com/silenceper/wechat/v2/util"
  6. )
  7. const (
  8. //发送订阅消息
  9. //https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.send.html
  10. subscribeSendURL = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send"
  11. // 获取当前帐号下的个人模板列表
  12. // https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.getTemplateList.html
  13. getTemplateURL = "https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate"
  14. )
  15. // Subscribe 订阅消息
  16. type Subscribe struct {
  17. *context.Context
  18. }
  19. // NewSubscribe 实例化
  20. func NewSubscribe(ctx *context.Context) *Subscribe {
  21. return &Subscribe{Context: ctx}
  22. }
  23. // Message 订阅消息请求参数
  24. type Message struct {
  25. ToUser string `json:"touser"` //必选,接收者(用户)的 openid
  26. TemplateID string `json:"template_id"` //必选,所需下发的订阅模板id
  27. Page string `json:"page"` //可选,点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
  28. Data map[string]*DataItem `json:"data"` //必选, 模板内容
  29. MiniprogramState string `json:"miniprogram_state"` //可选,跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
  30. Lang string `json:"lang"` //入小程序查看”的语言类型,支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CN
  31. }
  32. //DataItem 模版内某个 .DATA 的值
  33. type DataItem struct {
  34. Value string `json:"value"`
  35. }
  36. //TemplateItem template item
  37. type TemplateItem struct {
  38. PriTmplID string `json:"priTmplId"`
  39. Title string `json:"title"`
  40. Content string `json:"content"`
  41. Example string `json:"example"`
  42. Type int64 `json:"type"`
  43. }
  44. //TemplateList template list
  45. type TemplateList struct {
  46. util.CommonError
  47. Data []TemplateItem `json:"data"`
  48. }
  49. // Send 发送订阅消息
  50. func (s *Subscribe) Send(msg *Message) (err error) {
  51. var accessToken string
  52. accessToken, err = s.GetAccessToken()
  53. if err != nil {
  54. return
  55. }
  56. uri := fmt.Sprintf("%s?access_token=%s", subscribeSendURL, accessToken)
  57. response, err := util.PostJSON(uri, msg)
  58. if err != nil {
  59. return
  60. }
  61. return util.DecodeWithCommonError(response, "Send")
  62. }
  63. //ListTemplates 获取当前帐号下的个人模板列表
  64. // https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.getTemplateList.html
  65. func (s *Subscribe) ListTemplates() (*TemplateList, error) {
  66. accessToken, err := s.GetAccessToken()
  67. if err != nil {
  68. return nil, err
  69. }
  70. uri := fmt.Sprintf("%s?access_token=%s", getTemplateURL, accessToken)
  71. response, err := util.HTTPGet(uri)
  72. if err != nil {
  73. return nil, err
  74. }
  75. templateList := TemplateList{}
  76. err = util.DecodeWithError(response, &templateList, "ListTemplates")
  77. if err != nil {
  78. return nil, err
  79. }
  80. return &templateList, nil
  81. }