/
msg_receive.go
387 lines (339 loc) · 8.96 KB
/
msg_receive.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/**
* 服务号消息/事件的结构定义和解析
* Rosbit Xu
*/
package wxmsg
import (
"github.com/beevik/etree"
"fmt"
"strconv"
)
const (
// msgType
MT_TEXT = "text"
MT_IMAGE = "image"
MT_VOICE = "voice"
MT_VIDEO = "video"
MT_SHORTVIDEO = "shortvideo"
MT_LOCATION = "location"
MT_LINK = "link"
MT_EVENT = "event" // special message type
// event type
ET_VIEW = "VIEW"
ET_CLICK = "CLICK"
ET_SUBSCRIBE = "subscribe"
ET_UNSUBSCRIBE = "unsubscribe"
ET_SCAN = "SCAN" // SCAN事件内容和subscribe是一样的,注意和scancode_xxx事件的区分
ET_WHERE = "location" // 由location_select菜单引起的事件
ET_LOCATION = "LOCATION"
ET_PIC_SYSPHOTO = "pic_sysphoto"
ET_PIC_PHOTO_OR_ALBUM = "pic_photo_or_album"
ET_PIC_WEIXIN = "pic_weixin"
ET_SCANCODE_WAITMSG = "scancode_waitmsg"
ET_SCANCODE_PUSH = "scancode_push"
ET_MASSSENDJOBFINISH = "MASSSENDJOBFINISH"
ET_TEMPLATESENDJOBFINISH = "TEMPLATESENDJOBFINISH"
)
func _getText(el *etree.Element, tagName string) (string, error) {
t := el.SelectElement(tagName)
if t == nil {
return "", fmt.Errorf("%s not found", tagName)
}
return t.Text(), nil
}
// 所有接收消息都实现的接口。消息处理可以统一为 ReceivedMsg -> [处理] -> ReplyMsg
type ReceivedMsg interface {
parse(root *etree.Element)
}
// --------------------- message -------------------
type BaseMsg struct {
ToUserName string
FromUserName string
CreateTime int
MsgType string
}
func (m *BaseMsg) parse(root *etree.Element) {
m.ToUserName, _ = _getText(root, "ToUserName")
m.FromUserName, _ = _getText(root, "FromUserName")
m.MsgType, _ = _getText(root, "MsgType")
ct, _ := _getText(root, "CreateTime")
m.CreateTime, _ = strconv.Atoi(ct)
}
type Msg struct {
BaseMsg
MsgId string
}
func (m *Msg) parse(root *etree.Element) {
m.BaseMsg.parse(root)
m.MsgId, _ = _getText(root, "MsgId")
}
// ---- text message ----
type TextMsg struct {
Msg
Content string
}
func (m *TextMsg) parse(root *etree.Element) {
m.Msg.parse(root)
m.Content, _ = _getText(root, "Content")
}
// ---- image message ----
type ImageMsg struct {
Msg
PicUrl string
MediaId string
}
func (m *ImageMsg) parse(root *etree.Element) {
m.Msg.parse(root)
m.PicUrl, _ = _getText(root, "PicUrl")
m.MediaId, _ = _getText(root, "MediaId")
}
// ---- voice message ----
type VoiceMsg struct {
Msg
MediaId string
Format string
Recognition string
}
func (m *VoiceMsg) parse(root *etree.Element) {
m.Msg.parse(root)
m.MediaId, _ = _getText(root, "MediaId")
m.Format, _ = _getText(root, "Format")
m.Recognition, _ = _getText(root, "Recognition")
}
// ---- video message ----
type VideoMsg struct {
Msg
MediaId string
ThumbMediaId string
}
func (m *VideoMsg) parse(root *etree.Element) {
m.Msg.parse(root)
m.MediaId, _ = _getText(root, "MediaId")
m.ThumbMediaId, _ = _getText(root, "ThumbMediaId")
}
// ---- location message -----
type LocationMsg struct {
Msg
Location_X string
Location_Y string
Scale int
Label string
}
func (m *LocationMsg) parse(root *etree.Element) {
m.Msg.parse(root)
m.Location_X, _ = _getText(root, "Location_X")
m.Location_Y, _ = _getText(root, "Location_Y")
scale, _ := _getText(root, "Scale")
m.Scale, _ = strconv.Atoi(scale)
m.Label, _ = _getText(root, "Label")
}
// ---- link message -----
type LinkMsg struct {
Msg
Title string
Description string
Url string
}
func (m *LinkMsg) parse(root *etree.Element) {
m.Msg.parse(root)
m.Title, _ = _getText(root, "Title")
m.Description, _ = _getText(root, "Description")
m.Url, _ = _getText(root, "Url")
}
// ------------- event ---------------
type EventMsg struct {
BaseMsg
Event string
}
func (m *EventMsg) parse(root *etree.Element) {
m.BaseMsg.parse(root)
m.Event, _ = _getText(root, "Event")
}
// ---- event CLICK ----
type ClickEvent struct {
EventMsg
EventKey string
}
func (m *ClickEvent) parse(root *etree.Element) {
m.EventMsg.parse(root)
m.EventKey, _ = _getText(root, "EventKey")
}
// ---- event VIEW ----
type ViewEvent struct {
EventMsg
EventKey string
MenuId string
}
func (m *ViewEvent) parse(root *etree.Element) {
m.EventMsg.parse(root)
m.EventKey, _ = _getText(root, "EventKey")
m.MenuId, _ = _getText(root, "MenuId")
}
// ---- event subscribe, unsubscribe, SCAN ----
type SubscribeEvent struct {
EventMsg
EventKey string
Ticket string
}
func (m *SubscribeEvent) parse(root *etree.Element) {
m.EventMsg.parse(root)
m.EventKey, _ = _getText(root, "EventKey")
m.Ticket, _ = _getText(root, "Ticket")
}
// ---- event pic_sysphoto, pic_photo_or_album, pic_weixin ---
type PhotoEvent struct {
EventMsg
EventKey string
Count int // SendPicsInfo/Count
PicList []string // SendPicsInfo/item/PicMd5Sum
}
func (m *PhotoEvent) parse(root *etree.Element) {
m.EventMsg.parse(root)
m.EventKey, _ = _getText(root, "EventKey")
sendPicsInfo := root.SelectElement("SendPicsInfo")
if sendPicsInfo == nil {
return
}
c, _ := _getText(sendPicsInfo, "Count")
m.Count, _ = strconv.Atoi(c)
if m.Count <= 0 {
return
}
picList := sendPicsInfo.SelectElement("PicList")
if picList == nil {
return
}
items := picList.SelectElements("item")
if items == nil {
return
}
m.PicList = make([]string, len(items))
for i, item := range items {
m.PicList[i], _ = _getText(item, "PicMd5Sum")
}
}
// ---- event location ----- 由location_select菜单引起的事件
type WhereEvent struct {
EventMsg
Location_X string
Location_Y string
Scale int
Label string
MsgId string
}
func (m *WhereEvent) parse(root *etree.Element) {
m.EventMsg.parse(root)
m.Location_X, _ = _getText(root, "Location_X")
m.Location_Y, _ = _getText(root, "Location_Y")
scale, _ := _getText(root, "Scale")
m.Scale, _ = strconv.Atoi(scale)
m.Label, _ = _getText(root, "Label")
m.MsgId, _ = _getText(root, "MsgId")
}
// ---- event LOCATION -----
type LocationEvent struct {
EventMsg
Latitude string
Longitude string
Precision string
}
func (m *LocationEvent) parse(root *etree.Element) {
m.EventMsg.parse(root)
m.Latitude, _ = _getText(root, "Latitude")
m.Longitude, _ = _getText(root, "Longitude")
m.Precision, _ = _getText(root, "Precision")
}
// ---- event scancode_waitmsg, scancode_push ----
type ScancodeEvent struct {
EventMsg
EventKey string
ScanType string
ScanResult string
}
func (m *ScancodeEvent) parse(root *etree.Element) {
m.EventMsg.parse(root)
m.EventKey, _ = _getText(root, "EventKey")
scanCodeInfo := root.SelectElement("ScanCodeInfo")
if scanCodeInfo == nil {
return
}
m.ScanType, _ = _getText(scanCodeInfo, "ScanType")
m.ScanResult, _ = _getText(scanCodeInfo, "ScanResult")
}
// ---- event MASSSENDJOBFINISH ---- 群发结果推送事件
type ResultListItem struct {
ArticleIdx string
UserDeclareState string
AuditState string
OriginalArticleUrl string
OriginalArticleType string
CanReprint string
NeedReplaceContent string
NeedShowReprintSource string
}
func (m *ResultListItem) parse(root *etree.Element) {
m.ArticleIdx, _ = _getText(root, "ArticleIdx")
m.UserDeclareState, _ = _getText(root, "UserDeclareState")
m.AuditState, _ = _getText(root, "AuditState")
m.OriginalArticleUrl, _ = _getText(root, "OriginalArticleUrl")
m.OriginalArticleType, _ = _getText(root, "OriginalArticleType")
m.CanReprint, _ = _getText(root, "CanReprint")
m.NeedReplaceContent, _ = _getText(root, "NeedReplaceContent")
m.NeedShowReprintSource, _ = _getText(root, "NeedShowReprintSource")
}
type MassSentEvent struct {
EventMsg
MsgID string
Status string
TotalCount int
FilterCount int
SentCount int
ErrorCount int
ResultCount int
ResultList []ResultListItem
}
func (m *MassSentEvent) parse(root *etree.Element) {
m.EventMsg.parse(root)
m.MsgID, _ = _getText(root, "MsgID")
m.Status, _ = _getText(root, "Status")
c, _ := _getText(root, "TotalCount")
m.TotalCount, _ = strconv.Atoi(c)
c, _ = _getText(root, "FilterCount")
m.FilterCount, _ = strconv.Atoi(c)
c, _ = _getText(root, "SentCount")
m.SentCount, _ = strconv.Atoi(c)
c, _ = _getText(root, "ErrorCount")
m.ErrorCount, _ = strconv.Atoi(c)
copyrightCheckResult := root.SelectElement("CopyrightCheckResult")
if copyrightCheckResult == nil {
return
}
c, _ = _getText(copyrightCheckResult, "Count")
m.ResultCount, _ = strconv.Atoi(c)
if m.ResultCount <= 0 {
return
}
m.ResultList = make([]ResultListItem, m.ResultCount)
resultList := copyrightCheckResult.SelectElement("ResultList")
if resultList == nil {
return
}
items := resultList.SelectElements("item")
if items == nil {
return
}
for i, item := range items {
m.ResultList[i].parse(item)
}
}
// ---- event TEMPLATESENDJOBFINISH ----- 模板消息发送结果推送事件
type TemplateSentEvent struct {
EventMsg
MsgId string
Status string
}
func (m *TemplateSentEvent) parse(root *etree.Element) {
m.EventMsg.parse(root)
m.MsgId, _ = _getText(root, "MsgID")
m.Status, _ = _getText(root, "Status")
}