@tiledesk/tiledesk-whatsapp-connector 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +600 -0
- package/package.json +31 -0
- package/publish.sh +10 -0
- package/template/configure.html +168 -0
- package/test/test_translate_whatsapp.js +412 -0
- package/tiledesk/TiledeskChannel.js +147 -0
- package/tiledesk/TiledeskSubscriptionClient.js +140 -0
- package/tiledesk/TiledeskWhatsapp.js +192 -0
- package/tiledesk/TiledeskWhatsappTranslator.js +434 -0
- package/tmp/media-1wy7l3.jpeg +0 -0
- package/tmp/media-1wy8f7.mp4 +0 -0
- package/tmp/media-1wy8mz.jpeg +0 -0
- package/tmp/media-1wyb62.jpeg +0 -0
- package/tmp/media-1wyb7k.mp4 +0 -0
- package/tmp/media-1wyb98.pdf +0 -0
- package/tmp/media-1wybp7.pdf +0 -0
|
@@ -0,0 +1,434 @@
|
|
|
1
|
+
|
|
2
|
+
class TiledeskWhatsappTranslator {
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Constructor for TiledeskWhatsappTranslator
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* const { TiledeskWhatsappTranslator } = require('tiledesk-whatsapp-translator');
|
|
9
|
+
* const tlr = new TiledeskWhatsappTranslator({channelMessage: replyFromChatbot});
|
|
10
|
+
*
|
|
11
|
+
* @param {Object} config JSON configuration.
|
|
12
|
+
* @param {string} config.channelMessage Mandatory. Tiledesk channel native message
|
|
13
|
+
* @param {boolean} options.log Optional. If true HTTP requests are logged.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
static WHATSAPP_MESSAGING_PRODUCT = "whatsapp";
|
|
17
|
+
static CHANNEL_NAME = "whatsapp"
|
|
18
|
+
|
|
19
|
+
constructor(config) {
|
|
20
|
+
if (!config) {
|
|
21
|
+
throw new Error('config is mandatory');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/*
|
|
25
|
+
if (!config.tiledeskChannelMessage) {
|
|
26
|
+
throw new Error('config.tiledeskChannelMessage is mandatory');
|
|
27
|
+
}
|
|
28
|
+
this.tiledeskChannelMessage = config.tiledeskChannelMessage;
|
|
29
|
+
console.log("this.tiledeskChannelMessage: ", this.tiledeskChannelMessage);
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
if (!config.channelMessage) {
|
|
33
|
+
throw new Error('config.channelMessage is mandatory');
|
|
34
|
+
}
|
|
35
|
+
this.channelMessage = config.channelMessage;
|
|
36
|
+
this.log = false;
|
|
37
|
+
if (config.log) {
|
|
38
|
+
this.log = true;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
/*
|
|
45
|
+
*************** START ***************
|
|
46
|
+
********* WHATSAPP BUSINESS *********
|
|
47
|
+
*************************************
|
|
48
|
+
*/
|
|
49
|
+
toWhatsapp(whatsapp_receiver) {
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
// to --> recipient
|
|
53
|
+
let tiledeskChannelMessage = this.channelMessage;
|
|
54
|
+
if (this.log) {
|
|
55
|
+
console.log("[Translator] tiledesk message: ", tiledeskChannelMessage)
|
|
56
|
+
}
|
|
57
|
+
let text = tiledeskChannelMessage.text.replace(/-{1,}/g, '');
|
|
58
|
+
text = text.replace(/\*{2,}/g, '*')
|
|
59
|
+
|
|
60
|
+
let whatsapp_message = {
|
|
61
|
+
messaging_product: TiledeskWhatsappTranslator.WHATSAPP_MESSAGING_PRODUCT,
|
|
62
|
+
to: whatsapp_receiver,
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (tiledeskChannelMessage.metadata) {
|
|
66
|
+
|
|
67
|
+
if (tiledeskChannelMessage.metadata.type.startsWith('image/')) {
|
|
68
|
+
var imgUrl = tiledeskChannelMessage.metadata.src;
|
|
69
|
+
whatsapp_message.type = 'image'
|
|
70
|
+
whatsapp_message.image = {
|
|
71
|
+
link: imgUrl,
|
|
72
|
+
caption: text
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (tiledeskChannelMessage.metadata.type.startsWith('video/')) {
|
|
77
|
+
var videoUrl = tiledeskChannelMessage.metadata.src;
|
|
78
|
+
whatsapp_message.type = 'document'
|
|
79
|
+
whatsapp_message.document = {
|
|
80
|
+
link: videoUrl,
|
|
81
|
+
caption: text
|
|
82
|
+
}
|
|
83
|
+
/*
|
|
84
|
+
data.type = 'video'
|
|
85
|
+
data.video = {
|
|
86
|
+
link: videoUrl,
|
|
87
|
+
caption: text
|
|
88
|
+
}
|
|
89
|
+
*/
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (tiledeskChannelMessage.metadata.type.startsWith('application/')) {
|
|
93
|
+
var doc = tiledeskChannelMessage.metadata.src;
|
|
94
|
+
whatsapp_message.type = 'document'
|
|
95
|
+
whatsapp_message.document = {
|
|
96
|
+
link: doc,
|
|
97
|
+
caption: text
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (this.log) {
|
|
102
|
+
console.log("[Translator] whatsapp message: ", whatsapp_message)
|
|
103
|
+
}
|
|
104
|
+
return whatsapp_message;
|
|
105
|
+
|
|
106
|
+
} else if (tiledeskChannelMessage.attributes) {
|
|
107
|
+
if (tiledeskChannelMessage.attributes.attachment) {
|
|
108
|
+
if (tiledeskChannelMessage.attributes.attachment.buttons) {
|
|
109
|
+
|
|
110
|
+
let buttons = tiledeskChannelMessage.attributes.attachment.buttons;
|
|
111
|
+
|
|
112
|
+
let quick_replies = [];
|
|
113
|
+
let option_rows = [];
|
|
114
|
+
let action_rows = [];
|
|
115
|
+
|
|
116
|
+
let buttons_count = 0;
|
|
117
|
+
for (let btn of buttons) {
|
|
118
|
+
if (btn.type != 'url') {
|
|
119
|
+
buttons_count = buttons_count + 1;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (buttons_count < 4) {
|
|
124
|
+
|
|
125
|
+
for (let btn of buttons) {
|
|
126
|
+
let title = (btn.value.length > 20) ? btn.value.substr(0, 18) + '..' : btn.value;
|
|
127
|
+
|
|
128
|
+
if (btn.type == 'text') {
|
|
129
|
+
let text_btn = {
|
|
130
|
+
type: "reply",
|
|
131
|
+
reply: {
|
|
132
|
+
id: "quick_" + btn.value,
|
|
133
|
+
title: title
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
quick_replies.push(text_btn);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (btn.type == 'action') {
|
|
140
|
+
let action_btn = {
|
|
141
|
+
type: "reply",
|
|
142
|
+
reply: {
|
|
143
|
+
id: "action_" + btn.action,
|
|
144
|
+
title: title
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
quick_replies.push(action_btn);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (btn.type == 'url') {
|
|
151
|
+
text = text + "\n\n👉 " + btn.value + " (" + btn.link + ")";
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
whatsapp_message.type = "interactive";
|
|
156
|
+
whatsapp_message.interactive = {
|
|
157
|
+
type: "button",
|
|
158
|
+
body: { text: text },
|
|
159
|
+
action: { buttons: quick_replies }
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
if (this.log) {
|
|
163
|
+
console.log("[Translator] whatsapp message: ", whatsapp_message)
|
|
164
|
+
}
|
|
165
|
+
return whatsapp_message;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (buttons_count > 3 && buttons_count < 11) {
|
|
169
|
+
|
|
170
|
+
for (let btn of buttons) {
|
|
171
|
+
let title = (btn.value.length > 24) ? btn.value.substr(0, 22) + '..' : btn.value;
|
|
172
|
+
|
|
173
|
+
if (btn.type == 'text') {
|
|
174
|
+
let row = {
|
|
175
|
+
id: "quick_" + btn.value,
|
|
176
|
+
title: title
|
|
177
|
+
}
|
|
178
|
+
option_rows.push(row);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (btn.type == 'action') {
|
|
182
|
+
let row = {
|
|
183
|
+
id: "action_" + btn.action,
|
|
184
|
+
title: title
|
|
185
|
+
}
|
|
186
|
+
action_rows.push(row);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (btn.type == 'url') {
|
|
190
|
+
text = text + "\n\n👉 " + btn.value + " (" + btn.link + ")"
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
whatsapp_message.type = "interactive";
|
|
196
|
+
let sections;
|
|
197
|
+
|
|
198
|
+
if (option_rows.length > 0 && action_rows.length > 0) {
|
|
199
|
+
sections = [
|
|
200
|
+
{
|
|
201
|
+
title: "Options",
|
|
202
|
+
rows: option_rows
|
|
203
|
+
},
|
|
204
|
+
{
|
|
205
|
+
title: "Actions",
|
|
206
|
+
rows: action_rows
|
|
207
|
+
}
|
|
208
|
+
]
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (option_rows.length > 0 && action_rows.length == 0) {
|
|
212
|
+
sections = [
|
|
213
|
+
{
|
|
214
|
+
title: "Options",
|
|
215
|
+
rows: option_rows
|
|
216
|
+
}
|
|
217
|
+
]
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (option_rows.length == 0 && action_rows.length > 0) {
|
|
221
|
+
sections = [
|
|
222
|
+
{
|
|
223
|
+
title: "Actions",
|
|
224
|
+
rows: action_rows
|
|
225
|
+
}
|
|
226
|
+
]
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
whatsapp_message.interactive = {
|
|
230
|
+
type: "list",
|
|
231
|
+
body: { text: text },
|
|
232
|
+
action: {
|
|
233
|
+
button: "Choose an option",
|
|
234
|
+
sections: sections
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (this.log) {
|
|
239
|
+
console.log("[Translator] whatsapp message: ", whatsapp_message)
|
|
240
|
+
}
|
|
241
|
+
return whatsapp_message;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (buttons_count > 10) {
|
|
245
|
+
// too many buttons
|
|
246
|
+
// Option 1: Skip message
|
|
247
|
+
// Option 2: Cut buttons array -> display first 10 buttons only
|
|
248
|
+
// Option 3: Send message with *buttons (questa)
|
|
249
|
+
|
|
250
|
+
whatsapp_message.text = { body: tiledeskChannelMessage.attributes._raw_message };
|
|
251
|
+
|
|
252
|
+
if (this.log) {
|
|
253
|
+
console.log("[Translator] whatsapp message: ", whatsapp_message)
|
|
254
|
+
}
|
|
255
|
+
return whatsapp_message;
|
|
256
|
+
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
} else {
|
|
260
|
+
|
|
261
|
+
whatsapp_message.text = { body: text };
|
|
262
|
+
|
|
263
|
+
if (this.log) {
|
|
264
|
+
console.log("[Translator] whatsapp message: ", whatsapp_message)
|
|
265
|
+
}
|
|
266
|
+
return whatsapp_message;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
} else {
|
|
270
|
+
|
|
271
|
+
whatsapp_message.text = { body: text };
|
|
272
|
+
|
|
273
|
+
if (this.log) {
|
|
274
|
+
console.log("[Translator] whatsapp message: ", whatsapp_message)
|
|
275
|
+
}
|
|
276
|
+
return whatsapp_message;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
} else {
|
|
282
|
+
// Skip message - non si deve fare qui!
|
|
283
|
+
return null
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
toTiledesk(from, media_url) {
|
|
288
|
+
|
|
289
|
+
let whatsappChannelMessage = this.channelMessage;
|
|
290
|
+
if (this.log) {
|
|
291
|
+
console.log("[Translator] whatsapp message: ", whatsappChannelMessage)
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// text message
|
|
295
|
+
if (whatsappChannelMessage.type == 'text') {
|
|
296
|
+
var data = {
|
|
297
|
+
text: whatsappChannelMessage.text.body,
|
|
298
|
+
senderFullname: from,
|
|
299
|
+
channel: { name: TiledeskWhatsappTranslator.CHANNEL_NAME }
|
|
300
|
+
}
|
|
301
|
+
return data;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// interactive message
|
|
305
|
+
if (whatsappChannelMessage.type == 'interactive') {
|
|
306
|
+
|
|
307
|
+
// list reply
|
|
308
|
+
if (whatsappChannelMessage.interactive.type == 'list_reply') {
|
|
309
|
+
// action button
|
|
310
|
+
if (whatsappChannelMessage.interactive.list_reply.id.startsWith("action_")) {
|
|
311
|
+
var tiledeskMessage = {
|
|
312
|
+
senderFullname: from,
|
|
313
|
+
text: ' ',
|
|
314
|
+
type: 'text',
|
|
315
|
+
attributes: {
|
|
316
|
+
action: whatsappChannelMessage.interactive.list_reply.id.substring(7),
|
|
317
|
+
subtype: 'info'
|
|
318
|
+
},
|
|
319
|
+
channel: { name: TiledeskWhatsappTranslator.CHANNEL_NAME }
|
|
320
|
+
}
|
|
321
|
+
return tiledeskMessage;
|
|
322
|
+
}
|
|
323
|
+
// quick reply button
|
|
324
|
+
if (whatsappChannelMessage.interactive.list_reply.id.startsWith("quick_")) {
|
|
325
|
+
var tiledeskMessage = {
|
|
326
|
+
text: whatsappChannelMessage.interactive.list_reply.id.substring(6),
|
|
327
|
+
senderFullname: from,
|
|
328
|
+
channel: { name: TiledeskWhatsappTranslator.CHANNEL_NAME }
|
|
329
|
+
}
|
|
330
|
+
return tiledeskMessage;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// inline button reply
|
|
335
|
+
if (whatsappChannelMessage.interactive.type == 'button_reply') {
|
|
336
|
+
// action button
|
|
337
|
+
if (whatsappChannelMessage.interactive.button_reply.id.startsWith("action_")) {
|
|
338
|
+
var tiledeskMessage = {
|
|
339
|
+
senderFullname: from,
|
|
340
|
+
text: ' ',
|
|
341
|
+
type: 'text',
|
|
342
|
+
attributes: {
|
|
343
|
+
action: whatsappChannelMessage.interactive.button_reply.id.substring(7),
|
|
344
|
+
subtype: 'info'
|
|
345
|
+
},
|
|
346
|
+
channel: { name: TiledeskWhatsappTranslator.CHANNEL_NAME }
|
|
347
|
+
}
|
|
348
|
+
return tiledeskMessage;
|
|
349
|
+
}
|
|
350
|
+
// quick reply button
|
|
351
|
+
if (whatsappChannelMessage.interactive.button_reply.id.startsWith("quick_")) {
|
|
352
|
+
var tiledeskMessage = {
|
|
353
|
+
text: whatsappChannelMessage.interactive.button_reply.id.substring(6),
|
|
354
|
+
senderFullname: from,
|
|
355
|
+
channel: { name: TiledeskWhatsappTranslator.CHANNEL_NAME }
|
|
356
|
+
}
|
|
357
|
+
return tiledeskMessage;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// media message - image
|
|
363
|
+
if (whatsappChannelMessage.type == 'image') {
|
|
364
|
+
|
|
365
|
+
let text = "Image attached"
|
|
366
|
+
if (whatsappChannelMessage.image.caption) {
|
|
367
|
+
text = whatsappChannelMessage.image.caption;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
var tiledeskMessage = {
|
|
371
|
+
text: text,
|
|
372
|
+
senderFullname: from,
|
|
373
|
+
channel: { name: TiledeskWhatsappTranslator.CHANNEL_NAME },
|
|
374
|
+
type: "image",
|
|
375
|
+
metadata: {
|
|
376
|
+
src: media_url
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
return tiledeskMessage;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// media message - video
|
|
383
|
+
if (whatsappChannelMessage.type == 'video') {
|
|
384
|
+
|
|
385
|
+
let text = "Video attached"
|
|
386
|
+
if (whatsappChannelMessage.video.caption) {
|
|
387
|
+
text = whatsappChannelMessage.video.caption;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
var tiledeskMessage = {
|
|
391
|
+
text: "[Download video](" + media_url + ")",
|
|
392
|
+
senderFullname: from,
|
|
393
|
+
channel: { name: TiledeskWhatsappTranslator.CHANNEL_NAME },
|
|
394
|
+
type: "file",
|
|
395
|
+
metadata: {
|
|
396
|
+
name: "video.mp4",
|
|
397
|
+
type: "video/mp4",
|
|
398
|
+
src: media_url,
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
return tiledeskMessage;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// media message - document
|
|
405
|
+
if (whatsappChannelMessage.type == 'document') {
|
|
406
|
+
|
|
407
|
+
let text = "Document attached"
|
|
408
|
+
if (whatsappChannelMessage.document.caption) {
|
|
409
|
+
text = whatsappChannelMessage.document.caption
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
var tiledeskMessage = {
|
|
413
|
+
text: "[Dowload document](" + media_url + ")",
|
|
414
|
+
senderFullname: from,
|
|
415
|
+
channel: { name: TiledeskWhatsappTranslator.CHANNEL_NAME },
|
|
416
|
+
type: "file",
|
|
417
|
+
metadata: {
|
|
418
|
+
name: "document.pdf",
|
|
419
|
+
type: "application/pdf",
|
|
420
|
+
src: media_url
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
return tiledeskMessage;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
/*
|
|
427
|
+
*************************************
|
|
428
|
+
********* WHATSAPP BUSINESS *********
|
|
429
|
+
**************** END ****************
|
|
430
|
+
*/
|
|
431
|
+
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
module.exports = { TiledeskWhatsappTranslator };
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|