@tiledesk/tiledesk-whatsapp-connector 0.1.52 → 0.1.53

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
@@ -497,14 +497,14 @@ router.post('/tiledesk/broadcast', async (req, res) => {
497
497
  let tm = new TemplateManager({ token: settings.wab_token, business_account_id: settings.business_account_id, GRAPH_URL: GRAPH_URL })
498
498
  const tlr = new TiledeskWhatsappTranslator();
499
499
  const twClient = new TiledeskWhatsapp({ token: settings.wab_token, GRAPH_URL: GRAPH_URL, API_URL: API_URL, BASE_FILE_URL: BASE_FILE_URL });
500
-
500
+
501
501
  let response = await tm.getTemplates();
502
502
  let templates = response.data;
503
-
503
+
504
504
  let selected_template = templates.find(t => t.name === tiledeskChannelMessage.attributes.attachment.template.name);
505
505
 
506
506
  let params_object = await tm.generateParamsObject(selected_template);
507
-
507
+
508
508
  tiledeskChannelMessage.attributes.attachment.template.params = params_object;
509
509
  winston.verbose("(wab) --> tiledeskChannelMessage: ", tiledeskChannelMessage)
510
510
 
@@ -530,8 +530,8 @@ router.post('/tiledesk/broadcast', async (req, res) => {
530
530
  if (i < receiver_list.length) {
531
531
  execute(receiver_list[i])
532
532
  } else {
533
- return res.status(200).send({ success: true, message: "Broadcast terminated", messages_sent: messages_sent, errors: errors });
534
533
  winston.debug("(wab) End of list")
534
+ return res.status(200).send({ success: true, message: "Broadcast terminated", messages_sent: messages_sent, errors: errors });
535
535
  }
536
536
  }).catch((err) => {
537
537
  winston.error("(wab) error send message: " + err.response.data.error.message);
@@ -547,7 +547,7 @@ router.post('/tiledesk/broadcast', async (req, res) => {
547
547
  }
548
548
  execute(receiver_list[0]);
549
549
  }
550
-
550
+
551
551
  })
552
552
 
553
553
 
@@ -662,7 +662,19 @@ router.post('/tiledesk', async (req, res) => {
662
662
  let recipient_id = tiledeskChannelMessage.recipient;
663
663
  let sender = tiledeskChannelMessage.sender;
664
664
  let whatsapp_receiver = recipient_id.substring(recipient_id.lastIndexOf("-") + 1);
665
- let phone_number_id = recipient_id.substring(recipient_id.lastIndexOf("wab-") + 4, recipient_id.lastIndexOf("-"));
665
+
666
+ let phone_number_id;
667
+ if (attributes && attributes.whatsapp_phone_number_id) {
668
+ phone_number_id = attributes.whatsapp_phone_number_id;
669
+ } else {
670
+ phone_number_id = recipient_id.substring(recipient_id.lastIndexOf("wab-") + 4, recipient_id.lastIndexOf("-"));
671
+ }
672
+
673
+ if (!phone_number_id) {
674
+ return res.status(400).send({ success: false, message: "Phone number id undefined"})
675
+ }
676
+
677
+ console.log("phone_number_id: ", phone_number_id)
666
678
 
667
679
  /*
668
680
  if (settings.expired &&
@@ -679,7 +691,7 @@ router.post('/tiledesk', async (req, res) => {
679
691
  winston.verbose("settings expired: " + settings.expired);
680
692
  let tiledeskJsonMessage = {
681
693
  text: 'Expired. Upgrade Plan.',
682
- sender: sender,
694
+ sender: "system",
683
695
  senderFullname: "System",
684
696
  attributes: {
685
697
  subtype: 'info'
@@ -835,7 +847,8 @@ router.post("/webhook/:project_id", async (req, res) => {
835
847
  })
836
848
 
837
849
  // Standard message
838
- } else {
850
+ }
851
+ else {
839
852
 
840
853
  let firstname = req.body.entry[0].changes[0].value.contacts[0].profile.name;
841
854
 
@@ -851,6 +864,7 @@ router.post("/webhook/:project_id", async (req, res) => {
851
864
 
852
865
  let tiledeskJsonMessage;
853
866
 
867
+ /*
854
868
  if ((whatsappChannelMessage.type == 'text')) {
855
869
  winston.debug("(wab) message type: text")
856
870
  tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname);
@@ -932,6 +946,86 @@ router.post("/webhook/:project_id", async (req, res) => {
932
946
  // unsupported. Try anyway to send something.
933
947
  winston.debug("(wab) unsupported message")
934
948
  }
949
+ */
950
+
951
+
952
+
953
+ ////////////////////////////////////////////////////
954
+
955
+
956
+
957
+ if ((whatsappChannelMessage.type == 'image') || (whatsappChannelMessage.type == 'video') || (whatsappChannelMessage.type == 'document') || (whatsappChannelMessage.type == 'audio')) {
958
+ let media;
959
+ const util = new TiledeskWhatsapp({ token: settings.wab_token, GRAPH_URL: GRAPH_URL, API_URL: API_URL, BASE_FILE_URL: BASE_FILE_URL })
960
+
961
+ if (whatsappChannelMessage.type == 'image') {
962
+ media = whatsappChannelMessage.image;
963
+ const filename = await util.downloadMedia(media.id);
964
+ if (!filename) {
965
+ winston.debug("(wab) Unable to download media with id " + media.id + ". Message not sent.");
966
+ return res.status(500).send({ success: false, error: "unable to download media" })
967
+ }
968
+ let file_path = path.join(__dirname, 'tmp', filename);
969
+
970
+ const image_url = await util.uploadMedia(file_path, "images");
971
+ winston.debug("(wab) image_url: " + image_url)
972
+
973
+ tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname, image_url);
974
+ }
975
+
976
+ if (whatsappChannelMessage.type == 'video') {
977
+ media = whatsappChannelMessage.video;
978
+
979
+ const filename = await util.downloadMedia(media.id);
980
+ if (!filename) {
981
+ winston.debug("(wab) Unable to download media with id " + media.id + ". Message not sent.");
982
+ return res.status(500).send({ success: false, error: "unable to download media" })
983
+ }
984
+ let file_path = path.join(__dirname, 'tmp', filename);
985
+
986
+ const media_url = await util.uploadMedia(file_path, "files");
987
+ winston.debug("(wab) media_url: " + media_url)
988
+
989
+ tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname, media_url);
990
+ }
991
+
992
+ if (whatsappChannelMessage.type == 'document') {
993
+ media = whatsappChannelMessage.document;
994
+
995
+ const filename = await util.downloadMedia(media.id);
996
+ if (!filename) {
997
+ winston.debug("(wab) Unable to download media with id " + media.id + ". Message not sent.");
998
+ return res.status(500).send({ success: false, error: "unable to download media" })
999
+ }
1000
+ let file_path = path.join(__dirname, 'tmp', filename);
1001
+
1002
+ const media_url = await util.uploadMedia(file_path, "files");
1003
+ winston.debug("(wab) media_url: " + media_url)
1004
+
1005
+ tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname, media_url);
1006
+ }
1007
+
1008
+ if (whatsappChannelMessage.type == 'audio') {
1009
+ media = whatsappChannelMessage.audio;
1010
+
1011
+ const filename = await util.downloadMedia(media.id);
1012
+ if (!filename) {
1013
+ winston.debug("(wab) Unable to download media with id " + media.id + ". Message not sent.");
1014
+ return res.status(500).send({ success: false, error: "unable to download media" })
1015
+ }
1016
+ let file_path = path.join(__dirname, 'tmp', filename);
1017
+
1018
+ const media_url = await util.uploadMedia(file_path, "files");
1019
+ winston.debug("(wab) media_url: " + media_url)
1020
+
1021
+ tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname, media_url);
1022
+ }
1023
+
1024
+ }
1025
+ else {
1026
+ winston.debug("(wab) message type: ", whatsappChannelMessage.type)
1027
+ tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname);
1028
+ }
935
1029
 
