/
event.go
483 lines (439 loc) · 13.7 KB
/
event.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// Copyright 2016 LINE Corporation
//
// LINE Corporation licenses this file to you under the Apache License,
// version 2.0 (the "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package linebot
import (
"encoding/hex"
"encoding/json"
"time"
)
// EventType type
type EventType string
// EventType constants
const (
EventTypeMessage EventType = "message"
EventTypeFollow EventType = "follow"
EventTypeUnfollow EventType = "unfollow"
EventTypeJoin EventType = "join"
EventTypeLeave EventType = "leave"
EventTypeMemberJoined EventType = "memberJoined"
EventTypeMemberLeft EventType = "memberLeft"
EventTypePostback EventType = "postback"
EventTypeBeacon EventType = "beacon"
EventTypeAccountLink EventType = "accountLink"
EventTypeThings EventType = "things"
)
// EventMode type
type EventMode string
// EventMode constants
const (
EventModeActive EventMode = "active"
EventModeStandby EventMode = "standby"
)
// EventSourceType type
type EventSourceType string
// EventSourceType constants
const (
EventSourceTypeUser EventSourceType = "user"
EventSourceTypeGroup EventSourceType = "group"
EventSourceTypeRoom EventSourceType = "room"
)
// EventSource type
type EventSource struct {
Type EventSourceType `json:"type"`
UserID string `json:"userId,omitempty"`
GroupID string `json:"groupId,omitempty"`
RoomID string `json:"roomId,omitempty"`
}
// Params type
type Params struct {
Date string `json:"date,omitempty"`
Time string `json:"time,omitempty"`
Datetime string `json:"datetime,omitempty"`
}
// Members type
type Members struct {
Members []EventSource `json:"members"`
}
// Postback type
type Postback struct {
Data string `json:"data"`
Params *Params `json:"params,omitempty"`
}
// BeaconEventType type
type BeaconEventType string
// BeaconEventType constants
const (
BeaconEventTypeEnter BeaconEventType = "enter"
BeaconEventTypeLeave BeaconEventType = "leave"
BeaconEventTypeBanner BeaconEventType = "banner"
BeaconEventTypeStay BeaconEventType = "stay"
)
// Beacon type
type Beacon struct {
Hwid string
Type BeaconEventType
DeviceMessage []byte
}
// AccountLinkResult type
type AccountLinkResult string
// AccountLinkResult constants
const (
AccountLinkResultOK AccountLinkResult = "ok"
AccountLinkResultFailed AccountLinkResult = "failed"
)
// AccountLink type
type AccountLink struct {
Result AccountLinkResult
Nonce string
}
// ThingsResult type
type ThingsResult struct {
ScenarioID string
Revision int
StartTime int64
EndTime int64
ResultCode ThingsResultCode
ActionResults []*ThingsActionResult
BLENotificationPayload []byte
ErrorReason string
}
// ThingsResultCode type
type ThingsResultCode string
// ThingsResultCode constsnts
const (
ThingsResultCodeSuccess ThingsResultCode = "success"
ThingsResultCodeGattError ThingsResultCode = "gatt_error"
ThingsResultCodeRuntimeError ThingsResultCode = "runtime_error"
)
// ThingsActionResult type
type ThingsActionResult struct {
Type ThingsActionResultType
Data []byte
}
// ThingsActionResultType type
type ThingsActionResultType string
// ThingsActionResultType contants
const (
ThingsActionResultTypeBinary ThingsActionResultType = "binary"
ThingsActionResultTypeVoid ThingsActionResultType = "void"
)
// Things type
type Things struct {
DeviceID string
Type string
Result *ThingsResult
}
// StickerResourceType type
type StickerResourceType string
// StickerResourceType constants
const (
StickerResourceTypeStatic StickerResourceType = "STATIC"
StickerResourceTypeAnimation StickerResourceType = "ANIMATION"
StickerResourceTypeSound StickerResourceType = "SOUND"
StickerResourceTypeAnimationSound StickerResourceType = "ANIMATION_SOUND"
StickerResourceTypePopup StickerResourceType = "POPUP"
StickerResourceTypePopupSound StickerResourceType = "POPUP_SOUND"
StickerResourceTypeNameText StickerResourceType = "NAME_TEXT"
)
// Event type
type Event struct {
ReplyToken string
Type EventType
Mode EventMode
Timestamp time.Time
Source *EventSource
Message Message
Joined *Members
Left *Members
Postback *Postback
Beacon *Beacon
AccountLink *AccountLink
Things *Things
Members []*EventSource
}
type rawEvent struct {
ReplyToken string `json:"replyToken,omitempty"`
Type EventType `json:"type"`
Mode EventMode `json:"mode"`
Timestamp int64 `json:"timestamp"`
Source *EventSource `json:"source"`
Message *rawEventMessage `json:"message,omitempty"`
Postback *Postback `json:"postback,omitempty"`
Beacon *rawBeaconEvent `json:"beacon,omitempty"`
AccountLink *rawAccountLinkEvent `json:"link,omitempty"`
Joined *rawMemberEvent `json:"joined,omitempty"`
Left *rawMemberEvent `json:"left,omitempty"`
Things *rawThingsEvent `json:"things,omitempty"`
}
type rawMemberEvent struct {
Members []*EventSource `json:"members"`
}
type rawEventMessage struct {
ID string `json:"id"`
Type MessageType `json:"type"`
Text string `json:"text,omitempty"`
Duration int `json:"duration,omitempty"`
Title string `json:"title,omitempty"`
Address string `json:"address,omitempty"`
FileName string `json:"fileName,omitempty"`
FileSize int `json:"fileSize,omitempty"`
Latitude float64 `json:"latitude,omitempty"`
Longitude float64 `json:"longitude,omitempty"`
PackageID string `json:"packageId,omitempty"`
StickerID string `json:"stickerId,omitempty"`
StickerResourceType StickerResourceType `json:"stickerResourceType,omitempty"`
}
type rawBeaconEvent struct {
Hwid string `json:"hwid"`
Type BeaconEventType `json:"type"`
DM string `json:"dm,omitempty"`
}
type rawAccountLinkEvent struct {
Result AccountLinkResult `json:"result"`
Nonce string `json:"nonce"`
}
type rawThingsResult struct {
ScenarioID string `json:"scenarioId"`
Revision int `json:"revision"`
StartTime int64 `json:"startTime"`
EndTime int64 `json:"endTime"`
ResultCode ThingsResultCode `json:"resultCode"`
ActionResults []*rawThingsActionResult `json:"actionResults"`
BLENotificationPayload string `json:"bleNotificationPayload,omitempty"`
ErrorReason string `json:"errorReason,omitempty"`
}
type rawThingsActionResult struct {
Type ThingsActionResultType `json:"type,omitempty"`
Data string `json:"data,omitempty"`
}
type rawThingsEvent struct {
DeviceID string `json:"deviceId"`
Type string `json:"type"`
Result *rawThingsResult `json:"result,omitempty"`
}
const (
millisecPerSec = int64(time.Second / time.Millisecond)
nanosecPerMillisec = int64(time.Millisecond / time.Nanosecond)
)
// MarshalJSON method of Event
func (e *Event) MarshalJSON() ([]byte, error) {
raw := rawEvent{
ReplyToken: e.ReplyToken,
Type: e.Type,
Mode: e.Mode,
Timestamp: e.Timestamp.Unix()*millisecPerSec + int64(e.Timestamp.Nanosecond())/int64(time.Millisecond),
Source: e.Source,
Postback: e.Postback,
}
if e.Beacon != nil {
raw.Beacon = &rawBeaconEvent{
Hwid: e.Beacon.Hwid,
Type: e.Beacon.Type,
DM: hex.EncodeToString(e.Beacon.DeviceMessage),
}
}
if e.AccountLink != nil {
raw.AccountLink = &rawAccountLinkEvent{
Result: e.AccountLink.Result,
Nonce: e.AccountLink.Nonce,
}
}
switch e.Type {
case EventTypeMemberJoined:
raw.Joined = &rawMemberEvent{
Members: e.Members,
}
case EventTypeMemberLeft:
raw.Left = &rawMemberEvent{
Members: e.Members,
}
case EventTypeThings:
raw.Things = &rawThingsEvent{
DeviceID: e.Things.DeviceID,
Type: e.Things.Type,
}
if e.Things.Result != nil {
raw.Things.Result = &rawThingsResult{
ScenarioID: e.Things.Result.ScenarioID,
Revision: e.Things.Result.Revision,
StartTime: e.Things.Result.StartTime,
EndTime: e.Things.Result.EndTime,
ResultCode: e.Things.Result.ResultCode,
BLENotificationPayload: string(e.Things.Result.BLENotificationPayload),
ErrorReason: e.Things.Result.ErrorReason,
}
if e.Things.Result.ActionResults != nil {
raw.Things.Result.ActionResults = make([]*rawThingsActionResult, len(e.Things.Result.ActionResults))
}
for i := range e.Things.Result.ActionResults {
raw.Things.Result.ActionResults[i] = &rawThingsActionResult{
Type: e.Things.Result.ActionResults[i].Type,
Data: string(e.Things.Result.ActionResults[i].Data),
}
}
}
}
switch m := e.Message.(type) {
case *TextMessage:
raw.Message = &rawEventMessage{
Type: MessageTypeText,
ID: m.ID,
Text: m.Text,
}
case *ImageMessage:
raw.Message = &rawEventMessage{
Type: MessageTypeImage,
ID: m.ID,
}
case *VideoMessage:
raw.Message = &rawEventMessage{
Type: MessageTypeVideo,
ID: m.ID,
}
case *AudioMessage:
raw.Message = &rawEventMessage{
Type: MessageTypeAudio,
ID: m.ID,
Duration: m.Duration,
}
case *FileMessage:
raw.Message = &rawEventMessage{
Type: MessageTypeFile,
ID: m.ID,
FileName: m.FileName,
FileSize: m.FileSize,
}
case *LocationMessage:
raw.Message = &rawEventMessage{
Type: MessageTypeLocation,
ID: m.ID,
Title: m.Title,
Address: m.Address,
Latitude: m.Latitude,
Longitude: m.Longitude,
}
case *StickerMessage:
raw.Message = &rawEventMessage{
Type: MessageTypeSticker,
ID: m.ID,
PackageID: m.PackageID,
StickerID: m.StickerID,
StickerResourceType: m.StickerResourceType,
}
}
return json.Marshal(&raw)
}
// UnmarshalJSON method of Event
func (e *Event) UnmarshalJSON(body []byte) (err error) {
rawEvent := rawEvent{}
if err = json.Unmarshal(body, &rawEvent); err != nil {
return
}
e.ReplyToken = rawEvent.ReplyToken
e.Type = rawEvent.Type
e.Mode = rawEvent.Mode
e.Timestamp = time.Unix(rawEvent.Timestamp/millisecPerSec, (rawEvent.Timestamp%millisecPerSec)*nanosecPerMillisec).UTC()
e.Source = rawEvent.Source
switch rawEvent.Type {
case EventTypeMessage:
switch rawEvent.Message.Type {
case MessageTypeText:
e.Message = &TextMessage{
ID: rawEvent.Message.ID,
Text: rawEvent.Message.Text,
}
case MessageTypeImage:
e.Message = &ImageMessage{
ID: rawEvent.Message.ID,
}
case MessageTypeVideo:
e.Message = &VideoMessage{
ID: rawEvent.Message.ID,
}
case MessageTypeAudio:
e.Message = &AudioMessage{
ID: rawEvent.Message.ID,
Duration: rawEvent.Message.Duration,
}
case MessageTypeFile:
e.Message = &FileMessage{
ID: rawEvent.Message.ID,
FileName: rawEvent.Message.FileName,
FileSize: rawEvent.Message.FileSize,
}
case MessageTypeLocation:
e.Message = &LocationMessage{
ID: rawEvent.Message.ID,
Title: rawEvent.Message.Title,
Address: rawEvent.Message.Address,
Latitude: rawEvent.Message.Latitude,
Longitude: rawEvent.Message.Longitude,
}
case MessageTypeSticker:
e.Message = &StickerMessage{
ID: rawEvent.Message.ID,
PackageID: rawEvent.Message.PackageID,
StickerID: rawEvent.Message.StickerID,
StickerResourceType: rawEvent.Message.StickerResourceType,
}
}
case EventTypePostback:
e.Postback = rawEvent.Postback
case EventTypeBeacon:
var deviceMessage []byte
deviceMessage, err = hex.DecodeString(rawEvent.Beacon.DM)
if err != nil {
return
}
e.Beacon = &Beacon{
Hwid: rawEvent.Beacon.Hwid,
Type: rawEvent.Beacon.Type,
DeviceMessage: deviceMessage,
}
case EventTypeAccountLink:
e.AccountLink = &AccountLink{
Result: rawEvent.AccountLink.Result,
Nonce: rawEvent.AccountLink.Nonce,
}
case EventTypeMemberJoined:
e.Members = rawEvent.Joined.Members
case EventTypeMemberLeft:
e.Members = rawEvent.Left.Members
case EventTypeThings:
e.Things = &Things{
Type: rawEvent.Things.Type,
DeviceID: rawEvent.Things.DeviceID,
}
if rawEvent.Things.Result != nil {
rawResult := rawEvent.Things.Result
e.Things.Result = &ThingsResult{
ScenarioID: rawResult.ScenarioID,
Revision: rawResult.Revision,
StartTime: rawResult.StartTime,
EndTime: rawResult.EndTime,
ResultCode: rawResult.ResultCode,
ActionResults: make([]*ThingsActionResult, len(rawResult.ActionResults)),
BLENotificationPayload: []byte(rawResult.BLENotificationPayload),
ErrorReason: rawResult.ErrorReason,
}
for i := range rawResult.ActionResults {
e.Things.Result.ActionResults[i] = &ThingsActionResult{
Type: rawResult.ActionResults[i].Type,
Data: []byte(rawResult.ActionResults[i].Data),
}
}
}
}
return
}