1
0
mirror of https://github.com/silenceper/wechat.git synced 2026-02-09 15:12:26 +08:00

Compare commits

...

2 Commits

Author SHA1 Message Date
Wangrong
eb3dbf1646 增加订阅通知功能 (#599)
* fix issue #586 #543

* add officialaccount subscribe function

* update doc for officialaccount

* add comments

Co-authored-by: Wang Rong <wangron@tesla.com>
2022-08-08 10:03:56 +08:00
Wangrong
27e18b6958 fix officialaccount part in Issue 590 (#591)
* fix issue #586 #543

* fix issue #590 in officialaccount

Co-authored-by: Wang Rong <wangron@tesla.com>
2022-08-08 10:02:24 +08:00
3 changed files with 191 additions and 23 deletions

View File

@@ -19,9 +19,9 @@
| -------------------- | -------- | -------------------------------------- | ---------- | ----------------------- |
| 选用模板 | POST | /wxaapi/newtmpl/addtemplate | YES | (tpl *Subscribe) Add |
| 删除模板 | POST | /wxaapi/newtmpl/deltemplate | YES | (tpl *Subscribe) Delete |
| 获取公众号类目 | GET | /wxaapi/newtmpl/getcategory | NO | |
| 获取模板中的关键词 | GET | /wxaapi/newtmpl/getpubtemplatekeywords | NO | |
| 获取类目下的公共模板 | GET | /wxaapi/newtmpl/getpubtemplatetitles | NO | |
| 获取公众号类目 | GET | /wxaapi/newtmpl/getcategory | YES | (tpl *Subscribe) GetCategory |
| 获取模板中的关键词 | GET | /wxaapi/newtmpl/getpubtemplatekeywords | YES | (tpl *Subscribe) GetPubTplKeyWordsByID |
| 获取类目下的公共模板 | GET | /wxaapi/newtmpl/getpubtemplatetitles | YES | (tpl *Subscribe) GetPublicTemplateTitleList |
| 获取私有模板列表 | GET | /wxaapi/newtmpl/gettemplate | YES | (tpl *Subscribe) List() |
| 发送订阅通知 | POST | /cgi-bin/message/subscribe/bizsend | YES | (tpl *Subscribe) Send |

View File

@@ -8,10 +8,13 @@ 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"
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"
subscribeTemplateGetCategoryURL = "https://api.weixin.qq.com/wxaapi/newtmpl/getcategory"
subscribeTemplateGetPubTplKeyWorksURL = "https://api.weixin.qq.com/wxaapi/newtmpl/getpubtemplatekeywords"
subscribeTemplateGetPubTplTitles = "https://api.weixin.qq.com/wxaapi/newtmpl/getpubtemplatetitles"
)
// Subscribe 订阅消息
@@ -145,3 +148,108 @@ func (tpl *Subscribe) Delete(templateID string) (err error) {
}
return util.DecodeWithCommonError(response, "DeleteSubscribe")
}
// PublicTemplateCategory 公众号类目
type PublicTemplateCategory struct {
ID int `json:"id"` //类目ID
Name string `json:"name"` //类目的中文名
}
type resSubscribeCategoryList struct {
util.CommonError
CategoryList []*PublicTemplateCategory `json:"data"`
}
// GetCategory 获取公众号类目
func (tpl *Subscribe) GetCategory() (categoryList []*PublicTemplateCategory, err error) {
var accessToken string
accessToken, err = tpl.GetAccessToken()
if err != nil {
return
}
uri := fmt.Sprintf("%s?access_token=%s", subscribeTemplateGetCategoryURL, accessToken)
var response []byte
response, err = util.HTTPGet(uri)
if err != nil {
return
}
var result resSubscribeCategoryList
err = util.DecodeWithError(response, &result, "GetCategory")
if err != nil {
return
}
categoryList = result.CategoryList
return
}
// PublicTemplateKeyWords 模板中的关键词
type PublicTemplateKeyWords struct {
KeyWordsID int `json:"kid"` // 关键词 id
Name string `json:"name"` // 关键词内容
Example string `json:"example"` //关键词内容对应的示例
Rule string `json:"rule"` // 参数类型
}
type resPublicTemplateKeyWordsList struct {
util.CommonError
KeyWordsList []*PublicTemplateKeyWords `json:"data"` //关键词列表
}
// GetPubTplKeyWordsByID 获取模板中的关键词
func (tpl *Subscribe) GetPubTplKeyWordsByID(titleID string) (keyWordsList []*PublicTemplateKeyWords, err error) {
var accessToken string
accessToken, err = tpl.GetAccessToken()
if err != nil {
return
}
uri := fmt.Sprintf("%s?access_token=%s&tid=%s", subscribeTemplateGetPubTplKeyWorksURL, accessToken, titleID)
var response []byte
response, err = util.HTTPGet(uri)
if err != nil {
return
}
var result resPublicTemplateKeyWordsList
err = util.DecodeWithError(response, &result, "GetPublicTemplateKeyWords")
if err != nil {
return
}
keyWordsList = result.KeyWordsList
return
}
// PublicTemplateTitle 类目下的公共模板
type PublicTemplateTitle struct {
TitleID int `json:"tid"` // 模版标题 id
Title string `json:"title"` // 模版标题
Type int `json:"type"` // 模版类型2 为一次性订阅3 为长期订阅
CategoryID string `json:"categoryId"` // 模版所属类目 id
}
type resPublicTemplateTitleList struct {
util.CommonError
Count int `json:"count"` //公共模板列表总数
TemplateTitleList []*PublicTemplateTitle `json:"data"` //模板标题列表
}
// GetPublicTemplateTitleList 获取类目下的公共模板
func (tpl *Subscribe) GetPublicTemplateTitleList(ids string, start int, limit int) (count int, templateTitleList []*PublicTemplateTitle, err error) {
var accessToken string
accessToken, err = tpl.GetAccessToken()
if err != nil {
return
}
uri := fmt.Sprintf("%s?access_token=%s&ids=%s&start=%d&limit=%d", subscribeTemplateGetPubTplTitles, accessToken, ids, start, limit)
var response []byte
response, err = util.HTTPGet(uri)
if err != nil {
return
}
var result resPublicTemplateTitleList
err = util.DecodeWithError(response, &result, "GetPublicTemplateTitle")
if err != nil {
return
}
count = result.Count
templateTitleList = result.TemplateTitleList
return
}

