@tiledesk/tiledesk-whatsapp-connector 0.1.45 → 0.1.47

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 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);
@@ -114,7 +122,7 @@ router.post('/install', async (req, res) => {
114
122
 
115
123
  const appClient = new TiledeskAppsClient({ APPS_API_URL: APPS_API_URL });
116
124
  appClient.install(installation_info).then((installation) => {
117
- winston.debug("(wab) installation response: " + installation);
125
+ winston.debug("(wab) installation response: ", installation);
118
126
 
119
127
  res.redirect(url.format({
120
128
  pathname: "/detail",
@@ -142,7 +150,7 @@ router.post('/uninstall', async (req, res) => {
142
150
  const appClient = new TiledeskAppsClient({ APPS_API_URL: APPS_API_URL });
143
151
  appClient.uninstall(project_id, app_id).then((response) => {
144
152
 
145
- winston.debug("(wab) uninstallation response: " + response);
153
+ winston.debug("(wab) uninstallation response: ", response);
146
154
 
147
155
  res.redirect(url.format({
148
156
  pathname: "/detail",
@@ -186,7 +194,7 @@ router.get('/configure', async (req, res) => {
186
194
  let CONTENT_KEY = "whatsapp-" + project_id;
187
195
 
188
196
  let settings = await db.get(CONTENT_KEY);
189
- winston.debug("(wab) settings: " + settings);
197
+ winston.debug("(wab) settings: ", settings);
190
198
 
191
199
  // get departments
192
200
  const tdChannel = new TiledeskChannel({ settings: { project_id: project_id, token: token }, API_URL: API_URL })
@@ -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 projectId = req.body.project_id;
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-" + projectId;
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/" + projectId;
264
+ let proxy_url = BASE_URL + "/webhook/" + project_id;
255
265
 
256
266
  // get departments
257
- const tdChannel = new TiledeskChannel({ settings: { project_id: projectId, token: token }, API_URL: API_URL })
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: projectId,
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: projectId, token: token })
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(projectId, data, (err, data) => {
309
+ await tdClient.subscribe(project_id, data, (err, data) => {
298
310
  // code here
299
311
  })
300
312
  */
@@ -302,16 +314,17 @@ router.post('/update', async (req, res) => {
302
314
  // promise
303
315
  tdClient.subscribe(subscription_info).then((data) => {
304
316
  let subscription = data;
305
- winston.debug("\n(wab) Subscription: " + subscription)
317
+ winston.debug("\n(wab) Subscription: ", subscription)
306
318
 
307
319
  let settings = {
308
- project_id: projectId,
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: projectId,
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: projectId,
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 projectId = req.body.project_id;
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-" + projectId;
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/" + projectId;
386
+ let proxy_url = BASE_URL + "/webhook/" + project_id;
373
387
 
374
- const tdClient = new TiledeskSubscriptionClient({ API_URL: API_URL, project_id: projectId, token: token })
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: projectId, token: token }, API_URL: API_URL })
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(projectId, subscriptionId, (err, data) => {
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: projectId,
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
+ winston.error("(wab) error send message: ", err);
460
+ return res.status(400).send({success: false, error: 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
- //winston.verbose("(wab) tiledeskChannelMessage: ", tiledeskChannelMessage)
412
- var projectId = req.body.payload.id_project;
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-" + projectId;
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
- settings.expired === true) {
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
- winston.verbose("settings expired: " + settings.expired);
461
- let tiledeskJsonMessage = {
462
- text: 'Expired. Upgrade Plan.',
463
- sender: sender,
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,16 +552,17 @@ 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) {
497
559
  // message
498
560
  if (command.type === "message") {
499
561
  let tiledeskCommandMessage = await messageHandler.generateMessageObject(command);
500
- winston.debug("(wab) message generated from command: " + tiledeskCommandMessage)
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) 🟢 whatsappJsonMessage" + whatsappJsonMessage)
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 });
@@ -513,7 +575,7 @@ router.post('/tiledesk', async (req, res) => {
513
575
  winston.debug("(wab) End of commands")
514
576
  }
515
577
  }).catch((err) => {
516
- winston.error("(wab) send message error: " + err);
578
+ winston.error("(wab) send message error: ", err);
517
579
  })
518
580
  } else {
519
581
  winston.error("(wab) WhatsappJsonMessage is undefined!")
@@ -539,7 +601,7 @@ router.post('/tiledesk', async (req, res) => {
539
601
  else if (tiledeskChannelMessage.text || tiledeskChannelMessage.metadata) {
540
602
 
541
603
  let whatsappJsonMessage = await tlr.toWhatsapp(tiledeskChannelMessage, whatsapp_receiver);
542
- winston.verbose("(wab) 🟢 whatsappJsonMessage" + whatsappJsonMessage)
604
+ winston.verbose("(wab) whatsappJsonMessage", whatsappJsonMessage)
543
605
 
544
606
  if (whatsappJsonMessage) {
545
607
  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) => {
550
- winston.error("(wab) error send message: " + err);
612
+ res.status(400).send({success: false, error: "il template non esiste"});
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
 
@@ -558,7 +622,7 @@ router.post('/tiledesk', async (req, res) => {
558
622
  winston.debug("(wab) no command, no text --> skip")
559
623
  }
560
624
 
561
- return res.send(200);
625
+ return res.sendStatus(200);
562
626
  })
563
627
 
564
628
  // Endpoint for Whatsapp Business
@@ -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 projectId = req.params.project_id;
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,9 +651,9 @@ 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-" + projectId;
654
+ let CONTENT_KEY = "whatsapp-" + project_id;
591
655
  let settings = await db.get(CONTENT_KEY);
592
- winston.debug("(wab) settings: " + settings);
656
+ winston.debug("(wab) settings: ", settings);
593
657
 
594
658
  if (!settings) {
595
659
  winston.verbose("(wab) No settings found. Exit..");
@@ -604,12 +668,12 @@ 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: projectId, redis_client: redis_client, db: db, tdChannel: tdChannel, tlr: tlr });
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
- winston.debug("(wab) startBotConversation result: " + result);
674
+ winston.debug("(wab) startBotConversation result: ", result);
611
675
  }).catch((err) => {
612
- winston.error("(wab) start test onversation error: " + err);
676
+ winston.error("(wab) start test onversation error: ", err);
613
677
  })
614
678
 
615
679
  // Standard message
@@ -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,16 +754,32 @@ 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) 🟠 tiledeskJsonMessage: " + tiledeskJsonMessage);
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
- winston.debug("(wab) response: " + response)
782
+ winston.debug("(wab) response: ", response)
703
783
  } else {
704
784
  winston.verbose("(wab) tiledeskJsonMessage is undefined")
705
785
  }
@@ -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
 
@@ -784,7 +864,7 @@ router.post("/newtest", async (req, res) => {
784
864
  await redis_client.set(key, JSON.stringify(info), 'EX', 604800);
785
865
  redis_client.get(key, (err, value) => {
786
866
  if (err) {
787
- winston.error("(wab) redis get err: " + err)
867
+ winston.error("(wab) redis get err: ", err)
788
868
  return res.status(500).send({ success: "false", message: "Testing info could not be saved" });
789
869
  } else {
790
870
  winston.debug("(wab) new test initialized with id: " + short_uid)
@@ -794,6 +874,119 @@ 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
+
946
+
947
+ readHTMLFile('/templates.html', (err, html) => {
948
+ var template = handlebars.compile(html);
949
+ var replacements = {
950
+ app_version: pjson.version,
951
+ project_id: project_id,
952
+ token: token,
953
+ app_id: app_id,
954
+ templates: templates.data
955
+ }
956
+ var html = template(replacements);
957
+ res.send(html);
958
+ })
959
+ } else {
960
+ winston.verbose("No settings found.")
961
+ return res.status(404).send({ success: false, error: "whatsapp not installed for the project id " + project_id });
962
+ }
963
+
964
+ })
965
+
966
+ router.get("/ext/templates/:project_id", async (req, res) => {
967
+ winston.verbose("(wab) /ext/templates");
968
+
969
+ let project_id = req.params.project_id;
970
+
971
+ let CONTENT_KEY = "whatsapp-" + project_id;
972
+ let settings = await db.get(CONTENT_KEY);
973
+
974
+ if (settings) {
975
+ let tm = new TemplateManager({ token: settings.wab_token, business_account_id: settings.business_account_id, GRAPH_URL: GRAPH_URL })
976
+ let templates = await tm.getTemplates();
977
+
978
+ if (templates) {
979
+ res.status(200).send(templates.data);
980
+ } else {
981
+ res.status(500).send({ success: false, code: '02', message: "a problem occurred while getting templates from whatsapp" })
982
+ }
983
+
984
+ } else {
985
+ res.status(400).send({ success: false, code: '01', message: "whatsapp not installed for the project_id " + project_id })
986
+ }
987
+
988
+ })
989
+
797
990
  // *****************************
798
991
  // ********* FUNCTIONS *********
799
992
  // *****************************
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-whatsapp-connector",
3
- "version": "0.1.45",
3
+ "version": "0.1.47",
4
4
  "description": "Tiledesk Whatsapp connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -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>