forked from EddyTravels/smooch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
377 lines (322 loc) · 11.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
package smooch
import (
"bytes"
"encoding/json"
"time"
)
const (
nsMultiplier = 1e9
)
const (
MessageTypeText = MessageType("text")
MessageTypeImage = MessageType("image")
MessageTypeFile = MessageType("file")
MessageTypeLocation = MessageType("location")
MessageTypeCarousel = MessageType("carousel")
MessageTypeList = MessageType("list")
MessageTypeHSM = MessageType("hsm")
ActionTypePostback = ActionType("postback")
ActionTypeReply = ActionType("reply")
ActionTypeLocationRequest = ActionType("locationRequest")
ActionTypeShare = ActionType("share")
ActionTypeBuy = ActionType("buy")
ActionTypeLink = ActionType("link")
ActionTypeWebview = ActionType("webview")
AuthBasic = "basic_auth"
AuthJWT = "jwt"
SourceTypeWeb = "web"
SourceTypeIOS = "ios"
SourceTypeAndroid = "android"
SourceTypeMessenger = "messenger"
SourceTypeViber = "viber"
SourceTypeTelegram = "telegram"
SourceTypeWeChat = "wechat"
SourceTypeLine = "line"
SourceTypeTwilio = "twilio"
SourceTypeApi = "api"
SourceTypeWhatsApp = "whatsapp"
RoleAppUser = Role("appUser")
RoleAppMaker = Role("appMaker")
TriggerMessageAppUser = "message:appUser"
TriggerMessageAppMaker = "message:appMaker"
TriggerMessageDeliveryFailure = "message:delivery:failure"
TriggerMessageDeliveryChannel = "message:delivery:channel"
TriggerMessageDeliveryUser = "message:delivery:user"
ConfirmationTypeImmediate = "immediate"
ConfirmationTypeUserActivity = "userActivity"
ConfirmationTypePrompt = "prompt"
ImageRatioHorizontal = ImageRatio("horizontal")
ImageRatioSquare = ImageRatio("square")
SizeCompact = Size("compact")
SizeLarge = Size("large")
)
type Role string
type MessageType string
type ActionType string
type Size string
type Payload struct {
Trigger string `json:"trigger,omitempty"`
App Application `json:"app,omitempty"`
Messages []*Message `json:"messages,omitempty"`
AppUser AppUser `json:"appUser,omitempty"`
Conversation Conversation `json:"conversation,omitempty"`
Destination *SourceDestination `json:"destination,omitempty"`
IsFinalEvent bool `json:"isFinalEvent"`
Message *TruncatedMessage `json:"message,omitempty"`
Error *Error `json:"error,omitempty"`
Version string `json:"version,omitempty"`
}
type TruncatedMessage struct {
ID string `json:"_id"`
}
type Error struct {
Code string `json:"code"`
UnderlyingError map[string]interface{} `json:"underlyingError"`
Message string `json:"message"`
}
type Application struct {
ID string `json:"_id,omitempty"`
}
type SourceDestination struct {
Type string `json:"type,omitempty"`
Id string `json:"id,omitempty"`
IntegrationId string `json:"integrationId,omitempty"`
OriginalMessageId string `json:"originalMessageId,omitempty"`
}
type AppUser struct {
ID string `json:"_id,omitempty"`
UserID string `json:"userId,omitempty"`
Properties map[string]interface{} `json:"properties,omitempty"`
SignedUpAt time.Time `json:"signedUpAt,omitempty"`
Clients []*AppUserClient `json:"clients,omitempty"`
PendingClients []*AppUserClient `json:"pendingClients,omitempty"`
ConversationStarted bool `json:"conversationStarted"`
Email string `json:"email,omitempty"`
GivenName string `json:"givenName,omitempty"`
Surname string `json:"surname,omitempty"`
HasPaymentInfo bool `json:"hasPaymentInfo,omitmepty"`
}
type AppUserClient struct {
ID string `json:"_id,omitempty"`
Platform string `json:"platform,omitempty"`
IntegrationId string `json:"integrationId,omitempty"`
Primary bool `json:"primary"`
Active bool `json:"active"`
DeviceID string `json:"deviceId,omitempty"`
DisplayName string `json:"displayName,omitempty"`
AvatarURL string `json:"avatarUrl,omitempty"`
Info map[string]interface{} `json:"info,omitempty"`
Raw map[string]interface{} `json:"raw,omitempty"`
AppVersion string `json:"appVersion,omitempty"`
LastSeen time.Time `json:"lastSeen,omitempty"`
LinkedAt time.Time `json:"linkedAt,omitempty"`
Blocked bool `json:"blocked"`
}
type Conversation struct {
ID string `json:"_id"`
UnreadCount int `json:"unreadCount,omitempty"`
}
type Action struct {
ID string `json:"_id,omitempty"`
Type ActionType `json:"type,omitempty"`
Text string `json:"text,omitempty"`
Default bool `json:"default,omitempty"`
Payload string `json:"payload,omitempty"`
URI string `json:"uri,omitempty"`
Amount int `json:"amount,omitempty"`
Currency string `json:"currency,omitempty"`
State string `json:"state,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
type Item struct {
ID string `json:"_id,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Size Size `json:"size,omitempty"`
MediaURL string `json:"mediaUrl,omitempty"`
MediaType string `json:"mediaType,omitempty"`
Actions []*Action `json:"actions"`
}
type Message struct {
ID string `json:"_id,omitempty"`
Type MessageType `json:"type"`
Text string `json:"text,omitempty"`
Role Role `json:"role"`
AuthorID string `json:"authorId,omitempty"`
Name string `json:"name,omitempty"`
Received time.Time `json:"received,omitempty"`
Source *SourceDestination `json:"source,omitempty"`
MediaURL string `json:"mediaUrl,omitempty"`
Actions []*Action `json:"actions,omitempty"`
Items []*Item `json:"items,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
DisplaySettings *DisplaySettings `json:"displaySettings,omitempty"`
}
func (m *Message) UnmarshalJSON(data []byte) error {
type Alias Message
aux := &struct {
Received float64 `json:"received"`
*Alias
}{
Alias: (*Alias)(m),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
seconds := int64(aux.Received)
ns := (int64(aux.Received*1000) - seconds*1000) * nsMultiplier
m.Received = time.Unix(seconds, ns)
return nil
}
func (m *Message) MarshalJSON() ([]byte, error) {
type Alias Message
aux := &struct {
Received float64 `json:"received"`
*Alias
}{
Alias: (*Alias)(m),
}
aux.Received = float64(m.Received.UnixNano()) / nsMultiplier
return json.Marshal(aux)
}
// HsmLanguage defines hsm language payload
type HsmLanguage struct {
Policy string `json:"policy" mapstructure:"policy"`
Code string `json:"code" mapstructure:"code"`
}
// HsmLocalizableParams defines hsm localizable params data
type HsmLocalizableParams struct {
Default interface{} `json:"default" mapstructure:"default"`
}
// HsmPayload defines payload for hsm
type HsmPayload struct {
Namespace string `json:"namespace" mapstructure:"namespace"`
ElementName string `json:"element_name" mapstructure:"element_name"`
Language HsmLanguage `json:"language" mapstructure:"language"`
LocalizableParams []HsmLocalizableParams `json:"localizable_params" mapstructure:"localizable_params"`
}
// HsmMessageBody defines property for HSM message
type HsmMessageBody struct {
Type MessageType `json:"type" mapstructure:"type"`
Hsm HsmPayload `json:"hsm" mapstructure:"hsm"`
}
// HsmMessage defines struct payload for Whatsapp HSM message
type HsmMessage struct {
Role Role `json:"role"`
MessageSchema string `json:"messageSchema"`
Message HsmMessageBody `json:"message"`
Received time.Time `json:"received,omitempty"`
}
// UnmarshalJSON will unmarshall whatsapp HSM message
func (hm *HsmMessage) UnmarshalJSON(data []byte) error {
type Alias HsmMessage
aux := &struct {
Received float64 `json:"received"`
*Alias
}{
Alias: (*Alias)(hm),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
seconds := int64(aux.Received)
ns := (int64(aux.Received*1000) - seconds*1000) * nsMultiplier
hm.Received = time.Unix(seconds, ns)
return nil
}
// MarshalJSON will marshall whatsapp HSM message
func (hm *HsmMessage) MarshalJSON() ([]byte, error) {
type Alias HsmMessage
aux := &struct {
Received float64 `json:"received"`
*Alias
}{
Alias: (*Alias)(hm),
}
aux.Received = float64(hm.Received.UnixNano()) / nsMultiplier
return json.Marshal(aux)
}
type MenuPayload struct {
Menu Menu `json:"menu"`
}
type Menu struct {
Items []*MenuItem `json:"items"`
}
type MenuItem struct {
ID string `json:"_id,omitempty"`
Type string `json:"type,omitempty"`
Text string `json:"text,omitempty"`
URI string `json:"uri,omitempty"`
}
type ImageRatio string
type DisplaySettings struct {
ImageAspectRatio ImageRatio `json:"imageAspectRatio,omitempty"`
}
type ErrorPayload struct {
Details ErrorDetails `json:"error"`
}
type ErrorDetails struct {
Code string `json:"code"`
Description string `json:"description"`
}
type ResponsePayload struct {
Message *Message `json:"message,omitempty"`
ExtraMessages []*Message `json:"extraMessages,omitempty"`
Conversation *Conversation `json:"conversation,omitempty"`
}
type GetAppUserResponse struct {
AppUser *AppUser `json:"appUser,omitempty"`
}
// PreCreateAppUserPayload defines payload for pre-create app user request
type PreCreateAppUserPayload struct {
UserID string `json:"userId"`
Surname string `json:"surname"`
GivenName string `json:"givenName"`
}
// PreCreateAppUserResponse defines response for pre-create use request
type PreCreateAppUserResponse struct {
AppUser *AppUser `json:"appUser,omitempty"`
}
// LinkAppConfirmationData defines
type LinkAppConfirmationData struct {
Type string `json:"type"`
}
// LinkAppUserToChannelPayload will link app user to specified channel
type LinkAppUserToChannelPayload struct {
Type string `json:"type"`
Confirmation LinkAppConfirmationData `json:"confirmation"`
PhoneNumber string `json:"phoneNumber"`
}
// LinkAppUserToChannelResponse defines reponse for link app user to channel request
type LinkAppUserToChannelResponse struct {
AppUser *AppUser `json:"appUser,omitempty"`
}
type AttachmentUpload struct {
MIMEType string
Access string
// optionals
For string
AppUserID string
UserID string
}
func NewAttachmentUpload(mime string) AttachmentUpload {
return AttachmentUpload{
MIMEType: mime,
Access: "public",
}
}
type Attachment struct {
MediaURL string `json:"mediaUrl"`
MediaType string `json:"mediaType,omitempty"`
}
type BytesFileReader struct {
*bytes.Reader
Filename string
}
func NewBytesFileReader(filename string, b []byte) *BytesFileReader {
r := bytes.NewReader(b)
return &BytesFileReader{
Reader: r,
Filename: filename,
}
}