View File

@@ -26,7 +26,22 @@ import (
// OfficialAccount 微信公众号相关API
type OfficialAccount struct {
ctx *context.Context
ctx *context.Context
basic *basic.Basic
menu *menu.Menu
oauth *oauth.Oauth
material *material.Material
draft *draft.Draft
freepublish *freepublish.FreePublish
js *js.Js
user *user.User
templateMsg *message.Template
managerMsg *message.Manager
device *device.Device
broadcast *broadcast.Broadcast
datacube *datacube.DataCube
ocr *ocr.OCR
subscribeMsg *message.Subscribe
}
// NewOfficialAccount 实例化公众号API
@@ -51,12 +66,18 @@ func (officialAccount *OfficialAccount) GetContext() *context.Context {
// GetBasic qr/url 相关配置
func (officialAccount *OfficialAccount) GetBasic() *basic.Basic {
return basic.NewBasic(officialAccount.ctx)
if officialAccount.basic == nil {
officialAccount.basic = basic.NewBasic(officialAccount.ctx)
}
return officialAccount.basic
}
// GetMenu 菜单管理接口
func (officialAccount *OfficialAccount) GetMenu() *menu.Menu {
return menu.NewMenu(officialAccount.ctx)
if officialAccount.menu == nil {
officialAccount.menu = menu.NewMenu(officialAccount.ctx)
}
return officialAccount.menu
}
// GetServer 消息管理:接收事件,被动回复消息管理
@@ -74,66 +95,105 @@ func (officialAccount *OfficialAccount) GetAccessToken() (string, error) {
// GetOauth oauth2网页授权
func (officialAccount *OfficialAccount) GetOauth() *oauth.Oauth {
return oauth.NewOauth(officialAccount.ctx)
if officialAccount.oauth == nil {
officialAccount.oauth = oauth.NewOauth(officialAccount.ctx)
}
return officialAccount.oauth
}
// GetMaterial 素材管理
func (officialAccount *OfficialAccount) GetMaterial() *material.Material {
return material.NewMaterial(officialAccount.ctx)
if officialAccount.material == nil {
officialAccount.material = material.NewMaterial(officialAccount.ctx)
}
return officialAccount.material
}
// GetDraft 草稿箱
func (officialAccount *OfficialAccount) GetDraft() *draft.Draft {
return draft.NewDraft(officialAccount.ctx)
if officialAccount.draft == nil {
officialAccount.draft = draft.NewDraft(officialAccount.ctx)
}
return officialAccount.draft
}
// GetFreePublish 发布能力
func (officialAccount *OfficialAccount) GetFreePublish() *freepublish.FreePublish {
return freepublish.NewFreePublish(officialAccount.ctx)
if officialAccount.freepublish == nil {
officialAccount.freepublish = freepublish.NewFreePublish(officialAccount.ctx)
}
return officialAccount.freepublish
}
// GetJs js-sdk配置
func (officialAccount *OfficialAccount) GetJs() *js.Js {
return js.NewJs(officialAccount.ctx)
if officialAccount.js == nil {
officialAccount.js = js.NewJs(officialAccount.ctx)
}
return officialAccount.js
}
// GetUser 用户管理接口
func (officialAccount *OfficialAccount) GetUser() *user.User {
return user.NewUser(officialAccount.ctx)
if officialAccount.user == nil {
officialAccount.user = user.NewUser(officialAccount.ctx)
}
return officialAccount.user
}
// GetTemplate 模板消息接口
func (officialAccount *OfficialAccount) GetTemplate() *message.Template {
return message.NewTemplate(officialAccount.ctx)
if officialAccount.templateMsg == nil {
officialAccount.templateMsg = message.NewTemplate(officialAccount.ctx)
}
return officialAccount.templateMsg
}
// GetCustomerMessageManager 客服消息接口
func (officialAccount *OfficialAccount) GetCustomerMessageManager() *message.Manager {
return message.NewMessageManager(officialAccount.ctx)
if officialAccount.managerMsg == nil {
officialAccount.managerMsg = message.NewMessageManager(officialAccount.ctx)
}
return officialAccount.managerMsg
}
// GetDevice 获取智能设备的实例
func (officialAccount *OfficialAccount) GetDevice() *device.Device {
return device.NewDevice(officialAccount.ctx)
if officialAccount.device == nil {
officialAccount.device = device.NewDevice(officialAccount.ctx)
}
return officialAccount.device
}
// GetBroadcast 群发消息
// TODO 待完善
func (officialAccount *OfficialAccount) GetBroadcast() *broadcast.Broadcast {
return broadcast.NewBroadcast(officialAccount.ctx)
if officialAccount.broadcast == nil {
officialAccount.broadcast = broadcast.NewBroadcast(officialAccount.ctx)
}
return officialAccount.broadcast
}
// GetDataCube 数据统计
func (officialAccount *OfficialAccount) GetDataCube() *datacube.DataCube {
return datacube.NewCube(officialAccount.ctx)
if officialAccount.datacube == nil {
officialAccount.datacube = datacube.NewCube(officialAccount.ctx)
}
return officialAccount.datacube
}
// GetOCR OCR接口
func (officialAccount *OfficialAccount) GetOCR() *ocr.OCR {
return ocr.NewOCR(officialAccount.ctx)
if officialAccount.ocr == nil {
officialAccount.ocr = ocr.NewOCR(officialAccount.ctx)
}
return officialAccount.ocr
}
// GetSubscribe 公众号订阅消息
func (officialAccount *OfficialAccount) GetSubscribe() *message.Subscribe {
return message.NewSubscribe(officialAccount.ctx)
if officialAccount.subscribeMsg == nil {
officialAccount.subscribeMsg = message.NewSubscribe(officialAccount.ctx)
}
return officialAccount.subscribeMsg
}