From 4a8371e1781574fc5672b468a0376fa0550f4a5e Mon Sep 17 00:00:00 2001 From: yangyl12345 Date: Wed, 27 Nov 2024 11:56:14 +0800 Subject: [PATCH] Feature/mini program template (#806) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 小程序发送订阅消息支持返回 msgid * 小程序发送订阅消息支持返回 msgid --------- Co-authored-by: w_yangyili --- miniprogram/subscribe/subscribe.go | 35 ++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/miniprogram/subscribe/subscribe.go b/miniprogram/subscribe/subscribe.go index 9099044a..5be3bcfc 100644 --- a/miniprogram/subscribe/subscribe.go +++ b/miniprogram/subscribe/subscribe.go @@ -1,6 +1,7 @@ package subscribe import ( + "encoding/json" "fmt" "github.com/silenceper/wechat/v2/miniprogram/context" @@ -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 @@ -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) {