subscribe.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package message
  2. import (
  3. "fmt"
  4. "github.com/silenceper/wechat/v2/officialaccount/context"
  5. "github.com/silenceper/wechat/v2/util"
  6. )
  7. const (
  8. subscribeSendURL = "https://api.weixin.qq.com/cgi-bin/message/subscribe/bizsend"
  9. subscribeTemplateListURL = "https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate"
  10. )
  11. //Subscrib 订阅消息
  12. type Subscrib struct {
  13. *context.Context
  14. }
  15. //NewSubscrib 实例化
  16. func NewSubscrib(context *context.Context) *Subscrib {
  17. tpl := new(Subscrib)
  18. tpl.Context = context
  19. return tpl
  20. }
  21. //SubscribeMessage 发送的订阅消息内容
  22. type SubscribeMessage struct {
  23. ToUser string `json:"touser"` // 必须, 接受者OpenID
  24. TemplateID string `json:"template_id"` // 必须, 模版ID
  25. Page string `json:"page,omitempty"` // 可选, 跳转网页时填写
  26. Data map[string]*SubscribeDataItem `json:"data"` // 必须, 模板数据
  27. MiniProgram struct {
  28. AppID string `json:"appid"` //所需跳转到的小程序appid(该小程序appid必须与发模板消息的公众号是绑定关联关系)
  29. PagePath string `json:"pagepath"` //所需跳转到小程序的具体页面路径,支持带参数,(示例index?foo=bar)
  30. } `json:"miniprogram"` //可选,跳转至小程序地址
  31. }
  32. //SubscribeDataItem 模版内某个 .DATA 的值
  33. type SubscribeDataItem struct {
  34. Value string `json:"value"`
  35. }
  36. //Send 发送订阅消息
  37. func (tpl *Subscrib) Send(msg *SubscribeMessage) (err error) {
  38. var accessToken string
  39. accessToken, err = tpl.GetAccessToken()
  40. if err != nil {
  41. return
  42. }
  43. uri := fmt.Sprintf("%s?access_token=%s", subscribeSendURL, accessToken)
  44. response, err := util.PostJSON(uri, msg)
  45. if err != nil {
  46. return
  47. }
  48. return util.DecodeWithCommonError(response, "SendSubscribMessage")
  49. }
  50. // PrivateSubscribItem 私有订阅消息模板
  51. type PrivateSubscribItem struct {
  52. PriTmplID string `json:"priTmplId"` // 添加至帐号下的模板 id,发送订阅通知时所需
  53. Title string `json:"title"` //模版标题
  54. Content string `json:"content"` //模版内容
  55. Example string `json:"example"` //模板内容示例
  56. SubType int `json:"type"` //模版类型,2 为一次性订阅,3 为长期订阅
  57. }
  58. type resPrivateSubscribList struct {
  59. util.CommonError
  60. SubscriptionList []*PrivateSubscribItem `json:"data"`
  61. }
  62. //List 获取私有订阅消息模板列表
  63. func (tpl *Subscrib) List() (templateList []*PrivateSubscribItem, err error) {
  64. var accessToken string
  65. accessToken, err = tpl.GetAccessToken()
  66. if err != nil {
  67. return
  68. }
  69. uri := fmt.Sprintf("%s?access_token=%s", subscribeTemplateListURL, accessToken)
  70. var response []byte
  71. response, err = util.HTTPGet(uri)
  72. if err != nil {
  73. return
  74. }
  75. var res resPrivateSubscribList
  76. err = util.DecodeWithError(response, &res, "ListSubscription")
  77. if err != nil {
  78. return
  79. }
  80. templateList = res.SubscriptionList
  81. return
  82. }