Skip to content

Commit

Permalink
feat: add/delete subscribe template (silenceper#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
ackerr authored and fiochen committed Oct 8, 2021
1 parent d4ac51a commit 4d0a82f
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 1 deletion.
60 changes: 60 additions & 0 deletions miniprogram/subscribe/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ const (
// https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.getTemplateList.html
getTemplateURL = "https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate"

// 添加订阅模板
// https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.addTemplate.html
addTemplateURL = "https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate"

// 删除私有模板
// https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.deleteTemplate.html
delTemplateURL = "https://api.weixin.qq.com/wxaapi/newtmpl/deltemplate"

// 统一服务消息
// https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/uniform-message/uniformMessage.send.html
uniformMessageSend = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send"
Expand Down Expand Up @@ -133,3 +141,55 @@ func (s *Subscribe) UniformSend(msg *UniformMessage) (err error) {
}
return util.DecodeWithCommonError(response, "UniformSend")
}

type resSubscribeAdd struct {
util.CommonError

TemplateID string `json:"priTmplId"`
}

// Add 添加订阅消息模板
func (s *Subscribe) Add(ShortID string, kidList []int, sceneDesc string) (templateID string, err error) {
var accessToken string
accessToken, err = s.GetAccessToken()
if err != nil {
return
}
var msg = struct {
TemplateIDShort string `json:"tid"`
SceneDesc string `json:"sceneDesc"`
KidList []int `json:"kidList"`
}{TemplateIDShort: ShortID, SceneDesc: sceneDesc, KidList: kidList}
uri := fmt.Sprintf("%s?access_token=%s", addTemplateURL, accessToken)
var response []byte
response, err = util.PostJSON(uri, msg)
if err != nil {
return
}
var result resSubscribeAdd
err = util.DecodeWithError(response, &result, "AddSubscribe")
if err != nil {
return
}
templateID = result.TemplateID
return
}

// Delete 删除私有模板
func (s *Subscribe) Delete(templateID string) (err error) {
var accessToken string
accessToken, err = s.GetAccessToken()
if err != nil {
return
}
var msg = struct {
TemplateID string `json:"priTmplId"`
}{TemplateID: templateID}
uri := fmt.Sprintf("%s?access_token=%s", delTemplateURL, accessToken)
var response []byte
response, err = util.PostJSON(uri, msg)
if err != nil {
return
}
return util.DecodeWithCommonError(response, "DeleteSubscribe")
}
56 changes: 55 additions & 1 deletion officialaccount/message/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
const (
subscribeSendURL = "https://api.weixin.qq.com/cgi-bin/message/subscribe/bizsend"
subscribeTemplateListURL = "https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate"
subscribeTemplateAddURL = "https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate"
subscribeTemplateDelURL = "https://api.weixin.qq.com/wxaapi/newtmpl/deltemplate"
)

//Subscribe 订阅消息
Expand Down Expand Up @@ -84,10 +86,62 @@ func (tpl *Subscribe) List() (templateList []*PrivateSubscribeItem, err error) {
return
}
var res resPrivateSubscribeList
err = util.DecodeWithError(response, &res, "ListSubscription")
err = util.DecodeWithError(response, &res, "ListSubscribe")
if err != nil {
return
}
templateList = res.SubscriptionList
return
}

type resSubscribeAdd struct {
util.CommonError

TemplateID string `json:"priTmplId"`
}

// Add 添加订阅消息模板
func (tpl *Subscribe) Add(ShortID string, kidList []int, sceneDesc string) (templateID string, err error) {
var accessToken string
accessToken, err = tpl.GetAccessToken()
if err != nil {
return
}
var msg = struct {
TemplateIDShort string `json:"tid"`
SceneDesc string `json:"sceneDesc"`
KidList []int `json:"kidList"`
}{TemplateIDShort: ShortID, SceneDesc: sceneDesc, KidList: kidList}
uri := fmt.Sprintf("%s?access_token=%s", subscribeTemplateAddURL, accessToken)
var response []byte
response, err = util.PostJSON(uri, msg)
if err != nil {
return
}
var result resSubscribeAdd
err = util.DecodeWithError(response, &result, "AddSubscribe")
if err != nil {
return
}
templateID = result.TemplateID
return
}

// Delete 删除私有模板
func (tpl *Subscribe) Delete(templateID string) (err error) {
var accessToken string
accessToken, err = tpl.GetAccessToken()
if err != nil {
return
}
var msg = struct {
TemplateID string `json:"priTmplId"`
}{TemplateID: templateID}
uri := fmt.Sprintf("%s?access_token=%s", subscribeTemplateDelURL, accessToken)
var response []byte
response, err = util.PostJSON(uri, msg)
if err != nil {
return
}
return util.DecodeWithCommonError(response, "DeleteSubscribe")
}

0 comments on commit 4d0a82f

Please sign in to comment.