@tiledesk/tiledesk-whatsapp-connector 0.1.75 → 0.1.77

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
@@ -39,11 +39,6 @@ const { KVBaseMongo } = require("./tiledesk/KVBaseMongo");
39
39
  const kvbase_collection = "kvstore";
40
40
  const db = new KVBaseMongo(kvbase_collection);
41
41
 
42
- // mongo old
43
- //const { KVBaseMongo } = require('@tiledesk/tiledesk-kvbasemongo')
44
- //const kvbase_collection = 'kvstore';
45
- //const db = new KVBaseMongo(kvbase_collection);
46
-
47
42
  // redis
48
43
  var redis = require("redis");
49
44
  var redis_client;
@@ -52,7 +47,6 @@ router.use(bodyParser.json());
52
47
  router.use(bodyParser.urlencoded({ extended: true }));
53
48
  router.use(express.static(path.join(__dirname, "template")));
54
49
  router.use(cors());
55
-
56
50
  router.use("/ext", apiRoute);
57
51
  router.use("/api", apiRoute);
58
52
 
@@ -99,11 +93,7 @@ router.get("/detail", async (req, res) => {
99
93
  let isAvailable = await tdChannel.getProjectDetail();
100
94
  winston.debug("(wab) app is available: ", isAvailable);
101
95
  if (!project_id || !token || !app_id) {
102
- return res
103
- .status(500)
104
- .send(
105
- "<p>Ops! An error occured.</p><p>Missing query params! project_id, token and app_id are required.</p>"
106
- );
96
+ return res.status(500).send("<p>Ops! An error occured.</p><p>Missing query params! project_id, token and app_id are required.</p>");
107
97
  }
108
98
 
109
99
  const appClient = new TiledeskAppsClient({ APPS_API_URL: APPS_API_URL });
@@ -136,9 +126,7 @@ router.post("/install", async (req, res) => {
136
126
  let app_id = req.body.app_id;
137
127
  let token = req.body.token;
138
128
 
139
- winston.debug(
140
- "(wab) Install app " + app_id + " for project id " + project_id
141
- );
129
+ winston.debug("(wab) Install app " + app_id + " for project id " + project_id);
142
130
  let installation_info = {
143
131
  project_id: project_id,
144
132
  app_id: app_id,
@@ -175,26 +163,22 @@ router.post("/uninstall", async (req, res) => {
175
163
  let token = req.body.token;
176
164
 
177
165
  const appClient = new TiledeskAppsClient({ APPS_API_URL: APPS_API_URL });
178
- appClient
179
- .uninstall(project_id, app_id)
180
- .then((response) => {
181
- winston.debug("(wab) uninstallation response: ", response);
182
-
183
- res.redirect(
184
- url.format({
185
- pathname: BASE_URL + "/detail",
186
- query: {
187
- project_id: project_id,
188
- app_id: app_id,
189
- token: token,
190
- },
191
- })
192
- );
193
- })
194
- .catch((err) => {
195
- winston.error("(wab) uninstallation error: " + err.data);
196
- res.send("An error occurred during the uninstallation");
197
- });
166
+ appClient.uninstall(project_id, app_id).then((response) => {
167
+ winston.debug("(wab) uninstallation response: ", response);
168
+ res.redirect(
169
+ url.format({
170
+ pathname: BASE_URL + "/detail",
171
+ query: {
172
+ project_id: project_id,
173
+ app_id: app_id,
174
+ token: token,
175
+ },
176
+ })
177
+ );
178
+ }).catch((err) => {
179
+ winston.error("(wab) uninstallation error: " + err.data);
180
+ res.send("An error occurred during the uninstallation");
181
+ });
198
182
  });
199
183
 
200
184
  router.get("/configure", async (req, res) => {
@@ -257,9 +241,7 @@ router.get("/configure", async (req, res) => {
257
241
  });
258
242
  } else {
259
243
  readHTMLFile("/configure.html", (err, html) => {
260
- if (err) {
261
- winston.error("(wab) error read html file: " + err);
262
- }
244
+ if (err) { winston.error("(wab) error read html file: " + err); }
263
245
 
264
246
  var template = handlebars.compile(html);
265
247
  var replacements = {
@@ -347,63 +329,60 @@ router.post("/update", async (req, res) => {
347
329
  */
348
330
 
349
331
  // promise
350
- tdClient
351
- .subscribe(subscription_info)
352
- .then((data) => {
353
- let subscription = data;
354
- winston.debug("\n(wab) Subscription: ", subscription);
332
+ tdClient.subscribe(subscription_info).then((data) => {
333
+ let subscription = data;
334
+ winston.debug("\n(wab) Subscription: ", subscription);
335
+
336
+ let settings = {
337
+ project_id: project_id,
338
+ token: token,
339
+ proxy_url: proxy_url,
340
+ subscriptionId: subscription._id,
341
+ secret: subscription.secret,
342
+ wab_token: wab_token,
343
+ verify_token: verify_token,
344
+ business_account_id: business_account_id,
345
+ department_id: department_id,
346
+ };
347
+
348
+ db.set(CONTENT_KEY, settings);
349
+ //let cnt = db.get(CONTENT_KEY);
355
350
 
356
- let settings = {
351
+ readHTMLFile("/configure.html", (err, html) => {
352
+ var template = handlebars.compile(html);
353
+ var replacements = {
354
+ app_version: pjson.version,
357
355
  project_id: project_id,
358
356
  token: token,
359
357
  proxy_url: proxy_url,
360
- subscriptionId: subscription._id,
361
- secret: subscription.secret,
362
- wab_token: wab_token,
363
- verify_token: verify_token,
364
- business_account_id: business_account_id,
365
- department_id: department_id,
358
+ show_success_modal: true,
359
+ wab_token: settings.wab_token,
360
+ verify_token: settings.verify_token,
361
+ business_account_id: settings.business_account_id,
362
+ subscription_id: settings.subscriptionId,
363
+ department_id: settings.department_id,
364
+ departments: departments,
365
+ brand_name: BRAND_NAME
366
366
  };
367
-
368
- db.set(CONTENT_KEY, settings);
369
- //let cnt = db.get(CONTENT_KEY);
370
-
371
- readHTMLFile("/configure.html", (err, html) => {
372
- var template = handlebars.compile(html);
373
- var replacements = {
374
- app_version: pjson.version,
375
- project_id: project_id,
376
- token: token,
377
- proxy_url: proxy_url,
378
- show_success_modal: true,
379
- wab_token: settings.wab_token,
380
- verify_token: settings.verify_token,
381
- business_account_id: settings.business_account_id,
382
- subscription_id: settings.subscriptionId,
383
- department_id: settings.department_id,
384
- departments: departments,
385
- brand_name: BRAND_NAME
386
- };
387
- var html = template(replacements);
388
- res.send(html);
389
- });
390
- })
391
- .catch((err) => {
392
- readHTMLFile("/configure.html", (err, html) => {
393
- var template = handlebars.compile(html);
394
- var replacements = {
395
- app_version: pjson.version,
396
- project_id: project_id,
397
- token: token,
398
- proxy_url: proxy_url,
399
- departments: departments,
400
- show_error_modal: true,
401
- brand_name: BRAND_NAME
402
- };
403
- var html = template(replacements);
404
- res.send(html);
405
- });
367
+ var html = template(replacements);
368
+ res.send(html);
369
+ });
370
+ }).catch((err) => {
371
+ readHTMLFile("/configure.html", (err, html) => {
372
+ var template = handlebars.compile(html);
373
+ var replacements = {
374
+ app_version: pjson.version,
375
+ project_id: project_id,
376
+ token: token,
377
+ proxy_url: proxy_url,
378
+ departments: departments,
379
+ show_error_modal: true,
380
+ brand_name: BRAND_NAME
381
+ };
382
+ var html = template(replacements);
383
+ res.send(html);
406
384
  });
385
+ });
407
386
  }
408
387
  });
409
388
 
@@ -439,26 +418,23 @@ router.post("/disconnect", async (req, res) => {
439
418
  })
440
419
  */
441
420
 
442
- tdClient
443
- .unsubscribe(subscriptionId)
444
- .then((data) => {
445
- readHTMLFile("/configure.html", (err, html) => {
446
- var template = handlebars.compile(html);
447
- var replacements = {
448
- app_version: pjson.version,
449
- project_id: project_id,
450
- token: token,
451
- proxy_url: proxy_url,
452
- departments: departments,
453
- brand_name: BRAND_NAME
454
- };
455
- var html = template(replacements);
456
- res.send(html);
457
- });
458
- })
459
- .catch((err) => {
460
- winston.error("(wab) unsubscribe error: " + err);
421
+ tdClient.unsubscribe(subscriptionId).then((data) => {
422
+ readHTMLFile("/configure.html", (err, html) => {
423
+ var template = handlebars.compile(html);
424
+ var replacements = {
425
+ app_version: pjson.version,
426
+ project_id: project_id,
427
+ token: token,
428
+ proxy_url: proxy_url,
429
+ departments: departments,
430
+ brand_name: BRAND_NAME
431
+ };
432
+ var html = template(replacements);
433
+ res.send(html);
461
434
  });
435
+ }).catch((err) => {
436
+ winston.error("(wab) unsubscribe error: " + err);
437
+ });
462
438
  });
463
439
 
464
440
  router.get("/direct/tiledesk", async (req, res) => {
@@ -479,9 +455,9 @@ router.get("/direct/tiledesk", async (req, res) => {
479
455
  template: {
480
456
  language: "en_US",
481
457
  name: "hello_world",
482
- },
483
- },
484
- },
458
+ }
459
+ }
460
+ }
485
461
  };
486
462
  const tlr = new TiledeskWhatsappTranslator();
487
463
  const twClient = new TiledeskWhatsapp({
@@ -497,20 +473,17 @@ router.get("/direct/tiledesk", async (req, res) => {
497
473
  );
498
474
  winston.verbose("(wab) whatsappJsonMessage", whatsappJsonMessage);
499
475
 
500
- twClient
501
- .sendMessage(phone_number_id, whatsappJsonMessage)
502
- .then((response) => {
503
- winston.verbose(
504
- "(wab) Message sent to WhatsApp! " +
505
- response.status +
506
- " " +
507
- response.statusText
508
- );
509
- })
510
- .catch((err) => {
511
- //winston.error("(wab) error send message: ", err);
512
- return res.status(400).send({ success: false, error: err });
513
- });
476
+ twClient.sendMessage(phone_number_id, whatsappJsonMessage).then((response) => {
477
+ winston.verbose(
478
+ "(wab) Message sent to WhatsApp! " +
479
+ response.status +
480
+ " " +
481
+ response.statusText
482
+ );
483
+ }).catch((err) => {
484
+ //winston.error("(wab) error send message: ", err);
485
+ return res.status(400).send({ success: false, error: err });
486
+ });
514
487
 
515
488
  res.status(200).send("Message sent");
516
489
  });
@@ -528,18 +501,11 @@ router.post("/tiledesk/broadcast", async (req, res) => {
528
501
  let settings = await db.get(CONTENT_KEY);
529
502
 
530
503
  if (!settings) {
531
- return res.status(400).send({
532
- success: false,
533
- error: "WhatsApp is not installed for the project_id: " + project_id,
534
- });
504
+ return res.status(400).send({ success: false, error: "WhatsApp is not installed for the project_id: " + project_id });
535
505
  }
536
506
 
537
507
  if (!settings.business_account_id) {
538
- return res.status(400).send({
539
- success: false,
540
- error:
541
- "Missing parameter 'WhatsApp Business Account ID'. Please update your app.",
542
- });
508
+ return res.status(400).send({ success: false, error: "Missing parameter 'WhatsApp Business Account ID'. Please update your app." });
543
509
  }
544
510
 
545
511
  let receiver_list = req.body.receiver_list;
@@ -577,65 +543,34 @@ router.post("/tiledesk/broadcast", async (req, res) => {
577
543
  if (receiver_list) {
578
544
  let i = 0;
579
545
  async function execute(whatsapp_receiver) {
580
- let customTiledeskJsonMessage = await tlr.sanitizeTiledeskMessage(
581
- tiledeskChannelMessage,
582
- whatsapp_receiver
583
- );
546
+ let customTiledeskJsonMessage = await tlr.sanitizeTiledeskMessage(tiledeskChannelMessage, whatsapp_receiver);
584
547
  winston.verbose("customTiledeskJsonMessage: ", customTiledeskJsonMessage);
585
548
 
586
- let whatsappJsonMessage = await tlr.toWhatsapp(
587
- customTiledeskJsonMessage,
588
- whatsapp_receiver.phone_number
589
- );
590
- winston.verbose(
591
- "(wab) message created for receiver: " + whatsapp_receiver.phone_number
592
- );
549
+ let whatsappJsonMessage = await tlr.toWhatsapp(customTiledeskJsonMessage, whatsapp_receiver.phone_number);
550
+ winston.verbose("(wab) message created for receiver: " + whatsapp_receiver.phone_number);
593
551
  winston.verbose("(wab) whatsappJsonMessage", whatsappJsonMessage);
594
552
 
595
- await twClient
596
- .sendMessage(phone_number_id, whatsappJsonMessage)
597
- .then((response) => {
598
- winston.verbose(
599
- "(wab) Message sent to WhatsApp! " +
600
- response.status +
601
- " " +
602
- response.statusText
603
- );
604
- messages_sent += 1;
605
- i += 1;
606
- if (i < receiver_list.length) {
607
- execute(receiver_list[i]);
608
- } else {
609
- winston.debug("(wab) End of list");
610
- return res.status(200).send({
611
- success: true,
612
- message: "Broadcast terminated",
613
- messages_sent: messages_sent,
614
- errors: errors,
615
- });
616
- }
617
- })
618
- .catch((err) => {
619
- winston.error(
620
- "(wab) error send message: " + err.response.data.error.message
621
- );
622
- errors.push({
623
- receiver: whatsapp_receiver.phone_number,
624
- error: err.response.data.error.message,
625
- });
626
- i += 1;
627
- if (i < receiver_list.length) {
628
- execute(receiver_list[i]);
629
- } else {
630
- winston.debug("(wab) End of list");
631
- return res.status(200).send({
632
- success: true,
633
- message: "Broadcast terminated",
634
- messages_sent: messages_sent,
635
- errors: errors,
636
- });
637
- }
638
- });
553
+ await twClient.sendMessage(phone_number_id, whatsappJsonMessage).then((response) => {
554
+ winston.verbose("(wab) Message sent to WhatsApp! " + response.status + " " + response.statusText);
555
+ messages_sent += 1;
556
+ i += 1;
557
+ if (i < receiver_list.length) {
558
+ execute(receiver_list[i]);
559
+ } else {
560
+ winston.debug("(wab) End of list");
561
+ return res.status(200).send({ success: true, message: "Broadcast terminated", messages_sent: messages_sent, errors: errors });
562
+ }
563
+ }).catch((err) => {
564
+ winston.error("(wab) error send message: " + err.response.data.error.message);
565
+ errors.push({ receiver: whatsapp_receiver.phone_number, error: err.response.data.error.message });
566
+ i += 1;
567
+ if (i < receiver_list.length) {
568
+ execute(receiver_list[i]);
569
+ } else {
570
+ winston.debug("(wab) End of list");
571
+ return res.status(200).send({ success: true, message: "Broadcast terminated", messages_sent: messages_sent, errors: errors });
572
+ }
573
+ });
639
574
  }
640
575
  execute(receiver_list[0]);
641
576
  }
@@ -684,24 +619,17 @@ router.post("/tiledesk", async (req, res) => {
684
619
 
685
620
  let recipient_id = tiledeskChannelMessage.recipient;
686
621
  let sender = tiledeskChannelMessage.sender;
687
- let whatsapp_receiver = recipient_id.substring(
688
- recipient_id.lastIndexOf("-") + 1
689
- );
622
+ let whatsapp_receiver = recipient_id.substring(recipient_id.lastIndexOf("-") + 1);
690
623
 
691
624
  let phone_number_id;
692
625
  if (attributes && attributes.whatsapp_phone_number_id) {
693
626
  phone_number_id = attributes.whatsapp_phone_number_id;
694
627
  } else {
695
- phone_number_id = recipient_id.substring(
696
- recipient_id.lastIndexOf("wab-") + 4,
697
- recipient_id.lastIndexOf("-")
698
- );
628
+ phone_number_id = recipient_id.substring(recipient_id.lastIndexOf("wab-") + 4, recipient_id.lastIndexOf("-"));
699
629
  }
700
630
 
701
631
  if (!phone_number_id) {
702
- return res
703
- .status(400)
704
- .send({ success: false, message: "Phone number id undefined" });
632
+ return res.status(400).send({ success: false, message: "Phone number id undefined" });
705
633
  }
706
634
 
707
635
  /*
@@ -757,7 +685,7 @@ router.post("/tiledesk", async (req, res) => {
757
685
  });
758
686
  const tlr = new TiledeskWhatsappTranslator();
759
687
 
760
- if (commands) {
688
+ if (commands && commands.length > 0) {
761
689
  let i = 0;
762
690
  async function execute(command) {
763
691
  // message
@@ -765,15 +693,9 @@ router.post("/tiledesk", async (req, res) => {
765
693
  let tiledeskCommandMessage = await messageHandler.generateMessageObject(
766
694
  command
767
695
  );
768
- winston.debug(
769
- "(wab) message generated from command: ",
770
- tiledeskCommandMessage
771
- );
696
+ winston.debug("(wab) message generated from command: ", tiledeskCommandMessage);
772
697
 
773
- let whatsappJsonMessage = await tlr.toWhatsapp(
774
- tiledeskCommandMessage,
775
- whatsapp_receiver
776
- );
698
+ let whatsappJsonMessage = await tlr.toWhatsapp(tiledeskCommandMessage, whatsapp_receiver);
777
699
  winston.verbose("(wab) whatsappJsonMessage", whatsappJsonMessage);
778
700
 
779
701
  if (whatsappJsonMessage) {
@@ -783,26 +705,23 @@ router.post("/tiledesk", async (req, res) => {
783
705
  API_URL: API_URL,
784
706
  BASE_FILE_URL: BASE_FILE_URL,
785
707
  });
786
- twClient
787
- .sendMessage(phone_number_id, whatsappJsonMessage)
788
- .then((response) => {
789
- winston.verbose(
790
- "(wab) Message sent to WhatsApp! " +
791
- response.status +
792
- " " +
793
- response.statusText
794
- );
795
- i += 1;
796
- if (i < commands.length) {
797
- execute(commands[i]);
798
- } else {
799
- winston.debug("(wab) End of commands");
800
- return res.sendStatus(200);
801
- }
802
- })
803
- .catch((err) => {
804
- winston.error("(wab) send message error: ", err);
805
- });
708
+ twClient.sendMessage(phone_number_id, whatsappJsonMessage).then((response) => {
709
+ winston.verbose(
710
+ "(wab) Message sent to WhatsApp! " +
711
+ response.status +
712
+ " " +
713
+ response.statusText
714
+ );
715
+ i += 1;
716
+ if (i < commands.length) {
717
+ execute(commands[i]);
718
+ } else {
719
+ winston.debug("(wab) End of commands");
720
+ return res.sendStatus(200);
721
+ }
722
+ }).catch((err) => {
723
+ winston.error("(wab) send message error: ", err);
724
+ });
806
725
  } else {
807
726
  winston.error("(wab) WhatsappJsonMessage is undefined!");
808
727
  }
@@ -837,40 +756,25 @@ router.post("/tiledesk", async (req, res) => {
837
756
  BASE_FILE_URL: BASE_FILE_URL,
838
757
  });
839
758
 
840
- twClient
841
- .sendMessage(phone_number_id, whatsappJsonMessage)
842
- .then((response) => {
843
- winston.verbose(
844
- "(wab) Message sent to WhatsApp! " +
845
- response.status +
846
- " " +
847
- response.statusText
848
- );
849
- return res.sendStatus(200);
850
- })
851
- .catch((err) => {
852
- //winston.error("(wab) error send message: ", err);
853
- return res
854
- .status(400)
855
- .send({ success: false, error: "Template not existing" });
856
- });
759
+ twClient.sendMessage(phone_number_id, whatsappJsonMessage).then((response) => {
760
+ winston.verbose(
761
+ "(wab) Message sent to WhatsApp! " +
762
+ response.status +
763
+ " " +
764
+ response.statusText
765
+ );
766
+ return res.sendStatus(200);
767
+ }).catch((err) => {
768
+ //winston.error("(wab) error send message: ", err);
769
+ return res.status(400).send({ success: false, error: "Template not existing" });
770
+ });
857
771
  } else {
858
772
  winston.error("(wab) Whatsapp Json Message is undefined!");
859
- return res
860
- .status(400)
861
- .send({
862
- success: false,
863
- error: "An error occurred during message translation",
864
- });
773
+ return res.status(400).send({ success: false, error: "An error occurred during message translation" });
865
774
  }
866
775
  } else {
867
776
  winston.debug("(wab) no command, no text --> skip");
868
- return res
869
- .sendStatus(400)
870
- .send({
871
- success: false,
872
- error: "No command or text specified. Skip message.",
873
- });
777
+ return res.status(400).send({ success: false, error: "No command or text specified. Skip message." });
874
778
  }
875
779
  });
876
780
 
@@ -918,15 +822,12 @@ router.post("/webhook/:project_id", async (req, res) => {
918
822
  }
919
823
 
920
824
  if (req.body.entry[0].id !== settings.business_account_id) {
921
- winston.verbose(
922
- "(wab) Skip message with waba that not belong to Tiledesk project id"
923
- );
825
+ winston.verbose("(wab) Skip message with waba that not belong to Tiledesk project id");
924
826
  return res.sendStatus(200);
925
827
  }
926
828
 
927
- let whatsappChannelMessage =
928
- req.body.entry[0].changes[0].value.messages[0];
929
829
 
830
+ let whatsappChannelMessage = req.body.entry[0].changes[0].value.messages[0];
930
831
  /*
931
832
  let CONTENT_KEY = "whatsapp-" + project_id;
932
833
  let settings = await db.get(CONTENT_KEY);
@@ -945,10 +846,7 @@ router.post("/webhook/:project_id", async (req, res) => {
945
846
  });
946
847
 
947
848
  // Initialize conversation with chatbot
948
- if (
949
- whatsappChannelMessage.text &&
950
- whatsappChannelMessage.text.body.startsWith("#td")
951
- ) {
849
+ if (whatsappChannelMessage.text && whatsappChannelMessage.text.body.startsWith("#td")) {
952
850
  let code = whatsappChannelMessage.text.body.split(" ")[0];
953
851
 
954
852
  const bottester = new TiledeskBotTester({
@@ -958,30 +856,24 @@ router.post("/webhook/:project_id", async (req, res) => {
958
856
  tdChannel: tdChannel,
959
857
  tlr: tlr,
960
858
  });
961
- bottester
962
- .startBotConversation(req.body, code)
963
- .then((result) => {
964
- winston.verbose("(wab) test conversation started");
965
- winston.debug("(wab) startBotConversation result: ", result);
966
- })
967
- .catch((err) => {
968
- winston.error("(wab) start test onversation error: ", err);
969
- });
859
+ bottester.startBotConversation(req.body, code).then((result) => {
860
+ winston.verbose("(wab) test conversation started");
861
+ winston.debug("(wab) startBotConversation result: ", result);
862
+ }).catch((err) => {
863
+ winston.error("(wab) start test onversation error: ", err);
864
+ });
970
865
 
971
866
  // Standard message
972
867
  } else {
973
- let firstname =
974
- req.body.entry[0].changes[0].value.contacts[0].profile.name;
868
+ let firstname = req.body.entry[0].changes[0].value.contacts[0].profile.name;
975
869
 
976
870
  let message_info = {
977
871
  channel: "whatsapp",
978
872
  whatsapp: {
979
- phone_number_id:
980
- req.body.entry[0].changes[0].value.metadata.phone_number_id,
873
+ phone_number_id: req.body.entry[0].changes[0].value.metadata.phone_number_id,
981
874
  from: req.body.entry[0].changes[0].value.messages[0].from,
982
- firstname:
983
- req.body.entry[0].changes[0].value.contacts[0].profile.name,
984
- lastname: " ",
875
+ firstname: req.body.entry[0].changes[0].value.contacts[0].profile.name,
876
+ lastname: " "
985
877
  },
986
878
  };
987
879
 
@@ -1091,25 +983,15 @@ router.post("/webhook/:project_id", async (req, res) => {
1091
983
  media = whatsappChannelMessage.image;
1092
984
  const filename = await util.downloadMedia(media.id);
1093
985
  if (!filename) {
1094
- winston.debug(
1095
- "(wab) Unable to download media with id " +
1096
- media.id +
1097
- ". Message not sent."
1098
- );
1099
- return res
1100
- .status(500)
1101
- .send({ success: false, error: "unable to download media" });
986
+ winston.debug("(wab) Unable to download media with id " + media.id + ". Message not sent.");
987
+ return res.status(500).send({ success: false, error: "unable to download media" });
1102
988
  }
1103
- let file_path = path.join(__dirname, "tmp", filename);
1104
989
 
990
+ let file_path = path.join(__dirname, "tmp", filename);
1105
991
  const image_url = await util.uploadMedia(file_path, "images");
1106
992
  winston.debug("(wab) image_url: " + image_url);
1107
993
 
1108
- tiledeskJsonMessage = await tlr.toTiledesk(
1109
- whatsappChannelMessage,
1110
- firstname,
1111
- image_url
1112
- );
994
+ tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname, image_url);
1113
995
  }
1114
996
 
1115
997
  if (whatsappChannelMessage.type == "video") {
@@ -1117,25 +999,15 @@ router.post("/webhook/:project_id", async (req, res) => {
1117
999
 
1118
1000
  const filename = await util.downloadMedia(media.id);
1119
1001
  if (!filename) {
1120
- winston.debug(
1121
- "(wab) Unable to download media with id " +
1122
- media.id +
1123
- ". Message not sent."
1124
- );
1125
- return res
1126
- .status(500)
1127
- .send({ success: false, error: "unable to download media" });
1002
+ winston.debug("(wab) Unable to download media with id " + media.id + ". Message not sent.");
1003
+ return res.status(500).send({ success: false, error: "unable to download media" });
1128
1004
  }
1129
1005
  let file_path = path.join(__dirname, "tmp", filename);
1130
1006
 
1131
1007
  const media_url = await util.uploadMedia(file_path, "files");
1132
1008
  winston.debug("(wab) media_url: " + media_url);
1133
1009
 
1134
- tiledeskJsonMessage = await tlr.toTiledesk(
1135
- whatsappChannelMessage,
1136
- firstname,
1137
- media_url
1138
- );
1010
+ tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname, media_url);
1139
1011
  }
1140
1012
 
1141
1013
  if (whatsappChannelMessage.type == "document") {
@@ -1143,25 +1015,15 @@ router.post("/webhook/:project_id", async (req, res) => {
1143
1015
 
1144
1016
  const filename = await util.downloadMedia(media.id);
1145
1017
  if (!filename) {
1146
- winston.debug(
1147
- "(wab) Unable to download media with id " +
1148
- media.id +
1149
- ". Message not sent."
1150
- );
1151
- return res
1152
- .status(500)
1153
- .send({ success: false, error: "unable to download media" });
1018
+ winston.debug("(wab) Unable to download media with id " + media.id + ". Message not sent.");
1019
+ return res.status(500).send({ success: false, error: "unable to download media" });
1154
1020
  }
1155
1021
  let file_path = path.join(__dirname, "tmp", filename);
1156
1022
 
1157
1023
  const media_url = await util.uploadMedia(file_path, "files");
1158
1024
  winston.debug("(wab) media_url: " + media_url);
1159
1025
 
1160
- tiledeskJsonMessage = await tlr.toTiledesk(
1161
- whatsappChannelMessage,
1162
- firstname,
1163
- media_url
1164
- );
1026
+ tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname, media_url);
1165
1027
  }
1166
1028
 
1167
1029
  if (whatsappChannelMessage.type == "audio") {
@@ -1169,41 +1031,24 @@ router.post("/webhook/:project_id", async (req, res) => {
1169
1031
 
1170
1032
  const filename = await util.downloadMedia(media.id);
1171
1033
  if (!filename) {
1172
- winston.debug(
1173
- "(wab) Unable to download media with id " +
1174
- media.id +
1175
- ". Message not sent."
1176
- );
1177
- return res
1178
- .status(500)
1179
- .send({ success: false, error: "unable to download media" });
1034
+ winston.debug("(wab) Unable to download media with id " + media.id + ". Message not sent.");
1035
+ return res.status(500).send({ success: false, error: "unable to download media" });
1180
1036
  }
1181
1037
  let file_path = path.join(__dirname, "tmp", filename);
1182
1038
 
1183
1039
  const media_url = await util.uploadMedia(file_path, "files");
1184
1040
  winston.debug("(wab) media_url: " + media_url);
1185
1041
 
1186
- tiledeskJsonMessage = await tlr.toTiledesk(
1187
- whatsappChannelMessage,
1188
- firstname,
1189
- media_url
1190
- );
1042
+ tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname, media_url);
1191
1043
  }
1192
1044
  } else {
1193
1045
  winston.debug("(wab) message type: ", whatsappChannelMessage.type);
1194
- tiledeskJsonMessage = await tlr.toTiledesk(
1195
- whatsappChannelMessage,
1196
- firstname
1197
- );
1046
+ tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname);
1198
1047
  }
1199
1048
 
1200
1049
  if (tiledeskJsonMessage) {
1201
1050
  winston.verbose("(wab) tiledeskJsonMessage: ", tiledeskJsonMessage);
1202
- const response = await tdChannel.send(
1203
- tiledeskJsonMessage,
1204
- message_info,
1205
- settings.department_id
1206
- );
1051
+ const response = await tdChannel.send(tiledeskJsonMessage, message_info, settings.department_id);
1207
1052
  winston.verbose("(wab) Message sent to Tiledesk!");
1208
1053
  winston.debug("(wab) response: ", response);
1209
1054
  } else {
@@ -1219,8 +1064,7 @@ router.post("/webhook/:project_id", async (req, res) => {
1219
1064
  req.body.entry[0].changes[0].value.statuses &&
1220
1065
  req.body.entry[0].changes[0].value.statuses[0]
1221
1066
  ) {
1222
- let whatsappStatusMessage =
1223
- req.body.entry[0].changes[0].value.statuses[0];
1067
+ let whatsappStatusMessage = req.body.entry[0].changes[0].value.statuses[0];
1224
1068
  winston.verbose("(wab) whatsappStatusMessage: ", whatsappStatusMessage);
1225
1069
 
1226
1070
  let message_id = whatsappStatusMessage.id;
@@ -1229,9 +1073,7 @@ router.post("/webhook/:project_id", async (req, res) => {
1229
1073
  if (whatsappStatusMessage.errors) {
1230
1074
  error = whatsappStatusMessage.errors[0].title;
1231
1075
  }
1232
- winston.verbose(
1233
- "(wab) update status in " + status + " for message_id " + message_id
1234
- );
1076
+ winston.verbose("(wab) update status in " + status + " for message_id " + message_id);
1235
1077
 
1236
1078
  /*
1237
1079
  let CONTENT_KEY = "whatsapp-" + project_id;
@@ -1298,9 +1140,7 @@ router.get("/webhook/:project_id", async (req, res) => {
1298
1140
  }
1299
1141
  } else {
1300
1142
  winston.error("(wab) mode or token undefined");
1301
- res
1302
- .status(400)
1303
- .send("impossible to verify the webhook: mode or token undefined.");
1143
+ res.status(400).send("impossible to verify the webhook: mode or token undefined.");
1304
1144
  }
1305
1145
  }
1306
1146
  });
@@ -1320,18 +1160,14 @@ router.post("/newtest", async (req, res) => {
1320
1160
  let key = "bottest:" + short_uid;
1321
1161
 
1322
1162
  if (!redis_client) {
1323
- return res.status(500).send({
1324
- message: "Test it out on Whatsapp not available. Redis not ready.",
1325
- });
1163
+ return res.status(500).send({ message: "Test it out on Whatsapp not available. Redis not ready." });
1326
1164
  }
1327
1165
 
1328
1166
  await redis_client.set(key, JSON.stringify(info), "EX", 604800);
1329
1167
  redis_client.get(key, (err, value) => {
1330
1168
  if (err) {
1331
1169
  winston.error("(wab) redis get err: ", err);
1332
- return res
1333
- .status(500)
1334
- .send({ success: "false", message: "Testing info could not be saved" });
1170
+ return res.status(500).send({ success: "false", message: "Testing info could not be saved" });
1335
1171
  } else {
1336
1172
  winston.debug("(wab) new test initialized with id: " + short_uid);
1337
1173
  return res.status(200).send({ short_uid: short_uid });
@@ -1364,9 +1200,7 @@ router.get("/templates/detail", async (req, res) => {
1364
1200
  GRAPH_URL: GRAPH_URL,
1365
1201
  });
1366
1202
  let templates = await tm.getTemplates();
1367
- let template = JSON.parse(
1368
- JSON.stringify(templates.data.find((t) => t.id === template_id))
1369
- );
1203
+ let template = JSON.parse(JSON.stringify(templates.data.find((t) => t.id === template_id)));
1370
1204
  let template_name = template.name;
1371
1205
 
1372
1206
  let template_copy = {
@@ -1546,9 +1380,7 @@ async function startApp(settings, callback) {
1546
1380
  REDIS_PASSWORD = settings.REDIS_PASSWORD;
1547
1381
  connectRedis();
1548
1382
  } else {
1549
- winston.info(
1550
- "(wab) Missing redis parameters --> Test it out on WhatsApp disabled"
1551
- );
1383
+ winston.info("(wab) Missing redis parameters --> Test it out on WhatsApp disabled");
1552
1384
  }
1553
1385
 
1554
1386
  if (!settings.AMQP_MANAGER_URL) {
@@ -1559,9 +1391,7 @@ async function startApp(settings, callback) {
1559
1391
  }
1560
1392
 
1561
1393
  if (!settings.JOB_TOPIC_EXCHANGE) {
1562
- winston.info(
1563
- "(wab) JOB_TOPIC_EXCHANGE should be present. Using default value"
1564
- );
1394
+ winston.info("(wab) JOB_TOPIC_EXCHANGE should be present. Using default value");
1565
1395
  JOB_TOPIC_EXCHANGE = "tiledesk-whatsapp";
1566
1396
  } else {
1567
1397
  JOB_TOPIC_EXCHANGE = settings.JOB_TOPIC_EXCHANGE;
@@ -1594,14 +1424,11 @@ async function startApp(settings, callback) {
1594
1424
  LOG_MONGODB_URL = settings.LOG_MONGODB_URL;
1595
1425
  }
1596
1426
 
1597
- mongoose
1598
- .connect(LOG_MONGODB_URL)
1599
- .then(() => {
1600
- winston.info("Mongoose DB Connected");
1601
- })
1602
- .catch((err) => {
1603
- winston.error("(Mongoose) Unable to connect with MongoDB ", err);
1604
- });
1427
+ mongoose.connect(LOG_MONGODB_URL).then(() => {
1428
+ winston.info("Mongoose DB Connected");
1429
+ }).catch((err) => {
1430
+ winston.error("(Mongoose) Unable to connect with MongoDB ", err);
1431
+ });
1605
1432
 
1606
1433
  if (settings.dbconnection) {
1607
1434
  db.reuseConnection(settings.dbconnection, () => {
package/logs/app.log CHANGED
@@ -603,3 +603,20 @@ info: (wab) Connect Redis Error Error: Redis connection to redis-19030.c226.eu-w
603
603
  info: (wab) Redis ready!
604
604
  info: (wab) Connect Redis Error Error: Redis connection to redis-19030.c226.eu-west-1-3.ec2.cloud.redislabs.com:19030 failed - read ETIMEDOUT
605
605
  info: (wab) Redis ready!
606
+ info: (wab) info Starting Whatsapp App
607
+ info: (wab) API_URL: https://tiledesk-server-pre.herokuapp.com
608
+ info: (wab) BASE_FILE_URL: https://tiledesk-server-pre.herokuapp.com
609
+ info: (wab) BASE_URL: https://tiledesk-whatsapp-connector-dev.glitch.me
610
+ info: (wab) GRAPH_URL: https://graph.facebook.com/v14.0/
611
+ info: (wab) APPS_API_URL: https://tiledesk-apps.herokuapp.com
612
+ info: (wab) AMQP_MANAGER_URL is present
613
+ info: (wab) JOB_TOPIC_EXCHANGE is present
614
+ info: (wab api) Starting api route
615
+ info: (wab api) GRAPH_URL: https://graph.facebook.com/v14.0/
616
+ info: (wab api) API_URL: https://tiledesk-server-pre.herokuapp.com
617
+ info: (wab api) BASE_FILE_URL: https://tiledesk-server-pre.herokuapp.com
618
+ info: (wab api) AMQP_MANAGER_URL is present
619
+ info: (wab api) JOB_TOPIC_EXCHANGE is present
620
+ info: (wab) Redis ready!
621
+ info: Mongoose DB Connected
622
+ info: (wab) KVBaseMongo successfully connected.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-whatsapp-connector",
3
- "version": "0.1.75",
3
+ "version": "0.1.77",
4
4
  "description": "Tiledesk Whatsapp connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -36,10 +36,17 @@ class WhatsappLogger {
36
36
  };
37
37
  this.tdClient.fireEvent(event, (err, result) => {
38
38
  if (err) {
39
- winston.error("(wab) An error occurred invoking an event: ", err);
39
+ if (typeof err.response.data === 'string') {
40
+ winston.error("(wab) An error occured invoking an event: " + err.response.data + ". Enable verbose log to show the full log.");
41
+ winston.verbose("(wab) An error occured invoking an event: ", err);
42
+ } else {
43
+ winston.error("(wab) An error occured invoking an event: ", err.response.data);
44
+ }
45
+
46
+ } else {
47
+ winston.verbose("(wab) Message forwarding event fired");
48
+ winston.debug("(wab) Message forwarding event fired: ", result);
40
49
  }
41
- winston.verbose("(wab) Real time log event fired");
42
- winston.debug("(wab) Real time log event fired: ", result);
43
50
  });
44
51
  }
45
52
 
@@ -52,10 +59,17 @@ class WhatsappLogger {
52
59
  };
53
60
  this.tdClient.fireEvent(event, (err, result) => {
54
61
  if (err) {
55
- winston.error("(wab) An error occured invoking an event: ", err);
62
+ if (typeof err.response.data === 'string') {
63
+ winston.error("(wab) An error occured invoking an event: " + err.response.data + ". Enable verbose log to show the full log.");
64
+ winston.verbose("(wab) An error occured invoking an event: ", err);
65
+ } else {
66
+ winston.error("(wab) An error occured invoking an event: ", err.response.data);
67
+ }
68
+
69
+ } else {
70
+ winston.verbose("(wab) Message forwarding event fired");
71
+ winston.debug("(wab) Message forwarding event fired: ", result);
56
72
  }
57
- winston.verbose("(wab) Message forwarding event fired");
58
- winston.debug("(wab) Message forwarding event fired: ", result);
59
73
  })
60
74
  }
61
75