-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathmp_template.go
100 lines (82 loc) · 2.51 KB
/
mp_template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package wechat
import (
"errors"
"fmt"
"github.com/esap/wechat/util"
)
// MPTemplateGetAll 服务号模板消息接口
const (
MPTemplateGetAll = WXAPI + "template/get_all_private_template?access_token="
MPTemplateAdd = WXAPI + "template/api_add_template?access_token="
MPTemplateDel = WXAPI + "template/del_private_template?access_token="
MPTemplateSendMsg = WXAPI + "message/template/send?access_token="
)
// MpTemplate 模板信息
type MpTemplate struct {
TemplateId string `json:"template_id"`
Title string `json:"title"`
PrimaryIndustry string `json:"primary_industry"`
DeputyIndustry string `json:"deputy_industry"`
Content string `json:"content"`
Example string `json:"example"`
}
// AddTemplate 获取模板
func (s *Server) AddTemplate(IdShort string) (id string, err error) {
form := map[string]interface{}{"template_id_short": IdShort}
ret := make(map[string]interface{})
err = util.PostJsonPtr(MPTemplateAdd+s.GetAccessToken(), form, ret)
if err != nil {
return
}
if fmt.Sprint(ret["errcode"]) != "0" {
return "", errors.New(fmt.Sprint(ret["errcode"]))
}
return ret["template_id"].(string), nil
}
// DelTemplate 删除模板
func (s *Server) DelTemplate(id string) (err error) {
form := map[string]interface{}{"template_id": id}
ret := make(map[string]interface{})
err = util.PostJsonPtr(MPTemplateDel+s.GetAccessToken(), form, ret)
if err != nil {
return
}
if fmt.Sprint(ret["errcode"]) != "0" {
return errors.New(fmt.Sprint(ret["errcode"]))
}
return
}
// GetAllTemplate 获取模板
func (s *Server) GetAllTemplate() (templist []MpTemplate, err error) {
ret := make(map[string]interface{})
err = util.GetJson(MPTemplateGetAll+s.GetAccessToken(), ret)
if err != nil {
return
}
if fmt.Sprint(ret["errcode"]) != "0" {
return nil, errors.New(fmt.Sprint(ret["errcode"]))
}
return ret["template_id"].([]MpTemplate), nil
}
// SendTemplate 发送模板消息,data通常是map[string]struct{value string,color string}
func (s *Server) SendTemplate(to, id, url, appid, pagepath string, data interface{}) *WxErr {
form := map[string]interface{}{
"touser": to,
"template_id": id,
"data": data,
}
if pagepath != "" {
form["miniprogram"] = map[string]string{
"appid": appid,
"pagepath": pagepath,
}
} else if url != "" {
form["url"] = url
}
ret := new(WxErr)
err := util.PostJsonPtr(MPTemplateSendMsg+s.GetAccessToken(), form, &ret)
if err != nil {
return &WxErr{ErrCode: -1, ErrMsg: err.Error()}
}
return ret
}