forked from chanxuehong/wechat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
message.go
239 lines (218 loc) · 7.83 KB
/
message.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package wxa
import (
"github.com/charsunny/wechat/mp/core"
)
type TemplateCategory struct {
Id string `json:"id"` // 作为模板时返回
Name string `json:"name"` // 作为模板时和作为添加到模板库后都返回
}
type TemplateKey struct {
Id string `json:"kid"` // 作为模板时返回
Name string `json:"name"` // 作为模板时和作为添加到模板库后都返回
TemplateId string `json:"template_id"` // 作为添加到模板库后返回
Rule string `json:"rule"` // 作为添加到模板库后返回
Example string `json:"example"` // 作为添加到模板库后返回
}
type TemplateTitle struct {
Id string `json:"tid"` // 作为模板时返回
Title string `json:"title"` // 作为模板时和作为添加到模板库后都返回
Type int `json:"type"` // 作为添加到模板库后返回
CategoryId int `json:"categoryId"` // 作为添加到模板库后返回
}
type Template struct {
Id string `json:"priTmplId"` // 作为模板时返回
Title string `json:"title"` // 作为模板时和作为添加到模板库后都返回
Content string `json:"content"` // 作为添加到模板库后返回
Type int `json:"type"` // 作为添加到模板库后返回
Example string `json:"example"` // 作为添加到模板库后返回
}
type WxaTemplateMsg struct {
TemplateId string `json:"template_id"` // 模板id
Page string `json:"page"` //跳转小程序页面
FormId string `json:"form_id"` // formid
Data map[string]interface{} `json:"data"` // 模板数据
EmphasisKeyword string `json:"emphasis_keyword"` //加重数据
}
type MpTemplateMsg struct {
TemplateId string `json:"template_id"` // 模板id
AppId string `json:"appid"` //跳转小程序页面
Url string `json:"url"` // formid
Data map[string]interface{} `json:"data"` // 模板数据
MiniProgram struct { //跳转小程序页面
AppId string `json:"appid"`
PagePath string `json:"pagepath"`
} `json:"miniprogram"`
}
// 组合模板并添加至帐号下的个人模板库
// id string 是 模板标题id,可通过接口获取,也可登录小程序后台查看获取
// keyword_id_list []int 是 开发者自行组合好的模板关键词列表,关键词顺序可以自由搭配(例如[3,5,4]或[4,5,3]),最多支持10个关键词组合
func AddTemplate(clt *core.Client, id string, keyIds []int, desc string) (template_id string, err error) {
const incompleteURL = "https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate?access_token="
var request = struct {
Id string `json:"tid"` //路径
KeywordIdList []int `json:"kidList"` //宽度
SceneDesc string `json:"sceneDesc"`
}{
Id: id,
KeywordIdList: keyIds,
SceneDesc: desc,
}
var result struct {
core.Error
TemplateId string `json:"priTmplId"`
}
if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
return
}
if result.ErrCode != core.ErrCodeOK {
err = &result.Error
return
}
template_id = result.TemplateId
return
}
// 组合模板并添加至帐号下的个人模板库
// template_id string 要删除的模板id
func DeleteTemplate(clt *core.Client, template_id string) (err error) {
const incompleteURL = "https://api.weixin.qq.com/wxaapi/newtmpl/deltemplate?access_token="
var request = struct {
TemplateId string `json:"priTmplId"` //路径
}{
TemplateId: template_id,
}
var result struct {
core.Error
}
if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
return
}
if result.ErrCode != core.ErrCodeOK {
err = &result.Error
return
}
return
}
// 获取模板的keyword列表
// id string 是 模板标题id,可通过接口获取,也可登录小程序后台查看获取
func GetTemplateCategory(clt *core.Client) (list []*TemplateCategory, err error) {
const incompleteURL = "https://api.weixin.qq.com/wxaapi/newtmpl/getcategory?access_token="
var request = struct {
}{}
var result struct {
core.Error
Data []*TemplateCategory `json:"data"`
}
if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
return
}
if result.ErrCode != core.ErrCodeOK {
err = &result.Error
return
}
list = result.Data
return
}
// 获取模板的keyword列表
// id string 是 模板标题id,可通过接口获取,也可登录小程序后台查看获取
func GetPubTemplateKeywords(clt *core.Client, id string) (count int64, templates []*TemplateKey, err error) {
const incompleteURL = "https://api.weixin.qq.com/wxaapi/newtmpl/getpubtemplatekeywords?access_token="
var request = struct {
Id string `json:"tid"` //路径
}{
Id: id,
}
var result struct {
core.Error
Count int64 `json:"count"`
Data []*TemplateKey `json:"data"`
}
if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
return
}
if result.ErrCode != core.ErrCodeOK {
err = &result.Error
return
}
count = result.Count
templates = result.Data
return
}
// 获取模板的keyword列表
// id string 是 模板标题id,可通过接口获取,也可登录小程序后台查看获取
func GetPubTemplateTitles(clt *core.Client, ids string, page, count int) (total_count int64, list []*TemplateTitle, err error) {
const incompleteURL = "https://api.weixin.qq.com/wxaapi/newtmpl/getpubtemplatetitles?access_token="
var request = struct {
Ids string `json:"ids"` //类目 id,多个用逗号隔开
Offset int `json:"start"` //路径
Count int `json:"limit"` //路径
}{
Ids: ids,
Offset: page,
Count: count,
}
var result struct {
core.Error
Count int64 `json:"count"`
Data []*TemplateTitle `json:"data"`
}
if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
return
}
if result.ErrCode != core.ErrCodeOK {
err = &result.Error
return
}
total_count = result.Count
list = result.Data
return
}
// 获取小程序模板库列表
// count: 用于分页,表示拉取count条记录。最大为 20。最后一页的list长度可能小于请求的count。
func GetTemplateList(clt *core.Client, page, count int) (list []*Template, err error) {
const incompleteURL = "https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate?access_token="
var request = struct {
}{}
var result struct {
core.Error
Data []*Template `json:"data"`
}
if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
return
}
if result.ErrCode != core.ErrCodeOK {
err = &result.Error
return
}
list = result.Data
return
}
// 发送模板消息
// data: { "k1":{"value":""}}
// miniprogram_state: developer为开发版;trial为体验版;formal为正式版;默认为正式版
func SendMessage(clt *core.Client, touser, template_id, page, miniprogram_state string, data map[string]interface{}) (err error) {
const incompleteURL = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="
var request = struct {
ToUser string `json:"touser"` // 用户openid
TemplateId string `json:"template_id"` // 模板id
Page string `json:"page"` //跳转小程序页面
MiniprogramState string `json:"miniprogram_state"` // formid
Data map[string]interface{} `json:"data"` // 模板数据
}{
ToUser: touser,
TemplateId: template_id,
Page: page,
MiniprogramState: miniprogram_state,
Data: data,
}
var result struct {
core.Error
}
if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
return
}
if result.ErrCode != core.ErrCodeOK {
err = &result.Error
return
}
return
}