@tiledesk/tiledesk-whatsapp-connector 0.1.51 → 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/logs/app.log CHANGED
@@ -1,4 +0,0 @@
1
- info: (wab) Starting Whatsapp App
2
- info: (wab) API_URL: https://tiledesk-server-pre.herokuapp.com
3
- info: (wab) BASE_URL: https://tiledesk-whatsapp-app-pre.giovannitroisi3.repl.co
4
- info: (wab) GRAPH_URL: https://graph.facebook.com/v14.0/
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-whatsapp-connector",
3
- "version": "0.1.51",
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
 
@@ -199,7 +199,7 @@
199
199
  <label class="input-label" for="wab_token">Whatsapp Token
200
200
  </label>
201
201
  <div style="display: flex; flex-direction: row; align-items: center;">
202
- <input type="text" required class="form-control custom-input" name="wab_token" value="{{ wab_token }}" placeholder="Enter the Whatsapp Token">
202
+ <input id="tkn" type="text" required class="form-control custom-input" name="wab_token" value="{{ wab_token }}" placeholder="Enter the Whatsapp Token">
203
203
  <span style="margin-left: 10px;">
204
204
  <i class="fa fa-question-circle custom-tooltip">
205
205
  <span class="custom-tooltiptext">This is the permanent WhatsApp Business token.</span>
@@ -264,7 +264,8 @@
264
264
  </div>
265
265
  {{else}}
266
266
  <div style="width: 500px; text-align: right;">
267
- <input type="submit" class="btn" style="width: 200px; background-color: #0ba2dc; color: white; font-weight: 500;" value="Connect">
267
+ <input type="submit" class="btn" style="width: 200px; background-color: #0ba2dc; color: white; font-weight: 500;" value="Connect"
268
+ onclick="postMessage('connectWhatsapp')">
268
269
  </div>
269
270
  {{/if}}
270
271
  </form>
@@ -289,7 +290,7 @@
289
290
  <input type="hidden" name="project_id" value="{{ project_id }}">
290
291
  <input type="hidden" name="token" value="{{ token }}">
291
292
  <input type="hidden" name="subscription_id" value="{{ subscription_id }}">
292
- <input type="submit" class="btn disconnect-button" style="margin-top: 10px; width: 160px;" value="Disconnect">
293
+ <input type="submit" class="btn disconnect-button" style="margin-top: 10px; width: 160px;" value="Disconnect" onclick="postMessage('disconnectWhatsapp')">
293
294
  </form>
294
295
  </div>
295
296
  {{else}}
@@ -332,6 +333,19 @@
332
333
  console.error('Async: Could not copy text: ', err);
333
334
  }
334
335
  }
336
+
337
+ function postMessage(action) {
338
+
339
+ const tkn = document.getElementById('tkn').value;
340
+ let connected = false;
341
+ if (tkn) {
342
+ connected = true;
343
+ }
344
+ const msg = { action: action, calledBy: 'whatsapp_proxy', connected: connected}
345
+ window.parent.postMessage(msg, '*');
346
+ }
347
+
335
348
  </script>
349
+
336
350
 
337
351
  </body>
@@ -107,7 +107,7 @@
107
107
  <input type="hidden" name="project_id" value="{{ project_id }}" />
108
108
  <input type="hidden" name="app_id" value="{{ app_id }}" />
109
109
  <input type="hidden" name="token" value="{{ token }}" />
110
- <input type="submit" class="btn uninstall-button" value="Yes, uninstall">
110
+ <input type="submit" class="btn uninstall-button" value="Yes, uninstall" onclick="postMessage('uninstallWhatsapp')">
111
111
  </form>
112
112
  </div>
113
113
  </div>
@@ -138,7 +138,8 @@
138
138
  <input type="hidden" name="project_id" value="{{ project_id }}" />
139
139
  <input type="hidden" name="app_id" value="{{ app_id }}" />
140
140
  <input type="hidden" name="token" value="{{ token }}" />
141
- <input type="submit" class="btn install-button" value="Install">
141
+ <input type="submit" class="btn install-button" value="Install" onclick="postMessage('installWhatsapp')">
142
+ <!--<input type="submit" class="btn install-button" value="Install" onclick="postMessage('installWhatsapp')">-->
142
143
  </form>
143
144
  </div>
144
145
  </div>
@@ -215,37 +216,42 @@
215
216
 
216
217
  <div style="width: 100%; display: flex; flex-direction: row; justify-content: space-between; margin-bottom: 15px;">
217
218
  <div>
218
- <p style="margin-bottom: 0px">Bug fixes</p>
219
- <!--<p>Bug fixes and performance improvements</p>-->
219
+ <p>Bug fixes and performance improvements</p>
220
220
  </div>
221
- <p style="font-size: 12px; color: #676767; margin-bottom: 0px;">Version <b>0.1.50</b></p>
221
+ <p style="font-size: 12px; color: #676767; margin-bottom: 0px;">Version <b>0.1.53</b></p>
222
222
  </div>
223
223
 
224
224
  <div style="width: 100%; display: flex; flex-direction: row; justify-content: space-between; margin-bottom: 15px;">
