-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper.ts
354 lines (333 loc) · 10.5 KB
/
wrapper.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
/*
* Copyright © 2024 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/
import EventWrapper from '@/channel/lib/EventWrapper';
import {
AttachmentForeignKey,
AttachmentPayload,
FileType,
} from '@/chat/schemas/types/attachment';
import {
IncomingMessageType,
PayloadType,
StdEventType,
StdIncomingMessage,
} from '@/chat/schemas/types/message';
import { Payload } from '@/chat/schemas/types/quick-reply';
import MessengerHandler from './index.channel';
import { MESSENGER_CHANNEL_NAME } from './settings';
import { Messenger } from './types';
type MessengerEventAdapter =
| {
eventType: StdEventType.unknown;
messageType: never;
raw: Messenger.Event;
}
| {
eventType: StdEventType.read;
messageType: never;
raw: Messenger.MessageReadEvent;
}
| {
eventType: StdEventType.delivery;
messageType: never;
raw: Messenger.MessageDeliveryEvent;
}
| {
eventType: StdEventType.message;
messageType: IncomingMessageType.postback;
raw: Messenger.IncomingMessage<Messenger.IncomingPostback>;
}
| {
eventType: StdEventType.message | StdEventType.echo;
messageType: IncomingMessageType.message;
raw: Messenger.IncomingMessage<
Messenger.IncomingAnyMessage<Messenger.IncomingTextMessage>
>;
}
| {
eventType: StdEventType.message;
messageType: IncomingMessageType.quick_reply;
raw: Messenger.IncomingMessage<
Messenger.IncomingAnyMessage<Messenger.IncomingQuickReplyMessage>
>;
}
| {
eventType: StdEventType.message | StdEventType.echo;
messageType:
| IncomingMessageType.location
| IncomingMessageType.attachments;
raw: Messenger.IncomingMessage<
Messenger.IncomingAnyMessage<Messenger.IncomingAttachmentMessage>
>;
}
| {
eventType: StdEventType.message | StdEventType.echo;
messageType: IncomingMessageType.unknown;
raw: Messenger.IncomingMessage;
};
export default class MessengerEventWrapper extends EventWrapper<
MessengerEventAdapter,
Messenger.Event,
typeof MESSENGER_CHANNEL_NAME
> {
/**
* Constructor : channel's event wrapper
*
* @param handler - The channel's handler
* @param event - The message event received
*/
constructor(handler: MessengerHandler, event: Messenger.Event) {
super(handler, event);
}
/**
* Called by the parent constructor, it defines :
* - The type of event received
* - The type of message when the event is a message.
* - Sets a typed raw object of the event data
*
* @param event - The message event received
*/
_init(event: Messenger.Event) {
if ('message' in event) {
if ('is_echo' in event.message) {
// If needed, you can subscribe to this callback by selecting
// the message_echoes field when setting up your webhook.
// It could be useful to sync messages sent directly
// from the page itself
this._adapter.eventType = StdEventType.echo;
} else {
this._adapter.eventType = StdEventType.message;
}
const message = event.message;
if ('quick_reply' in message) {
this._adapter.messageType = IncomingMessageType.quick_reply;
} else if ('text' in message) {
this._adapter.messageType = IncomingMessageType.message;
} else if (
message.attachments &&
Array.isArray(message.attachments) &&
message.attachments.length > 0
) {
if (message.attachments[0].type === Messenger.AttachmentType.location) {
this._adapter.messageType = IncomingMessageType.location;
} else {
this._adapter.messageType = IncomingMessageType.attachments;
}
} else {
this._adapter.messageType = IncomingMessageType.unknown;
}
} else if ('postback' in event) {
this._adapter.eventType = StdEventType.message;
this._adapter.messageType = IncomingMessageType.postback;
} else if ('delivery' in event) {
this._adapter.eventType = StdEventType.delivery;
} else if ('read' in event) {
this._adapter.eventType = StdEventType.read;
} else {
this._adapter.eventType = StdEventType.unknown;
}
this._adapter.raw = event;
}
/**
* Returns the message id
*
* @returns Message ID
*/
getId(): string {
// Either event type is a message or a message echo
if ('message' in this._adapter.raw) {
if (this._adapter.raw.message.mid) {
return this._adapter.raw.message.mid;
}
throw new Error('The message id `mid` is missing');
}
throw new Error(
'The id (`mid`) is only available in message events (excluding postbacks)',
);
}
/**
* Returns event sender (subscriber) id in Facebook
*
* @returns Subscriber foreign ID
*/
getSenderForeignId(): string {
return this._adapter.raw.sender.id;
}
/**
* Returns event recipient id
*
* @returns Subscriber foreign ID
*/
getRecipientForeignId(): string {
return this._adapter.raw.recipient.id;
}
/**
* Returns the type of event received
*
* @returns Standard event type
*/
getEventType(): StdEventType {
return this._adapter.eventType;
}
/**
* Finds out and return the type of the event recieved from messenger
*
* @returns The type of message
*/
getMessageType(): IncomingMessageType {
return this._adapter.messageType || IncomingMessageType.unknown;
}
/**
* Return payload whenever user clicks on a button/quick_reply or sends an attachment
*
* @returns The payload content
*/
getPayload(): Payload | string | undefined {
if (this._adapter.eventType === StdEventType.message) {
switch (this._adapter.messageType) {
case IncomingMessageType.postback:
return this._adapter.raw.postback.payload;
case IncomingMessageType.quick_reply:
return this._adapter.raw.message.quick_reply.payload;
case IncomingMessageType.location: {
const coordinates =
this._adapter.raw.message.attachments[0].payload.coordinates;
return {
type: PayloadType.location,
coordinates: {
lat: coordinates?.lat || 0,
lon: coordinates?.long || 0,
},
};
}
case IncomingMessageType.attachments: {
const attachment: Messenger.Attachment =
this._adapter.raw.message.attachments[0];
return {
type: PayloadType.attachments,
attachments: {
type: <FileType>(<unknown>attachment.type),
payload: {
url: attachment?.payload?.url || '',
},
},
};
}
}
}
return undefined;
}
/**
* Return a standard message format that can be stored in DB
*
* @returns Received message in standard format
*/
getMessage(): StdIncomingMessage {
if (
[StdEventType.message, StdEventType.echo].indexOf(
this._adapter.eventType,
) === -1
) {
throw new Error('Called getMessage() on a non-message event');
}
switch (this._adapter.messageType) {
case IncomingMessageType.message:
return {
text: this._adapter.raw.message.text,
};
case IncomingMessageType.postback:
return {
postback: this._adapter.raw.postback.payload,
text: this._adapter.raw.postback.title,
};
case IncomingMessageType.quick_reply:
return {
postback: this._adapter.raw.message.quick_reply.payload,
text: this._adapter.raw.message.text,
};
case IncomingMessageType.location: {
const coordinates =
this._adapter.raw.message.attachments[0].payload.coordinates;
return {
type: PayloadType.location,
coordinates: {
lat: coordinates?.lat || 0,
lon: coordinates?.long || 0,
},
};
}
case IncomingMessageType.attachments: {
const attachments = this._adapter.raw.message.attachments;
let serialized_text = 'attachment:';
if (attachments[0].type === Messenger.AttachmentType.fallback) {
// Handle fallback
serialized_text += 'fallback';
} else if (
attachments[0].payload &&
attachments[0].payload.sticker_id
) {
// Handle stickers
serialized_text += `sticker:${attachments[0].payload.sticker_id}`;
} else {
serialized_text += `${attachments[0].type}:${attachments[0].payload.url}`;
}
const stdAttachments = attachments.map((att) => {
return {
type: Object.values(FileType).includes(
<FileType>(<unknown>att.type),
)
? <FileType>(<unknown>att.type)
: FileType.unknown,
payload: {
url: att.payload.url || '',
},
};
});
return {
type: PayloadType.attachments,
serialized_text,
attachment:
stdAttachments.length > 0 ? stdAttachments[0] : stdAttachments,
};
}
default:
throw new Error('Unknown incoming message type');
}
}
/**
* Return the list of recieved attachments
* @deprecated
* @returns Received attachments message
*/
getAttachments(): AttachmentPayload<AttachmentForeignKey>[] {
return this._adapter.eventType === StdEventType.message &&
this._adapter.messageType === IncomingMessageType.attachments
? [].concat(this._adapter.raw.message.attachments as any)
: [];
}
/**
* Return the delivered messages ids
*
* @returns return delivered messages ids
*/
getDeliveredMessages(): string[] {
return this.getEventType() === StdEventType.delivery
? (<Messenger.MessageDeliveryEvent>this._adapter.raw).delivery.mids
: [];
}
/**
* Return the message's watermark
*
* @returns The watermark
*/
getWatermark() {
return this.getEventType() === StdEventType.read
? (<Messenger.MessageReadEvent>this._adapter.raw).read.watermark
: 0;
}
}