@tiledesk/tiledesk-whatsapp-connector 0.1.72 → 0.1.74

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.
Files changed (3) hide show
  1. package/index.js +97 -142
  2. package/package.json +1 -1
  3. package/routes/api.js +7 -2
package/index.js CHANGED
@@ -502,19 +502,21 @@ router.get("/direct/tiledesk", async (req, res) => {
502
502
  .then((response) => {
503
503
  winston.verbose(
504
504
  "(wab) Message sent to WhatsApp! " +
505
- response.status +
506
- " " +
507
- response.statusText
505
+ response.status +
506
+ " " +
507
+ response.statusText
508
508
  );
509
509
  })
510
510
  .catch((err) => {
511
- winston.error("(wab) error send message: ", err);
511
+ //winston.error("(wab) error send message: ", err);
512
512
  return res.status(400).send({ success: false, error: err });
513
513
  });
514
514
 
515
515
  res.status(200).send("Message sent");
516
516
  });
517
517
 
518
+
519
+ // Can be deleted?
518
520
  router.post("/tiledesk/broadcast", async (req, res) => {
519
521
  winston.verbose("(wab) Message received from Tiledesk (Broadcast)");
520
522
 
@@ -526,22 +528,18 @@ router.post("/tiledesk/broadcast", async (req, res) => {
526
528
  let settings = await db.get(CONTENT_KEY);
527
529
 
528
530
  if (!settings) {
529
- return res
530
- .status(400)
531
- .send({
532
- success: false,
533
- error: "WhatsApp is not installed for the project_id: " + project_id,
534
- });
531
+ return res.status(400).send({
532
+ success: false,
533
+ error: "WhatsApp is not installed for the project_id: " + project_id,
534
+ });
535
535
  }
536
536
 
537
537
  if (!settings.business_account_id) {
538
- return res
539
- .status(400)
540
- .send({
541
- success: false,
542
- error:
543
- "Missing parameter 'WhatsApp Business Account ID'. Please update your app.",
544
- });
538
+ return res.status(400).send({
539
+ success: false,
540
+ error:
541
+ "Missing parameter 'WhatsApp Business Account ID'. Please update your app.",
542
+ });
545
543
  }
546
544
 
547
545
  let receiver_list = req.body.receiver_list;
@@ -599,9 +597,9 @@ router.post("/tiledesk/broadcast", async (req, res) => {
599
597
  .then((response) => {
600
598
  winston.verbose(
601
599
  "(wab) Message sent to WhatsApp! " +
602
- response.status +
603
- " " +
604
- response.statusText
600
+ response.status +
601
+ " " +
602
+ response.statusText
605
603
  );
606
604
  messages_sent += 1;
607
605
  i += 1;
@@ -609,14 +607,12 @@ router.post("/tiledesk/broadcast", async (req, res) => {
609
607
  execute(receiver_list[i]);
610
608
  } else {
611
609
  winston.debug("(wab) End of list");
612
- return res
613
- .status(200)
614
- .send({
615
- success: true,
616
- message: "Broadcast terminated",
617
- messages_sent: messages_sent,
618
- errors: errors,
619
- });
610
+ return res.status(200).send({
611
+ success: true,
612
+ message: "Broadcast terminated",
613
+ messages_sent: messages_sent,
614
+ errors: errors,
615
+ });
620
616
  }
621
617
  })
622
618
  .catch((err) => {
@@ -632,14 +628,12 @@ router.post("/tiledesk/broadcast", async (req, res) => {
632
628
  execute(receiver_list[i]);
633
629
  } else {
634
630
  winston.debug("(wab) End of list");
635
- return res
636
- .status(200)
637
- .send({
638
- success: true,
639
- message: "Broadcast terminated",
640
- messages_sent: messages_sent,
641
- errors: errors,
642
- });
631
+ return res.status(200).send({
632
+ success: true,
633
+ message: "Broadcast terminated",
634
+ messages_sent: messages_sent,
635
+ errors: errors,
636
+ });
643
637
  }
644
638
  });
645
639
  }
@@ -647,84 +641,14 @@ router.post("/tiledesk/broadcast", async (req, res) => {
647
641
  }
648
642
  });
649
643
 
650
- /*
651
- router.post('/tiledesk/broadcast2', async (req, res) => {
652
-
653
- winston.verbose("(wab) Message received from Tiledesk (Broadcast)");
654
-
655
- let tiledeskChannelMessage = req.body.payload;
656
- winston.verbose("(wab) tiledeskChannelMessage: ", tiledeskChannelMessage)
657
- let project_id = req.body.payload.id_project;
658
-
659
- let CONTENT_KEY = "whatsapp-" + project_id;
660
- let settings = await db.get(CONTENT_KEY);
661
-
662
- if (!settings) {
663
- return res.status(400).send({ success: false, error: "WhatsApp is not installed for the project_id: " + project_id });
664
- }
665
-
666
- let receiver_list = req.body.receiver_list;
667
- let phone_number_id = req.body.phone_number_id;
668
-
669
- if (!tiledeskChannelMessage.attributes.attachment.template) {
670
- return res.status(400).send({ success: false, error: "Message must contain a template." })
671
- }
672
-
673
- const tlr = new TiledeskWhatsappTranslator();
674
- const twClient = new TiledeskWhatsapp({ token: settings.wab_token, GRAPH_URL: GRAPH_URL, API_URL: API_URL, BASE_FILE_URL: BASE_FILE_URL });
675
-
676
- let messages_sent = 0;
677
- let errors = [];
678
- let error_count = 0;
679
-
680
- if (receiver_list) {
681
- let i = 0;
682
- async function execute(whatsapp_receiver) {
683
-
684
- let customTiledeskJsonMessage = await tlr.sanitizeTiledeskMessage(tiledeskChannelMessage, whatsapp_receiver)
685
- winston.info("customTiledeskJsonMessage: ", customTiledeskJsonMessage)
686
-
687
- let whatsappJsonMessage = await tlr.toWhatsapp(customTiledeskJsonMessage, whatsapp_receiver.phone_number);
688
- winston.verbose("(wab) message created for receiver: " + whatsapp_receiver.phone_number);
689
- winston.verbose("(wab) whatsappJsonMessage", whatsappJsonMessage);
690
-
691
- //winston.debug("(wab) End of list")
692
- //return res.sendStatus(200);
693
-
694
- await twClient.sendMessage(phone_number_id, whatsappJsonMessage).then((response) => {
695
- winston.verbose("(wab) Message sent to WhatsApp! " + response.status + " " + response.statusText);
696
- messages_sent += 1;
697
- i += 1;
698
- if (i < receiver_list.length) {
699
- execute(receiver_list[i])
700
- } else {
701
- return res.status(200).send({ success: true, message: "Broadcast terminated", messages_sent: messages_sent, errors: errors });
702
- winston.debug("(wab) End of list")
703
- }
704
- }).catch((err) => {
705
- winston.error("(wab) error send message: " + err.response.data.error.message);
706
- errors.push({ receiver: whatsapp_receiver.phone_number, error: err.response.data.error.message });
707
- i += 1;
708
- if (i < receiver_list.length) {
709
- execute(receiver_list[i])
710
- } else {
711
- winston.debug("(wab) End of list")
712
- return res.status(200).send({ success: true, message: "Broadcast terminated", messages_sent: messages_sent, errors: errors });
713
- }
714
- })
715
- }
716
- execute(receiver_list[0]);
717
- }
718
- })
719
- */
720
-
721
644
  router.post("/tiledesk", async (req, res) => {
722
645
  winston.verbose("(wab) Message received from Tiledesk");
646
+ console.log("req.body: ", JSON.stringify(req.body));
723
647
 
724
648
  var tiledeskChannelMessage = req.body.payload;
725
649
  winston.debug("(wab) tiledeskChannelMessage: ", tiledeskChannelMessage);
726
650
  var project_id = req.body.payload.id_project;
727
-
651
+
728
652
  // get settings from mongo
729
653
  let CONTENT_KEY = "whatsapp-" + project_id;
730
654
  let settings = await db.get(CONTENT_KEY);
@@ -776,7 +700,9 @@ router.post("/tiledesk", async (req, res) => {
776
700
  }
777
701
 
778
702
  if (!phone_number_id) {
779
- return res.status(400).send({ success: false, message: "Phone number id undefined" });
703
+ return res
704
+ .status(400)
705
+ .send({ success: false, message: "Phone number id undefined" });
780
706
  }
781
707
 
782
708
  /*
@@ -861,7 +787,12 @@ router.post("/tiledesk", async (req, res) => {
861
787
  twClient
862
788
  .sendMessage(phone_number_id, whatsappJsonMessage)
863
789
  .then((response) => {
864
- winston.verbose("(wab) Message sent to WhatsApp! " + response.status + " " + response.statusText);
790
+ winston.verbose(
791
+ "(wab) Message sent to WhatsApp! " +
792
+ response.status +
793
+ " " +
794
+ response.statusText
795
+ );
865
796
  i += 1;
866
797
  if (i < commands.length) {
867
798
  execute(commands[i]);
@@ -892,7 +823,6 @@ router.post("/tiledesk", async (req, res) => {
892
823
  }
893
824
  }
894
825
  execute(commands[0]);
895
-
896
826
  } else if (tiledeskChannelMessage.text || tiledeskChannelMessage.metadata) {
897
827
  let whatsappJsonMessage = await tlr.toWhatsapp(
898
828
  tiledeskChannelMessage,
@@ -911,22 +841,38 @@ router.post("/tiledesk", async (req, res) => {
911
841
  twClient
912
842
  .sendMessage(phone_number_id, whatsappJsonMessage)
913
843
  .then((response) => {
914
- winston.verbose("(wab) Message sent to WhatsApp! " + response.status + " " + response.statusText);
844
+ winston.verbose(
845
+ "(wab) Message sent to WhatsApp! " +
846
+ response.status +
847
+ " " +
848
+ response.statusText
849
+ );
915
850
  return res.sendStatus(200);
916
851
  })
917
852
  .catch((err) => {
918
- winston.error("(wab) error send message: ", err);
919
- return res.status(400).send({ success: false, error: "Template not existing" });
853
+ //winston.error("(wab) error send message: ", err);
854
+ return res
855
+ .status(400)
856
+ .send({ success: false, error: "Template not existing" });
920
857
  });
921
858
  } else {
922
859
  winston.error("(wab) Whatsapp Json Message is undefined!");
923
- return res.status(400).send({ success: false, error: "An error occurred during message translation" });
860
+ return res
861
+ .status(400)
862
+ .send({
863
+ success: false,
864
+ error: "An error occurred during message translation",
865
+ });
924
866
  }
925
867
  } else {
926
868
  winston.debug("(wab) no command, no text --> skip");
927
- return res.sendStatus(400).send({ success: false, error: "No command or text specified. Skip message." });
869
+ return res
870
+ .sendStatus(400)
871
+ .send({
872
+ success: false,
873
+ error: "No command or text specified. Skip message.",
874
+ });
928
875
  }
929
-
930
876
  });
931
877
 
932
878
  // Endpoint for Whatsapp Business
@@ -935,15 +881,20 @@ router.post("/webhook/:project_id", async (req, res) => {
935
881
  // Parse the request body from the POST
936
882
  let project_id = req.params.project_id;
937
883
  winston.verbose("(wab) Message received from WhatsApp");
938
-
884
+
939
885
  // Check the Incoming webhook message
940
886
  // info on WhatsApp text message payload: https://developers.facebook.com/docs/whatsapp/cloud-api/webhooks/payload-examples#text-messages
941
887
  if (req.body.object) {
942
-
943
888
  let CONTENT_KEY = "whatsapp-" + project_id;
944
889
  let settings = await db.get(CONTENT_KEY);
945
890
  winston.debug("(wab) settings: ", settings);
946
-
891
+
892
+ if (!settings) {
893
+ winston.warn("(wab) settings not found for project_id: " + project_id);
894
+ winston.warn("(wab) settings not found for request: ", req.body);
895
+ return res.sendStatus(200);
896
+ }
897
+
947
898
  const tdClient = new TiledeskClient({
948
899
  projectId: project_id,
949
900
  token: settings.token,
@@ -966,9 +917,11 @@ router.post("/webhook/:project_id", async (req, res) => {
966
917
  winston.verbose("(wab) Skip system message");
967
918
  return res.sendStatus(200);
968
919
  }
969
-
920
+
970
921
  if (req.body.entry[0].id !== settings.business_account_id) {
971
- winston.verbose("(wab) Skip message with waba that not belong to Tiledesk project id");
922
+ winston.verbose(
923
+ "(wab) Skip message with waba that not belong to Tiledesk project id"
924
+ );
972
925
  return res.sendStatus(200);
973
926
  }
974
927
 
@@ -1032,7 +985,7 @@ router.post("/webhook/:project_id", async (req, res) => {
1032
985
  lastname: " ",
1033
986
  },
1034
987
  };
1035
-
988
+
1036
989
  let tiledeskJsonMessage;
1037
990
 
1038
991
  /*
@@ -1141,8 +1094,8 @@ router.post("/webhook/:project_id", async (req, res) => {
1141
1094
  if (!filename) {
1142
1095
  winston.debug(
1143
1096
  "(wab) Unable to download media with id " +
1144
- media.id +
1145
- ". Message not sent."
1097
+ media.id +
1098
+ ". Message not sent."
1146
1099
  );
1147
1100
  return res
1148
1101
  .status(500)
@@ -1167,8 +1120,8 @@ router.post("/webhook/:project_id", async (req, res) => {
1167
1120
  if (!filename) {
1168
1121
  winston.debug(
1169
1122
  "(wab) Unable to download media with id " +
1170
- media.id +
1171
- ". Message not sent."
1123
+ media.id +
1124
+ ". Message not sent."
1172
1125
  );
1173
1126
  return res
1174
1127
  .status(500)
@@ -1193,8 +1146,8 @@ router.post("/webhook/:project_id", async (req, res) => {
1193
1146
  if (!filename) {
1194
1147
  winston.debug(
1195
1148
  "(wab) Unable to download media with id " +
1196
- media.id +
1197
- ". Message not sent."
1149
+ media.id +
1150
+ ". Message not sent."
1198
1151
  );
1199
1152
  return res
1200
1153
  .status(500)
@@ -1219,8 +1172,8 @@ router.post("/webhook/:project_id", async (req, res) => {
1219
1172
  if (!filename) {
1220
1173
  winston.debug(
1221
1174
  "(wab) Unable to download media with id " +
1222
- media.id +
1223
- ". Message not sent."
1175
+ media.id +
1176
+ ". Message not sent."
1224
1177
  );
1225
1178
  return res
1226
1179
  .status(500)
@@ -1368,11 +1321,9 @@ router.post("/newtest", async (req, res) => {
1368
1321
  let key = "bottest:" + short_uid;
1369
1322
 
1370
1323
  if (!redis_client) {
1371
- return res
1372
- .status(500)
1373
- .send({
1374
- message: "Test it out on Whatsapp not available. Redis not ready.",
1375
- });
1324
+ return res.status(500).send({
1325
+ message: "Test it out on Whatsapp not available. Redis not ready.",
1326
+ });
1376
1327
  }
1377
1328
 
1378
1329
  await redis_client.set(key, JSON.stringify(info), "EX", 604800);
@@ -1446,6 +1397,7 @@ router.get("/templates/detail", async (req, res) => {
1446
1397
  }
1447
1398
  });
1448
1399
 
1400
+ /*
1449
1401
  router.get("/templates/:project_id", async (req, res) => {
1450
1402
  winston.verbose("(wab) /templates");
1451
1403
 
@@ -1487,6 +1439,7 @@ router.get("/templates/:project_id", async (req, res) => {
1487
1439
  });
1488
1440
  }
1489
1441
  });
1442
+ */
1490
1443
 
1491
1444
  /*
1492
1445
  router.get("/ext/templates/:project_id", async (req, res) => {
@@ -1607,17 +1560,19 @@ async function startApp(settings, callback) {
1607
1560
  }
1608
1561
 
1609
1562
  if (!settings.JOB_TOPIC_EXCHANGE) {
1610
- winston.info("(wab) JOB_TOPIC_EXCHANGE should be present. Using default value");
1563
+ winston.info(
1564
+ "(wab) JOB_TOPIC_EXCHANGE should be present. Using default value"
1565
+ );
1611
1566
  JOB_TOPIC_EXCHANGE = "tiledesk-whatsapp";
1612
1567
  } else {
1613
1568
  JOB_TOPIC_EXCHANGE = settings.JOB_TOPIC_EXCHANGE;
1614
1569
  winston.info("(wab) JOB_TOPIC_EXCHANGE is present");
1615
1570
  }
1616
-
1571
+
1617
1572
  if (settings.BRAND_NAME) {
1618
- BRAND_NAME = settings.BRAND_NAME
1573
+ BRAND_NAME = settings.BRAND_NAME;
1619
1574
  }
1620
-
1575
+
1621
1576
  // // For test only
1622
1577
  // if (settings.LOG_MONGODB_URL) {
1623
1578
  // /**
@@ -1655,11 +1610,11 @@ async function startApp(settings, callback) {
1655
1610
  if (callback) {
1656
1611
  callback(null);
1657
1612
  }
1658
- })
1613
+ });
1659
1614
  } else {
1660
1615
  db.connect(settings.MONGODB_URL, () => {
1661
1616
  winston.info("(wab) KVBaseMongo successfully connected.");
1662
-
1617
+
1663
1618
  if (callback) {
1664
1619
  callback(null);
1665
1620
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-whatsapp-connector",
3
- "version": "0.1.72",
3
+ "version": "0.1.74",
4
4
  "description": "Tiledesk Whatsapp connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/routes/api.js CHANGED
@@ -13,6 +13,7 @@ let API_URL = null;
13
13
  let GRAPH_URL = null;
14
14
  let BASE_FILE_URL = null;
15
15
  let AMQP_MANAGER_URL = null;
16
+ let JOB_TOPIC_EXCHANGE = null;
16
17
 
17
18
  router.get('/', async (req, res) => {
18
19
  res.status(200).send({ message: "API route works"})
@@ -78,8 +79,9 @@ router.post('/customer/io', async (req, res) => {
78
79
  })
79
80
 
80
81
  } else {
82
+ winston.error("(wab) whatsappJsonMessage is undefined");
81
83
  res.status(400).send({ success: false, error: "whatsappJsonMessage is undefined" });
82
- winston.error("(wab) error send message: ", err);
84
+
83
85
  }
84
86
  })
85
87
 
@@ -136,6 +138,7 @@ router.post('/tiledesk/broadcast', async (req, res) => {
136
138
  let phone_number_id = body.phone_number_id;
137
139
  let template = body.template;
138
140
  let transaction_id = body.transaction_id;
141
+ let broadcast = body.broadcast;
139
142
 
140
143
  if (!transaction_id) {
141
144
  transaction_id = "tiledesk-broadcast-" + Date.now();
@@ -144,6 +147,7 @@ router.post('/tiledesk/broadcast', async (req, res) => {
144
147
  let CONTENT_KEY = "whatsapp-" + project_id;
145
148
  let settings = await db.get(CONTENT_KEY);
146
149
 
150
+ console.log("Settings: ", settings);
147
151
  if (!settings) {
148
152
  return res.status(400).send({ success: false, error: "WhatsApp is not installed for the project_id: " + project_id });
149
153
  }
@@ -159,7 +163,8 @@ router.post('/tiledesk/broadcast', async (req, res) => {
159
163
  phone_number_id: phone_number_id,
160
164
  transaction_id: transaction_id,
161
165
  template: template,
162
- settings: settings
166
+ settings: settings,
167
+ broadcast: broadcast
163
168
  };
164
169
 
165
170
  winston.debug('(wab) data_To_scheduler: ', data_To_scheduler);