Skip to content

Commit

Permalink
Feature/mini program template (#806)
Browse files Browse the repository at this point in the history
* feat: 小程序发送订阅消息支持返回 msgid

* 小程序发送订阅消息支持返回 msgid

---------

Co-authored-by: w_yangyili <w_yangyili@xiwang.com>
  • Loading branch information
yangyl12345 and w_yangyili authored Nov 27, 2024
1 parent a571bf3 commit 4a8371e
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions miniprogram/subscribe/subscribe.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package subscribe

import (
"encoding/json"
"fmt"

"github.com/silenceper/wechat/v2/miniprogram/context"
Expand Down Expand Up @@ -70,6 +71,13 @@ type TemplateList struct {
Data []TemplateItem `json:"data"`
}

// resTemplateSend 发送获取 msg id
type resTemplateSend struct {
util.CommonError

MsgID int64 `json:"msgid"`
}

// Send 发送订阅消息
func (s *Subscribe) Send(msg *Message) (err error) {
var accessToken string
Expand All @@ -85,6 +93,33 @@ func (s *Subscribe) Send(msg *Message) (err error) {
return util.DecodeWithCommonError(response, "Send")
}

// SendGetMsgID 发送订阅消息返回 msgid
func (s *Subscribe) SendGetMsgID(msg *Message) (msgID int64, err error) {
var accessToken string
accessToken, err = s.GetAccessToken()
if err != nil {
return
}
uri := fmt.Sprintf("%s?access_token=%s", subscribeSendURL, accessToken)
response, err := util.PostJSON(uri, msg)
if err != nil {
return
}

var result resTemplateSend
if err = json.Unmarshal(response, &result); err != nil {
return
}
if result.ErrCode != 0 {
err = fmt.Errorf("template msg send error : errcode=%v , errmsg=%v", result.ErrCode, result.ErrMsg)
return
}

msgID = result.MsgID

return
}

// ListTemplates 获取当前帐号下的个人模板列表
// https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.getTemplateList.html
func (s *Subscribe) ListTemplates() (*TemplateList, error) {
Expand Down

0 comments on commit 4a8371e

Please sign in to comment.