Skip to content

Commit

Permalink
add: 统一服务消息下发接口
Browse files Browse the repository at this point in the history
  • Loading branch information
royalrick committed Sep 20, 2018
1 parent 226c78c commit 6280279
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 4 deletions.
69 changes: 69 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- [组合模板并添加至帐号下的个人模板库](#组合模板并添加至帐号下的个人模板库)
- [删除帐号下的某个模板](#删除帐号下的某个模板)
- [发送模板消息](#发送模板消息)
- [统一服务消息](#统一服务消息)
- [客服消息](#客服消息)
- [接收客服消息](#接收客服消息)
- [发送客服消息](#发送客服消息)
Expand Down Expand Up @@ -258,6 +259,74 @@ err := template.Send(openid, template, page, formID string, msg template.Message

---

## 统一服务消息

小程序和公众号模板消息统一的服务消息下发接口

[官方文档](https://developers.weixin.qq.com/miniprogram/dev/api/notice-uniform.html)

```go

// 消息体
msg := template.UniformMsg{
ToUser: "用户 openid",
// 小程序模板消息
WeappTemplateMsg: template.WeappTemplateMsg{
TemplateID: schedule.ActivityWillStartTemplateID,
Page: "pages/messages/main",
FormID: "1537411865951",
EmphasisKeyword: "keyword1.DATA", // dev:
Data: template.Data{
"first": template.Keyword{
Value: "恭喜你购买成功!",
Color: "#173177",
},
"keyword1": template.Keyword{
Value: "巧克力",
Color: "#173177",
},
"keyword2": template.Keyword{
Value: "39.8元",
Color: "#173177",
},
},
},
// 公众号模板消息
MPTemplateMsg: template.MPTemplateMsg{
AppID: "wx2c5a33d31b4ee88f",
TemplateID: "UmuX15eBoonYkLy-7Xle1rA6xHhv4bsbie1Viidg2Cs",
URL: "https://medivhzhan.me",
Miniprogram: template.Miniprogram{
AppID: "wx7ad9cfdc85a2fdb2",
Pagepath: "pages/me/main",
},
Data: template.Data{
"first": template.Keyword{
Value: "恭喜你购买成功!",
Color: "#173177",
},
"keyword1": template.Keyword{
Value: "巧克力",
Color: "#173177",
},
"keyword2": template.Keyword{
Value: "39.8元",
Color: "#173177",
},
"remark": template.Keyword{
Value: "rrrrrrrmmmaaarrrrkkkk",
Color: "#173177",
},
},
},
}

err := msg.Send(token)

```

---

## 客服消息

### 接收客服消息
Expand Down
8 changes: 4 additions & 4 deletions message/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const (
sendAPI = "/cgi-bin/message/wxopen/template/send"
)

// Keyword 关键字
type Keyword struct {
// KeywordItem 关键字
type KeywordItem struct {
KeywordID uint `json:"keyword_id"`
Name string `json:"name"`
Example string `json:"example"`
Expand All @@ -38,7 +38,7 @@ type Template struct {
Content string `json:"content,omitempty"`
Example string `json:"example,omitempty"`

KeywordList []Keyword `json:"keyword_list,omitempty"`
KeywordList []KeywordItem `json:"keyword_list,omitempty"`
}

// Templates 获取模板列表返回的数据
Expand Down Expand Up @@ -116,7 +116,7 @@ func templates(api string, offset, count uint, token string) (list []Template, t
//
// @id 模板ID
// @token 微信 access_token
func Get(id, token string) (keywords []Keyword, err error) {
func Get(id, token string) (keywords []KeywordItem, err error) {
api, err := util.TokenAPI(weapp.BaseURL+detailAPI, token)
if err != nil {
return
Expand Down
90 changes: 90 additions & 0 deletions message/template/uniform_send.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package template

import (
"encoding/json"
"errors"
"net/http"
"strings"

"github.com/medivhzhan/weapp"
"github.com/medivhzhan/weapp/util"
)

const uniformSendAPI = "/cgi-bin/message/wxopen/template/uniform_send"

// WeappTemplateMsg 小程序模板消息
type WeappTemplateMsg struct {
TemplateID string `json:"template_id"`
Page string `json:"page"`
FormID string `json:"form_id"`
Data Data `json:"data"`
EmphasisKeyword string `json:"emphasis_keyword,omitempty"`
}

// Data 模板消息内容
type Data map[string]Keyword

// Keyword 关键字
type Keyword struct {
Value string `json:"value"`
Color string `json:"color"`
}

// MPTemplateMsg 公众号模板消息
type MPTemplateMsg struct {
AppID string `json:"appid"`
TemplateID string `json:"template_id"`
URL string `json:"url"`
Miniprogram Miniprogram `json:"miniprogram"`
Data map[string]Keyword `json:"data"`
}

// Miniprogram 小程序
type Miniprogram struct {
AppID string `json:"appid"`
Pagepath string `json:"pagepath"`
}

// UniformMsg 统一服务消息
type UniformMsg struct {
ToUser string `json:"touser"` // 用户 openid
MPTemplateMsg MPTemplateMsg `json:"mp_template_msg"`
WeappTemplateMsg WeappTemplateMsg `json:"weapp_template_msg"`
}

// Send 统一服务消息
//
// @token access_token
func (msg UniformMsg) Send(token string) error {
api, err := util.TokenAPI(weapp.BaseURL+uniformSendAPI, token)
if err != nil {
return err
}

body, err := json.Marshal(msg)
if err != nil {
return err
}

res, err := http.Post(api, "application/json", strings.NewReader(string(body)))
if err != nil {
return err
}
defer res.Body.Close()

if res.StatusCode != 200 {
err = errors.New(weapp.WeChatServerError)
return err
}

var resp weapp.Response
if err = json.NewDecoder(res.Body).Decode(&resp); err != nil {
return err
}

if resp.Errcode != 0 {
return errors.New(resp.Errmsg)
}

return nil
}

0 comments on commit 6280279

Please sign in to comment.