@tiledesk/tiledesk-whatsapp-connector 0.1.40 → 0.1.41

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
@@ -76,7 +76,9 @@ router.get('/detail', async (req, res) => {
76
76
  }
77
77
 
78
78
  readHTMLFile('/detail.html', (err, html) => {
79
- if (err) { console.log("(wab) error read html file: ", err); }
79
+ if (err) {
80
+ console.error("(wab) error read html file: ", err);
81
+ }
80
82
  var template = handlebars.compile(html);
81
83
  var replacements = {
82
84
  app_version: pjson.version,
@@ -96,7 +98,7 @@ router.post('/install', async (req, res) => {
96
98
  let app_id = req.body.app_id;
97
99
  let token = req.body.token;
98
100
 
99
- console.log("(wab) Install app " + app_id + " for project id " + project_id);
101
+ //console.log("(wab) Install app " + app_id + " for project id " + project_id);
100
102
  let installation_info = {
101
103
  project_id: project_id,
102
104
  app_id: app_id,
@@ -106,15 +108,15 @@ router.post('/install', async (req, res) => {
106
108
  const appClient = new TiledeskAppsClient({ APPS_API_URL: APPS_API_URL });
107
109
  appClient.install(installation_info).then((installation) => {
108
110
 
109
- if (log) {
110
- console.log("(wab) installation response: ", installation);
111
- }
111
+ //if (log) {
112
+ // console.log("(wab) installation response: ", installation);
113
+ //}
112
114
 
113
115
  let installed = true;
114
116
 
115
117
  readHTMLFile('/detail.html', (err, html) => {
116
118
  if (err) {
117
- console.log("(wab) error read html file: ", err);
119
+ console.error("(wab) error read html file: ", err);
118
120
  }
119
121
 
120
122
  var template = handlebars.compile(html);
@@ -144,15 +146,15 @@ router.post('/uninstall', async (req, res) => {
144
146
  const appClient = new TiledeskAppsClient({ APPS_API_URL: APPS_API_URL });
145
147
  appClient.uninstall(project_id, app_id).then((response) => {
146
148
 
147
- if (log) {
148
- console.log("(wab) uninstallation response: ", response);
149
- }
149
+ //if (log) {
150
+ //console.log("(wab) uninstallation response: ", response);
151
+ //}
150
152
 
151
153
  let installed = false;
152
154
 
153
155
  readHTMLFile('/detail.html', (err, html) => {
154
156
  if (err) {
155
- console.log("(wab) error read html file: ", err);
157
+ console.error("(wab) error read html file: ", err);
156
158
  }
157
159
 
158
160
  var template = handlebars.compile(html);
@@ -174,10 +176,10 @@ router.post('/uninstall', async (req, res) => {
174
176
  })
175
177
 
176
178
  router.get('/configure', async (req, res) => {
177
- console.log("(wab) /configure");
178
- if (log) {
179
- console.log("(wab) /configure query: ", req.query);
180
- }
179
+ //console.log("(wab) /configure");
180
+ //if (log) {
181
+ //console.log("(wab) /configure query: ", req.query);
182
+ //}
181
183
 
182
184
  let projectId = "";
183
185
  let token = "";
@@ -191,21 +193,19 @@ router.get('/configure', async (req, res) => {
191
193
 
192
194
  let settings = await db.get(CONTENT_KEY);
193
195
  if (log) {
194
- console.log("(wab) settings: ", settings);
196
+ //console.log("(wab) settings: ", settings);
195
197
  }
196
198
 
197
199
  // get departments
198
200
  const tdChannel = new TiledeskChannel({ settings: { project_id: projectId, token: token }, API_URL: API_URL })
199
201
  let departments = await tdChannel.getDepartments(token);
200
- console.log("(wab) found " + departments.length + " departments")
201
- if (log) {
202
- }
202
+ //console.log("(wab) found " + departments.length + " departments")
203
203
 
204
204
  if (settings) {
205
205
 
206
206
  readHTMLFile('/configure.html', (err, html) => {
207
207
  if (err) {
208
- console.log("(wab) error read html file: ", err);
208
+ console.error("(wab) error read html file: ", err);
209
209
  }
210
210
 
211
211
  var template = handlebars.compile(html);
@@ -230,7 +230,7 @@ router.get('/configure', async (req, res) => {
230
230
  readHTMLFile('/configure.html', (err, html) => {
231
231
 
232
232
  if (err) {
233
- console.log("(departments) error read html file: ", err);
233
+ console.error("(wab) error read html file: ", err);
234
234
  }
235
235
 
236
236
  var template = handlebars.compile(html);
@@ -249,10 +249,10 @@ router.get('/configure', async (req, res) => {
249
249
  })
250
250
 
251
251
  router.post('/update', async (req, res) => {
252
- console.log("(wab) /update");
253
- if (log) {
254
- console.log("(wab) body: ", req.body);
255
- }
252
+ //console.log("(wab) /update");
253
+ //if (log) {
254
+ // console.log("(wab) body: ", req.body);
255
+ //}
256
256
 
257
257
  let projectId = req.body.project_id;
258
258
  let token = req.body.token;
@@ -279,7 +279,7 @@ router.post('/update', async (req, res) => {
279
279
 
280
280
  readHTMLFile('/configure.html', (err, html) => {
281
281
  if (err) {
282
- console.log("(wab) error read html file: ", err);
282
+ console.error("(wab) error read html file: ", err);
283
283
  }
284
284
 
285
285
  var template = handlebars.compile(html);
@@ -318,9 +318,9 @@ router.post('/update', async (req, res) => {
318
318
  // promise
319
319
  tdClient.subscribe(subscription_info).then((data) => {
320
320
  let subscription = data;
321
- if (log) {
322
- console.log("\n(wab) Subscription: ", subscription)
323
- }
321
+ //if (log) {
322
+ //console.log("\n(wab) Subscription: ", subscription)
323
+ //}
324
324
 
325
325
  let settings = {
326
326
  project_id: projectId,
@@ -338,7 +338,7 @@ router.post('/update', async (req, res) => {
338
338
 
339
339
  readHTMLFile('/configure.html', (err, html) => {
340
340
  if (err) {
341
- console.log("(wab) error read html file: ", err);
341
+ console.error("(wab) error read html file: ", err);
342
342
  }
343
343
 
344
344
  var template = handlebars.compile(html);
@@ -362,7 +362,7 @@ router.post('/update', async (req, res) => {
362
362
 
363
363
  readHTMLFile('/configure.html', (err, html) => {
364
364
  if (err) {
365
- console.log("(wab) error read html file: ", err);
365
+ console.error("(wab) error read html file: ", err);
366
366
  }
367
367
 
368
368
  var template = handlebars.compile(html);
@@ -383,10 +383,10 @@ router.post('/update', async (req, res) => {
383
383
  })
384
384
 
385
385
  router.post('/disconnect', async (req, res) => {
386
- console.log("\n(wab) /disconnect")
387
- if (log) {
388
- console.log("(wab) body: ", req.body)
389
- }
386
+ //console.log("\n(wab) /disconnect")
387
+ //if (log) {
388
+ //console.log("(wab) body: ", req.body)
389
+ //}
390
390
 
391
391
  let projectId = req.body.project_id;
392
392
  let token = req.body.token;
@@ -395,7 +395,7 @@ router.post('/disconnect', async (req, res) => {
395
395
 
396
396
  let CONTENT_KEY = "whatsapp-" + projectId;
397
397
  await db.remove(CONTENT_KEY);
398
- console.log("(wab) Content deleted.");
398
+ //console.log("(wab) Content deleted.");
399
399
 
400
400
  let proxy_url = BASE_URL + "/webhook/" + projectId;
401
401
 
@@ -416,7 +416,7 @@ router.post('/disconnect', async (req, res) => {
416
416
  readHTMLFile('/configure.html', (err, html) => {
417
417
 
418
418
  if (err) {
419
- console.log("(wab) error read html file: ", err);
419
+ console.error("(wab) error read html file: ", err);
420
420
  }
421
421
 
422
422
  var template = handlebars.compile(html);
@@ -438,10 +438,10 @@ router.post('/disconnect', async (req, res) => {
438
438
  })
439
439
 
440
440
  router.post('/tiledesk', async (req, res) => {
441
- console.log("(wab) Message received from Tiledesk")
442
- if (log) {
443
- console.log("(wab) tiledeskChannelMessage: ", JSON.stringify(req.body.payload));
444
- }
441
+ //console.log("(wab) Message received from Tiledesk")
442
+ //if (log) {
443
+ //console.log("(wab) tiledeskChannelMessage: ", JSON.stringify(req.body.payload));
444
+ //}
445
445
 
446
446
  var tiledeskChannelMessage = req.body.payload;
447
447
  var projectId = req.body.payload.id_project;
@@ -461,17 +461,17 @@ router.post('/tiledesk', async (req, res) => {
461
461
  var sender_id = req.body.payload.sender;
462
462
 
463
463
  if (sender_id.indexOf("wab") > -1) {
464
- console.log("(wab) Skip same sender");
464
+ //console.log("(wab) Skip same sender");
465
465
  return res.sendStatus(200);
466
466
  }
467
467
 
468
468
  if (attributes && attributes.subtype === "info") {
469
- console.log("(wab) Skip subtype (info)");
469
+ //console.log("(wab) Skip subtype (info)");
470
470
  return res.sendStatus(200);
471
471
  }
472
472
 
473
473
  if (attributes && attributes.subtype === 'info/support') {
474
- console.log("(wab) Skip subtype: ", attributes.subtype);
474
+ //console.log("(wab) Skip subtype: ", attributes.subtype);
475
475
  return res.sendStatus(200);
476
476
  }
477
477
 
@@ -479,14 +479,14 @@ router.post('/tiledesk', async (req, res) => {
479
479
  let whatsapp_receiver = recipient_id.substring(recipient_id.lastIndexOf("-") + 1);
480
480
  let phone_number_id = recipient_id.substring(recipient_id.lastIndexOf("wab-") + 4, recipient_id.lastIndexOf("-"));
481
481
 
482
- if (log) {
483
- console.log("(wab) text: ", text);
484
- console.log("(wab) attributes: ", JSON.stringify(attributes))
485
- console.log("(wab) tiledesk sender_id: ", sender_id);
486
- console.log("(wab) recipient_id: ", recipient_id);
487
- console.log("(wab) whatsapp_receiver: ", whatsapp_receiver);
488
- console.log("(wab) phone_number_id: ", phone_number_id)
489
- }
482
+ //if (log) {
483
+ //console.log("(wab) text: ", text);
484
+ //console.log("(wab) attributes: ", JSON.stringify(attributes))
485
+ //console.log("(wab) tiledesk sender_id: ", sender_id);
486
+ //console.log("(wab) recipient_id: ", recipient_id);
487
+ //console.log("(wab) whatsapp_receiver: ", whatsapp_receiver);
488
+ //console.log("(wab) phone_number_id: ", phone_number_id)
489
+ //}
490
490
 
491
491
  const messageHandler = new MessageHandler({ tiledeskChannelMessage: tiledeskChannelMessage });
492
492
  const tlr = new TiledeskWhatsappTranslator();
@@ -497,22 +497,22 @@ router.post('/tiledesk', async (req, res) => {
497
497
  // message
498
498
  if (command.type === "message") {
499
499
  let tiledeskCommandMessage = await messageHandler.generateMessageObject(command);
500
- if (log) {
501
- console.log("(wab) message generated from commanf: ", JSON.stringify(tiledeskCommandMessage))
502
- }
500
+ //if (log) {
501
+ //console.log("(wab) message generated from command: ", JSON.stringify(tiledeskCommandMessage))
502
+ //}
503
503
 
504
504
  let whatsappJsonMessage = await tlr.toWhatsapp(tiledeskCommandMessage, whatsapp_receiver);
505
- console.log("(wab) 🟢 whatsappJsonMessage", JSON.stringify(whatsappJsonMessage))
505
+ //console.log("(wab) 🟢 whatsappJsonMessage", JSON.stringify(whatsappJsonMessage))
506
506
 
507
507
  if (whatsappJsonMessage) {
508
508
  const twClient = new TiledeskWhatsapp({ token: settings.wab_token, GRAPH_URL: GRAPH_URL });
509
509
  twClient.sendMessage(phone_number_id, whatsappJsonMessage).then((response) => {
510
- console.log("(wab) Message sent to WhatsApp! ", response.status, response.statusText);
510
+ //console.log("(wab) Message sent to WhatsApp! ", response.status, response.statusText);
511
511
  i += 1;
512
512
  if (i < commands.length) {
513
513
  execute(commands[i]);
514
514
  } else {
515
- console.log("(wab) End of commands")
515
+ //console.log("(wab) End of commands")
516
516
  }
517
517
  }).catch((err) => {
518
518
  console.error("(wab) send message error: ", err);
@@ -530,7 +530,7 @@ router.post('/tiledesk', async (req, res) => {
530
530
  if (i < commands.length) {
531
531
  execute(commands[i]);
532
532
  } else {
533
- console.log("(wab) End of commands")
533
+ //console.log("(wab) End of commands")
534
534
  }
535
535
  }, command.time)
536
536
  }
@@ -541,13 +541,13 @@ router.post('/tiledesk', async (req, res) => {
541
541
  else if (tiledeskChannelMessage.text || tiledeskChannelMessage.metadata) {
542
542
 
543
543
  let whatsappJsonMessage = await tlr.toWhatsapp(tiledeskChannelMessage, whatsapp_receiver);
544
- console.log("(wab) 🟢 whatsappJsonMessage", JSON.stringify(whatsappJsonMessage))
544
+ //console.log("(wab) 🟢 whatsappJsonMessage", JSON.stringify(whatsappJsonMessage))
545
545
 
546
546
  if (whatsappJsonMessage) {
547
547
  const twClient = new TiledeskWhatsapp({ token: settings.wab_token, GRAPH_URL: GRAPH_URL });
548
548
 
549
549
  twClient.sendMessage(phone_number_id, whatsappJsonMessage).then((response) => {
550
- console.log("(wab) Message sent to WhatsApp! ", response.status, response.statusText);
550
+ //console.log("(wab) Message sent to WhatsApp! ", response.status, response.statusText);
551
551
  }).catch((err) => {
552
552
  console.error("(wab) error send message: ", err);
553
553
  })
@@ -557,7 +557,7 @@ router.post('/tiledesk', async (req, res) => {
557
557
  }
558
558
 
559
559
  } else {
560
- console.log("(wab) no command, no text --> skip")
560
+ //console.log("(wab) no command, no text --> skip")
561
561
  }
562
562
 
563
563
  return res.send(200);
@@ -569,6 +569,7 @@ router.post("/webhook/:project_id", async (req, res) => {
569
569
 
570
570
  // Parse the request body from the POST
571
571
  let projectId = req.params.project_id;
572
+ //console.log("(wab) Message received from WhatsApp");
572
573
  //console.log("/webhook req.body: ", JSON.stringify(req.body, null, 2))
573
574
 
574
575
  // Check the Incoming webhook message
@@ -583,13 +584,12 @@ router.post("/webhook/:project_id", async (req, res) => {
583
584
  req.body.entry[0].changes[0].value.messages[0]
584
585
  ) {
585
586
 
586
- console.log("(wab) Message received from WhatsApp");
587
- if (log) {
588
- console.log("\n(wab) body: ", JSON.stringify(req.body));
589
- }
587
+ //if (log) {
588
+ // console.log("\n(wab) body: ", JSON.stringify(req.body));
589
+ //}
590
590
 
591
591
  if (req.body.entry[0].changes[0].value.messages[0].type == "system") {
592
- console.log("(wab) Skip system message")
592
+ //console.log("(wab) Skip system message")
593
593
  return res.sendStatus(200);
594
594
  }
595
595
 
@@ -597,11 +597,11 @@ router.post("/webhook/:project_id", async (req, res) => {
597
597
 
598
598
  let CONTENT_KEY = "whatsapp-" + projectId;
599
599
  let settings = await db.get(CONTENT_KEY);
600
- if (log) {
601
- console.log("(wab) kvdb settings: ", settings);
602
- }
600
+ //if (log) {
601
+ //console.log("(wab) kvdb settings: ", settings);
602
+ //}
603
603
  if (!settings) {
604
- console.log("(wab) No settings found. Exit..");
604
+ //console.log("(wab) No settings found. Exit..");
605
605
  return res.sendStatus(200);
606
606
  }
607
607
 
@@ -612,14 +612,14 @@ router.post("/webhook/:project_id", async (req, res) => {
612
612
  if (whatsappChannelMessage.text && whatsappChannelMessage.text.body.startsWith("#td")) {
613
613
 
614
614
  let code = whatsappChannelMessage.text.body.split(' ')[0];
615
- console.log("(wab) test code: ", code)
615
+ //console.log("(wab) test code: ", code)
616
616
 
617
617
  const bottester = new TiledeskBotTester({project_id: projectId, redis_client: redis_client, db: db, tdChannel: tdChannel, tlr: tlr});
618
618
  bottester.startBotConversation(req.body, code).then((result) => {
619
- console.log("(wab) test conversation started");
620
- if (log) {
621
- console.log("(wab) result: ", result);
622
- }
619
+ //console.log("(wab) test conversation started");
620
+ //if (log) {
621
+ //console.log("(wab) result: ", result);
622
+ //}
623
623
  }).catch((err) => {
624
624
  console.error("(wab) start test onversation error: ", err);
625
625
  })
@@ -642,12 +642,12 @@ router.post("/webhook/:project_id", async (req, res) => {
642
642
  let tiledeskJsonMessage;
643
643
 
644
644
  if ((whatsappChannelMessage.type == 'text')) {
645
- console.log("(wab) message type: text")
645
+ //console.log("(wab) message type: text")
646
646
  tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname);
647
647
  }
648
648
 
649
649
  else if (whatsappChannelMessage.type == 'interactive') {
650
- console.log("(wab) message type: interactive")
650
+ //console.log("(wab) message type: interactive")
651
651
  tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname);
652
652
  }
653
653
 
@@ -659,13 +659,13 @@ router.post("/webhook/:project_id", async (req, res) => {
659
659
  media = whatsappChannelMessage.image;
660
660
  const filename = await util.downloadMedia(media.id);
661
661
  if (!filename) {
662
- console.log("(wab) Unable to download media with id " + media.id + ". Message not sent.");
662
+ //console.log("(wab) Unable to download media with id " + media.id + ". Message not sent.");
663
663
  return res.status(500).send({ success: false, error: "unable to download media" })
664
664
  }
665
665
  let file_path = path.join(__dirname, 'tmp', filename);
666
666
 
667
667
  const image_url = await util.uploadMedia(file_path, "images");
668
- console.log("(wab) image_url: ", image_url)
668
+ //console.log("(wab) image_url: ", image_url)
669
669
 
670
670
  tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname, image_url);
671
671
  }
@@ -675,13 +675,13 @@ router.post("/webhook/:project_id", async (req, res) => {
675
675
 
676
676
  const filename = await util.downloadMedia(media.id);
677
677
  if (!filename) {
678
- console.log("(wab) Unable to download media with id " + media.id + ". Message not sent.");
678
+ //console.log("(wab) Unable to download media with id " + media.id + ". Message not sent.");
679
679
  return res.status(500).send({ success: false, error: "unable to download media" })
680
680
  }
681
681
  let file_path = path.join(__dirname, 'tmp', filename);
682
682
 
683
683
  const media_url = await util.uploadMedia(file_path, "files");
684
- console.log("(wab) media_url: ", media_url)
684
+ //console.log("(wab) media_url: ", media_url)
685
685
 
686
686
  tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname, media_url);
687
687
  }
@@ -691,31 +691,31 @@ router.post("/webhook/:project_id", async (req, res) => {
691
691
 
692
692
  const filename = await util.downloadMedia(media.id);
693
693
  if (!filename) {
694
- console.log("(wab) Unable to download media with id " + media.id + ". Message not sent.");
694
+ //console.log("(wab) Unable to download media with id " + media.id + ". Message not sent.");
695
695
  return res.status(500).send({ success: false, error: "unable to download media" })
696
696
  }
697
697
  let file_path = path.join(__dirname, 'tmp', filename);
698
698
 
699
699
  const media_url = await util.uploadMedia(file_path, "files");
700
- console.log("(wab) media_url: ", media_url)
700
+ //console.log("(wab) media_url: ", media_url)
701
701
 
702
702
  tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname, media_url);
703
703
  }
704
704
 
705
705
  } else {
706
706
  // unsupported. Try anyway to send something.
707
- console.log("(wab) unsupported message")
707
+ //console.log("(wab) unsupported message")
708
708
  }
709
709
 
710
710
  if (tiledeskJsonMessage) {
711
- console.log("(wab) 🟠 tiledeskJsonMessage: ", JSON.stringify(tiledeskJsonMessage));
711
+ //console.log("(wab) 🟠 tiledeskJsonMessage: ", JSON.stringify(tiledeskJsonMessage));
712
712
  const response = await tdChannel.send(tiledeskJsonMessage, message_info, settings.department_id);
713
- console.log("(wab) Message sent to Tiledesk!")
714
- if (log) {
715
- console.log("(wab) response: ", response)
716
- }
713
+ //console.log("(wab) Message sent to Tiledesk!")
714
+ //if (log) {
715
+ //console.log("(wab) response: ", response)
716
+ //}
717
717
  } else {
718
- console.log("(wab) tiledeskJsonMessage is undefined")
718
+ //console.log("(wab) tiledeskJsonMessage is undefined")
719
719
  }
720
720
 
721
721
  }
@@ -724,7 +724,7 @@ router.post("/webhook/:project_id", async (req, res) => {
724
724
 
725
725
  } else {
726
726
  // Return a '404 Not Found' if event is not from a WhatsApp API
727
- console.log("(wab) event not from whatsapp")
727
+ //console.log("(wab) event not from whatsapp")
728
728
  res.sendStatus(404);
729
729
  }
730
730
  });
@@ -736,8 +736,8 @@ router.get("/webhook/:project_id", async (req, res) => {
736
736
  * UPDATE YOUR VERIFY TOKEN
737
737
  *This will be the Verify Token value when you set up webhook
738
738
  **/
739
- console.log("(wab) Verify the webhook... ");
740
- console.log("(wab) req.query: ", req.query);
739
+ //console.log("(wab) Verify the webhook... ");
740
+ //console.log("(wab) req.query: ", req.query);
741
741
 
742
742
  // Parse params from the webhook verification request
743
743
  let mode = req.query["hub.mode"];
@@ -754,10 +754,10 @@ router.get("/webhook/:project_id", async (req, res) => {
754
754
  } else {
755
755
  let VERIFY_TOKEN = settings.verify_token;
756
756
 
757
- if (log) {
758
- console.log("token: ", token);
759
- console.log("verify token: ", VERIFY_TOKEN);
760
- }
757
+ //if (log) {
758
+ //console.log("token: ", token);
759
+ //console.log("verify token: ", VERIFY_TOKEN);
760
+ //}
761
761
 
762
762
  // Check if a token and mode were sent
763
763
  if (mode && token) {
@@ -765,7 +765,7 @@ router.get("/webhook/:project_id", async (req, res) => {
765
765
  if (mode === "subscribe" && token === VERIFY_TOKEN) {
766
766
 
767
767
  // Respond with 200 OK and challenge token from the request
768
- console.log("(wab) WEBHOOK_VERIFIED");
768
+ //console.log("(wab) WEBHOOK_VERIFIED");
769
769
  res.status(200).send(challenge);
770
770
  } else {
771
771
  // Responds with '403 Forbidden' if verify tokens do not match
@@ -784,7 +784,7 @@ router.get("/webhook/:project_id", async (req, res) => {
784
784
 
785
785
  router.post("/newtest", async (req, res) => {
786
786
 
787
- console.log("(wab) initializing new test..");
787
+ //console.log("(wab) initializing new test..");
788
788
 
789
789
  let project_id = req.body.project_id;
790
790
  let bot_id = req.body.bot_id;
@@ -804,10 +804,10 @@ router.post("/newtest", async (req, res) => {
804
804
  await redis_client.set(key, JSON.stringify(info), 'EX', 604800);
805
805
  redis_client.get(key, (err, value) => {
806
806
  if (err) {
807
- console.log("(wab) redis get err: ", err)
807
+ //console.log("(wab) redis get err: ", err)
808
808
  return res.status(500).send({success: "false", message: "Testing info could not be saved"});
809
809
  } else {
810
- console.log("(wab) new test initialized with id: ", short_uid)
810
+ //console.log("(wab) new test initialized with id: ", short_uid)
811
811
  return res.status(200).send({short_uid: short_uid});
812
812
  }
813
813
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-whatsapp-connector",
3
- "version": "0.1.40",
3
+ "version": "0.1.41",
4
4
  "description": "Tiledesk Whatsapp connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -150,6 +150,7 @@
150
150
 
151
151
  <div style="min-width: 600px">
152
152
  <form action="./update" method="post">
153
+
153
154
  <div class="form-group" style="width: 590px;">
154
155
  <label class="input-label" for="proxy_url">Webhook Endpoint</label>
155
156
  <div style="display: flex; flex-direction: row;">
@@ -286,7 +287,7 @@
286
287
  //document.querySelector('textarea').select();
287
288
  try {
288
289
  document.execCommand('copy');
289
- console.log('Async: Copied to clipboard!');
290
+ //console.log('Async: Copied to clipboard!');
290
291
  let el1 = document.getElementById('copy-div');
291
292
  el1.style.visibility = 'hidden';
292
293
 
@@ -107,7 +107,8 @@ ul {
107
107
 
108
108
 
109
109
  .input-label {
110
- margin-bottom: 2px;
110
+ margin-bottom: 5px;
111
+ font-weight: 600;
111
112
  }
112
113
 
113
114
 
@@ -419,7 +419,7 @@ describe('Test Translator\n', function() {
419
419
  }
420
420
  }
421
421
  let fullname = "John Doe";
422
- let media_url = "https://fakeimageurl.com/"
422
+ let media_url = "https://fakevideourl.com/"
423
423
 
424
424
  const tlr = new TiledeskWhatsappTranslator();
425
425
  assert(tlr != null);
@@ -16,7 +16,7 @@ class KVBaseMongo {
16
16
  throw new Error('KVBASE_COLLECTION (the name of the Mongodb collection used as key-value store) is mandatory.');
17
17
  }
18
18
  this.KV_COLLECTION = config.KVBASE_COLLECTION;
19
- console.log("KV_COLLECTION: ", this.KV_COLLECTION)
19
+ //console.log("KV_COLLECTION: ", this.KV_COLLECTION)
20
20
 
21
21
  this.log = false;
22
22
  if (config.log) {
@@ -27,7 +27,7 @@ class KVBaseMongo {
27
27
  connect(MONGODB_URI, callback) {
28
28
  mongodb.MongoClient.connect(MONGODB_URI, { useNewUrlParser: true, useUnifiedTopology: true }, (err, client) => {
29
29
  if (err) {
30
- console.log(err);
30
+ console.error(err);
31
31
  process.exit(1);
32
32
  } else {
33
33
  this.db = client.db();
@@ -57,9 +57,9 @@ class KVBaseMongo {
57
57
  get(k) {
58
58
  return new Promise(resolve => {
59
59
  //this.db.get(k).then(value => {resolve(value)});
60
- if (this.log) {
61
- console.log("Searching on ", this.db)
62
- }
60
+ //if (this.log) {
61
+ //console.log("Searching on ", this.db)
62
+ //}
63
63
  //console.log("Searching on Collection", this.KV_COLLECTION)
64
64
 
65
65
  this.db.collection(this.KV_COLLECTION).findOne({ key: k }, function(err, doc) {
@@ -69,11 +69,11 @@ class KVBaseMongo {
69
69
  }
70
70
  else {
71
71
  if (doc) {
72
- console.log("Doc found with key: ", doc.key);
72
+ //console.log("Doc found with key: ", doc.key);
73
73
  resolve(doc.value);
74
74
  }
75
75
  else {
76
- console.log("No Doc found!");
76
+ //console.log("No Doc found!");
77
77
  resolve(null);
78
78
  }
79
79
  }
@@ -38,9 +38,9 @@ class MessageHandler {
38
38
  }
39
39
 
40
40
  async generateMessageObject(command) {
41
- if (this.log) {
42
- console.log("(wab) [MessageHandler] command: ", JSON.stringify(command));
43
- }
41
+ //if (this.log) {
42
+ //console.log("(wab) [MessageHandler] command: ", JSON.stringify(command));
43
+ //}
44
44
 
45
45
  let tiledeskCommandMessage = command.message;
46
46
  tiledeskCommandMessage.recipient = this.tiledeskChannelMessage.recipient;
@@ -59,7 +59,7 @@ class TiledeskAppsClient {
59
59
  if (callback) {
60
60
  callback(null, resbody);
61
61
  }
62
- console.log("[Tiledesk Apps Client] Installed!");
62
+ //console.log("[Tiledesk Apps Client] Installed!");
63
63
  resolve(resbody);
64
64
  }
65
65
  }, true);
@@ -119,7 +119,7 @@ class TiledeskAppsClient {
119
119
  if (callback) {
120
120
  callback(null, resbody);
121
121
  }
122
- console.log("[Tiledesk Apps Client] Uninstalled!");
122
+ //console.log("[Tiledesk Apps Client] Uninstalled!");
123
123
  resolve(resbody);
124
124
  }
125
125
  }, true);
@@ -132,9 +132,9 @@ class TiledeskAppsClient {
132
132
  // HTTP REQUEST
133
133
 
134
134
  static async myrequest(options, callback, log) {
135
- if (this.log) {
136
- console.log("[Tiledesk Subscription Client] Options: ", options);
137
- }
135
+ //if (this.log) {
136
+ //console.log("[Tiledesk Subscription Client] Options: ", options);
137
+ //}
138
138
  return await axios({
139
139
  url: options.url,
140
140
  method: options.method,
@@ -142,9 +142,9 @@ class TiledeskAppsClient {
142
142
  params: options.params,
143
143
  headers: options.headers
144
144
  }).then((res) => {
145
- if (this.log) {
146
- console.log("[Tiledesk Subscription Client] Response headers:\n", res.headers);
147
- }
145
+ //if (this.log) {
146
+ //console.log("[Tiledesk Subscription Client] Response headers:\n", res.headers);
147
+ //}
148
148
  if (res && res.status == 200 && res.data) {
149
149
  if (callback) {
150
150
  callback(null, res.data);
@@ -50,7 +50,7 @@ class TiledeskBotTester {
50
50
  async startBotConversation(body, code) {
51
51
 
52
52
  return new Promise( async (resolve, reject) => {
53
- console.log("(wab) [TiledeskBotTester] starting bot conversation");
53
+ //console.log("(wab) [TiledeskBotTester] starting bot conversation");
54
54
 
55
55
  let whatsappChannelMessage = body.entry[0].changes[0].value.messages[0];
56
56
  let whatsappContact = body.entry[0].changes[0].value.contacts[0];
@@ -66,7 +66,7 @@ class TiledeskBotTester {
66
66
  } else {
67
67
 
68
68
  if (!value) {
69
- console.log("No test info found on redis. Exit..");
69
+ //console.log("No test info found on redis. Exit..");
70
70
  reject("No test info found on redis");
71
71
  return;
72
72
 
@@ -86,7 +86,7 @@ class TiledeskBotTester {
86
86
  let settings = await this.db.get(CONTENT_KEY);
87
87
 
88
88
  if (!settings) {
89
- console.log("No settings found. Exit..");
89
+ //console.log("No settings found. Exit..");
90
90
  return reject("No settings found. Exit..")
91
91
  }
92
92
 
@@ -97,7 +97,7 @@ class TiledeskBotTester {
97
97
  const response = await this.tdChannel.sendAndAddBot(tiledeskJsonMessage, message_info, test_info.bot_id)
98
98
  return resolve(response)
99
99
  } else {
100
- console.log("(wab) No bot selected for test. Exit..")
100
+ //console.log("(wab) No bot selected for test. Exit..")
101
101
  return reject("Test skipped")
102
102
  }
103
103
 
@@ -66,7 +66,8 @@ class TiledeskChannel {
66
66
  //new_request_id = hased_request_id = "support-group-" + projectId + "-" + uuidv4() + "-" + sender_id + "-" + webhook_event.recipient.id;
67
67
 
68
68
  } else {
69
- console.log("(wab) [TiledeskChannel] Channel not supported")
69
+ //console.log("(wab) [TiledeskChannel] Channel not supported")
70
+ return null;
70
71
  }
71
72
 
72
73
  var payload = {
@@ -101,22 +102,22 @@ class TiledeskChannel {
101
102
  method: 'GET'
102
103
  }).then((response) => {
103
104
 
104
- if (this.log) {
105
- console.log("(wab) [TiledeskChannel] get request response: ", response.data);
106
- }
105
+ //if (this.log) {
106
+ // console.log("(wab) [TiledeskChannel] get request response: ", response.data);
107
+ //}
107
108
 
108
109
  let request_id;
109
110
  if (response.data.requests[0]) {
110
111
  request_id = response.data.requests[0].request_id;
111
- console.log("(wab) [TiledeskChannel] Old request_id: ", request_id);
112
+ //console.log("(wab) [TiledeskChannel] Old request_id: ", request_id);
112
113
  } else {
113
114
  request_id = new_request_id;
114
- console.log("(wab) [TiledeskChannel] New request_id: ", request_id);
115
+ //console.log("(wab) [TiledeskChannel] New request_id: ", request_id);
115
116
  }
116
117
 
117
- if (this.log) {
118
- console.log("(wab) [TiledeskChannel] tiledeskMessage:", tiledeskMessage);
119
- }
118
+ //if (this.log) {
119
+ //console.log("(wab) [TiledeskChannel] tiledeskMessage:", tiledeskMessage);
120
+ //}
120
121
 
121
122
 
122
123
  return axios({
@@ -129,9 +130,9 @@ class TiledeskChannel {
129
130
  method: 'POST'
130
131
  }).then((response) => {
131
132
 
132
- if (this.log) {
133
- console.log("(wab) [TiledeskChannel] send message response: ", response.data);
134
- }
133
+ //if (this.log) {
134
+ // console.log("(wab) [TiledeskChannel] send message response: ", response.data);
135
+ //}
135
136
 
136
137
  return response.data;
137
138
 
@@ -159,9 +160,9 @@ class TiledeskChannel {
159
160
  },
160
161
  method: 'GET'
161
162
  }).then((response) => {
162
- if (this.log) {
163
- console.log("(wab) [TiledeskChannel] get departments response.data: ", response.data)
164
- }
163
+ //if (this.log) {
164
+ // console.log("(wab) [TiledeskChannel] get departments response.data: ", response.data)
165
+ //}
165
166
  return response.data;
166
167
  }).catch((err) => {
167
168
  console.error("(wab) [TiledeskChannel] get departments error: ", err);
@@ -181,7 +182,7 @@ class TiledeskChannel {
181
182
  channel = messageInfo.whatsapp;
182
183
  new_request_id = "support-group-" + this.settings.project_id + "-" + uuidv4().substring(0, 8) + "-wab-" + channel.phone_number_id + "-" + channel.from;
183
184
  } else {
184
- console.log("(wab) [TiledeskChannel] Channel not supported")
185
+ //console.log("(wab) [TiledeskChannel] Channel not supported")
185
186
  return null;
186
187
  }
187
188
 
@@ -58,7 +58,7 @@ class TiledeskSubscriptionClient {
58
58
  if (callback) {
59
59
  callback(null, resbody);
60
60
  }
61
- console.log("(wab) [TiledeskSubscriptionClient] Subscribed");
61
+ //console.log("(wab) [TiledeskSubscriptionClient] Subscribed");
62
62
  resolve(resbody);
63
63
  }
64
64
  }, true);
@@ -91,7 +91,7 @@ class TiledeskSubscriptionClient {
91
91
  if (callback) {
92
92
  callback(null, resbody);
93
93
  }
94
- console.log("(wab) [TiledeskSubscriptionClient] Unsubscribed");
94
+ //console.log("(wab) [TiledeskSubscriptionClient] Unsubscribed");
95
95
  resolve(resbody);
96
96
  }
97
97
  }, true);
@@ -112,7 +112,7 @@ class TiledeskSubscriptionClient {
112
112
  headers: options.headers
113
113
  }).then((res) => {
114
114
  if (this.log) {
115
- console.log("(wab) [TiledeskSubscriptionClient] Response headers:\n", res.headers);
115
+ //console.log("(wab) [TiledeskSubscriptionClient] Response headers:\n", res.headers);
116
116
  }
117
117
  if (res && res.status == 200 && res.data) {
118
118
  if (callback) {
@@ -41,11 +41,11 @@ class TiledeskWhatsapp {
41
41
  }
42
42
 
43
43
  async sendMessage(phone_number_id, message) {
44
- if (this.log) {
45
- console.log("(wab) [TiledeskWhatsapp] Sending message...", message);
46
- } else {
47
- console.log("(wab) [TiledeskWhatsapp] Sending message...");
48
- }
44
+ //if (this.log) {
45
+ //console.log("(wab) [TiledeskWhatsapp] Sending message...", message);
46
+ //} else {
47
+ //console.log("(wab) [TiledeskWhatsapp] Sending message...");
48
+ //}
49
49
 
50
50
  return await axios({
51
51
  url: this.GRAPH_URL + phone_number_id + "/messages?access_token=" + this.token,
@@ -55,7 +55,7 @@ class TiledeskWhatsapp {
55
55
  data: message,
56
56
  method: "POST"
57
57
  }).then((response) => {
58
- console.log("(wab) [TiledeskWhatsapp] Message sent!");
58
+ //console.log("(wab) [TiledeskWhatsapp] Message sent!");
59
59
  return response
60
60
  }).catch((err) => {
61
61
  console.error("(wab) [TiledeskWhatsapp] Send message error: ", err.response.data);
@@ -81,7 +81,7 @@ class TiledeskWhatsapp {
81
81
 
82
82
  let example_path = path.join(__dirname, '..', 'tmp', type);
83
83
  const writeStream = fs.createWriteStream(example_path);
84
- console.log("(wab) [TiledeskWhatsapp] Downloading file...", example_path);
84
+ //console.log("(wab) [TiledeskWhatsapp] Downloading file...", example_path);
85
85
 
86
86
  return await axios({
87
87
  url: download_url,
@@ -98,27 +98,27 @@ class TiledeskWhatsapp {
98
98
  writeStream.on('error', err => {
99
99
  error = err;
100
100
  writeStream.close();
101
- console.log("(wab) [TiledeskWhatsapp] Download failed")
101
+ //console.log("(wab) [TiledeskWhatsapp] Download failed")
102
102
  reject(err);
103
103
  });
104
104
  writeStream.on('close', () => {
105
105
  if (!error) {
106
- console.log("(wab) [TiledeskWhatsapp] Download completed")
106
+ //console.log("(wab) [TiledeskWhatsapp] Download completed")
107
107
  resolve(type);
108
108
  }
109
109
  })
110
110
  })
111
111
  }).catch((err) => {
112
- console.log("(wab) [TiledeskWhatsapp] download file error: ", err.data);
112
+ console.error("(wab) [TiledeskWhatsapp] download file error: ", err.data);
113
113
  })
114
114
  }).catch((err) => {
115
- console.log("(wab) [TiledeskWhatsapp] get file error: ", err.data);
115
+ console.error("(wab) [TiledeskWhatsapp] get file error: ", err.data);
116
116
  })
117
117
  }
118
118
 
119
119
  async uploadMedia(path, type) {
120
120
  let url = "https://tiledesk-server-pre.herokuapp.com/" + type + "/public";
121
- console.log("(wab) [TiledeskWhatsapp] Uploading file...");
121
+ //console.log("(wab) [TiledeskWhatsapp] Uploading file...");
122
122
 
123
123
  const form = new FormData();
124
124
  form.append('file', fs.createReadStream(path));
@@ -131,9 +131,9 @@ class TiledeskWhatsapp {
131
131
  }
132
132
 
133
133
  return await axios.post(url, form, request_config).then((response) => {
134
- if (this.log) {
135
- console.log("(wab) [TiledeskWhatsapp] upload response: ", response.data);
136
- }
134
+ //if (this.log) {
135
+ //console.log("(wab) [TiledeskWhatsapp] upload response: ", response.data);
136
+ //}
137
137
 
138
138
  if (type == "images") {
139
139
  let image_url = "https://tiledesk-server-pre.herokuapp.com/images/?path=" + response.data.filename;
@@ -50,9 +50,9 @@ const path = require('path');
50
50
  */
51
51
  toWhatsapp(tiledeskChannelMessage, whatsapp_receiver) {
52
52
 
53
- if (this.log) {
54
- console.log("(wab) [Translator] tiledesk message: ", JSON.stringify(tiledeskChannelMessage));
55
- }
53
+ //if (this.log) {
54
+ // console.log("(wab) [Translator] tiledesk message: ", JSON.stringify(tiledeskChannelMessage));
55
+ //}
56
56
 
57
57
  let text = '';
58
58
  if (tiledeskChannelMessage.text) {
@@ -100,7 +100,7 @@ const path = require('path');
100
100
  }
101
101
 
102
102
  else {
103
- console.log("(wab) [Translator] file type not supported")
103
+ //console.log("(wab) [Translator] file type not supported")
104
104
  return null
105
105
  }
106
106
 
@@ -280,9 +280,9 @@ const path = require('path');
280
280
 
281
281
  async toTiledesk(whatsappChannelMessage, from, media_url) {
282
282
 
283
- if (this.log) {
284
- console.log("(wab) [Translator] whatsapp message: ", JSON.stringify(whatsappChannelMessage));
285
- }
283
+ //if (this.log) {
284
+ //console.log("(wab) [Translator] whatsapp message: ", JSON.stringify(whatsappChannelMessage));
285
+ //}
286
286
 
287
287
  // text message
288
288
  if (whatsappChannelMessage.type == 'text') {