@tiledesk/tiledesk-whatsapp-connector 1.0.8 → 1.0.10

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
@@ -185,7 +185,13 @@ router.get("/configure", async (req, res) => {
185
185
 
186
186
  router.get('/onboarding/callback', async (req, res) => {
187
187
  const { code, waba_id, phone_number_id, business_id } = req.query;
188
- const state = JSON.parse(req.query.state)
188
+ let state;
189
+ try {
190
+ state = JSON.parse(req.query.state)
191
+ } catch(err) {
192
+ winston.error("(wab) Error parsing query state");
193
+ return res.status(500).send({ success: false, message: "Error parsing query state" });
194
+ }
189
195
  const project_id = state.project_id;
190
196
  const token = state.token;
191
197
 
@@ -700,9 +706,11 @@ router.post("/tiledesk", async (req, res) => {
700
706
  }
701
707
  }).catch((err) => {
702
708
  winston.error("(wab) send message error: ", err.response.data);
709
+ return res.status(500).send({ success: false, error: "Send message error" });
703
710
  });
704
711
  } else {
705
712
  winston.error("(wab) WhatsappJsonMessage is undefined!");
713
+ return res.status(500).send({ success: false, error: "Translation to whatsapp message failed" });
706
714
  }
707
715
  }
708
716
 
@@ -993,105 +1001,21 @@ router.post("/webhook/:project_id", async (req, res) => {
993
1001
 
994
1002
  // Standard message
995
1003
  } else {
996
- let firstname = req.body.entry[0].changes[0].value.contacts[0].profile.name;
1004
+ const value = req.body?.entry?.[0]?.changes?.[0]?.value;
997
1005
 
998
1006
  let message_info = {
999
1007
  channel: "whatsapp",
1000
1008
  whatsapp: {
1001
- phone_number_id: req.body.entry[0].changes[0].value.metadata.phone_number_id,
1002
- from: req.body.entry[0].changes[0].value.messages[0].from,
1003
- firstname: req.body.entry[0].changes[0].value.contacts[0].profile.name,
1009
+ phone_number_id: value?.metadata?.phone_number_id || null,
1010
+ from: value?.messages?.[0]?.from || null,
1011
+ firstname: value?.contacts?.[0]?.profile?.name || "Unknown",
1004
1012
  lastname: " "
1005
1013
  },
1006
1014
  };
1007
1015
 
1008
- let tiledeskJsonMessage;
1009
-
1010
- /*
1011
- if ((whatsappChannelMessage.type == 'text')) {
1012
- winston.debug("(wab) message type: text")
1013
- tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname);
1014
- }
1015
-
1016
- else if (whatsappChannelMessage.type == 'interactive') {
1017
- winston.debug("(wab) message type: interactive")
1018
- tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname);
1019
- }
1020
-
1021
- else if ((whatsappChannelMessage.type == 'image') || (whatsappChannelMessage.type == 'video') || (whatsappChannelMessage.type == 'document') || (whatsappChannelMessage.type == 'audio')) {
1022
- let media;
1023
- const util = new TiledeskWhatsapp({ token: settings.wab_token, GRAPH_URL: GRAPH_URL, API_URL: API_URL, BASE_FILE_URL: BASE_FILE_URL })
1024
-
1025
- if (whatsappChannelMessage.type == 'image') {
1026
- media = whatsappChannelMessage.image;
1027
- const filename = await util.downloadMedia(media.id);
1028
- if (!filename) {
1029
- winston.debug("(wab) Unable to download media with id " + media.id + ". Message not sent.");
1030
- return res.status(500).send({ success: false, error: "unable to download media" })
1031
- }
1032
- let file_path = path.join(__dirname, 'tmp', filename);
1033
-
1034
- const image_url = await util.uploadMedia(file_path, "images");
1035
- winston.debug("(wab) image_url: " + image_url)
1036
-
1037
- tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname, image_url);
1038
- }
1039
-
1040
- if (whatsappChannelMessage.type == 'video') {
1041
- media = whatsappChannelMessage.video;
1042
-
1043
- const filename = await util.downloadMedia(media.id);
1044
- if (!filename) {
1045
- winston.debug("(wab) Unable to download media with id " + media.id + ". Message not sent.");
1046
- return res.status(500).send({ success: false, error: "unable to download media" })
1047
- }
1048
- let file_path = path.join(__dirname, 'tmp', filename);
1049
-
1050
- const media_url = await util.uploadMedia(file_path, "files");
1051
- winston.debug("(wab) media_url: " + media_url)
1052
-
1053
- tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname, media_url);
1054
- }
1055
-
1056
- if (whatsappChannelMessage.type == 'document') {
1057
- media = whatsappChannelMessage.document;
1058
-
1059
- const filename = await util.downloadMedia(media.id);
1060
- if (!filename) {
1061
- winston.debug("(wab) Unable to download media with id " + media.id + ". Message not sent.");
1062
- return res.status(500).send({ success: false, error: "unable to download media" })
1063
- }
1064
- let file_path = path.join(__dirname, 'tmp', filename);
1065
-
1066
- const media_url = await util.uploadMedia(file_path, "files");
1067
- winston.debug("(wab) media_url: " + media_url)
1068
-
1069
- tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname, media_url);
1070
- }
1071
-
1072
- if (whatsappChannelMessage.type == 'audio') {
1073
- media = whatsappChannelMessage.audio;
1074
-
1075
- const filename = await util.downloadMedia(media.id);
1076
- if (!filename) {
1077
- winston.debug("(wab) Unable to download media with id " + media.id + ". Message not sent.");
1078
- return res.status(500).send({ success: false, error: "unable to download media" })
1079
- }
1080
- let file_path = path.join(__dirname, 'tmp', filename);
1081
-
1082
- const media_url = await util.uploadMedia(file_path, "files");
1083
- winston.debug("(wab) media_url: " + media_url)
1084
-
1085
- tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname, media_url);
1086
- }
1016
+ let firstname = message_info.whatsapp.firstname;
1087
1017
 
1088
- } else {
1089
- // unsupported. Try anyway to send something.
1090
- winston.debug("(wab) unsupported message")
1091
- }
1092
- */
1093
-
1094
- ////////////////////////////////////////////////////
1018
+ let tiledeskJsonMessage;
1095
1019
 
1096
1020
  if (
1097
1021
  whatsappChannelMessage.type == "image" ||
@@ -1491,37 +1415,42 @@ router.get("/templates/detail", async (req, res) => {
1491
1415
  let namespace = templates_info.message_template_namespace;
1492
1416
  let template = await tm.getTemplateById(namespace);
1493
1417
  */
1494
- let tm = new TemplateManager({
1495
- token: settings.wab_token,
1496
- business_account_id: settings.business_account_id,
1497
- GRAPH_URL: GRAPH_URL,
1498
- });
1499
- let templates = await tm.getTemplates();
1500
- let template = JSON.parse(JSON.stringify(templates.data.find((t) => t.id === template_id)));
1501
- let template_name = template.name;
1502
-
1503
- let template_copy = {
1504
- name: template.name,
1505
- components: template.components,
1506
- language: template.language,
1507
- status: template.status,
1508
- id: template.id,
1509
- category: template.category,
1510
- };
1511
-
1512
- readHTMLFile("/template_detail.html", (err, html) => {
1513
- var template = handlebars.compile(html);
1514
- var replacements = {
1515
- app_version: pjson.version,
1516
- project_id: project_id,
1517
- token: token,
1518
- app_id: app_id,
1519
- name: template_name,
1520
- template: template_copy,
1521
- };
1522
- var html = template(replacements);
1523
- res.send(html);
1524
- });
1418
+ try {
1419
+ let tm = new TemplateManager({
1420
+ token: settings.wab_token,
1421
+ business_account_id: settings.business_account_id,
1422
+ GRAPH_URL: GRAPH_URL,
1423
+ });
1424
+ let templates = await tm.getTemplates();
1425
+ let template = JSON.parse(JSON.stringify(templates.data.find((t) => t.id === template_id)));
1426
+ let template_name = template.name;
1427
+
1428
+ let template_copy = {
1429
+ name: template.name,
1430
+ components: template.components,
1431
+ language: template.language,
1432
+ status: template.status,
1433
+ id: template.id,
1434
+ category: template.category,
1435
+ };
1436
+
1437
+ readHTMLFile("/template_detail.html", (err, html) => {
1438
+ var template = handlebars.compile(html);
1439
+ var replacements = {
1440
+ app_version: pjson.version,
1441
+ project_id: project_id,
1442
+ token: token,
1443
+ app_id: app_id,
1444
+ name: template_name,
1445
+ template: template_copy,
1446
+ };
1447
+ var html = template(replacements);
1448
+ res.send(html);
1449
+ });
1450
+ } catch (err) {
1451
+ winston.error("(wab) Error geting templates: ", err);
1452
+ return res.status(500).send({ success: false, error: err?.response?.data || "General error" });
1453
+ }
1525
1454
  } else {
1526
1455
  return res.send("whatsapp not installed on this project");
1527
1456
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-whatsapp-connector",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
4
4
  "description": "Tiledesk Whatsapp connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -75,7 +75,12 @@ class TiledeskBotTester {
75
75
  return;
76
76
 
77
77
  } else {
78
- test_info = JSON.parse(value)
78
+ try {
79
+ test_info = JSON.parse(value)
80
+ } catch(err) {
81
+ winston.error("(wab) Error parsing value: " + value);
82
+ reject(err);
83
+ }
79
84
  let message_info = {
80
85
  channel: "whatsapp",
81
86
  whatsapp: {
@@ -86,9 +91,9 @@ class TiledeskBotTester {
86
91
  }
87
92
  }
88
93
 
89
- let settings = await utils.getSettingsByProjectId(this.project_id);
94
+ let settings = await this.utils.getSettingsByProjectId(this.project_id);
90
95
  if (!settings) {
91
- settings = await utils.getSettings(this.project_id, null);
96
+ settings = await this.utils.getSettings(this.project_id, null);
92
97
  }
93
98
 
94
99
  if (!settings) {