forked from retailcrm/mg-transport-api-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
434 lines (379 loc) · 13.6 KB
/
types.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
package v1
import (
"errors"
"net/http"
"time"
)
//noinspection ALL
const (
// ChannelFeatureNone channel can not implement feature
ChannelFeatureNone string = "none"
// ChannelFeatureReceive channel implement feature on receive
ChannelFeatureReceive string = "receive"
// ChannelFeatureSend channel implement feature on send
ChannelFeatureSend string = "send"
// ChannelFeatureBoth channel implement feature on both directions
ChannelFeatureBoth string = "both"
// MsgTypeText text message
MsgTypeText string = "text"
// MsgTypeSystem system message
MsgTypeSystem string = "system"
// MsgTypeCommand command (for bots)
MsgTypeCommand string = "command"
// MsgTypeOrder order card
MsgTypeOrder string = "order"
// MsgTypeProduct product card
MsgTypeProduct string = "product"
// MsgTypeFile file card
MsgTypeFile string = "file"
// MsgTypeImage image card
MsgTypeImage string = "image"
// MsgTypeAudio audio
MsgTypeAudio string = "audio"
// MsgOrderStatusCodeNew order status group new
MsgOrderStatusCodeNew = "new"
// MsgOrderStatusCodeApproval order status group approval
MsgOrderStatusCodeApproval = "approval"
// MsgOrderStatusCodeAssembling order status group assembling
MsgOrderStatusCodeAssembling = "assembling"
// MsgOrderStatusCodeDelivery order status group delivery
MsgOrderStatusCodeDelivery = "delivery"
// MsgOrderStatusCodeComplete order status group complete
MsgOrderStatusCodeComplete = "complete"
// MsgOrderStatusCodeCancel order status group cancel
MsgOrderStatusCodeCancel = "cancel"
FileSizeLimit = 20 * 1024 * 1024
)
const (
// OriginatorCustomer means message was created by customer
OriginatorCustomer Originator = iota + 1
// OriginatorChannel means message was created by channel, for example via messenger mobile application
OriginatorChannel
)
// MgClient type
type MgClient struct {
URL string `json:"url"`
Token string `json:"token"`
Debug bool `json:"debug"`
httpClient *http.Client `json:"-"`
}
// Channel type
type Channel struct {
ID uint64 `json:"id,omitempty"`
ExternalID string `json:"external_id,omitempty"`
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
AvatarUrl string `json:"avatar_url,omitempty"`
Settings ChannelSettings `json:"settings,omitempty,brackets"`
}
// ChannelSettings struct
type ChannelSettings struct {
SpamAllowed bool `json:"spam_allowed"`
Status Status `json:"status"`
Text ChannelSettingsText `json:"text"`
Product Product `json:"product"`
Order Order `json:"order"`
File ChannelSettingsFilesBase `json:"file"`
Image ChannelSettingsFilesBase `json:"image"`
}
// Product type
type Product struct {
Creating string `json:"creating,omitempty"`
Editing string `json:"editing,omitempty"`
Deleting string `json:"deleting,omitempty"`
}
// Order type
type Order struct {
Creating string `json:"creating,omitempty"`
Editing string `json:"editing,omitempty"`
Deleting string `json:"deleting,omitempty"`
}
// Status struct
type Status struct {
Delivered string `json:"delivered,omitempty"`
Read string `json:"read,omitempty"`
}
// ChannelSettingsText struct
type ChannelSettingsText struct {
Creating string `json:"creating,omitempty"`
Editing string `json:"editing,omitempty"`
Quoting string `json:"quoting,omitempty"`
Deleting string `json:"deleting,omitempty"`
MaxCharsCount uint16 `json:"max_chars_count,omitempty"`
}
// ChannelSettingsFilesBase struct
type ChannelSettingsFilesBase struct {
Creating string `json:"creating,omitempty"`
Editing string `json:"editing,omitempty"`
Quoting string `json:"quoting,omitempty"`
Deleting string `json:"deleting,omitempty"`
Max uint64 `json:"max_items_count,omitempty"`
CommentAttribute string `json:"comment_attribute,omitempty"`
CommentMaxCharsCount int `json:"comment_max_chars_count,omitempty"`
}
// FullFileResponse uploaded file data
type FullFileResponse struct {
ID string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Size int `json:"size,omitempty"`
Url string `json:"url,omitempty"`
}
// UploadFileResponse uploaded file data
type UploadFileResponse struct {
ID string `json:"id"`
Hash string `json:"hash"`
Type string `json:"type"`
Meta FileMeta `json:"meta"`
MimeType string `json:"mime_type"`
Size int `json:"size"`
Url *string `json:"source_url"`
CreatedAt time.Time `json:"created_at"`
}
// FileMeta file metadata
type FileMeta struct {
Width *int `json:"width,omitempty"`
Height *int `json:"height,omitempty"`
}
// UploadFileByUrlRequest file url to upload
type UploadFileByUrlRequest struct {
Url string `json:"url"`
}
// ActivateResponse channel activation response
type ActivateResponse struct {
ChannelID uint64 `json:"id"`
ExternalID string `json:"external_id"`
ActivatedAt time.Time `json:"activated_at"`
}
// UpdateResponse channel update response
type UpdateResponse struct {
ChannelID uint64 `json:"id"`
ExternalID string `json:"external_id"`
UpdatedAt time.Time `json:"updated_at"`
}
// DeleteResponse channel deactivation response
type DeleteResponse struct {
ChannelID uint64 `json:"id"`
DeactivatedAt time.Time `json:"deactivated_at"`
}
// ChannelListItem response struct
type ChannelListItem struct {
ID uint64 `json:"id"`
ExternalID string `json:"external_id"`
Type string `json:"type"`
Name *string `json:"name"`
Settings ChannelSettings `json:"settings"`
CreatedAt string `json:"created_at"`
UpdatedAt *string `json:"updated_at"`
ActivatedAt string `json:"activated_at"`
DeactivatedAt *string `json:"deactivated_at"`
IsActive bool `json:"is_active"`
}
// Channels request type
type Channels struct {
ID int `url:"id,omitempty" json:"id,omitempty"`
Types []string `url:"types,omitempty" json:"types,omitempty"`
Active bool `url:"active,omitempty" json:"active,omitempty"`
Since time.Time `url:"since,omitempty" json:"since,omitempty"`
Until time.Time `url:"until,omitempty" json:"until,omitempty"`
TransportID uint64 `url:"transport_id,omitempty" json:"transport_id,omitempty"`
Sort string `url:"sort,omitempty" json:"sort,omitempty"`
Limit int `url:"limit,omitempty" json:"limit,omitempty"`
}
// Customer struct
type Customer struct {
ExternalID string `json:"external_id"`
Nickname string `json:"nickname"`
Firstname string `json:"first_name,omitempty"`
Lastname string `json:"last_name,omitempty"`
Avatar string `json:"avatar,omitempty"`
ProfileURL string `json:"profile_url,omitempty"`
Country string `json:"country,omitempty"`
Language string `json:"language,omitempty"`
Phone string `json:"phone,omitempty"`
Email string `json:"email,omitempty"`
}
// Message struct
type Message struct {
ExternalID string `json:"external_id"`
Type string `json:"type,omitempty"`
Text string `json:"text,omitempty"`
Note string `json:"note,omitempty"`
Items []Item `json:"items,omitempty"`
}
// SendMessage struct
type SendMessage struct {
Message
SentAt time.Time `json:"sent_at,omitempty"`
}
// EditMessageRequest type
type EditMessageRequest struct {
Message EditMessageRequestMessage `json:"message"`
Channel uint64 `json:"channel"`
}
// EditMessageRequestMessage type
type EditMessageRequestMessage struct {
ExternalID string `json:"external_id"`
Text string `json:"text"`
EditedAt int64 `json:"edited_at"`
}
// SendData struct
type SendData struct {
Message Message `json:"message"`
Originator Originator `json:"originator,omitempty"`
Customer Customer `json:"customer"`
Channel uint64 `json:"channel"`
ExternalChatID string `json:"external_chat_id"`
Quote *SendMessageRequestQuote `json:"quote,omitempty"`
}
// Item struct
type Item struct {
ID string `json:"id"`
Caption string `json:"caption"`
}
// SendMessageRequestQuote type
type SendMessageRequestQuote struct {
ExternalID string `json:"external_id"`
}
// MarkMessageReadResponse type
type MarkMessageReadResponse struct{}
// MarkMessageReadRequest type
type MarkMessageReadRequest struct {
Message MarkMessageReadRequestMessage `json:"message"`
ChannelID uint64 `json:"channel_id"`
}
// MarkMessageReadRequestMessage type
type MarkMessageReadRequestMessage struct {
ExternalID string `json:"external_id"`
}
// DeleteData struct
type DeleteData struct {
Message Message `json:"message"`
Channel uint64 `json:"channel"`
}
// MessagesResponse message event response
type MessagesResponse struct {
MessageID int `json:"message_id,omitempty"`
Time time.Time `json:"time,omitempty"`
}
// WebhookRequest type
type WebhookRequest struct {
Type string `json:"type"`
Meta TransportRequestMeta `json:"meta"`
Data WebhookData `json:"data"`
}
// WebhookData request data
type WebhookData struct {
ExternalUserID string `json:"external_user_id"`
ExternalMessageID string `json:"external_message_id,omitempty"`
ExternalChatID string `json:"external_chat_id"`
ChannelID uint64 `json:"channel_id"`
Content string `json:"content"`
QuoteExternalID string `json:"quote_external_id,omitempty"`
QuoteContent string `json:"quote_content,omitempty"`
Type string `json:"type"`
User *MessageDataUser `json:"user,omitempty"`
Bot *MessageDataBot `json:"bot,omitempty"`
Product *MessageDataProduct `json:"product,omitempty"`
Order *MessageDataOrder `json:"order,omitempty"`
Items *[]FileItem `json:"items,omitempty"`
}
// FileItem struct
type FileItem struct {
ID string `json:"id"`
Size int `json:"size"`
Caption string `json:"caption"`
Height *int `json:"height,omitempty"`
Width *int `json:"width,omitempty"`
}
// MessageDataUser user data from webhook
type MessageDataUser struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Avatar string `json:"avatar"`
}
// MessageDataBot bot data from webhook
type MessageDataBot struct {
Name string `json:"name"`
Avatar string `json:"avatar"`
}
// MessageDataProduct product data from webhook
type MessageDataProduct struct {
ID uint64 `json:"id"`
Name string `json:"name"`
Article string `json:"article,omitempty"`
Url string `json:"url,omitempty"`
Img string `json:"img,omitempty"`
Cost *MessageDataOrderCost `json:"cost,omitempty"`
Quantity *MessageDataOrderQuantity `json:"quantity,omitempty"`
}
// MessageDataOrder order data from webhook
type MessageDataOrder struct {
Number string `json:"number"`
Url string `json:"url,omitempty"`
Date string `json:"date,omitempty"`
Cost *MessageDataOrderCost `json:"cost,omitempty"`
Discount *MessageDataOrderCost `json:"discount,omitempty"`
Status *MessageDataOrderStatus `json:"status,omitempty"`
Delivery *MessageDataOrderDelivery `json:"delivery"`
Payments []MessageDataOrderPayment `json:"payments"`
Items []MessageDataOrderItem `json:"items,omitempty"`
}
// MessageDataOrderStatus type
type MessageDataOrderStatus struct {
Code string `json:"code,omitempty"`
Name string `json:"name,omitempty"`
}
// MessageDataOrderItem type
type MessageDataOrderItem struct {
Name string `json:"name,omitempty"`
Url string `json:"url,omitempty"`
Img string `json:"img,omitempty"`
Quantity *MessageDataOrderQuantity `json:"quantity,omitempty"`
Price *MessageDataOrderCost `json:"price,omitempty"`
}
// MessageDataOrderCost type
type MessageDataOrderCost struct {
Value float32 `json:"value,omitempty"`
Currency string `json:"currency"`
}
// MessageDataOrderQuantity type
type MessageDataOrderQuantity struct {
Value float32 `json:"value"`
Unit string `json:"unit"`
}
// MessageDataOrderPayment type
type MessageDataOrderPayment struct {
Name string `json:"name"`
Status *MessageDataOrderPaymentStatus `json:"status"`
Amount *MessageDataOrderCost `json:"amount"`
}
// MessageDataOrderPaymentStatus type
type MessageDataOrderPaymentStatus struct {
Name string `json:"name"`
Payed bool `json:"payed"`
}
// MessageDataOrderDelivery type
type MessageDataOrderDelivery struct {
Name string `json:"name"`
Price *MessageDataOrderCost `json:"price"`
Address string `json:"address"`
Comment string `json:"comment,omitempty"`
}
// TransportRequestMeta request metadata
type TransportRequestMeta struct {
ID uint64 `json:"id"`
Timestamp int64 `json:"timestamp"`
}
var ErrInvalidOriginator = errors.New("invalid originator")
// Originator of message
type Originator byte
// MarshalText marshals originator to text
func (o Originator) MarshalText() ([]byte, error) {
switch o {
case OriginatorCustomer:
return []byte("customer"), nil
case OriginatorChannel:
return []byte("channel"), nil
}
return nil, ErrInvalidOriginator
}