@tiledesk/tiledesk-whatsapp-connector 0.1.73 → 0.1.75
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 +91 -143
- package/package.json +1 -1
- package/routes/api.js +6 -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
|
-
|
|
506
|
-
|
|
507
|
-
|
|
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
|
-
|
|
531
|
-
|
|
532
|
-
|
|
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
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
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
|
-
|
|
603
|
-
|
|
604
|
-
|
|
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
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
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
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
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,13 @@ 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");
|
|
723
646
|
|
|
724
647
|
var tiledeskChannelMessage = req.body.payload;
|
|
725
648
|
winston.debug("(wab) tiledeskChannelMessage: ", tiledeskChannelMessage);
|
|
726
649
|
var project_id = req.body.payload.id_project;
|
|
727
|
-
|
|
650
|
+
|
|
728
651
|
// get settings from mongo
|
|
729
652
|
let CONTENT_KEY = "whatsapp-" + project_id;
|
|
730
653
|
let settings = await db.get(CONTENT_KEY);
|
|
@@ -776,7 +699,9 @@ router.post("/tiledesk", async (req, res) => {
|
|
|
776
699
|
}
|
|
777
700
|
|
|
778
701
|
if (!phone_number_id) {
|
|
779
|
-
return res
|
|
702
|
+
return res
|
|
703
|
+
.status(400)
|
|
704
|
+
.send({ success: false, message: "Phone number id undefined" });
|
|
780
705
|
}
|
|
781
706
|
|
|
782
707
|
/*
|
|
@@ -861,7 +786,12 @@ router.post("/tiledesk", async (req, res) => {
|
|
|
861
786
|
twClient
|
|
862
787
|
.sendMessage(phone_number_id, whatsappJsonMessage)
|
|
863
788
|
.then((response) => {
|
|
864
|
-
winston.verbose(
|
|
789
|
+
winston.verbose(
|
|
790
|
+
"(wab) Message sent to WhatsApp! " +
|
|
791
|
+
response.status +
|
|
792
|
+
" " +
|
|
793
|
+
response.statusText
|
|
794
|
+
);
|
|
865
795
|
i += 1;
|
|
866
796
|
if (i < commands.length) {
|
|
867
797
|
execute(commands[i]);
|
|
@@ -892,7 +822,6 @@ router.post("/tiledesk", async (req, res) => {
|
|
|
892
822
|
}
|
|
893
823
|
}
|
|
894
824
|
execute(commands[0]);
|
|
895
|
-
|
|
896
825
|
} else if (tiledeskChannelMessage.text || tiledeskChannelMessage.metadata) {
|
|
897
826
|
let whatsappJsonMessage = await tlr.toWhatsapp(
|
|
898
827
|
tiledeskChannelMessage,
|
|
@@ -911,22 +840,38 @@ router.post("/tiledesk", async (req, res) => {
|
|
|
911
840
|
twClient
|
|
912
841
|
.sendMessage(phone_number_id, whatsappJsonMessage)
|
|
913
842
|
.then((response) => {
|
|
914
|
-
winston.verbose(
|
|
843
|
+
winston.verbose(
|
|
844
|
+
"(wab) Message sent to WhatsApp! " +
|
|
845
|
+
response.status +
|
|
846
|
+
" " +
|
|
847
|
+
response.statusText
|
|
848
|
+
);
|
|
915
849
|
return res.sendStatus(200);
|
|
916
850
|
})
|
|
917
851
|
.catch((err) => {
|
|
918
|
-
winston.error("(wab) error send message: ", err);
|
|
919
|
-
return res
|
|
852
|
+
//winston.error("(wab) error send message: ", err);
|
|
853
|
+
return res
|
|
854
|
+
.status(400)
|
|
855
|
+
.send({ success: false, error: "Template not existing" });
|
|
920
856
|
});
|
|
921
857
|
} else {
|
|
922
858
|
winston.error("(wab) Whatsapp Json Message is undefined!");
|
|
923
|
-
return res
|
|
859
|
+
return res
|
|
860
|
+
.status(400)
|
|
861
|
+
.send({
|
|
862
|
+
success: false,
|
|
863
|
+
error: "An error occurred during message translation",
|
|
864
|
+
});
|
|
924
865
|
}
|
|
925
866
|
} else {
|
|
926
867
|
winston.debug("(wab) no command, no text --> skip");
|
|
927
|
-
return res
|
|
868
|
+
return res
|
|
869
|
+
.sendStatus(400)
|
|
870
|
+
.send({
|
|
871
|
+
success: false,
|
|
872
|
+
error: "No command or text specified. Skip message.",
|
|
873
|
+
});
|
|
928
874
|
}
|
|
929
|
-
|
|
930
875
|
});
|
|
931
876
|
|
|
932
877
|
// Endpoint for Whatsapp Business
|
|
@@ -935,18 +880,17 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
935
880
|
// Parse the request body from the POST
|
|
936
881
|
let project_id = req.params.project_id;
|
|
937
882
|
winston.verbose("(wab) Message received from WhatsApp");
|
|
938
|
-
|
|
883
|
+
|
|
939
884
|
// Check the Incoming webhook message
|
|
940
885
|
// info on WhatsApp text message payload: https://developers.facebook.com/docs/whatsapp/cloud-api/webhooks/payload-examples#text-messages
|
|
941
886
|
if (req.body.object) {
|
|
942
|
-
|
|
943
887
|
let CONTENT_KEY = "whatsapp-" + project_id;
|
|
944
888
|
let settings = await db.get(CONTENT_KEY);
|
|
945
889
|
winston.debug("(wab) settings: ", settings);
|
|
946
|
-
|
|
890
|
+
|
|
947
891
|
if (!settings) {
|
|
948
892
|
winston.warn("(wab) settings not found for project_id: " + project_id);
|
|
949
|
-
winston.warn("(wab) settings not found for request: ", req.body)
|
|
893
|
+
winston.warn("(wab) settings not found for request: ", req.body);
|
|
950
894
|
return res.sendStatus(200);
|
|
951
895
|
}
|
|
952
896
|
|
|
@@ -972,9 +916,11 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
972
916
|
winston.verbose("(wab) Skip system message");
|
|
973
917
|
return res.sendStatus(200);
|
|
974
918
|
}
|
|
975
|
-
|
|
919
|
+
|
|
976
920
|
if (req.body.entry[0].id !== settings.business_account_id) {
|
|
977
|
-
winston.verbose(
|
|
921
|
+
winston.verbose(
|
|
922
|
+
"(wab) Skip message with waba that not belong to Tiledesk project id"
|
|
923
|
+
);
|
|
978
924
|
return res.sendStatus(200);
|
|
979
925
|
}
|
|
980
926
|
|
|
@@ -1038,7 +984,7 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
1038
984
|
lastname: " ",
|
|
1039
985
|
},
|
|
1040
986
|
};
|
|
1041
|
-
|
|
987
|
+
|
|
1042
988
|
let tiledeskJsonMessage;
|
|
1043
989
|
|
|
1044
990
|
/*
|
|
@@ -1147,8 +1093,8 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
1147
1093
|
if (!filename) {
|
|
1148
1094
|
winston.debug(
|
|
1149
1095
|
"(wab) Unable to download media with id " +
|
|
1150
|
-
|
|
1151
|
-
|
|
1096
|
+
media.id +
|
|
1097
|
+
". Message not sent."
|
|
1152
1098
|
);
|
|
1153
1099
|
return res
|
|
1154
1100
|
.status(500)
|
|
@@ -1173,8 +1119,8 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
1173
1119
|
if (!filename) {
|
|
1174
1120
|
winston.debug(
|
|
1175
1121
|
"(wab) Unable to download media with id " +
|
|
1176
|
-
|
|
1177
|
-
|
|
1122
|
+
media.id +
|
|
1123
|
+
". Message not sent."
|
|
1178
1124
|
);
|
|
1179
1125
|
return res
|
|
1180
1126
|
.status(500)
|
|
@@ -1199,8 +1145,8 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
1199
1145
|
if (!filename) {
|
|
1200
1146
|
winston.debug(
|
|
1201
1147
|
"(wab) Unable to download media with id " +
|
|
1202
|
-
|
|
1203
|
-
|
|
1148
|
+
media.id +
|
|
1149
|
+
". Message not sent."
|
|
1204
1150
|
);
|
|
1205
1151
|
return res
|
|
1206
1152
|
.status(500)
|
|
@@ -1225,8 +1171,8 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
1225
1171
|
if (!filename) {
|
|
1226
1172
|
winston.debug(
|
|
1227
1173
|
"(wab) Unable to download media with id " +
|
|
1228
|
-
|
|
1229
|
-
|
|
1174
|
+
media.id +
|
|
1175
|
+
". Message not sent."
|
|
1230
1176
|
);
|
|
1231
1177
|
return res
|
|
1232
1178
|
.status(500)
|
|
@@ -1374,11 +1320,9 @@ router.post("/newtest", async (req, res) => {
|
|
|
1374
1320
|
let key = "bottest:" + short_uid;
|
|
1375
1321
|
|
|
1376
1322
|
if (!redis_client) {
|
|
1377
|
-
return res
|
|
1378
|
-
.
|
|
1379
|
-
|
|
1380
|
-
message: "Test it out on Whatsapp not available. Redis not ready.",
|
|
1381
|
-
});
|
|
1323
|
+
return res.status(500).send({
|
|
1324
|
+
message: "Test it out on Whatsapp not available. Redis not ready.",
|
|
1325
|
+
});
|
|
1382
1326
|
}
|
|
1383
1327
|
|
|
1384
1328
|
await redis_client.set(key, JSON.stringify(info), "EX", 604800);
|
|
@@ -1452,6 +1396,7 @@ router.get("/templates/detail", async (req, res) => {
|
|
|
1452
1396
|
}
|
|
1453
1397
|
});
|
|
1454
1398
|
|
|
1399
|
+
/*
|
|
1455
1400
|
router.get("/templates/:project_id", async (req, res) => {
|
|
1456
1401
|
winston.verbose("(wab) /templates");
|
|
1457
1402
|
|
|
@@ -1493,6 +1438,7 @@ router.get("/templates/:project_id", async (req, res) => {
|
|
|
1493
1438
|
});
|
|
1494
1439
|
}
|
|
1495
1440
|
});
|
|
1441
|
+
*/
|
|
1496
1442
|
|
|
1497
1443
|
/*
|
|
1498
1444
|
router.get("/ext/templates/:project_id", async (req, res) => {
|
|
@@ -1613,17 +1559,19 @@ async function startApp(settings, callback) {
|
|
|
1613
1559
|
}
|
|
1614
1560
|
|
|
1615
1561
|
if (!settings.JOB_TOPIC_EXCHANGE) {
|
|
1616
|
-
winston.info(
|
|
1562
|
+
winston.info(
|
|
1563
|
+
"(wab) JOB_TOPIC_EXCHANGE should be present. Using default value"
|
|
1564
|
+
);
|
|
1617
1565
|
JOB_TOPIC_EXCHANGE = "tiledesk-whatsapp";
|
|
1618
1566
|
} else {
|
|
1619
1567
|
JOB_TOPIC_EXCHANGE = settings.JOB_TOPIC_EXCHANGE;
|
|
1620
1568
|
winston.info("(wab) JOB_TOPIC_EXCHANGE is present");
|
|
1621
1569
|
}
|
|
1622
|
-
|
|
1570
|
+
|
|
1623
1571
|
if (settings.BRAND_NAME) {
|
|
1624
|
-
BRAND_NAME = settings.BRAND_NAME
|
|
1572
|
+
BRAND_NAME = settings.BRAND_NAME;
|
|
1625
1573
|
}
|
|
1626
|
-
|
|
1574
|
+
|
|
1627
1575
|
// // For test only
|
|
1628
1576
|
// if (settings.LOG_MONGODB_URL) {
|
|
1629
1577
|
// /**
|
|
@@ -1661,11 +1609,11 @@ async function startApp(settings, callback) {
|
|
|
1661
1609
|
if (callback) {
|
|
1662
1610
|
callback(null);
|
|
1663
1611
|
}
|
|
1664
|
-
})
|
|
1612
|
+
});
|
|
1665
1613
|
} else {
|
|
1666
1614
|
db.connect(settings.MONGODB_URL, () => {
|
|
1667
1615
|
winston.info("(wab) KVBaseMongo successfully connected.");
|
|
1668
|
-
|
|
1616
|
+
|
|
1669
1617
|
if (callback) {
|
|
1670
1618
|
callback(null);
|
|
1671
1619
|
}
|
package/package.json
CHANGED
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
|
-
|
|
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();
|
|
@@ -159,7 +162,8 @@ router.post('/tiledesk/broadcast', async (req, res) => {
|
|
|
159
162
|
phone_number_id: phone_number_id,
|
|
160
163
|
transaction_id: transaction_id,
|
|
161
164
|
template: template,
|
|
162
|
-
settings: settings
|
|
165
|
+
settings: settings,
|
|
166
|
+
broadcast: broadcast
|
|
163
167
|
};
|
|
164
168
|
|
|
165
169
|
winston.debug('(wab) data_To_scheduler: ', data_To_scheduler);
|