forked from esap/wechat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mp_template.go
103 lines (84 loc) · 2.58 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
101
102
103
package wechat
import (
"errors"
"github.com/esap/wechat/util"
)
// MP_Template 公众号模板消息接口
const (
MP_AddTemplate = WXAPI + "template/api_add_template?access_token="
MP_GetAllTemplate = WXAPI + "template/get_all_private_template?access_token="
MP_DelTemplate = WXAPI + "template/del_private_template?access_token="
MP_SendTemplateMsg = 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:"template_id"`
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(MP_AddTemplate+s.GetAccessToken(), form, ret)
if err != nil {
return
}
if ret["errcode"].(int64) != 0 {
return "", errors.New(ret["errcode"].(string))
}
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(MP_DelTemplate+s.GetAccessToken(), form, ret)
if err != nil {
return
}
if ret["errcode"].(int64) != 0 {
return errors.New(ret["errcode"].(string))
}
return
}
// GetAllTemplate 获取模板
func (s *Server) GetAllTemplate() (templist []MpTemplate, err error) {
ret := make(map[string]interface{})
err = util.GetJson(MP_GetAllTemplate+s.GetAccessToken(), ret)
if err != nil {
return
}
if ret["errcode"].(int64) != 0 {
return nil, errors.New(ret["errcode"].(string))
}
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{}) (msgid int64, err error) {
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 := make(map[string]interface{})
err = util.PostJsonPtr(MP_SendTemplateMsg+s.GetAccessToken(), form, ret)
if err != nil {
return
}
if ret["errcode"].(int64) != 0 {
return 0, errors.New(ret["errcode"].(string))
}
return ret["msgid"].(int64), nil
}