225
225
  <div>
226
- <p style="margin-bottom: 0px">Added support for WhatsApp voice messages</p>
227
- <p style="margin-bottom: 0px">Bug fixes and minor improvements</p>
228
- <!--<p>Bug fixes and performance improvements</p>-->
226
+ <p>Bug fixes and performance improvements</p>
229
227
  </div>
230
- <p style="font-size: 12px; color: #676767; margin-bottom: 0px;">Version <b>0.1.49</b></p>
228
+ <p style="font-size: 12px; color: #676767; margin-bottom: 0px;">Version <b>0.1.52</b></p>
231
229
  </div>
232
230
 
233
231
  <div style="width: 100%; display: flex; flex-direction: row; justify-content: space-between; margin-bottom: 15px;">
234
232
  <div>
235
- <p style="margin-bottom: 0px">Added support for WhatsApp templates</p>
233
+ <p style="margin-bottom: 0px">Bug fixes</p>
236
234
  <!--<p>Bug fixes and performance improvements</p>-->
237
235
  </div>
238
- <p style="font-size: 12px; color: #676767; margin-bottom: 0px;">Version <b>0.1.48</b></p>
236
+ <p style="font-size: 12px; color: #676767; margin-bottom: 0px;">Version <b>0.1.50</b></p>
239
237
  </div>
240
238
 
241
239
  <div style="width: 100%; display: flex; flex-direction: row; justify-content: space-between; margin-bottom: 15px;">
242
240
  <div>
241
+ <p style="margin-bottom: 0px">Added support for WhatsApp voice messages</p>
243
242
  <p style="margin-bottom: 0px">Bug fixes and minor improvements</p>
244
243
  <!--<p>Bug fixes and performance improvements</p>-->
245
244
  </div>
246
- <p style="font-size: 12px; color: #676767; margin-bottom: 0px;">Version <b>0.1.45</b></p>
245
+ <p style="font-size: 12px; color: #676767; margin-bottom: 0px;">Version <b>0.1.49</b></p>
247
246
  </div>
248
247
 
249
248
  <hr style="margin-top: 50px; margin-bottom: 20px">
249
+
250
+ <script>
251
+ function postMessage(action) {
252
+ const msg = { action: action, calledBy: 'whatsapp_proxy' }
253
+ window.parent.postMessage(msg, '*');
254
+ }
255
+ </script>
250
256
 
251
257
  </body>
@@ -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
  }
@@ -446,9 +445,17 @@ const path = require('path');
446
445
  return data;
447
446
  }
448
447
 
448
+ else if (whatsappChannelMessage.type == 'button') {
449
+ var tiledeskMessage = {
450
+ text: whatsappChannelMessage.button.text, // or whatsappChannelMessage.button.payload (?)
451
+ senderFullname: from,
452
+ channel: { name: TiledeskWhatsappTranslator.CHANNEL_NAME }
453
+ }
454
+ return tiledeskMessage;
455
+ }
449
456
 
450
457
  // interactive message
451
- if (whatsappChannelMessage.type == 'interactive') {
458
+ else if (whatsappChannelMessage.type == 'interactive') {
452
459
 
453
460
  // list reply
454
461
  if (whatsappChannelMessage.interactive.type == 'list_reply') {
@@ -506,7 +513,7 @@ const path = require('path');
506
513
  }
507
514
 
508
515
  // media message - image
509
- if (whatsappChannelMessage.type == 'image') {
516
+ else if (whatsappChannelMessage.type == 'image') {
510
517
 
511
518
  let text = "Image attached"
512
519
  if (whatsappChannelMessage.image.caption) {
@@ -526,7 +533,7 @@ const path = require('path');
526
533
  }
527
534
 
528
535
  // media message - video
529
- if (whatsappChannelMessage.type == 'video') {
536
+ else if (whatsappChannelMessage.type == 'video') {
530
537
 
531
538
  let text = "Video attached"
532
539
  if (whatsappChannelMessage.video.caption) {
@@ -548,7 +555,7 @@ const path = require('path');
548
555
  }
549
556
 
550
557
  // media message - document
551
- if (whatsappChannelMessage.type == 'document') {
558
+ else if (whatsappChannelMessage.type == 'document') {
552
559
 
553
560
  let text = "Document attached"
554
561
  if (whatsappChannelMessage.document.caption) {
@@ -570,7 +577,7 @@ const path = require('path');
570
577
  }
571
578
 
572
579
  // media message - audio
573
- if (whatsappChannelMessage.type == 'audio') {
580
+ else if (whatsappChannelMessage.type == 'audio') {
574
581
 
575
582
  let text = "Audio attached"
576
583
  /*if (whatsappChannelMessage.document.caption) {
@@ -590,6 +597,11 @@ const path = require('path');
590
597
  }
591
598
  return tiledeskMessage;
592
599
  }
600
+
601
+ else {
602
+ winston.verbose("(wab) [Translator] unsupported whatsapp messsage type");
603
+ return null;
604
+ }
593
605
  }
594
606
 
595
607
  async fillParams(params, rcv_option) {