-
Notifications
You must be signed in to change notification settings - Fork 698
/
Copy pathmsg.go
334 lines (298 loc) · 8.55 KB
/
msg.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package custom
import (
"github.com/chanxuehong/wechat/mp/core"
)
const (
MsgTypeText core.MsgType = "text" // 文本消息
MsgTypeImage core.MsgType = "image" // 图片消息
MsgTypeVoice core.MsgType = "voice" // 语音消息
MsgTypeVideo core.MsgType = "video" // 视频消息
MsgTypeMusic core.MsgType = "music" // 音乐消息
MsgTypeNews core.MsgType = "news" // 图文消息
MsgTypeMPNews core.MsgType = "mpnews" // 图文消息, 发送已经创建好的图文
MsgTypeWxCard core.MsgType = "wxcard" // 卡卷消息
MsgTypeWxMiniLink core.MsgType = "link" // 小程序客服消息:图文链接
)
type MsgHeader struct {
ToUser string `json:"touser"` // 接收方 OpenID
MsgType core.MsgType `json:"msgtype"`
}
type CustomService struct {
KfAccount string `json:"kf_account"`
}
// 文本消息
type Text struct {
MsgHeader
Text struct {
Content string `json:"content"` // 支持换行符
} `json:"text"`
CustomService *CustomService `json:"customservice,omitempty"`
}
// 新建文本消息.
// 如果不指定客服则 kfAccount 留空.
func NewText(toUser, content, kfAccount string) (text *Text) {
text = &Text{
MsgHeader: MsgHeader{
ToUser: toUser,
MsgType: MsgTypeText,
},
}
text.Text.Content = content
if kfAccount != "" {
text.CustomService = &CustomService{
KfAccount: kfAccount,
}
}
return
}
// 图片消息
type Image struct {
MsgHeader
Image struct {
MediaId string `json:"media_id"` // 通过素材管理接口上传多媒体文件得到 MediaId
} `json:"image"`
CustomService *CustomService `json:"customservice,omitempty"`
}
// 新建图片消息.
// 如果不指定客服则 kfAccount 留空.
func NewImage(toUser, mediaId, kfAccount string) (image *Image) {
image = &Image{
MsgHeader: MsgHeader{
ToUser: toUser,
MsgType: MsgTypeImage,
},
}
image.Image.MediaId = mediaId
if kfAccount != "" {
image.CustomService = &CustomService{
KfAccount: kfAccount,
}
}
return
}
// 语音消息
type Voice struct {
MsgHeader
Voice struct {
MediaId string `json:"media_id"` // 通过素材管理接口上传多媒体文件得到 MediaId
} `json:"voice"`
CustomService *CustomService `json:"customservice,omitempty"`
}
// 新建语音消息.
// 如果不指定客服则 kfAccount 留空.
func NewVoice(toUser, mediaId, kfAccount string) (voice *Voice) {
voice = &Voice{
MsgHeader: MsgHeader{
ToUser: toUser,
MsgType: MsgTypeVoice,
},
}
voice.Voice.MediaId = mediaId
if kfAccount != "" {
voice.CustomService = &CustomService{
KfAccount: kfAccount,
}
}
return
}
// 视频消息
type Video struct {
MsgHeader
Video struct {
MediaId string `json:"media_id"` // 通过素材管理接口上传多媒体文件得到 MediaId
ThumbMediaId string `json:"thumb_media_id"` // 通过素材管理接口上传多媒体文件得到 ThumbMediaId
Title string `json:"title,omitempty"` // 视频消息的标题, 可以为 ""
Description string `json:"description,omitempty"` // 视频消息的描述, 可以为 ""
} `json:"video"`
CustomService *CustomService `json:"customservice,omitempty"`
}
// 新建视频消息.
// 如果不指定客服则 kfAccount 留空.
func NewVideo(toUser, mediaId, thumbMediaId, title, description, kfAccount string) (video *Video) {
video = &Video{
MsgHeader: MsgHeader{
ToUser: toUser,
MsgType: MsgTypeVideo,
},
}
video.Video.MediaId = mediaId
video.Video.ThumbMediaId = thumbMediaId
video.Video.Title = title
video.Video.Description = description
if kfAccount != "" {
video.CustomService = &CustomService{
KfAccount: kfAccount,
}
}
return
}
// 音乐消息
type Music struct {
MsgHeader
Music struct {
Title string `json:"title,omitempty"` // 音乐标题, 可以为 ""
Description string `json:"description,omitempty"` // 音乐描述, 可以为 ""
MusicURL string `json:"musicurl"` // 音乐链接
HQMusicURL string `json:"hqmusicurl"` // 高质量音乐链接, WIFI环境优先使用该链接播放音乐
ThumbMediaId string `json:"thumb_media_id"` // 通过素材管理接口上传多媒体文件得到 ThumbMediaId
} `json:"music"`
CustomService *CustomService `json:"customservice,omitempty"`
}
// 新建音乐消息.
// 如果不指定客服则 kfAccount 留空.
func NewMusic(toUser, thumbMediaId, musicURL, HQMusicURL, title, description, kfAccount string) (music *Music) {
music = &Music{
MsgHeader: MsgHeader{
ToUser: toUser,
MsgType: MsgTypeMusic,
},
}
music.Music.ThumbMediaId = thumbMediaId
music.Music.MusicURL = musicURL
music.Music.HQMusicURL = HQMusicURL
music.Music.Title = title
music.Music.Description = description
if kfAccount != "" {
music.CustomService = &CustomService{
KfAccount: kfAccount,
}
}
return
}
// 图文消息里的 Article
type Article struct {
Title string `json:"title,omitempty"` // 图文消息标题
Description string `json:"description,omitempty"` // 图文消息描述
URL string `json:"url,omitempty"` // 点击图文消息跳转链接
PicURL string `json:"picurl,omitempty"` // 图文消息的图片链接, 支持JPG, PNG格式, 较好的效果为大图640*320, 小图80*80
}
// 图文消息
type News struct {
MsgHeader
News struct {
Articles []Article `json:"articles,omitempty"` // 多条图文消息信息, 默认第一个item为大图, 注意, 如果图文数超过8, 则将会无响应
} `json:"news"`
CustomService *CustomService `json:"customservice,omitempty"`
}
// 新建图文消息.
// 如果不指定客服则 kfAccount 留空.
func NewNews(toUser string, articles []Article, kfAccount string) (news *News) {
news = &News{
MsgHeader: MsgHeader{
ToUser: toUser,
MsgType: MsgTypeNews,
},
}
news.News.Articles = articles
if kfAccount != "" {
news.CustomService = &CustomService{
KfAccount: kfAccount,
}
}
return
}
type MPNews struct {
MsgHeader
MPNews struct {
MediaId string `json:"media_id"` // 通过素材管理接口上传多媒体文件得到 MediaId
} `json:"mpnews"`
CustomService *CustomService `json:"customservice,omitempty"`
}
// 新建图文消息.
// 如果不指定客服则 kfAccount 留空.
func NewMPNews(toUser, mediaId, kfAccount string) (mpnews *MPNews) {
mpnews = &MPNews{
MsgHeader: MsgHeader{
ToUser: toUser,
MsgType: MsgTypeMPNews,
},
}
mpnews.MPNews.MediaId = mediaId
if kfAccount != "" {
mpnews.CustomService = &CustomService{
KfAccount: kfAccount,
}
}
return
}
// 卡券消息, 特别注意客服消息接口投放卡券仅支持非自定义Code码的卡券
type WxCard struct {
MsgHeader
WxCard struct {
CardId string `json:"card_id"`
CardExt string `json:"card_ext,omitempty"`
} `json:"wxcard"`
CustomService *CustomService `json:"customservice,omitempty"`
}
// 新建卡券消息.
// 如果不指定客服则 kfAccount 留空.
func NewWxCard(toUser, cardId, cardExt, kfAccount string) (card *WxCard) {
card = &WxCard{
MsgHeader: MsgHeader{
ToUser: toUser,
MsgType: MsgTypeWxCard,
},
}
card.WxCard.CardId = cardId
card.WxCard.CardExt = cardExt
if kfAccount != "" {
card.CustomService = &CustomService{
KfAccount: kfAccount,
}
}
return
}
type WxMiniLink struct {
MsgHeader
Link struct {
Title string `json:"title"`
Description string `json:"description"`
URL string `json:"url"`
ThumbURL string `json:"thumb_url"`
} `json:"link"`
CustomService *CustomService `json:"customservice,omitempty"`
}
func NewMiniLink(toUser, title, desc, url, thumbUrl, kfAccount string) (link *WxMiniLink) {
link = &WxMiniLink{
MsgHeader: MsgHeader{
ToUser: toUser,
MsgType: MsgTypeWxMiniLink,
},
}
link.Link.Title = title
link.Link.Description = desc
link.Link.URL = url
link.Link.ThumbURL = thumbUrl
if kfAccount != "" {
link.CustomService = &CustomService{
KfAccount: kfAccount,
}
}
return
}
type WxMiniPage struct {
MsgHeader
MiniProgramPage struct {
Title string `json:"title"`
PagePath string `json:"pagepath"`
ThumbMediaId string `json:"thumb_media_id"`
} `json:"miniprogrampage"`
CustomService *CustomService `json:"customservice,omitempty"`
}
func NewMiniPage(toUser, title, pagePath, thumbMediaId, kfAccount string) (page *WxMiniPage) {
page = &WxMiniPage{
MsgHeader: MsgHeader{
ToUser: toUser,
MsgType: MsgTypeWxMiniLink,
},
}
page.MiniProgramPage.Title = title
page.MiniProgramPage.PagePath = pagePath
page.MiniProgramPage.ThumbMediaId = thumbMediaId
if kfAccount != "" {
page.CustomService = &CustomService{
KfAccount: kfAccount,
}
}
return
}