@tiledesk/tiledesk-whatsapp-connector 0.1.38 → 0.1.40
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 +115 -126
- package/package.json +1 -1
- package/template/configure.html +0 -24
- package/test/test_translate_whatsapp.js +20 -33
- package/tiledesk/KVBaseMongo.js +2 -2
- package/tiledesk/MessageHandler.js +10 -6
- package/tiledesk/TiledeskAppsClient.js +1 -1
- package/tiledesk/TiledeskBotTester.js +7 -31
- package/tiledesk/TiledeskChannel.js +21 -19
- package/tiledesk/TiledeskSubscriptionClient.js +4 -7
- package/tiledesk/TiledeskWhatsapp.js +13 -26
- package/tiledesk/TiledeskWhatsappTranslator.js +16 -66
- package/tmp/media-1z2nit.jpeg +0 -0
- package/tmp/media-1z2nkr.mp4 +0 -0
- package/tmp/media-20g7pe.mp4 +0 -0
- package/tmp/media-20g7pi.mp4 +0 -0
package/index.js
CHANGED
|
@@ -76,10 +76,7 @@ router.get('/detail', async (req, res) => {
|
|
|
76
76
|
}
|
|
77
77
|
|
|
78
78
|
readHTMLFile('/detail.html', (err, html) => {
|
|
79
|
-
if (err) {
|
|
80
|
-
console.log("(whatsapp) error read html file: ", err);
|
|
81
|
-
}
|
|
82
|
-
|
|
79
|
+
if (err) { console.log("(wab) error read html file: ", err); }
|
|
83
80
|
var template = handlebars.compile(html);
|
|
84
81
|
var replacements = {
|
|
85
82
|
app_version: pjson.version,
|
|
@@ -99,7 +96,7 @@ router.post('/install', async (req, res) => {
|
|
|
99
96
|
let app_id = req.body.app_id;
|
|
100
97
|
let token = req.body.token;
|
|
101
98
|
|
|
102
|
-
console.log("(
|
|
99
|
+
console.log("(wab) Install app " + app_id + " for project id " + project_id);
|
|
103
100
|
let installation_info = {
|
|
104
101
|
project_id: project_id,
|
|
105
102
|
app_id: app_id,
|
|
@@ -110,14 +107,14 @@ router.post('/install', async (req, res) => {
|
|
|
110
107
|
appClient.install(installation_info).then((installation) => {
|
|
111
108
|
|
|
112
109
|
if (log) {
|
|
113
|
-
console.log("(
|
|
110
|
+
console.log("(wab) installation response: ", installation);
|
|
114
111
|
}
|
|
115
112
|
|
|
116
113
|
let installed = true;
|
|
117
114
|
|
|
118
115
|
readHTMLFile('/detail.html', (err, html) => {
|
|
119
116
|
if (err) {
|
|
120
|
-
console.log("(
|
|
117
|
+
console.log("(wab) error read html file: ", err);
|
|
121
118
|
}
|
|
122
119
|
|
|
123
120
|
var template = handlebars.compile(html);
|
|
@@ -133,7 +130,7 @@ router.post('/install', async (req, res) => {
|
|
|
133
130
|
})
|
|
134
131
|
|
|
135
132
|
}).catch((err) => {
|
|
136
|
-
console.error("(
|
|
133
|
+
console.error("(wab) installation error: ", err.data)
|
|
137
134
|
res.send("An error occurred during the installation");
|
|
138
135
|
})
|
|
139
136
|
|
|
@@ -148,14 +145,14 @@ router.post('/uninstall', async (req, res) => {
|
|
|
148
145
|
appClient.uninstall(project_id, app_id).then((response) => {
|
|
149
146
|
|
|
150
147
|
if (log) {
|
|
151
|
-
console.log("(
|
|
148
|
+
console.log("(wab) uninstallation response: ", response);
|
|
152
149
|
}
|
|
153
150
|
|
|
154
151
|
let installed = false;
|
|
155
152
|
|
|
156
153
|
readHTMLFile('/detail.html', (err, html) => {
|
|
157
154
|
if (err) {
|
|
158
|
-
console.log("(
|
|
155
|
+
console.log("(wab) error read html file: ", err);
|
|
159
156
|
}
|
|
160
157
|
|
|
161
158
|
var template = handlebars.compile(html);
|
|
@@ -171,15 +168,15 @@ router.post('/uninstall', async (req, res) => {
|
|
|
171
168
|
})
|
|
172
169
|
|
|
173
170
|
}).catch((err) => {
|
|
174
|
-
console.error("(
|
|
171
|
+
console.error("(wab) uninsallation error: ", err.data)
|
|
175
172
|
res.send("An error occurred during the uninstallation");
|
|
176
173
|
})
|
|
177
174
|
})
|
|
178
175
|
|
|
179
176
|
router.get('/configure', async (req, res) => {
|
|
180
|
-
console.log("
|
|
177
|
+
console.log("(wab) /configure");
|
|
181
178
|
if (log) {
|
|
182
|
-
console.log("(
|
|
179
|
+
console.log("(wab) /configure query: ", req.query);
|
|
183
180
|
}
|
|
184
181
|
|
|
185
182
|
let projectId = "";
|
|
@@ -194,21 +191,21 @@ router.get('/configure', async (req, res) => {
|
|
|
194
191
|
|
|
195
192
|
let settings = await db.get(CONTENT_KEY);
|
|
196
193
|
if (log) {
|
|
197
|
-
console.log("(
|
|
194
|
+
console.log("(wab) settings: ", settings);
|
|
198
195
|
}
|
|
199
196
|
|
|
200
197
|
// get departments
|
|
201
198
|
const tdChannel = new TiledeskChannel({ settings: { project_id: projectId, token: token }, API_URL: API_URL })
|
|
202
199
|
let departments = await tdChannel.getDepartments(token);
|
|
200
|
+
console.log("(wab) found " + departments.length + " departments")
|
|
203
201
|
if (log) {
|
|
204
|
-
console.log("departments: ", JSON.stringify(departments))
|
|
205
202
|
}
|
|
206
203
|
|
|
207
204
|
if (settings) {
|
|
208
205
|
|
|
209
206
|
readHTMLFile('/configure.html', (err, html) => {
|
|
210
207
|
if (err) {
|
|
211
|
-
console.log("(
|
|
208
|
+
console.log("(wab) error read html file: ", err);
|
|
212
209
|
}
|
|
213
210
|
|
|
214
211
|
var template = handlebars.compile(html);
|
|
@@ -233,7 +230,7 @@ router.get('/configure', async (req, res) => {
|
|
|
233
230
|
readHTMLFile('/configure.html', (err, html) => {
|
|
234
231
|
|
|
235
232
|
if (err) {
|
|
236
|
-
console.log("(
|
|
233
|
+
console.log("(departments) error read html file: ", err);
|
|
237
234
|
}
|
|
238
235
|
|
|
239
236
|
var template = handlebars.compile(html);
|
|
@@ -252,9 +249,9 @@ router.get('/configure', async (req, res) => {
|
|
|
252
249
|
})
|
|
253
250
|
|
|
254
251
|
router.post('/update', async (req, res) => {
|
|
255
|
-
console.log("
|
|
256
|
-
console.log("(whatsapp) /update body: ", req.body);
|
|
252
|
+
console.log("(wab) /update");
|
|
257
253
|
if (log) {
|
|
254
|
+
console.log("(wab) body: ", req.body);
|
|
258
255
|
}
|
|
259
256
|
|
|
260
257
|
let projectId = req.body.project_id;
|
|
@@ -271,10 +268,7 @@ router.post('/update', async (req, res) => {
|
|
|
271
268
|
// get departments
|
|
272
269
|
const tdChannel = new TiledeskChannel({ settings: { project_id: projectId, token: token }, API_URL: API_URL })
|
|
273
270
|
let departments = await tdChannel.getDepartments(token);
|
|
274
|
-
|
|
275
|
-
console.log("departments: ", JSON.stringify(departments))
|
|
276
|
-
}
|
|
277
|
-
|
|
271
|
+
|
|
278
272
|
if (settings) {
|
|
279
273
|
|
|
280
274
|
settings.wab_token = wab_token;
|
|
@@ -285,7 +279,7 @@ router.post('/update', async (req, res) => {
|
|
|
285
279
|
|
|
286
280
|
readHTMLFile('/configure.html', (err, html) => {
|
|
287
281
|
if (err) {
|
|
288
|
-
console.log("(
|
|
282
|
+
console.log("(wab) error read html file: ", err);
|
|
289
283
|
}
|
|
290
284
|
|
|
291
285
|
var template = handlebars.compile(html);
|
|
@@ -325,7 +319,7 @@ router.post('/update', async (req, res) => {
|
|
|
325
319
|
tdClient.subscribe(subscription_info).then((data) => {
|
|
326
320
|
let subscription = data;
|
|
327
321
|
if (log) {
|
|
328
|
-
console.log("\n(
|
|
322
|
+
console.log("\n(wab) Subscription: ", subscription)
|
|
329
323
|
}
|
|
330
324
|
|
|
331
325
|
let settings = {
|
|
@@ -344,7 +338,7 @@ router.post('/update', async (req, res) => {
|
|
|
344
338
|
|
|
345
339
|
readHTMLFile('/configure.html', (err, html) => {
|
|
346
340
|
if (err) {
|
|
347
|
-
console.log("(
|
|
341
|
+
console.log("(wab) error read html file: ", err);
|
|
348
342
|
}
|
|
349
343
|
|
|
350
344
|
var template = handlebars.compile(html);
|
|
@@ -368,7 +362,7 @@ router.post('/update', async (req, res) => {
|
|
|
368
362
|
|
|
369
363
|
readHTMLFile('/configure.html', (err, html) => {
|
|
370
364
|
if (err) {
|
|
371
|
-
console.log("(
|
|
365
|
+
console.log("(wab) error read html file: ", err);
|
|
372
366
|
}
|
|
373
367
|
|
|
374
368
|
var template = handlebars.compile(html);
|
|
@@ -389,9 +383,9 @@ router.post('/update', async (req, res) => {
|
|
|
389
383
|
})
|
|
390
384
|
|
|
391
385
|
router.post('/disconnect', async (req, res) => {
|
|
392
|
-
console.log("\n(
|
|
386
|
+
console.log("\n(wab) /disconnect")
|
|
393
387
|
if (log) {
|
|
394
|
-
console.log("(
|
|
388
|
+
console.log("(wab) body: ", req.body)
|
|
395
389
|
}
|
|
396
390
|
|
|
397
391
|
let projectId = req.body.project_id;
|
|
@@ -401,7 +395,7 @@ router.post('/disconnect', async (req, res) => {
|
|
|
401
395
|
|
|
402
396
|
let CONTENT_KEY = "whatsapp-" + projectId;
|
|
403
397
|
await db.remove(CONTENT_KEY);
|
|
404
|
-
console.log("(
|
|
398
|
+
console.log("(wab) Content deleted.");
|
|
405
399
|
|
|
406
400
|
let proxy_url = BASE_URL + "/webhook/" + projectId;
|
|
407
401
|
|
|
@@ -409,10 +403,7 @@ router.post('/disconnect', async (req, res) => {
|
|
|
409
403
|
// get departments
|
|
410
404
|
const tdChannel = new TiledeskChannel({ settings: { project_id: projectId, token: token }, API_URL: API_URL })
|
|
411
405
|
let departments = await tdChannel.getDepartments(token);
|
|
412
|
-
|
|
413
|
-
console.log("departments: ", JSON.stringify(departments))
|
|
414
|
-
}
|
|
415
|
-
|
|
406
|
+
|
|
416
407
|
/*
|
|
417
408
|
// callback
|
|
418
409
|
tdClient.unsubsribe(projectId, subscriptionId, (err, data) => {
|
|
@@ -425,7 +416,7 @@ router.post('/disconnect', async (req, res) => {
|
|
|
425
416
|
readHTMLFile('/configure.html', (err, html) => {
|
|
426
417
|
|
|
427
418
|
if (err) {
|
|
428
|
-
console.log("(
|
|
419
|
+
console.log("(wab) error read html file: ", err);
|
|
429
420
|
}
|
|
430
421
|
|
|
431
422
|
var template = handlebars.compile(html);
|
|
@@ -441,19 +432,18 @@ router.post('/disconnect', async (req, res) => {
|
|
|
441
432
|
})
|
|
442
433
|
|
|
443
434
|
}).catch((err) => {
|
|
444
|
-
console.error("(
|
|
435
|
+
console.error("(wab) unsubscribe error: ", err);
|
|
445
436
|
})
|
|
446
437
|
|
|
447
438
|
})
|
|
448
439
|
|
|
449
440
|
router.post('/tiledesk', async (req, res) => {
|
|
450
|
-
console.log("
|
|
451
|
-
console.log("(whatsapp) /tiledesk ---> tiledeskChannelMessage: " + JSON.stringify(req.body.payload));
|
|
441
|
+
console.log("(wab) Message received from Tiledesk")
|
|
452
442
|
if (log) {
|
|
443
|
+
console.log("(wab) tiledeskChannelMessage: ", JSON.stringify(req.body.payload));
|
|
453
444
|
}
|
|
454
445
|
|
|
455
446
|
var tiledeskChannelMessage = req.body.payload;
|
|
456
|
-
|
|
457
447
|
var projectId = req.body.payload.id_project;
|
|
458
448
|
|
|
459
449
|
// get settings from mongo
|
|
@@ -462,51 +452,40 @@ router.post('/tiledesk', async (req, res) => {
|
|
|
462
452
|
let wab_token = settings.wab_token;
|
|
463
453
|
|
|
464
454
|
var text = req.body.payload.text;
|
|
465
|
-
if (log) {
|
|
466
|
-
console.log("(whatsapp) /tiledesk text: ", text);
|
|
467
|
-
}
|
|
468
|
-
|
|
469
455
|
let attributes = req.body.payload.attributes;
|
|
470
|
-
if (log) {
|
|
471
|
-
console.log("(whatsapp) /tiledesk attributes: ", JSON.stringify(attributes))
|
|
472
|
-
}
|
|
473
|
-
|
|
474
456
|
let commands;
|
|
475
457
|
if (attributes && attributes.commands) {
|
|
476
458
|
commands = attributes.commands;
|
|
477
459
|
}
|
|
478
460
|
|
|
479
461
|
var sender_id = req.body.payload.sender;
|
|
480
|
-
console.log("(whatsapp) /tiledesk sender_id: ", sender_id);
|
|
481
462
|
|
|
482
463
|
if (sender_id.indexOf("wab") > -1) {
|
|
483
|
-
console.log("Skip same sender");
|
|
484
|
-
return res.
|
|
464
|
+
console.log("(wab) Skip same sender");
|
|
465
|
+
return res.sendStatus(200);
|
|
485
466
|
}
|
|
486
467
|
|
|
487
468
|
if (attributes && attributes.subtype === "info") {
|
|
488
|
-
console.log("(
|
|
489
|
-
return res.
|
|
469
|
+
console.log("(wab) Skip subtype (info)");
|
|
470
|
+
return res.sendStatus(200);
|
|
490
471
|
}
|
|
491
472
|
|
|
492
473
|
if (attributes && attributes.subtype === 'info/support') {
|
|
493
|
-
console.log("(
|
|
494
|
-
return res.
|
|
474
|
+
console.log("(wab) Skip subtype: ", attributes.subtype);
|
|
475
|
+
return res.sendStatus(200);
|
|
495
476
|
}
|
|
496
477
|
|
|
497
478
|
let recipient_id = tiledeskChannelMessage.recipient;
|
|
498
|
-
if (log) {
|
|
499
|
-
console.log("(whatsapp) /tiledesk Recipient_id: ", recipient_id);
|
|
500
|
-
}
|
|
501
|
-
|
|
502
479
|
let whatsapp_receiver = recipient_id.substring(recipient_id.lastIndexOf("-") + 1);
|
|
503
|
-
if (log) {
|
|
504
|
-
console.log("(whatsapp) /tiledesk whatsapp_receiver: ", whatsapp_receiver);
|
|
505
|
-
}
|
|
506
|
-
|
|
507
480
|
let phone_number_id = recipient_id.substring(recipient_id.lastIndexOf("wab-") + 4, recipient_id.lastIndexOf("-"));
|
|
481
|
+
|
|
508
482
|
if (log) {
|
|
509
|
-
console.log("(
|
|
483
|
+
console.log("(wab) text: ", text);
|
|
484
|
+
console.log("(wab) attributes: ", JSON.stringify(attributes))
|
|
485
|
+
console.log("(wab) tiledesk sender_id: ", sender_id);
|
|
486
|
+
console.log("(wab) recipient_id: ", recipient_id);
|
|
487
|
+
console.log("(wab) whatsapp_receiver: ", whatsapp_receiver);
|
|
488
|
+
console.log("(wab) phone_number_id: ", phone_number_id)
|
|
510
489
|
}
|
|
511
490
|
|
|
512
491
|
const messageHandler = new MessageHandler({ tiledeskChannelMessage: tiledeskChannelMessage });
|
|
@@ -518,25 +497,28 @@ router.post('/tiledesk', async (req, res) => {
|
|
|
518
497
|
// message
|
|
519
498
|
if (command.type === "message") {
|
|
520
499
|
let tiledeskCommandMessage = await messageHandler.generateMessageObject(command);
|
|
521
|
-
|
|
500
|
+
if (log) {
|
|
501
|
+
console.log("(wab) message generated from commanf: ", JSON.stringify(tiledeskCommandMessage))
|
|
502
|
+
}
|
|
522
503
|
|
|
523
504
|
let whatsappJsonMessage = await tlr.toWhatsapp(tiledeskCommandMessage, whatsapp_receiver);
|
|
524
|
-
console.log("🟢 whatsappJsonMessage", JSON.stringify(whatsappJsonMessage))
|
|
505
|
+
console.log("(wab) 🟢 whatsappJsonMessage", JSON.stringify(whatsappJsonMessage))
|
|
525
506
|
|
|
526
507
|
if (whatsappJsonMessage) {
|
|
527
508
|
const twClient = new TiledeskWhatsapp({ token: settings.wab_token, GRAPH_URL: GRAPH_URL });
|
|
528
509
|
twClient.sendMessage(phone_number_id, whatsappJsonMessage).then((response) => {
|
|
510
|
+
console.log("(wab) Message sent to WhatsApp! ", response.status, response.statusText);
|
|
529
511
|
i += 1;
|
|
530
512
|
if (i < commands.length) {
|
|
531
513
|
execute(commands[i]);
|
|
532
514
|
} else {
|
|
533
|
-
console.log("(
|
|
515
|
+
console.log("(wab) End of commands")
|
|
534
516
|
}
|
|
535
517
|
}).catch((err) => {
|
|
536
|
-
console.error("(
|
|
518
|
+
console.error("(wab) send message error: ", err);
|
|
537
519
|
})
|
|
538
520
|
} else {
|
|
539
|
-
console.error("(
|
|
521
|
+
console.error("(wab) WhatsappJsonMessage is undefined!")
|
|
540
522
|
}
|
|
541
523
|
|
|
542
524
|
}
|
|
@@ -548,7 +530,7 @@ router.post('/tiledesk', async (req, res) => {
|
|
|
548
530
|
if (i < commands.length) {
|
|
549
531
|
execute(commands[i]);
|
|
550
532
|
} else {
|
|
551
|
-
console.log("(
|
|
533
|
+
console.log("(wab) End of commands")
|
|
552
534
|
}
|
|
553
535
|
}, command.time)
|
|
554
536
|
}
|
|
@@ -559,23 +541,23 @@ router.post('/tiledesk', async (req, res) => {
|
|
|
559
541
|
else if (tiledeskChannelMessage.text || tiledeskChannelMessage.metadata) {
|
|
560
542
|
|
|
561
543
|
let whatsappJsonMessage = await tlr.toWhatsapp(tiledeskChannelMessage, whatsapp_receiver);
|
|
562
|
-
console.log("🟢 whatsappJsonMessage", JSON.stringify(whatsappJsonMessage))
|
|
544
|
+
console.log("(wab) 🟢 whatsappJsonMessage", JSON.stringify(whatsappJsonMessage))
|
|
563
545
|
|
|
564
546
|
if (whatsappJsonMessage) {
|
|
565
547
|
const twClient = new TiledeskWhatsapp({ token: settings.wab_token, GRAPH_URL: GRAPH_URL });
|
|
566
548
|
|
|
567
549
|
twClient.sendMessage(phone_number_id, whatsappJsonMessage).then((response) => {
|
|
568
|
-
console.log("(
|
|
550
|
+
console.log("(wab) Message sent to WhatsApp! ", response.status, response.statusText);
|
|
569
551
|
}).catch((err) => {
|
|
570
|
-
console.error("(
|
|
552
|
+
console.error("(wab) error send message: ", err);
|
|
571
553
|
})
|
|
572
554
|
|
|
573
555
|
} else {
|
|
574
|
-
console.error("(
|
|
556
|
+
console.error("(wab) Whatsapp Json Message is undefined!")
|
|
575
557
|
}
|
|
576
558
|
|
|
577
559
|
} else {
|
|
578
|
-
console.log("(
|
|
560
|
+
console.log("(wab) no command, no text --> skip")
|
|
579
561
|
}
|
|
580
562
|
|
|
581
563
|
return res.send(200);
|
|
@@ -601,10 +583,13 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
601
583
|
req.body.entry[0].changes[0].value.messages[0]
|
|
602
584
|
) {
|
|
603
585
|
|
|
604
|
-
console.log("
|
|
586
|
+
console.log("(wab) Message received from WhatsApp");
|
|
587
|
+
if (log) {
|
|
588
|
+
console.log("\n(wab) body: ", JSON.stringify(req.body));
|
|
589
|
+
}
|
|
605
590
|
|
|
606
591
|
if (req.body.entry[0].changes[0].value.messages[0].type == "system") {
|
|
607
|
-
console.log("(
|
|
592
|
+
console.log("(wab) Skip system message")
|
|
608
593
|
return res.sendStatus(200);
|
|
609
594
|
}
|
|
610
595
|
|
|
@@ -613,10 +598,10 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
613
598
|
let CONTENT_KEY = "whatsapp-" + projectId;
|
|
614
599
|
let settings = await db.get(CONTENT_KEY);
|
|
615
600
|
if (log) {
|
|
616
|
-
console.log("(
|
|
601
|
+
console.log("(wab) kvdb settings: ", settings);
|
|
617
602
|
}
|
|
618
603
|
if (!settings) {
|
|
619
|
-
console.log("(
|
|
604
|
+
console.log("(wab) No settings found. Exit..");
|
|
620
605
|
return res.sendStatus(200);
|
|
621
606
|
}
|
|
622
607
|
|
|
@@ -626,25 +611,24 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
626
611
|
// Initialize conversation with chatbot
|
|
627
612
|
if (whatsappChannelMessage.text && whatsappChannelMessage.text.body.startsWith("#td")) {
|
|
628
613
|
|
|
629
|
-
console.log("\nwhatsappChannelMessage.text.body: ", whatsappChannelMessage.text.body)
|
|
630
614
|
let code = whatsappChannelMessage.text.body.split(' ')[0];
|
|
631
|
-
console.log("code: ", code)
|
|
615
|
+
console.log("(wab) test code: ", code)
|
|
632
616
|
|
|
633
617
|
const bottester = new TiledeskBotTester({project_id: projectId, redis_client: redis_client, db: db, tdChannel: tdChannel, tlr: tlr});
|
|
634
618
|
bottester.startBotConversation(req.body, code).then((result) => {
|
|
635
|
-
console.log("(
|
|
619
|
+
console.log("(wab) test conversation started");
|
|
620
|
+
if (log) {
|
|
621
|
+
console.log("(wab) result: ", result);
|
|
622
|
+
}
|
|
636
623
|
}).catch((err) => {
|
|
637
|
-
console.
|
|
624
|
+
console.error("(wab) start test onversation error: ", err);
|
|
638
625
|
})
|
|
639
626
|
|
|
640
627
|
// Standard message
|
|
641
628
|
} else {
|
|
642
629
|
|
|
643
630
|
let firstname = req.body.entry[0].changes[0].value.contacts[0].profile.name;
|
|
644
|
-
|
|
645
|
-
console.log("(whatsapp) /whatsapp firstname: ", firstname)
|
|
646
|
-
}
|
|
647
|
-
|
|
631
|
+
|
|
648
632
|
let message_info = {
|
|
649
633
|
channel: "whatsapp",
|
|
650
634
|
whatsapp: {
|
|
@@ -658,13 +642,13 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
658
642
|
let tiledeskJsonMessage;
|
|
659
643
|
|
|
660
644
|
if ((whatsappChannelMessage.type == 'text')) {
|
|
661
|
-
console.log("(
|
|
662
|
-
tiledeskJsonMessage = tlr.toTiledesk(whatsappChannelMessage, firstname);
|
|
663
|
-
|
|
645
|
+
console.log("(wab) message type: text")
|
|
646
|
+
tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname);
|
|
664
647
|
}
|
|
648
|
+
|
|
665
649
|
else if (whatsappChannelMessage.type == 'interactive') {
|
|
666
|
-
console.log("(
|
|
667
|
-
tiledeskJsonMessage = tlr.toTiledesk(whatsappChannelMessage, firstname);
|
|
650
|
+
console.log("(wab) message type: interactive")
|
|
651
|
+
tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname);
|
|
668
652
|
}
|
|
669
653
|
|
|
670
654
|
else if ((whatsappChannelMessage.type == 'image') || (whatsappChannelMessage.type == 'video') || (whatsappChannelMessage.type == 'document')) {
|
|
@@ -673,19 +657,17 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
673
657
|
|
|
674
658
|
if (whatsappChannelMessage.type == 'image') {
|
|
675
659
|
media = whatsappChannelMessage.image;
|
|
676
|
-
console.log("(whatsapp) /whatsapp media_id: ", media.id);
|
|
677
|
-
|
|
678
660
|
const filename = await util.downloadMedia(media.id);
|
|
679
661
|
if (!filename) {
|
|
680
|
-
console.log("(
|
|
662
|
+
console.log("(wab) Unable to download media with id " + media.id + ". Message not sent.");
|
|
681
663
|
return res.status(500).send({ success: false, error: "unable to download media" })
|
|
682
664
|
}
|
|
683
665
|
let file_path = path.join(__dirname, 'tmp', filename);
|
|
684
666
|
|
|
685
667
|
const image_url = await util.uploadMedia(file_path, "images");
|
|
686
|
-
console.log("(
|
|
668
|
+
console.log("(wab) image_url: ", image_url)
|
|
687
669
|
|
|
688
|
-
tiledeskJsonMessage = tlr.toTiledesk(whatsappChannelMessage, firstname, image_url);
|
|
670
|
+
tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname, image_url);
|
|
689
671
|
}
|
|
690
672
|
|
|
691
673
|
if (whatsappChannelMessage.type == 'video') {
|
|
@@ -693,16 +675,15 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
693
675
|
|
|
694
676
|
const filename = await util.downloadMedia(media.id);
|
|
695
677
|
if (!filename) {
|
|
696
|
-
console.log("(
|
|
678
|
+
console.log("(wab) Unable to download media with id " + media.id + ". Message not sent.");
|
|
697
679
|
return res.status(500).send({ success: false, error: "unable to download media" })
|
|
698
680
|
}
|
|
699
681
|
let file_path = path.join(__dirname, 'tmp', filename);
|
|
700
682
|
|
|
701
|
-
|
|
702
683
|
const media_url = await util.uploadMedia(file_path, "files");
|
|
703
|
-
console.log("(
|
|
684
|
+
console.log("(wab) media_url: ", media_url)
|
|
704
685
|
|
|
705
|
-
tiledeskJsonMessage = tlr.toTiledesk(whatsappChannelMessage, firstname, media_url);
|
|
686
|
+
tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname, media_url);
|
|
706
687
|
}
|
|
707
688
|
|
|
708
689
|
if (whatsappChannelMessage.type == 'document') {
|
|
@@ -710,33 +691,40 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
710
691
|
|
|
711
692
|
const filename = await util.downloadMedia(media.id);
|
|
712
693
|
if (!filename) {
|
|
713
|
-
console.log("(
|
|
694
|
+
console.log("(wab) Unable to download media with id " + media.id + ". Message not sent.");
|
|
714
695
|
return res.status(500).send({ success: false, error: "unable to download media" })
|
|
715
696
|
}
|
|
716
697
|
let file_path = path.join(__dirname, 'tmp', filename);
|
|
717
698
|
|
|
718
699
|
const media_url = await util.uploadMedia(file_path, "files");
|
|
719
|
-
console.log("(
|
|
700
|
+
console.log("(wab) media_url: ", media_url)
|
|
720
701
|
|
|
721
|
-
tiledeskJsonMessage = tlr.toTiledesk(whatsappChannelMessage, firstname, media_url);
|
|
702
|
+
tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname, media_url);
|
|
722
703
|
}
|
|
723
704
|
|
|
724
705
|
} else {
|
|
725
706
|
// unsupported. Try anyway to send something.
|
|
707
|
+
console.log("(wab) unsupported message")
|
|
726
708
|
}
|
|
727
709
|
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
console.log("(
|
|
710
|
+
if (tiledeskJsonMessage) {
|
|
711
|
+
console.log("(wab) 🟠 tiledeskJsonMessage: ", JSON.stringify(tiledeskJsonMessage));
|
|
712
|
+
const response = await tdChannel.send(tiledeskJsonMessage, message_info, settings.department_id);
|
|
713
|
+
console.log("(wab) Message sent to Tiledesk!")
|
|
714
|
+
if (log) {
|
|
715
|
+
console.log("(wab) response: ", response)
|
|
716
|
+
}
|
|
717
|
+
} else {
|
|
718
|
+
console.log("(wab) tiledeskJsonMessage is undefined")
|
|
732
719
|
}
|
|
733
720
|
|
|
734
721
|
}
|
|
735
722
|
}
|
|
736
723
|
res.sendStatus(200);
|
|
724
|
+
|
|
737
725
|
} else {
|
|
738
726
|
// Return a '404 Not Found' if event is not from a WhatsApp API
|
|
739
|
-
console.log("event not from whatsapp")
|
|
727
|
+
console.log("(wab) event not from whatsapp")
|
|
740
728
|
res.sendStatus(404);
|
|
741
729
|
}
|
|
742
730
|
});
|
|
@@ -748,8 +736,8 @@ router.get("/webhook/:project_id", async (req, res) => {
|
|
|
748
736
|
* UPDATE YOUR VERIFY TOKEN
|
|
749
737
|
*This will be the Verify Token value when you set up webhook
|
|
750
738
|
**/
|
|
751
|
-
console.log("
|
|
752
|
-
console.log("(
|
|
739
|
+
console.log("(wab) Verify the webhook... ");
|
|
740
|
+
console.log("(wab) req.query: ", req.query);
|
|
753
741
|
|
|
754
742
|
// Parse params from the webhook verification request
|
|
755
743
|
let mode = req.query["hub.mode"];
|
|
@@ -761,7 +749,7 @@ router.get("/webhook/:project_id", async (req, res) => {
|
|
|
761
749
|
let settings = await db.get(CONTENT_KEY);
|
|
762
750
|
|
|
763
751
|
if (!settings || !settings.verify_token) {
|
|
764
|
-
console.error("(
|
|
752
|
+
console.error("(wab) No settings found! Unable to verify token.")
|
|
765
753
|
res.sendStatus(403);
|
|
766
754
|
} else {
|
|
767
755
|
let VERIFY_TOKEN = settings.verify_token;
|
|
@@ -777,15 +765,15 @@ router.get("/webhook/:project_id", async (req, res) => {
|
|
|
777
765
|
if (mode === "subscribe" && token === VERIFY_TOKEN) {
|
|
778
766
|
|
|
779
767
|
// Respond with 200 OK and challenge token from the request
|
|
780
|
-
console.log("(
|
|
768
|
+
console.log("(wab) WEBHOOK_VERIFIED");
|
|
781
769
|
res.status(200).send(challenge);
|
|
782
770
|
} else {
|
|
783
771
|
// Responds with '403 Forbidden' if verify tokens do not match
|
|
784
|
-
console.error("(
|
|
772
|
+
console.error("(wab) mode is not 'subscribe' or token do not match");
|
|
785
773
|
res.sendStatus(403);
|
|
786
774
|
}
|
|
787
775
|
} else {
|
|
788
|
-
console.error("(
|
|
776
|
+
console.error("(wab) mode or token undefined");
|
|
789
777
|
res.status(400).send("impossible to verify the webhook: mode or token undefined.")
|
|
790
778
|
}
|
|
791
779
|
|
|
@@ -796,7 +784,8 @@ router.get("/webhook/:project_id", async (req, res) => {
|
|
|
796
784
|
|
|
797
785
|
router.post("/newtest", async (req, res) => {
|
|
798
786
|
|
|
799
|
-
console.log("(
|
|
787
|
+
console.log("(wab) initializing new test..");
|
|
788
|
+
|
|
800
789
|
let project_id = req.body.project_id;
|
|
801
790
|
let bot_id = req.body.bot_id;
|
|
802
791
|
|
|
@@ -815,10 +804,10 @@ router.post("/newtest", async (req, res) => {
|
|
|
815
804
|
await redis_client.set(key, JSON.stringify(info), 'EX', 604800);
|
|
816
805
|
redis_client.get(key, (err, value) => {
|
|
817
806
|
if (err) {
|
|
818
|
-
console.log("(
|
|
807
|
+
console.log("(wab) redis get err: ", err)
|
|
819
808
|
return res.status(500).send({success: "false", message: "Testing info could not be saved"});
|
|
820
809
|
} else {
|
|
821
|
-
console.log("
|
|
810
|
+
console.log("(wab) new test initialized with id: ", short_uid)
|
|
822
811
|
return res.status(200).send({short_uid: short_uid});
|
|
823
812
|
}
|
|
824
813
|
})
|
|
@@ -830,7 +819,7 @@ router.post("/newtest", async (req, res) => {
|
|
|
830
819
|
// *****************************
|
|
831
820
|
|
|
832
821
|
async function startApp(settings, callback) {
|
|
833
|
-
console.log("(
|
|
822
|
+
console.log("(wab) Starting Whatsapp App");
|
|
834
823
|
|
|
835
824
|
if (!settings.MONGODB_URL) {
|
|
836
825
|
throw new Error("settings.MONGODB_URL is mandatory");
|
|
@@ -840,28 +829,28 @@ async function startApp(settings, callback) {
|
|
|
840
829
|
throw new Error("settings.API_URL is mandatory");
|
|
841
830
|
} else {
|
|
842
831
|
API_URL = settings.API_URL;
|
|
843
|
-
console.log("(
|
|
832
|
+
console.log("(wab) API_URL: ", API_URL);
|
|
844
833
|
}
|
|
845
834
|
|
|
846
835
|
if (!settings.BASE_URL) {
|
|
847
836
|
throw new Error("settings.BASE_URL is mandatory");
|
|
848
837
|
} else {
|
|
849
838
|
BASE_URL = settings.BASE_URL;
|
|
850
|
-
console.log("(
|
|
839
|
+
console.log("(wab) BASE_URL: ", BASE_URL);
|
|
851
840
|
}
|
|
852
841
|
|
|
853
842
|
if (!settings.GRAPH_URL) {
|
|
854
843
|
throw new Error("settings.GRAPH_URL is mandatory");
|
|
855
844
|
} else {
|
|
856
845
|
GRAPH_URL = settings.GRAPH_URL;
|
|
857
|
-
console.log("(
|
|
846
|
+
console.log("(wab) GRAPH_URL: ", GRAPH_URL);
|
|
858
847
|
}
|
|
859
848
|
|
|
860
849
|
if (!settings.APPS_API_URL) {
|
|
861
850
|
throw new Error("settings.APPS_API_URL is mandatory");
|
|
862
851
|
} else {
|
|
863
852
|
APPS_API_URL = settings.APPS_API_URL;
|
|
864
|
-
console.log("(
|
|
853
|
+
console.log("(wab) APPS_API_URL: ", APPS_API_URL);
|
|
865
854
|
}
|
|
866
855
|
|
|
867
856
|
if (settings.REDIS_HOST && settings.REDIS_PORT) {
|
|
@@ -870,7 +859,7 @@ async function startApp(settings, callback) {
|
|
|
870
859
|
REDIS_PASSWORD = settings.REDIS_PASSWORD;
|
|
871
860
|
connectRedis();
|
|
872
861
|
} else {
|
|
873
|
-
console.log("(
|
|
862
|
+
console.log("(wab) Missing redis parameters --> Test it out on WhatsApp disabled");
|
|
874
863
|
}
|
|
875
864
|
|
|
876
865
|
if (settings.log) {
|
|
@@ -878,7 +867,7 @@ async function startApp(settings, callback) {
|
|
|
878
867
|
}
|
|
879
868
|
|
|
880
869
|
db.connect(settings.MONGODB_URL, () => {
|
|
881
|
-
console.log("(
|
|
870
|
+
console.log("(wab) KVBaseMongo successfully connected.");
|
|
882
871
|
|
|
883
872
|
if (callback) {
|
|
884
873
|
callback();
|
|
@@ -894,7 +883,7 @@ function connectRedis() {
|
|
|
894
883
|
});
|
|
895
884
|
|
|
896
885
|
redis_client.on('error', err => {
|
|
897
|
-
console.log('(
|
|
886
|
+
console.log('(wab) Connect Redis Error ' + err);
|
|
898
887
|
})
|
|
899
888
|
/*
|
|
900
889
|
redis_client.on('connect', () => {
|
|
@@ -902,7 +891,7 @@ function connectRedis() {
|
|
|
902
891
|
});
|
|
903
892
|
*/
|
|
904
893
|
redis_client.on('ready', () => {
|
|
905
|
-
console.log("(
|
|
894
|
+
console.log("(wab) Redis ready!")
|
|
906
895
|
})
|
|
907
896
|
//await redis_client.connect(); // only for v4
|
|
908
897
|
|