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) {