@tiledesk/tiledesk-whatsapp-connector 0.1.45 → 0.1.46
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 +243 -52
- package/package.json +1 -1
- package/template/configure.html +32 -1
- package/template/css/template_detail.css +261 -0
- package/template/css/templates.css +134 -0
- package/template/detail.html +9 -11
- package/template/template_detail.html +191 -0
- package/template/templates.html +117 -0
- package/tiledesk/KVBaseMongo.js +1 -1
- package/tiledesk/TemplateManager.js +64 -0
- package/tiledesk/TiledeskWhatsapp.js +1 -1
- package/tiledesk/TiledeskWhatsappTranslator.js +69 -24
- package/tmp/media-296tga.ogg +0 -0
- package/tmp/media-296tsi.ogg +0 -0
- package/tmp/media-296tuv.ogg +0 -0
package/index.js
CHANGED
|
@@ -21,6 +21,7 @@ const { TiledeskChannel } = require('./tiledesk/TiledeskChannel');
|
|
|
21
21
|
const { TiledeskAppsClient } = require('./tiledesk/TiledeskAppsClient');
|
|
22
22
|
const { MessageHandler } = require('./tiledesk/MessageHandler');
|
|
23
23
|
const { TiledeskBotTester } = require('./tiledesk/TiledeskBotTester');
|
|
24
|
+
const { TemplateManager } = require('./tiledesk/TemplateManager');
|
|
24
25
|
|
|
25
26
|
// mongo
|
|
26
27
|
const { KVBaseMongo } = require('./tiledesk/KVBaseMongo');
|
|
@@ -58,6 +59,10 @@ handlebars.registerHelper('isEqual', (a, b) => {
|
|
|
58
59
|
}
|
|
59
60
|
})
|
|
60
61
|
|
|
62
|
+
handlebars.registerHelper('json', (a) => {
|
|
63
|
+
return JSON.stringify(a);
|
|
64
|
+
})
|
|
65
|
+
|
|
61
66
|
router.get('/', async (req, res) => {
|
|
62
67
|
res.send('Welcome to Tiledesk-WhatsApp Business connector!')
|
|
63
68
|
})
|
|
@@ -73,6 +78,9 @@ router.get('/detail', async (req, res) => {
|
|
|
73
78
|
const tdChannel = new TiledeskChannel({ settings: { project_id: project_id, token: token }, API_URL: API_URL })
|
|
74
79
|
let isAvailable = await tdChannel.getProjectDetail();
|
|
75
80
|
winston.debug("(wab) app is available: ", isAvailable);
|
|
81
|
+
if (!project_id || !token || !app_id) {
|
|
82
|
+
return res.status(500).send("<p>Ops! An error occured.</p><p>Missing query params! project_id, token and app_id are required.</p>")
|
|
83
|
+
}
|
|
76
84
|
|
|
77
85
|
const appClient = new TiledeskAppsClient({ APPS_API_URL: APPS_API_URL });
|
|
78
86
|
let installation = await appClient.getInstallations(project_id, app_id);
|
|
@@ -204,6 +212,7 @@ router.get('/configure', async (req, res) => {
|
|
|
204
212
|
proxy_url: proxy_url,
|
|
205
213
|
wab_token: settings.wab_token,
|
|
206
214
|
verify_token: settings.verify_token,
|
|
215
|
+
business_account_id: settings.business_account_id,
|
|
207
216
|
subscription_id: settings.subscriptionId,
|
|
208
217
|
department_id: settings.department_id,
|
|
209
218
|
departments: departments
|
|
@@ -242,19 +251,20 @@ router.post('/update', async (req, res) => {
|
|
|
242
251
|
|
|
243
252
|
winston.verbose("(wab) /update");
|
|
244
253
|
|
|
245
|
-
let
|
|
254
|
+
let project_id = req.body.project_id;
|
|
246
255
|
let token = req.body.token;
|
|
247
256
|
let wab_token = req.body.wab_token;
|
|
248
257
|
let verify_token = req.body.verify_token;
|
|
249
258
|
let department_id = req.body.department;
|
|
259
|
+
let business_account_id = req.body.business_account_id;
|
|
250
260
|
|
|
251
|
-
let CONTENT_KEY = "whatsapp-" +
|
|
261
|
+
let CONTENT_KEY = "whatsapp-" + project_id;
|
|
252
262
|
let settings = await db.get(CONTENT_KEY);
|
|
253
263
|
|
|
254
|
-
let proxy_url = BASE_URL + "/webhook/" +
|
|
264
|
+
let proxy_url = BASE_URL + "/webhook/" + project_id;
|
|
255
265
|
|
|
256
266
|
// get departments
|
|
257
|
-
const tdChannel = new TiledeskChannel({ settings: { project_id:
|
|
267
|
+
const tdChannel = new TiledeskChannel({ settings: { project_id: project_id, token: token }, API_URL: API_URL })
|
|
258
268
|
let departments = await tdChannel.getDepartments(token);
|
|
259
269
|
|
|
260
270
|
if (settings) {
|
|
@@ -262,6 +272,7 @@ router.post('/update', async (req, res) => {
|
|
|
262
272
|
settings.wab_token = wab_token;
|
|
263
273
|
settings.verify_token = verify_token;
|
|
264
274
|
settings.department_id = department_id;
|
|
275
|
+
settings.business_account_id = business_account_id;
|
|
265
276
|
|
|
266
277
|
await db.set(CONTENT_KEY, settings);
|
|
267
278
|
|
|
@@ -269,12 +280,13 @@ router.post('/update', async (req, res) => {
|
|
|
269
280
|
var template = handlebars.compile(html);
|
|
270
281
|
var replacements = {
|
|
271
282
|
app_version: pjson.version,
|
|
272
|
-
project_id:
|
|
283
|
+
project_id: project_id,
|
|
273
284
|
token: token,
|
|
274
285
|
proxy_url: proxy_url,
|
|
275
286
|
wab_token: settings.wab_token,
|
|
276
287
|
show_success_modal: true,
|
|
277
288
|
verify_token: settings.verify_token,
|
|
289
|
+
business_account_id: settings.business_account_id,
|
|
278
290
|
subscription_id: settings.subscriptionId,
|
|
279
291
|
department_id: settings.department_id,
|
|
280
292
|
departments: departments
|
|
@@ -285,7 +297,7 @@ router.post('/update', async (req, res) => {
|
|
|
285
297
|
|
|
286
298
|
} else {
|
|
287
299
|
|
|
288
|
-
const tdClient = new TiledeskSubscriptionClient({ API_URL: API_URL, project_id:
|
|
300
|
+
const tdClient = new TiledeskSubscriptionClient({ API_URL: API_URL, project_id: project_id, token: token })
|
|
289
301
|
|
|
290
302
|
const subscription_info = {
|
|
291
303
|
target: BASE_URL + "/tiledesk",
|
|
@@ -294,7 +306,7 @@ router.post('/update', async (req, res) => {
|
|
|
294
306
|
|
|
295
307
|
/*
|
|
296
308
|
// callback
|
|
297
|
-
await tdClient.subscribe(
|
|
309
|
+
await tdClient.subscribe(project_id, data, (err, data) => {
|
|
298
310
|
// code here
|
|
299
311
|
})
|
|
300
312
|
*/
|
|
@@ -305,13 +317,14 @@ router.post('/update', async (req, res) => {
|
|
|
305
317
|
winston.debug("\n(wab) Subscription: " + subscription)
|
|
306
318
|
|
|
307
319
|
let settings = {
|
|
308
|
-
project_id:
|
|
320
|
+
project_id: project_id,
|
|
309
321
|
token: token,
|
|
310
322
|
proxy_url: proxy_url,
|
|
311
323
|
subscriptionId: subscription._id,
|
|
312
324
|
secret: subscription.secret,
|
|
313
325
|
wab_token: wab_token,
|
|
314
326
|
verify_token: verify_token,
|
|
327
|
+
business_account_id: business_account_id,
|
|
315
328
|
department_id: department_id
|
|
316
329
|
}
|
|
317
330
|
|
|
@@ -322,12 +335,13 @@ router.post('/update', async (req, res) => {
|
|
|
322
335
|
var template = handlebars.compile(html);
|
|
323
336
|
var replacements = {
|
|
324
337
|
app_version: pjson.version,
|
|
325
|
-
project_id:
|
|
338
|
+
project_id: project_id,
|
|
326
339
|
token: token,
|
|
327
340
|
proxy_url: proxy_url,
|
|
328
341
|
show_success_modal: true,
|
|
329
342
|
wab_token: settings.wab_token,
|
|
330
343
|
verify_token: settings.verify_token,
|
|
344
|
+
business_account_id: settings.business_account_id,
|
|
331
345
|
subscription_id: settings.subscriptionId,
|
|
332
346
|
department_id: settings.department_id,
|
|
333
347
|
departments: departments
|
|
@@ -342,7 +356,7 @@ router.post('/update', async (req, res) => {
|
|
|
342
356
|
var template = handlebars.compile(html);
|
|
343
357
|
var replacements = {
|
|
344
358
|
app_version: pjson.version,
|
|
345
|
-
project_id:
|
|
359
|
+
project_id: project_id,
|
|
346
360
|
token: token,
|
|
347
361
|
proxy_url: proxy_url,
|
|
348
362
|
departments: departments,
|
|
@@ -360,25 +374,25 @@ router.post('/disconnect', async (req, res) => {
|
|
|
360
374
|
|
|
361
375
|
winston.verbose("(wab) /disconnect")
|
|
362
376
|
|
|
363
|
-
let
|
|
377
|
+
let project_id = req.body.project_id;
|
|
364
378
|
let token = req.body.token;
|
|
365
379
|
let subscriptionId = req.body.subscription_id;
|
|
366
380
|
|
|
367
381
|
|
|
368
|
-
let CONTENT_KEY = "whatsapp-" +
|
|
382
|
+
let CONTENT_KEY = "whatsapp-" + project_id;
|
|
369
383
|
await db.remove(CONTENT_KEY);
|
|
370
384
|
winston.verbose("(wab) Content deleted.");
|
|
371
385
|
|
|
372
|
-
let proxy_url = BASE_URL + "/webhook/" +
|
|
386
|
+
let proxy_url = BASE_URL + "/webhook/" + project_id;
|
|
373
387
|
|
|
374
|
-
const tdClient = new TiledeskSubscriptionClient({ API_URL: API_URL, project_id:
|
|
388
|
+
const tdClient = new TiledeskSubscriptionClient({ API_URL: API_URL, project_id: project_id, token: token })
|
|
375
389
|
// get departments
|
|
376
|
-
const tdChannel = new TiledeskChannel({ settings: { project_id:
|
|
390
|
+
const tdChannel = new TiledeskChannel({ settings: { project_id: project_id, token: token }, API_URL: API_URL })
|
|
377
391
|
let departments = await tdChannel.getDepartments(token);
|
|
378
392
|
|
|
379
393
|
/*
|
|
380
394
|
// callback
|
|
381
|
-
tdClient.unsubsribe(
|
|
395
|
+
tdClient.unsubsribe(project_id, subscriptionId, (err, data) => {
|
|
382
396
|
// code here
|
|
383
397
|
})
|
|
384
398
|
*/
|
|
@@ -389,7 +403,7 @@ router.post('/disconnect', async (req, res) => {
|
|
|
389
403
|
var template = handlebars.compile(html);
|
|
390
404
|
var replacements = {
|
|
391
405
|
app_version: pjson.version,
|
|
392
|
-
project_id:
|
|
406
|
+
project_id: project_id,
|
|
393
407
|
token: token,
|
|
394
408
|
proxy_url: proxy_url,
|
|
395
409
|
departments: departments
|
|
@@ -404,15 +418,62 @@ router.post('/disconnect', async (req, res) => {
|
|
|
404
418
|
|
|
405
419
|
})
|
|
406
420
|
|
|
421
|
+
router.get('/direct/tiledesk', async (req, res) => {
|
|
422
|
+
|
|
423
|
+
winston.verbose("/direct/tiledesk")
|
|
424
|
+
|
|
425
|
+
let project_id = req.query.project_id;
|
|
426
|
+
let whatsapp_receiver = req.query.whatsapp_receiver;
|
|
427
|
+
let phone_number_id = req.query.phone_number_id;
|
|
428
|
+
|
|
429
|
+
let CONTENT_KEY = "whatsapp-" + project_id;
|
|
430
|
+
let settings = await db.get(CONTENT_KEY);
|
|
431
|
+
|
|
432
|
+
/*
|
|
433
|
+
let tiledeskChannelMessage = {
|
|
434
|
+
text: "Ciao, benvenuto sull'ambiente di sviluppo di Tiledesk! Questo messaggio serve ad aprire una nuova conversazione. Ha funzionato?"
|
|
435
|
+
}
|
|
436
|
+
*/
|
|
437
|
+
|
|
438
|
+
let tiledeskChannelMessage = {
|
|
439
|
+
text: "Sample text",
|
|
440
|
+
attributes: {
|
|
441
|
+
attachment: {
|
|
442
|
+
type: "wa_template",
|
|
443
|
+
template: {
|
|
444
|
+
language: "en_US",
|
|
445
|
+
name: "hello_world"
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
const tlr = new TiledeskWhatsappTranslator();
|
|
451
|
+
const twClient = new TiledeskWhatsapp({ token: settings.wab_token, GRAPH_URL: GRAPH_URL });
|
|
452
|
+
|
|
453
|
+
let whatsappJsonMessage = await tlr.toWhatsapp(tiledeskChannelMessage, whatsapp_receiver);
|
|
454
|
+
winston.verbose("(wab) whatsappJsonMessage", whatsappJsonMessage)
|
|
455
|
+
|
|
456
|
+
twClient.sendMessage(phone_number_id, whatsappJsonMessage).then((response) => {
|
|
457
|
+
winston.verbose("(wab) Message sent to WhatsApp! " + response.status + " " + response.statusText);
|
|
458
|
+
}).catch((err) => {
|
|
459
|
+
return res.status(400).send({success: false, error: err});
|
|
460
|
+
winston.error("(wab) error send message: " + err);
|
|
461
|
+
})
|
|
462
|
+
|
|
463
|
+
res.status(200).send("Message sent");
|
|
464
|
+
|
|
465
|
+
})
|
|
466
|
+
|
|
407
467
|
router.post('/tiledesk', async (req, res) => {
|
|
468
|
+
|
|
408
469
|
winston.verbose("(wab) Message received from Tiledesk")
|
|
409
470
|
|
|
410
471
|
var tiledeskChannelMessage = req.body.payload;
|
|
411
|
-
|
|
412
|
-
var
|
|
472
|
+
winston.verbose("(wab) tiledeskChannelMessage: ", tiledeskChannelMessage)
|
|
473
|
+
var project_id = req.body.payload.id_project;
|
|
413
474
|
|
|
414
475
|
// get settings from mongo
|
|
415
|
-
let CONTENT_KEY = "whatsapp-" +
|
|
476
|
+
let CONTENT_KEY = "whatsapp-" + project_id;
|
|
416
477
|
let settings = await db.get(CONTENT_KEY);
|
|
417
478
|
let wab_token = settings.wab_token;
|
|
418
479
|
|
|
@@ -452,35 +513,35 @@ router.post('/tiledesk', async (req, res) => {
|
|
|
452
513
|
return res.status(200).send({ success: 'false', message: "plan expired"})
|
|
453
514
|
|
|
454
515
|
*/
|
|
455
|
-
|
|
516
|
+
|
|
456
517
|
// Return an info message option
|
|
457
518
|
if (settings.expired &&
|
|
458
|
-
|
|
519
|
+
settings.expired === true) {
|
|
520
|
+
|
|
521
|
+
winston.verbose("settings expired: " + settings.expired);
|
|
522
|
+
let tiledeskJsonMessage = {
|
|
523
|
+
text: 'Expired. Upgrade Plan.',
|
|
524
|
+
sender: sender,
|
|
525
|
+
senderFullname: "System",
|
|
526
|
+
attributes: {
|
|
527
|
+
subtype: 'info'
|
|
528
|
+
},
|
|
529
|
+
channel: { name: 'whatsapp' }
|
|
530
|
+
}
|
|
531
|
+
let message_info = {
|
|
532
|
+
channel: "whatsapp",
|
|
533
|
+
whatsapp: {
|
|
534
|
+
from: whatsapp_receiver,
|
|
535
|
+
phone_number_id: phone_number_id
|
|
536
|
+
}
|
|
537
|
+
}
|
|
459
538
|
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
senderFullname: "System",
|
|
465
|
-
attributes: {
|
|
466
|
-
subtype: 'info'
|
|
467
|
-
},
|
|
468
|
-
channel: { name: 'whatsapp'}
|
|
469
|
-
}
|
|
470
|
-
let message_info = {
|
|
471
|
-
channel: "whatsapp",
|
|
472
|
-
whatsapp: {
|
|
473
|
-
from: whatsapp_receiver,
|
|
474
|
-
phone_number_id: phone_number_id
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
const tdChannel = new TiledeskChannel({ settings: settings, API_URL: API_URL })
|
|
479
|
-
const response = await tdChannel.send(tiledeskJsonMessage, message_info, settings.department_id);
|
|
480
|
-
winston.verbose("(wab) Expiration message sent to Tiledesk")
|
|
481
|
-
return res.sendStatus(200);
|
|
539
|
+
const tdChannel = new TiledeskChannel({ settings: settings, API_URL: API_URL })
|
|
540
|
+
const response = await tdChannel.send(tiledeskJsonMessage, message_info, settings.department_id);
|
|
541
|
+
winston.verbose("(wab) Expiration message sent to Tiledesk")
|
|
542
|
+
return res.sendStatus(200);
|
|
482
543
|
}
|
|
483
|
-
|
|
544
|
+
|
|
484
545
|
winston.debug("(wab) text: " + text);
|
|
485
546
|
winston.debug("(wab) attributes: " + attributes)
|
|
486
547
|
winston.debug("(wab) tiledesk sender_id: " + sender_id);
|
|
@@ -491,6 +552,7 @@ router.post('/tiledesk', async (req, res) => {
|
|
|
491
552
|
const messageHandler = new MessageHandler({ tiledeskChannelMessage: tiledeskChannelMessage });
|
|
492
553
|
const tlr = new TiledeskWhatsappTranslator();
|
|
493
554
|
|
|
555
|
+
|
|
494
556
|
if (commands) {
|
|
495
557
|
let i = 0;
|
|
496
558
|
async function execute(command) {
|
|
@@ -500,7 +562,7 @@ router.post('/tiledesk', async (req, res) => {
|
|
|
500
562
|
winston.debug("(wab) message generated from command: " + tiledeskCommandMessage)
|
|
501
563
|
|
|
502
564
|
let whatsappJsonMessage = await tlr.toWhatsapp(tiledeskCommandMessage, whatsapp_receiver);
|
|
503
|
-
winston.verbose("(wab)
|
|
565
|
+
winston.verbose("(wab) whatsappJsonMessage", whatsappJsonMessage)
|
|
504
566
|
|
|
505
567
|
if (whatsappJsonMessage) {
|
|
506
568
|
const twClient = new TiledeskWhatsapp({ token: settings.wab_token, GRAPH_URL: GRAPH_URL });
|
|
@@ -547,10 +609,12 @@ router.post('/tiledesk', async (req, res) => {
|
|
|
547
609
|
twClient.sendMessage(phone_number_id, whatsappJsonMessage).then((response) => {
|
|
548
610
|
winston.verbose("(wab) Message sent to WhatsApp! " + response.status + " " + response.statusText);
|
|
549
611
|
}).catch((err) => {
|
|
612
|
+
res.status(400).send({success: false, error: "il template non esiste"});
|
|
550
613
|
winston.error("(wab) error send message: " + err);
|
|
551
614
|
})
|
|
552
615
|
|
|
553
616
|
} else {
|
|
617
|
+
res.status(400).send({success: false, error: "il template non esiste"});
|
|
554
618
|
winston.error("(wab) Whatsapp Json Message is undefined!")
|
|
555
619
|
}
|
|
556
620
|
|
|
@@ -566,7 +630,7 @@ router.post('/tiledesk', async (req, res) => {
|
|
|
566
630
|
router.post("/webhook/:project_id", async (req, res) => {
|
|
567
631
|
|
|
568
632
|
// Parse the request body from the POST
|
|
569
|
-
let
|
|
633
|
+
let project_id = req.params.project_id;
|
|
570
634
|
winston.verbose("(wab) Message received from WhatsApp");
|
|
571
635
|
|
|
572
636
|
// Check the Incoming webhook message
|
|
@@ -587,7 +651,7 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
587
651
|
|
|
588
652
|
let whatsappChannelMessage = req.body.entry[0].changes[0].value.messages[0];
|
|
589
653
|
|
|
590
|
-
let CONTENT_KEY = "whatsapp-" +
|
|
654
|
+
let CONTENT_KEY = "whatsapp-" + project_id;
|
|
591
655
|
let settings = await db.get(CONTENT_KEY);
|
|
592
656
|
winston.debug("(wab) settings: " + settings);
|
|
593
657
|
|
|
@@ -604,7 +668,7 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
604
668
|
|
|
605
669
|
let code = whatsappChannelMessage.text.body.split(' ')[0];
|
|
606
670
|
|
|
607
|
-
const bottester = new TiledeskBotTester({ project_id:
|
|
671
|
+
const bottester = new TiledeskBotTester({ project_id: project_id, redis_client: redis_client, db: db, tdChannel: tdChannel, tlr: tlr });
|
|
608
672
|
bottester.startBotConversation(req.body, code).then((result) => {
|
|
609
673
|
winston.verbose("(wab) test conversation started");
|
|
610
674
|
winston.debug("(wab) startBotConversation result: " + result);
|
|
@@ -639,7 +703,7 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
639
703
|
tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname);
|
|
640
704
|
}
|
|
641
705
|
|
|
642
|
-
else if ((whatsappChannelMessage.type == 'image') || (whatsappChannelMessage.type == 'video') || (whatsappChannelMessage.type == 'document')) {
|
|
706
|
+
else if ((whatsappChannelMessage.type == 'image') || (whatsappChannelMessage.type == 'video') || (whatsappChannelMessage.type == 'document') || (whatsappChannelMessage.type == 'audio')) {
|
|
643
707
|
let media;
|
|
644
708
|
const util = new TiledeskWhatsapp({ token: settings.wab_token, GRAPH_URL: GRAPH_URL })
|
|
645
709
|
|
|
@@ -690,13 +754,29 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
690
754
|
tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname, media_url);
|
|
691
755
|
}
|
|
692
756
|
|
|
757
|
+
if (whatsappChannelMessage.type == 'audio') {
|
|
758
|
+
media = whatsappChannelMessage.audio;
|
|
759
|
+
|
|
760
|
+
const filename = await util.downloadMedia(media.id);
|
|
761
|
+
if (!filename) {
|
|
762
|
+
winston.debug("(wab) Unable to download media with id " + media.id + ". Message not sent.");
|
|
763
|
+
return res.status(500).send({ success: false, error: "unable to download media" })
|
|
764
|
+
}
|
|
765
|
+
let file_path = path.join(__dirname, 'tmp', filename);
|
|
766
|
+
|
|
767
|
+
const media_url = await util.uploadMedia(file_path, "files");
|
|
768
|
+
winston.debug("(wab) media_url: " + media_url)
|
|
769
|
+
|
|
770
|
+
tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname, media_url);
|
|
771
|
+
}
|
|
772
|
+
|
|
693
773
|
} else {
|
|
694
774
|
// unsupported. Try anyway to send something.
|
|
695
775
|
winston.debug("(wab) unsupported message")
|
|
696
776
|
}
|
|
697
777
|
|
|
698
778
|
if (tiledeskJsonMessage) {
|
|
699
|
-
winston.verbose("(wab)
|
|
779
|
+
winston.verbose("(wab) tiledeskJsonMessage: ", tiledeskJsonMessage);
|
|
700
780
|
const response = await tdChannel.send(tiledeskJsonMessage, message_info, settings.department_id);
|
|
701
781
|
winston.verbose("(wab) Message sent to Tiledesk!")
|
|
702
782
|
winston.debug("(wab) response: " + response)
|
|
@@ -721,7 +801,7 @@ router.get("/webhook/:project_id", async (req, res) => {
|
|
|
721
801
|
/**
|
|
722
802
|
* UPDATE YOUR VERIFY TOKEN
|
|
723
803
|
*This will be the Verify Token value when you set up webhook
|
|
724
|
-
|
|
804
|
+
*/
|
|
725
805
|
winston.verbose("(wab) Verify the webhook... ");
|
|
726
806
|
winston.debug("(wab) req.query: " + req.query);
|
|
727
807
|
|
|
@@ -794,6 +874,117 @@ router.post("/newtest", async (req, res) => {
|
|
|
794
874
|
|
|
795
875
|
})
|
|
796
876
|
|
|
877
|
+
router.get('/templates/detail', async (req, res) => {
|
|
878
|
+
|
|
879
|
+
let project_id = req.query.project_id;
|
|
880
|
+
let token = req.query.token;
|
|
881
|
+
let app_id = req.query.app_id;
|
|
882
|
+
let template_id = req.query.id_template;
|
|
883
|
+
winston.debug("get template_id: " + template_id);
|
|
884
|
+
|
|
885
|
+
let CONTENT_KEY = "whatsapp-" + project_id;
|
|
886
|
+
let settings = await db.get(CONTENT_KEY);
|
|
887
|
+
winston.debug("(wab) settings: ", settings);
|
|
888
|
+
|
|
889
|
+
if (settings) {
|
|
890
|
+
// forse non serve, comunque non si può prendere un singolo template
|
|
891
|
+
/*
|
|
892
|
+
let tm = new TemplateManager({ token: settings.wab_token, business_account_id: settings.business_account_id, GRAPH_URL: GRAPH_URL })
|
|
893
|
+
let templates_info = await tm.getTemplateNamespace();
|
|
894
|
+
let namespace = templates_info.message_template_namespace;
|
|
895
|
+
let template = await tm.getTemplateById(namespace);
|
|
896
|
+
*/
|
|
897
|
+
let tm = new TemplateManager({ token: settings.wab_token, business_account_id: settings.business_account_id, GRAPH_URL: GRAPH_URL })
|
|
898
|
+
let templates = await tm.getTemplates();
|
|
899
|
+
let template = JSON.parse(JSON.stringify(templates.data.find(t => t.id === template_id)));
|
|
900
|
+
let template_name = template.name;
|
|
901
|
+
|
|
902
|
+
let template_copy = {
|
|
903
|
+
name: template.name,
|
|
904
|
+
components: template.components,
|
|
905
|
+
language: template.language,
|
|
906
|
+
status: template.status,
|
|
907
|
+
id: template.id,
|
|
908
|
+
category: template.category
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
readHTMLFile('/template_detail.html', (err, html) => {
|
|
912
|
+
var template = handlebars.compile(html);
|
|
913
|
+
var replacements = {
|
|
914
|
+
app_version: pjson.version,
|
|
915
|
+
project_id: project_id,
|
|
916
|
+
token: token,
|
|
917
|
+
app_id: app_id,
|
|
918
|
+
name: template_name,
|
|
919
|
+
template: template_copy
|
|
920
|
+
}
|
|
921
|
+
var html = template(replacements);
|
|
922
|
+
res.send(html);
|
|
923
|
+
})
|
|
924
|
+
|
|
925
|
+
} else {
|
|
926
|
+
return res.send("whatsapp not installed on this project")
|
|
927
|
+
}
|
|
928
|
+
})
|
|
929
|
+
|
|
930
|
+
router.get("/templates/:project_id", async (req, res) => {
|
|
931
|
+
winston.verbose("(wab) /templates");
|
|
932
|
+
|
|
933
|
+
let project_id = req.params.project_id;
|
|
934
|
+
let token = req.query.token;
|
|
935
|
+
let app_id = req.query.app_id;
|
|
936
|
+
|
|
937
|
+
let CONTENT_KEY = "whatsapp-" + project_id;
|
|
938
|
+
let settings = await db.get(CONTENT_KEY);
|
|
939
|
+
winston.debug("(wab) settings: ", settings);
|
|
940
|
+
|
|
941
|
+
if (settings) {
|
|
942
|
+
let tm = new TemplateManager({ token: settings.wab_token, business_account_id: settings.business_account_id, GRAPH_URL: GRAPH_URL })
|
|
943
|
+
let templates = await tm.getTemplates();
|
|
944
|
+
|
|
945
|
+
readHTMLFile('/templates.html', (err, html) => {
|
|
946
|
+
var template = handlebars.compile(html);
|
|
947
|
+
var replacements = {
|
|
948
|
+
app_version: pjson.version,
|
|
949
|
+
project_id: project_id,
|
|
950
|
+
token: token,
|
|
951
|
+
app_id: app_id,
|
|
952
|
+
templates: templates.data
|
|
953
|
+
}
|
|
954
|
+
var html = template(replacements);
|
|
955
|
+
res.send(html);
|
|
956
|
+
})
|
|
957
|
+
} else {
|
|
958
|
+
winston.verbose("No settings found.")
|
|
959
|
+
return res.status(404).send({ success: false, error: "whatsapp not installed for the project id " + project_id });
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
})
|
|
963
|
+
|
|
964
|
+
router.get("/ext/templates/:project_id", async (req, res) => {
|
|
965
|
+
winston.verbose("(wab) /ext/templates");
|
|
966
|
+
|
|
967
|
+
let project_id = req.params.project_id;
|
|
968
|
+
|
|
969
|
+
let CONTENT_KEY = "whatsapp-" + project_id;
|
|
970
|
+
let settings = await db.get(CONTENT_KEY);
|
|
971
|
+
|
|
972
|
+
if (settings) {
|
|
973
|
+
let tm = new TemplateManager({ token: settings.wab_token, business_account_id: settings.business_account_id, GRAPH_URL: GRAPH_URL })
|
|
974
|
+
let templates = await tm.getTemplates();
|
|
975
|
+
|
|
976
|
+
if (templates) {
|
|
977
|
+
res.status(200).send(templates.data);
|
|
978
|
+
} else {
|
|
979
|
+
res.status(500).send({ success: false, code: '02', message: "a problem occurred while getting templates from whatsapp" })
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
} else {
|
|
983
|
+
res.status(400).send({ success: false, code: '01', message: "whatsapp not installed for the project_id " + project_id })
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
})
|
|
987
|
+
|
|
797
988
|
// *****************************
|
|
798
989
|
// ********* FUNCTIONS *********
|
|
799
990
|
// *****************************
|
package/package.json
CHANGED
package/template/configure.html
CHANGED
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
</script>
|
|
26
26
|
|
|
27
27
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
|
|
28
|
-
<link rel="stylesheet" href="
|
|
28
|
+
<link rel="stylesheet" href="/css/configure.css">
|
|
29
29
|
|
|
30
30
|
<style>
|
|
31
31
|
body {
|
|
@@ -87,11 +87,29 @@
|
|
|
87
87
|
{{/if}}
|
|
88
88
|
</div>
|
|
89
89
|
</div>
|
|
90
|
+
|
|
90
91
|
</div>
|
|
91
92
|
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
|
|
92
96
|
<!-- INFO BOX -->
|
|
93
97
|
<div class="info-box">
|
|
98
|
+
|
|
94
99
|
<div class="info-list">
|
|
100
|
+
<!--
|
|
101
|
+
<div style="margin-bottom:20px;">
|
|
102
|
+
{{#if wab_token}}
|
|
103
|
+
<form action="./templates/{{project_id}}" method="get">
|
|
104
|
+
<input type="hidden" name="project_id" value="{{ project_id }}" />
|
|
105
|
+
<input type="hidden" name="app_id" value="{{ app_id }}" />
|
|
106
|
+
<input type="hidden" name="token" value="{{ token }}" />
|
|
107
|
+
<input type="submit" class="btn install-button" value="Manage Templates">
|
|
108
|
+
</form>
|
|
109
|
+
{{/if}}
|
|
110
|
+
</div>
|
|
111
|
+
-->
|
|
112
|
+
|
|
95
113
|
<ul>
|
|
96
114
|
<div class="list-element">
|
|
97
115
|
<img src="https://cdn-icons-png.flaticon.com/512/1443/1443000.png" width=20 height=20 style="margin-right: 10px;"/>
|
|
@@ -202,6 +220,19 @@
|
|
|
202
220
|
</div>
|
|
203
221
|
</div>
|
|
204
222
|
|
|
223
|
+
<!-- WhatsApp Business Account ID -->
|
|
224
|
+
<div class="form-group" style="width: 522px;">
|
|
225
|
+
<label class="input-label" for="verify_token">WhatsApp Business Account ID</label>
|
|
226
|
+
<div style="display: flex; flex-direction: row; align-items: center;">
|
|
227
|
+
<input type="text" required class="form-control custom-input" name="business_account_id" value="{{ business_account_id }}" placeholder="Enter your Business Account ID">
|
|
228
|
+
<span style="margin-left: 10px;">
|
|
229
|
+
<i class="fa fa-question-circle custom-tooltip">
|
|
230
|
+
<span class="custom-tooltiptext">This is the WhatsApp Business Account ID that can be found in Whatsapp Getting Started on the Facebook Developer console.</span>
|
|
231
|
+
</i>
|
|
232
|
+
</span>
|
|
233
|
+
</div>
|
|
234
|
+
</div>
|
|
235
|
+
|
|
205
236
|
<!-- Departments -->
|
|
206
237
|
<div class="form-group" style="width: 522px;">
|
|
207
238
|
<label class="input-label" for="department">Department <span style="font-weight: normal;">(Optional)</span></label>
|