/
mixin.ts
618 lines (536 loc) · 13.6 KB
/
mixin.ts
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/* eslint-disable camelcase */
import { MESSENGER } from '../constant';
import type { PSIDTarget, UserRefTarget } from '../types';
/**
* @category Event Mixin
*/
export interface EventBase {
readonly platform: typeof MESSENGER;
/** Indicate whether the event is sent to a standby channel. */
readonly isStandby: boolean;
/** Indicate whether the event is an echo. */
readonly isEcho: boolean;
/** The user that triggered the webhook event. */
readonly sender: PSIDTarget | UserRefTarget;
readonly timestamp: number;
readonly [Symbol.toStringTag]: 'MessengerEvent';
}
export const EventBase: EventBase = {
platform: MESSENGER,
isStandby: false,
isEcho: false,
[Symbol.toStringTag]: 'MessengerEvent',
get timestamp(): number {
return this.payload.timestamp;
},
get sender() {
return this.payload.sender;
},
};
/**
* @category Event Mixin
*/
export interface Message {
/** Message ID. */
readonly messageId: string;
/** Reference to the message id (mid) that this message is replying to. */
readonly replyTo: undefined | string;
/**
* Reference to the product id when the user sends a message from Facebook
* Shops product detail page.
*/
readonly referralProductId: undefined | string;
}
export const Message: Message = {
get messageId() {
return this.payload.message.mid;
},
get replyTo() {
return this.payload.message.reply_to?.mid;
},
get referralProductId() {
return this.payload.message.referral?.product.id;
},
};
/**
* @category Event Mixin
*/
export interface Text {
/** Text of message. */
readonly text: string;
}
export const Text: Text = {
get text() {
return this.payload.message.text;
},
};
/**
* @category Event Mixin
*/
export interface Fallback {
/** The fallback payload. */
readonly fallback: undefined | { title: string; url: string };
}
export const Fallback: Fallback = {
get fallback() {
return this.payload.message.attachments?.[0];
},
};
/**
* @category Event Mixin
*/
export interface Media {
/** URL of the attachment type. */
readonly url: string;
}
export const Media: Media = {
get url() {
return this.payload.message.attachments[0].payload.url;
},
};
/**
* @category Event Mixin
*/
export interface Sticker {
/** Persistent id of the sticker if a sticker is sent. */
readonly stickerId: undefined | number;
}
export const Sticker: Sticker = {
get stickerId() {
return this.payload.message.attachments[0].payload.sticker_id;
},
};
/**
* @category Event Mixin
*/
export interface QuickReply {
/** Custom data provided by the app with the quick_reply. */
readonly data: string;
}
export const QuickReply: QuickReply = {
get data() {
return this.payload.message.quick_reply.payload;
},
};
/**
* @category Event Mixin
*/
export interface NLP {
/** The raw nlp object. */
readonly nlp?: any;
}
export const NLP: NLP = {
get nlp() {
return this.payload.message.nlp;
},
};
/**
* @category Event Mixin
*/
export interface Location {
readonly latitude: number;
readonly longitude: number;
}
export const Location: Location = {
get latitude() {
return this.payload.message.attachments[0].payload.lat;
},
get longitude() {
return this.payload.message.attachments[0].payload.long;
},
};
type ReactionType =
| 'smile'
| 'angry'
| 'sad'
| 'wow'
| 'love'
| 'like'
| 'dislike'
| 'other';
/**
* @category Event Mixin
*/
export interface Reaction {
/** Text description of the reaction. */
readonly reactionType: ReactionType;
/** Reference to the emoji corresponding to the reaction. */
readonly emoji: string;
/** Action performed by the user. */
readonly action: 'react' | 'unreact';
/** Reference to the Message ID that the user performed the reaction on. */
readonly messageId: string;
}
export const Reaction: Reaction = {
get reactionType() {
return this.payload.reaction.reaction;
},
get emoji() {
return this.payload.reaction.emoji;
},
get action() {
return this.payload.reaction.action;
},
get messageId() {
return this.payload.reaction.mid;
},
};
/**
* @category Event Mixin
*/
export interface TemplateProduct {
readonly products: {
id: string;
retailerId: string;
imageUrl: string;
title: string;
subtitle: string;
}[];
}
export const TemplateProduct: TemplateProduct = {
get products() {
return this.payload.message.attachments[0].payload.product.elements.map(
({ retailer_id: retailerId, image_url: imageUrl, ...restElement }) => ({
...restElement,
retailerId,
imageUrl,
})
);
},
};
/**
* @category Event Mixin
*/
export interface Delivery {
/**
* Array containing message IDs of messages that were delivered. Field may not
* be present.
*/
readonly messageIds: undefined | string[];
/** All messages that were sent before this timestamp were delivered. */
readonly watermark: number;
}
export const Delivery: Delivery = {
get messageIds() {
return this.payload.delivery.mids;
},
get watermark() {
return this.payload.delivery.watermark;
},
};
/**
* @category Event Mixin
*/
export interface Read {
/** All messages that were sent before or at this timestamp were read. */
readonly watermark: number;
}
export const Read: Read = {
get watermark() {
return this.payload.read.watermark;
},
};
/**
* @category Event Mixin
*/
export interface Echo {
/** Indicate whether the event is an echo. */
readonly isEcho: boolean;
/** ID of the app from which the message was sent. */
readonly appId: string;
/**
* Custom string passed to the Send API as the metadata field. Only present
* if the metadata property was set in the original message.
*/
readonly metadata: undefined | string;
}
export const Echo: Echo = {
isEcho: true,
get appId() {
return this.payload.message.app_id;
},
get metadata() {
return this.payload.message.metadata;
},
};
/**
* @category Event Mixin
*/
export interface Standby {
/** Indicate whether the event is sent to a standby channel. */
readonly isStandby: boolean;
}
export const Standby: Standby = {
isStandby: true,
};
/**
* @category Event Mixin
*/
export interface AccountLinking {
/** Indicate whether the user linked or unlinked their account. */
readonly status: 'linked' | 'unlinked';
/**
* Value of pass-through `authorization_code` provided in the Account Linking
* flow.
*/
readonly authorizationCode: string;
}
export const AccountLinking: AccountLinking = {
get status() {
return this.payload.account_linking.status;
},
get authorizationCode() {
return this.payload.account_linking.authorization_code;
},
};
/**
* @category Event Mixin
*/
export interface GamePlay {
/** App ID of the game */
readonly gameId: string;
/** ID of the user in the Instant Game name-space. By linking this ID to the PSID received in the sender field, the bot can send messages to a user after a game play */
readonly playerId: string;
/** Type of the social context a game is played in */
readonly contextType: 'SOLO' | 'THREAD' | 'GROUP';
/** ID of the context if not a SOLO type. This ID is in the Instant Game name-space */
readonly contextId: string;
/** Best score achieved by this user in this game round. Only available to Classic score based games */
readonly score: undefined | number;
/** JSON encoded payload data, set using FBInstant.setSessionData(). Only available to game with Rich Games Feature enabled */
readonly data: undefined | string;
}
export const GamePlay: GamePlay = {
get gameId() {
return this.payload.game_play.game_id;
},
get playerId() {
return this.payload.game_play.player_id;
},
get contextType() {
return this.payload.game_play.context_type;
},
get contextId() {
return this.payload.game_play.context_id;
},
get score() {
return this.payload.game_play.score;
},
get data() {
return this.payload.game_play.payload;
},
};
/**
* @category Event Mixin
*/
export interface PassThreadControl {
/** App ID that thread control is passed to. */
readonly newOwnerAppId: string;
/** Custom string specified in the API request. */
readonly metadata: string;
}
export const PassThreadControl: PassThreadControl = {
get newOwnerAppId() {
return this.payload.pass_thread_control.new_owner_app_id;
},
get metadata() {
return this.payload.pass_thread_control.metadata;
},
};
/**
* @category Event Mixin
*/
export interface TakeThreadControl {
/** App ID that thread control was taken from. */
readonly previousOwnerAppId: string;
/** Custom string specified in the API request. */
readonly metadata: string;
}
export const TakeThreadControl: TakeThreadControl = {
get previousOwnerAppId() {
return this.payload.take_thread_control.previous_owner_app_id;
},
get metadata() {
return this.payload.take_thread_control.metadata;
},
};
/**
* @category Event Mixin
*/
export interface RequestThreadControl {
/** App ID of the Secondary Receiver that is requesting thread control. */
readonly requestedOwnerAppId: string;
/** Custom string specified in the API request. */
readonly metadata: string;
}
export const RequestThreadControl: RequestThreadControl = {
get requestedOwnerAppId() {
return this.payload.request_thread_control.requested_owner_app_id;
},
get metadata() {
return this.payload.request_thread_control.metadata;
},
};
/**
* @category Event Mixin
*/
export interface AppRoles {
/** The app id and roles mapping object. */
readonly appRoles: { [id: string]: string[] };
}
export const AppRoles: AppRoles = {
get appRoles() {
return this.payload.app_roles;
},
};
/**
* @category Event Mixin
*/
export interface Optin {
/** The `data-ref` attribute that was defined with the entry point. */
readonly dataRef: string;
/**
* [Checkbox plugin](https://developers.facebook.com/docs/messenger-platform/discovery/checkbox-plugin)
* only. user_ref attribute that was defined in the checkbox plugin include.
*/
readonly userRef: undefined | string;
}
export const Optin: Optin = {
get dataRef() {
return this.payload.optin.ref;
},
get userRef() {
return this.payload.optin.user_ref;
},
};
/**
* @category Event Mixin
*/
export interface OneTimeNotifOptin {
/** The payload attached with the request message */
readonly data: string;
/** The token to send one time notification with */
readonly token: string;
}
export const OneTimeNotifOptin: OneTimeNotifOptin = {
get data() {
return this.payload.optin.payload;
},
get token() {
return this.payload.optin.one_time_notif_token;
},
};
/**
* @category Event Mixin
*/
export interface PolicyEnforcement {
readonly action: 'warning' | 'block' | 'unblock';
/**
* The reason for being warned or blocked. This field is absent if `action` is
* `'unblock'`.
*/
readonly reason: undefined | string;
}
export const PolicyEnforcement: PolicyEnforcement = {
get action() {
return this.payload['policy-enforcement'].action;
},
get reason() {
return this.payload['policy-enforcement'].reason;
},
};
type ReferralSource =
| 'MESSENGER_CODE'
| 'DISCOVER_TAB'
| 'ADS'
| 'SHORTLINK'
| 'CUSTOMER_CHAT_PLUGIN';
type RawReferral = {
source: ReferralSource;
type: 'OPEN_THREAD';
ref: string;
ad_id: undefined | string;
referer_uri: undefined | string;
is_guest_user: undefined | boolean;
};
/**
* @category Event Mixin
*/
export interface Referral {
/** The source of the referral. */
readonly source: ReferralSource;
/** The optional `ref` attribute set in the referrer. */
readonly ref: string;
/** Id of ad if `source` is `'ADS'` */
readonly adId: undefined | string;
/**
* The URI of the site where the message was sent in the Facebook chat plugin.
*/
readonly refererUri: undefined | string;
/**
* A flag indicating whether the user is a guest user from Facebook Chat
* Plugin.
*/
readonly isGuestUser: boolean;
}
export const Referral: Referral = {
get source() {
return this.payload.referral.source;
},
get ref() {
return this.payload.referral.ref;
},
get adId() {
return this.payload.referral.ad_id;
},
get refererUri() {
return this.payload.referral.referer_uri;
},
get isGuestUser() {
return !!this.payload.referral.is_guest_user;
},
};
/**
* @category Event Mixin
*/
export interface Postback {
/**
* Title for the CTA that was clicked on. This is sent to all apps subscribed
* to the page. For apps other than the original CTA sender, the postback
* event will be delivered via the standby channel.
*/
readonly title: string;
/**
* Payload parameter that was defined with the button. This is only visible to
* the app that send the original template message.
*/
readonly data: string;
/** Referral information for how the user got into the thread. */
readonly referral: undefined | RawReferral;
/** The source of the referral if referral exist. */
readonly referralSource: undefined | ReferralSource;
/** The optional `ref` attribute set in the referrer if referral exist. */
readonly referralRef: undefined | string;
/** Id of ad if `referral.source` is `'ADS'`. */
readonly referralAdId: undefined | string;
}
export const Postback: Postback = {
get title() {
return this.payload.postback.title;
},
get data() {
return this.payload.postback.payload;
},
get referral() {
return this.payload.postback.referral;
},
get referralSource() {
return this.payload.postback.referral?.source;
},
get referralRef() {
return this.payload.postback.referral?.ref;
},
get referralAdId() {
return this.payload.postback.referral?.ad_id;
},
};