936
1030
  if (tiledeskJsonMessage) {
937
1031
  winston.verbose("(wab) tiledeskJsonMessage: ", tiledeskJsonMessage);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-whatsapp-connector",
3
- "version": "0.1.52",
3
+ "version": "0.1.53",
4
4
  "description": "Tiledesk Whatsapp connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/routes/api.js CHANGED
@@ -171,7 +171,7 @@ router.post('/tiledesk/broadcast', async (req, res) => {
171
171
  messages_sent += 1;
172
172
  i += 1;
173
173
  if (i < receiver_list.length) {
174
- execute(receiver_list[i])
174
+ execute(receiver_list[i])
175
175
  } else {
176
176
  winston.debug("(wab) End of list")
177
177
  return res.status(200).send({ success: true, message: "Broadcast terminated", messages_sent: messages_sent, errors: errors });
@@ -181,7 +181,7 @@ router.post('/tiledesk/broadcast', async (req, res) => {
181
181
  errors.push({ receiver: receiver.phone_number, error: err.response.data.error.message });
182
182
  i += 1;
183
183
  if (i < receiver_list.length) {
184
- execute(receiver_list[i])
184
+ execute(receiver_list[i])
185
185
  } else {
186
186
  winston.debug("(wab) End of list")
187
187
  return res.status(200).send({ success: true, message: "Broadcast terminated", messages_sent: messages_sent, errors: errors });
@@ -196,38 +196,38 @@ router.post('/tiledesk/broadcast', async (req, res) => {
196
196
 
197
197
  // start api route from whatsappRoute
198
198
  async function startRoute(settings, callback) {
199
- winston.info("(wab) Starting api route", settings);
199
+ winston.info("(wab api) Starting api route", settings);
200
200
 
201
201
  if (!settings.DB) {
202
- winston.error("(wab) db id mandatory. Exit...");
202
+ winston.error("(wab api) db id mandatory. Exit...");
203
203
  return callback('Missing parameter: db');
204
204
  } else {
205
205
  db = settings.DB;
206
- winston.info("(wab) db " + db);
206
+ //winston.info("(wab) db " + db);
207
207
  }
208
208
 
209
209
  if (!settings.GRAPH_URL) {
210
- winston.error("(wab) GRAPH_URL is mandatory. Exit...");
210
+ winston.error("(wab api) GRAPH_URL is mandatory. Exit...");
211
211
  return callback('Missing parameter: GRAPH_URL');
212
212
  } else {
213
213
  GRAPH_URL = settings.GRAPH_URL;
214
- winston.info("(wab) GRAPH_URL: " + GRAPH_URL);
214
+ winston.info("(wab api) GRAPH_URL: " + GRAPH_URL);
215
215
  }
216
216
 
217
217
  if (!settings.API_URL) {
218
- winston.error("(wab) API_URL is mandatory. Exit...");
218
+ winston.error("(wab api) API_URL is mandatory. Exit...");
219
219
  return callback('Missing parameter: API_URL');
220
220
  } else {
221
221
  API_URL = settings.API_URL;
222
- winston.info("(wab) API_URL: " + API_URL);
222
+ winston.info("(wab api) API_URL: " + API_URL);
223
223
  }
224
224
 
225
225
  if (!settings.BASE_FILE_URL) {
226
- winston.error("(wab) BASE_FILE_URL is mandatory. Exit...");
226
+ winston.error("(wab api) BASE_FILE_URL is mandatory. Exit...");
227
227
  return callback('Missing parameter: BASE_FILE_URL');
228
228
  } else {
229
229
  BASE_FILE_URL = settings.BASE_FILE_URL;
230
- winston.info("(wab) BASE_FILE_URL: " + BASE_FILE_URL);
230
+ winston.info("(wab api) BASE_FILE_URL: " + BASE_FILE_URL);
231
231
  }
232
232
 
233
233
 
@@ -214,6 +214,13 @@
214
214
 
215
215
  <p style="font-size: 18px;"><b>What's new?</b></p>
216
216
 
217
+ <div style="width: 100%; display: flex; flex-direction: row; justify-content: space-between; margin-bottom: 15px;">
218
+ <div>
219
+ <p>Bug fixes and performance improvements</p>
220
+ </div>
221
+ <p style="font-size: 12px; color: #676767; margin-bottom: 0px;">Version <b>0.1.53</b></p>
222
+ </div>
223
+
217
224
  <div style="width: 100%; display: flex; flex-direction: row; justify-content: space-between; margin-bottom: 15px;">
218
225
  <div>
219
226
  <p>Bug fixes and performance improvements</p>
@@ -238,14 +245,6 @@
238
245
  <p style="font-size: 12px; color: #676767; margin-bottom: 0px;">Version <b>0.1.49</b></p>
239
246
  </div>
240
247
 
241
- <div style="width: 100%; display: flex; flex-direction: row; justify-content: space-between; margin-bottom: 15px;">
242
- <div>
243
- <p style="margin-bottom: 0px">Added support for WhatsApp templates</p>
244
- <!--<p>Bug fixes and performance improvements</p>-->
245
- </div>
246
- <p style="font-size: 12px; color: #676767; margin-bottom: 0px;">Version <b>0.1.48</b></p>
247
- </div>
248
-
249
248
  <hr style="margin-top: 50px; margin-bottom: 20px">
250
249
 
251
250
  <script>
@@ -71,6 +71,7 @@ class TemplateManager {
71
71
  }
72
72
 
73
73
  async generateParamsObject(template) {
74
+
74
75
  let header_params = [];
75
76
  let body_params = [];
76
77
  let url_button_params = [];
@@ -106,7 +107,11 @@ class TemplateManager {
106
107
 
107
108
  } else {
108
109
 
109
- if (header_component.format === 'LOCATION') {
110
+ if (header_component.format === "TEXT") {
111
+ header_params.push({ type: "text", text: header_component.text })
112
+ }
113
+
114
+ else if (header_component.format === 'LOCATION') {
110
115
  header_params.push({ type: header_component.format, location: { latitude: null, longitude: null, name: null, address: null } })
111
116
  }
112
117
  else {
@@ -119,7 +119,8 @@ class TiledeskSubscriptionClient {
119
119
  }
120
120
  else {
121
121
  if (callback) {
122
- callback(TiledeskClient.getErr({ message: "Response status not 200" }, options, res), null, null);
122
+ callback("Response status not 200", null, null);
123
+ //callback({ message: "Response status not 200" }, options, res), null, null);
123
124
  }
124
125
  }
125
126
  }).catch((err) => {
@@ -260,7 +260,7 @@ const path = require('path');
260
260
 
261
261
  }
262
262
  else if (tiledeskChannelMessage.attributes.attachment.template) {
263
- winston.verbose("(wab) [Translator] template: ", tiledeskChannelMessage.attributes.attachment.template)
263
+ winston.debug("(wab) [Translator] template: ", tiledeskChannelMessage.attributes.attachment.template)
264
264
 
265
265
  let template = tiledeskChannelMessage.attributes.attachment.template;
266
266
 
@@ -314,7 +314,6 @@ const path = require('path');
314
314
  whatsapp_message.template.components = components;
315
315
  }
316
316
 
317
- winston.verbose("(wab) [Translator] whatsapp_message: ", whatsapp_message);
318
317
  return whatsapp_message;
319
318
 
320
319
  }