-
Notifications
You must be signed in to change notification settings - Fork 6
/
objects.go
457 lines (406 loc) · 12.8 KB
/
objects.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
// Package objects defines common LiveChat structures.
//
// General common LiveChat structures documentation is available here:
// https://developers.livechatinc.com/docs/messaging/customer-chat-api/#other-common-structures
package objects
import (
"encoding/json"
"fmt"
"strconv"
"time"
)
// Properties represents LiveChat properties in form of property_namespace -> property -> value.
type Properties map[string]map[string]interface{}
// User represents base of both Customer and Agent
//
// To get speficic user type's structure, call Agent() or Customer() (based on Type value).
type User struct {
ID string `json:"id"`
Type string `json:"type"`
Name string `json:"name"`
Avatar string `json:"avatar"`
Email string `json:"email"`
Present bool `json:"present"`
LastSeen Time `json:"last_seen_timestamp"`
userSpecific
}
type userSpecific struct {
RoutingStatus json.RawMessage `json:"routing_status"`
LastVisit json.RawMessage `json:"last_visit"`
Statistics json.RawMessage `json:"statistics"`
AgentLastEventCreatedAt json.RawMessage `json:"agent_last_event_created_at"`
CustomerLastEventCreatedAt json.RawMessage `json:"customer_last_event_created_at"`
}
// Agent function converts User object to Agent object if User's Type is "agent".
// If Type is different or User is malformed, then it returns nil.
func (u *User) Agent() *Agent {
if u.Type != "agent" {
return nil
}
var a Agent
a.User = u
if err := json.Unmarshal(u.RoutingStatus, &a.RoutingStatus); err != nil {
return nil
}
return &a
}
// Customer function converts User object to Customer object if User's Type is "customer".
// If Type is different or User is malformed, then it returns nil.
func (u *User) Customer() *Customer {
if u.Type != "customer" {
return nil
}
var c Customer
c.User = u
if err := json.Unmarshal(u.LastVisit, &c.LastVisit); err != nil {
return nil
}
if err := json.Unmarshal(u.Statistics, &c.Statistics); err != nil {
return nil
}
if err := json.Unmarshal(u.AgentLastEventCreatedAt, &c.AgentLastEventCreatedAt); err != nil {
return nil
}
if err := json.Unmarshal(u.CustomerLastEventCreatedAt, &c.CustomerLastEventCreatedAt); err != nil {
return nil
}
return &c
}
// The Time type is a helper type to convert string time into golang time representation.
type Time struct {
time.Time
}
// MarshalJSON implements json.Marshaler interface for Time.
func (t Time) MarshalJSON() ([]byte, error) {
return []byte(strconv.FormatInt(t.Unix(), 10)), nil
}
// UnmarshalJSON implements json.Unmarshaler interface for Time.
func (t *Time) UnmarshalJSON(s []byte) (err error) {
q, err := strconv.ParseInt(string(s), 10, 64)
if err != nil {
return err
}
*t = Time{Time: time.Unix(q, 0)}
return
}
// Visit contains information about particular customer's visit.
type Visit struct {
IP string `json:"ip"`
UserAgent string `json:"user_agent"`
Geolocation struct {
Country string `json:"country"`
CountryCode string `json:"country_code"`
Region string `json:"region"`
City string `json:"city"`
Timezone string `json:"timezone"`
} `json:"geolocation"`
StartedAt time.Time `json:"started_at"`
LastPages []struct {
OpenedAt time.Time `json:"opened_at"`
URL string `json:"url"`
Title string `json:"title"`
} `json:"last_pages"`
}
// Chat represents LiveChat chat.
type Chat struct {
ID string `json:"id,omitempty"`
Properties Properties `json:"properties,omitempty"`
Access Access `json:"access,omitempty"`
Threads []Thread `json:"threads,omitempty"`
Agents map[string]*Agent
Customers map[string]*Customer
}
// Users function returns combined list of Chat's Agents and Customers.
func (c *Chat) Users() []*User {
u := make([]*User, 0, len(c.Agents)+len(c.Customers))
for _, a := range c.Agents {
u = append(u, a.User)
}
for _, cu := range c.Customers {
u = append(u, cu.User)
}
return u
}
// UnmarshalJSON implements json.Unmarshaler interface for Chat.
func (c *Chat) UnmarshalJSON(data []byte) error {
type ChatAlias Chat
var cs struct {
*ChatAlias
Users []json.RawMessage `json:"users"`
}
if err := json.Unmarshal(data, &cs); err != nil {
return err
}
var t struct {
Type string `json:"type"`
}
*c = (Chat)(*cs.ChatAlias)
c.Agents = make(map[string]*Agent)
c.Customers = make(map[string]*Customer)
for _, u := range cs.Users {
if err := json.Unmarshal(u, &t); err != nil {
return err
}
switch t.Type {
case "agent":
var a Agent
if err := json.Unmarshal(u, &a); err != nil {
return err
}
c.Agents[a.ID] = &a
case "customer":
var cu Customer
if err := json.Unmarshal(u, &cu); err != nil {
return err
}
c.Customers[cu.ID] = &cu
}
}
return nil
}
// Thread represents LiveChat chat thread
type Thread struct {
ID string `json:"id"`
Timestamp Time `json:"timestamp"`
Active bool `json:"active"`
UserIDs []string `json:"user_ids"`
RestrictedAccess bool `json:"restricted_access"`
Order int `json:"order"`
Properties Properties `json:"properties"`
Access Access `json:"access"`
Events []*Event `json:"events"`
}
// Access represents LiveChat chat and thread access
type Access struct {
GroupIDs []int `json:"group_ids"`
}
// Agent represents LiveChat agent.
type Agent struct {
*User
RoutingStatus string `json:"routing_status"`
}
// Customer represents LiveChat customer.
type Customer struct {
*User
LastVisit Visit `json:"last_visit"`
Statistics struct {
VisitsCount int `json:"visits_count"`
ThreadsCount int `json:"threads_count"`
ChatsCount int `json:"chats_count"`
PageViewsCount int `json:"page_views_count"`
GreetingsShownCount int `json:"greetings_shown_count"`
GreetingsAcceptedCount int `json:"greetings_accepted_count"`
} `json:"statistics"`
AgentLastEventCreatedAt time.Time `json:"agent_last_event_created_at"`
CustomerLastEventCreatedAt time.Time `json:"customer_last_event_created_at"`
CreatedAt time.Time `json:"created_at"`
Fields map[string]string `json:"fields"`
}
// ThreadSummary represents a short summary of a thread.
type ThreadSummary struct {
ID string `json:"id"`
Order int32 `json:"order"`
TotalEvents uint `json:"total_events"`
}
// ChatSummary represents a short summary of a chat
type ChatSummary struct {
ID string `json:"id"`
LastEventPerType interface{} `json:"last_event_per_type,omitempty"`
Users []interface{} `json:"users"`
LastThreadSummary *ThreadSummary `json:"last_thread_summary,omitempty"`
Properties Properties `json:"properties,omitempty"`
Access interface{} `json:"access,omitempty"`
Order uint64 `json:"order,omitempty"`
IsFollowed bool `json:"is_followed"`
}
// InitialChat represents initial chat used in StartChat or ActivateChat.
type InitialChat struct {
ID string `json:"id"`
Access *Access `json:"access,omitempty"`
Properties Properties `json:"properties,omitempty"`
Thread *InitialThread `json:"thread,omitempty"`
}
// Validate checks if there are no unsupported event types in InitialChat Thread
func (chat *InitialChat) Validate() error {
if chat.Thread != nil {
for _, e := range chat.Thread.Events {
if err := ValidateEvent(e); err != nil {
return err
}
}
}
return nil
}
// ValidateEvent checks if given interface resolves into supported event type
func ValidateEvent(e interface{}) error {
switch v := e.(type) {
case *File:
case *Event:
case *Message:
case *SystemMessage:
case File:
case Event:
case Message:
case SystemMessage:
default:
return fmt.Errorf("event type %T not supported", v)
}
return nil
}
// InitialThread represents initial chat thread used in StartChat or ActivateChat.
type InitialThread struct {
Events []interface{} `json:"events,omitempty"`
Properties Properties `json:"properties,omitempty"`
}
type eventSpecific struct {
Text json.RawMessage `json:"text"`
Fields json.RawMessage `json:"fields"`
ContentType json.RawMessage `json:"content_type"`
URL json.RawMessage `json:"url"`
Width json.RawMessage `json:"width"`
Height json.RawMessage `json:"height"`
Name json.RawMessage `json:"name"`
TemplateID json.RawMessage `json:"template_id"`
Elements json.RawMessage `json:"elements"`
}
// Event represents base of all LiveChat chat events.
//
// To get speficic event type's structure, call appropriate function based on Event's Type.
type Event struct {
ID string `json:"id,omitempty"`
CustomID string `json:"custom_id,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
AuthorID string `json:"author_id"`
Properties Properties `json:"properties,omitempty"`
Recipients string `json:"recipients,omitempty"`
Type string `json:"type,omitempty"`
eventSpecific
}
// FilledForm represents LiveChat filled form event.
type FilledForm struct {
Fields []struct {
Label string `json:"label"`
Type string `json:"type"`
Value string `json:"value"`
} `json:"fields"`
Event
}
// FilledForm function converts Event object to FilledForm object if Event's Type is "filled_form".
// If Type is different or Event is malformed, then it returns nil.
func (e *Event) FilledForm() *FilledForm {
if e.Type != "filled_form" {
return nil
}
var f FilledForm
f.Event = *e
if err := json.Unmarshal(e.Fields, &f.Fields); err != nil {
return nil
}
return &f
}
// Message represents LiveChat message event.
type Message struct {
Event
Text string `json:"text,omitempty"`
}
// Message function converts Event object to Message object if Event's Type is "message".
// If Type is different or Event is malformed, then it returns nil.
func (e *Event) Message() *Message {
if e.Type != "message" {
return nil
}
var m Message
m.Event = *e
if err := json.Unmarshal(e.Text, &m.Text); err != nil {
return nil
}
return &m
}
// SystemMessage represents LiveChat system message event.
type SystemMessage struct {
Event
Type string `json:"system_message_type,omitempty"`
Text string `json:"text,omitempty"`
}
// File represents LiveChat file event
type File struct {
Event
ContentType string `json:"content_type"`
URL string `json:"url"`
Width int `json:"width,omitempty"`
Height int `json:"height,omitempty"`
Name string `json:"name"`
}
// File function converts Event object to File object if Event's Type is "file".
// If Type is different or Event is malformed, then it returns nil.
func (e *Event) File() *File {
if e.Type != "file" {
return nil
}
var f File
f.Event = *e
if err := json.Unmarshal(e.ContentType, &f.ContentType); err != nil {
return nil
}
if err := json.Unmarshal(e.URL, &f.URL); err != nil {
return nil
}
if err := json.Unmarshal(e.Width, &f.Width); err != nil {
return nil
}
if err := json.Unmarshal(e.Height, &f.Height); err != nil {
return nil
}
if err := json.Unmarshal(e.Name, &f.Name); err != nil {
return nil
}
return &f
}
// RichMessage represents LiveChat rich message event
type RichMessage struct {
Event
TemplateID string `json:"template_id"`
Elements []RichMessageElement `json:"elements"`
}
// RichMessageElement represents element of LiveChat rich message
type RichMessageElement struct {
Buttons []RichMessageButton `json:"buttons"`
Title string `json:"title"`
Subtitle string `json:"subtitle"`
Image RichMessageImage `json:"image"`
}
// RichMessageButton represents button in LiveChat rich message
type RichMessageButton struct {
Text string `json:"text"`
Type string `json:"type"`
UserIds []string `json:"user_ids"`
Value string `json:"value"`
PostbackID string `json:"postback_id"`
// Allowed values: compact, full, tall
WebviewHeight string `json:"webview_height"`
}
// RichMessageImage represents image in LiveChat rich message
type RichMessageImage struct {
Name string `json:"name"`
URL string `json:"url"`
ContentType string `json:"content_type"`
Size int `json:"size"`
Width int `json:"width,omitempty"`
Height int `json:"height,omitempty"`
}
// RichMessage function converts Event object to RichMessage object if Event's Type is "rich_message".
// If Type is different or Event is malformed, then it returns nil.
func (e *Event) RichMessage() *RichMessage {
if e.Type != "rich_message" {
return nil
}
var rm RichMessage
rm.Event = *e
if err := json.Unmarshal(e.TemplateID, &rm.TemplateID); err != nil {
return nil
}
if err := json.Unmarshal(e.Elements, &rm.Elements); err != nil {
return nil
}
return &rm
}