@tiledesk/tiledesk-whatsapp-connector 0.1.52 → 0.1.54
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 +145 -11
- package/models/WhatsappLog.js +36 -0
- package/package.json +2 -1
- package/routes/api.js +37 -54
- package/template/detail.html +11 -11
- package/tiledesk/Scheduler.js +42 -0
- package/tiledesk/TemplateManager.js +6 -1
- package/tiledesk/TiledeskSubscriptionClient.js +2 -1
- package/tiledesk/TiledeskWhatsappTranslator.js +1 -2
- package/tiledesk/WhatsappLogger.js +48 -0
package/index.js
CHANGED
|
@@ -11,6 +11,7 @@ const { v4: uuidv4 } = require('uuid');
|
|
|
11
11
|
const cors = require('cors');
|
|
12
12
|
var winston = require('./winston');
|
|
13
13
|
const url = require('url');
|
|
14
|
+
const mongoose = require('mongoose');
|
|
14
15
|
|
|
15
16
|
//const ext = require('./routes/ext')
|
|
16
17
|
//const extRoute = ext.router;
|
|
@@ -27,6 +28,7 @@ const { TiledeskAppsClient } = require('./tiledesk/TiledeskAppsClient');
|
|
|
27
28
|
const { MessageHandler } = require('./tiledesk/MessageHandler');
|
|
28
29
|
const { TiledeskBotTester } = require('./tiledesk/TiledeskBotTester');
|
|
29
30
|
const { TemplateManager } = require('./tiledesk/TemplateManager');
|
|
31
|
+
const { WhatsappLogger } = require('./tiledesk/WhatsappLogger');
|
|
30
32
|
|
|
31
33
|
// mongo
|
|
32
34
|
const { KVBaseMongo } = require('./tiledesk/KVBaseMongo');
|
|
@@ -58,6 +60,8 @@ let REDIS_HOST = null;
|
|
|
58
60
|
let REDIS_PORT = null;
|
|
59
61
|
let REDIS_PASSWORD = null;
|
|
60
62
|
let BASE_FILE_URL = null;
|
|
63
|
+
let AMQP_MANAGER_URL = null;
|
|
64
|
+
|
|
61
65
|
|
|
62
66
|
// Handlebars register helpers
|
|
63
67
|
handlebars.registerHelper('isEqual', (a, b) => {
|
|
@@ -497,14 +501,14 @@ router.post('/tiledesk/broadcast', async (req, res) => {
|
|
|
497
501
|
let tm = new TemplateManager({ token: settings.wab_token, business_account_id: settings.business_account_id, GRAPH_URL: GRAPH_URL })
|
|
498
502
|
const tlr = new TiledeskWhatsappTranslator();
|
|
499
503
|
const twClient = new TiledeskWhatsapp({ token: settings.wab_token, GRAPH_URL: GRAPH_URL, API_URL: API_URL, BASE_FILE_URL: BASE_FILE_URL });
|
|
500
|
-
|
|
504
|
+
|
|
501
505
|
let response = await tm.getTemplates();
|
|
502
506
|
let templates = response.data;
|
|
503
|
-
|
|
507
|
+
|
|
504
508
|
let selected_template = templates.find(t => t.name === tiledeskChannelMessage.attributes.attachment.template.name);
|
|
505
509
|
|
|
506
510
|
let params_object = await tm.generateParamsObject(selected_template);
|
|
507
|
-
|
|
511
|
+
|
|
508
512
|
tiledeskChannelMessage.attributes.attachment.template.params = params_object;
|
|
509
513
|
winston.verbose("(wab) --> tiledeskChannelMessage: ", tiledeskChannelMessage)
|
|
510
514
|
|
|
@@ -530,8 +534,8 @@ router.post('/tiledesk/broadcast', async (req, res) => {
|
|
|
530
534
|
if (i < receiver_list.length) {
|
|
531
535
|
execute(receiver_list[i])
|
|
532
536
|
} else {
|
|
533
|
-
return res.status(200).send({ success: true, message: "Broadcast terminated", messages_sent: messages_sent, errors: errors });
|
|
534
537
|
winston.debug("(wab) End of list")
|
|
538
|
+
return res.status(200).send({ success: true, message: "Broadcast terminated", messages_sent: messages_sent, errors: errors });
|
|
535
539
|
}
|
|
536
540
|
}).catch((err) => {
|
|
537
541
|
winston.error("(wab) error send message: " + err.response.data.error.message);
|
|
@@ -547,7 +551,7 @@ router.post('/tiledesk/broadcast', async (req, res) => {
|
|
|
547
551
|
}
|
|
548
552
|
execute(receiver_list[0]);
|
|
549
553
|
}
|
|
550
|
-
|
|
554
|
+
|
|
551
555
|
})
|
|
552
556
|
|
|
553
557
|
|
|
@@ -662,7 +666,19 @@ router.post('/tiledesk', async (req, res) => {
|
|
|
662
666
|
let recipient_id = tiledeskChannelMessage.recipient;
|
|
663
667
|
let sender = tiledeskChannelMessage.sender;
|
|
664
668
|
let whatsapp_receiver = recipient_id.substring(recipient_id.lastIndexOf("-") + 1);
|
|
665
|
-
|
|
669
|
+
|
|
670
|
+
let phone_number_id;
|
|
671
|
+
if (attributes && attributes.whatsapp_phone_number_id) {
|
|
672
|
+
phone_number_id = attributes.whatsapp_phone_number_id;
|
|
673
|
+
} else {
|
|
674
|
+
phone_number_id = recipient_id.substring(recipient_id.lastIndexOf("wab-") + 4, recipient_id.lastIndexOf("-"));
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
if (!phone_number_id) {
|
|
678
|
+
return res.status(400).send({ success: false, message: "Phone number id undefined"})
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
console.log("phone_number_id: ", phone_number_id)
|
|
666
682
|
|
|
667
683
|
/*
|
|
668
684
|
if (settings.expired &&
|
|
@@ -679,7 +695,7 @@ router.post('/tiledesk', async (req, res) => {
|
|
|
679
695
|
winston.verbose("settings expired: " + settings.expired);
|
|
680
696
|
let tiledeskJsonMessage = {
|
|
681
697
|
text: 'Expired. Upgrade Plan.',
|
|
682
|
-
sender:
|
|
698
|
+
sender: "system",
|
|
683
699
|
senderFullname: "System",
|
|
684
700
|
attributes: {
|
|
685
701
|
subtype: 'info'
|
|
@@ -767,13 +783,13 @@ router.post('/tiledesk', async (req, res) => {
|
|
|
767
783
|
twClient.sendMessage(phone_number_id, whatsappJsonMessage).then((response) => {
|
|
768
784
|
winston.verbose("(wab) Message sent to WhatsApp! " + response.status + " " + response.statusText);
|
|
769
785
|
}).catch((err) => {
|
|
770
|
-
res.status(400).send({ success: false, error: "il template non esiste" });
|
|
771
786
|
winston.error("(wab) error send message: ", err);
|
|
787
|
+
res.status(400).send({ success: false, error: "Template not existing" });
|
|
772
788
|
})
|
|
773
789
|
|
|
774
790
|
} else {
|
|
775
|
-
res.status(400).send({ success: false, error: "il template non esiste" });
|
|
776
791
|
winston.error("(wab) Whatsapp Json Message is undefined!")
|
|
792
|
+
res.status(400).send({ success: false, error: "An error occurred during message translation" });
|
|
777
793
|
}
|
|
778
794
|
|
|
779
795
|
} else {
|
|
@@ -835,7 +851,8 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
835
851
|
})
|
|
836
852
|
|
|
837
853
|
// Standard message
|
|
838
|
-
}
|
|
854
|
+
}
|
|
855
|
+
else {
|
|
839
856
|
|
|
840
857
|
let firstname = req.body.entry[0].changes[0].value.contacts[0].profile.name;
|
|
841
858
|
|
|
@@ -851,6 +868,7 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
851
868
|
|
|
852
869
|
let tiledeskJsonMessage;
|
|
853
870
|
|
|
871
|
+
/*
|
|
854
872
|
if ((whatsappChannelMessage.type == 'text')) {
|
|
855
873
|
winston.debug("(wab) message type: text")
|
|
856
874
|
tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname);
|
|
@@ -932,6 +950,86 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
932
950
|
// unsupported. Try anyway to send something.
|
|
933
951
|
winston.debug("(wab) unsupported message")
|
|
934
952
|
}
|
|
953
|
+
*/
|
|
954
|
+
|
|
955
|
+
|
|
956
|
+
|
|
957
|
+
////////////////////////////////////////////////////
|
|
958
|
+
|
|
959
|
+
|
|
960
|
+
|
|
961
|
+
if ((whatsappChannelMessage.type == 'image') || (whatsappChannelMessage.type == 'video') || (whatsappChannelMessage.type == 'document') || (whatsappChannelMessage.type == 'audio')) {
|
|
962
|
+
let media;
|
|
963
|
+
const util = new TiledeskWhatsapp({ token: settings.wab_token, GRAPH_URL: GRAPH_URL, API_URL: API_URL, BASE_FILE_URL: BASE_FILE_URL })
|
|
964
|
+
|
|
965
|
+
if (whatsappChannelMessage.type == 'image') {
|
|
966
|
+
media = whatsappChannelMessage.image;
|
|
967
|
+
const filename = await util.downloadMedia(media.id);
|
|
968
|
+
if (!filename) {
|
|
969
|
+
winston.debug("(wab) Unable to download media with id " + media.id + ". Message not sent.");
|
|
970
|
+
return res.status(500).send({ success: false, error: "unable to download media" })
|
|
971
|
+
}
|
|
972
|
+
let file_path = path.join(__dirname, 'tmp', filename);
|
|
973
|
+
|
|
974
|
+
const image_url = await util.uploadMedia(file_path, "images");
|
|
975
|
+
winston.debug("(wab) image_url: " + image_url)
|
|
976
|
+
|
|
977
|
+
tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname, image_url);
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
if (whatsappChannelMessage.type == 'video') {
|
|
981
|
+
media = whatsappChannelMessage.video;
|
|
982
|
+
|
|
983
|
+
const filename = await util.downloadMedia(media.id);
|
|
984
|
+
if (!filename) {
|
|
985
|
+
winston.debug("(wab) Unable to download media with id " + media.id + ". Message not sent.");
|
|
986
|
+
return res.status(500).send({ success: false, error: "unable to download media" })
|
|
987
|
+
}
|
|
988
|
+
let file_path = path.join(__dirname, 'tmp', filename);
|
|
989
|
+
|
|
990
|
+
const media_url = await util.uploadMedia(file_path, "files");
|
|
991
|
+
winston.debug("(wab) media_url: " + media_url)
|
|
992
|
+
|
|
993
|
+
tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname, media_url);
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
if (whatsappChannelMessage.type == 'document') {
|
|
997
|
+
media = whatsappChannelMessage.document;
|
|
998
|
+
|
|
999
|
+
const filename = await util.downloadMedia(media.id);
|
|
1000
|
+
if (!filename) {
|
|
1001
|
+
winston.debug("(wab) Unable to download media with id " + media.id + ". Message not sent.");
|
|
1002
|
+
return res.status(500).send({ success: false, error: "unable to download media" })
|
|
1003
|
+
}
|
|
1004
|
+
let file_path = path.join(__dirname, 'tmp', filename);
|
|
1005
|
+
|
|
1006
|
+
const media_url = await util.uploadMedia(file_path, "files");
|
|
1007
|
+
winston.debug("(wab) media_url: " + media_url)
|
|
1008
|
+
|
|
1009
|
+
tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname, media_url);
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
if (whatsappChannelMessage.type == 'audio') {
|
|
1013
|
+
media = whatsappChannelMessage.audio;
|
|
1014
|
+
|
|
1015
|
+
const filename = await util.downloadMedia(media.id);
|
|
1016
|
+
if (!filename) {
|
|
1017
|
+
winston.debug("(wab) Unable to download media with id " + media.id + ". Message not sent.");
|
|
1018
|
+
return res.status(500).send({ success: false, error: "unable to download media" })
|
|
1019
|
+
}
|
|
1020
|
+
let file_path = path.join(__dirname, 'tmp', filename);
|
|
1021
|
+
|
|
1022
|
+
const media_url = await util.uploadMedia(file_path, "files");
|
|
1023
|
+
winston.debug("(wab) media_url: " + media_url)
|
|
1024
|
+
|
|
1025
|
+
tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname, media_url);
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
}
|
|
1029
|
+
else {
|
|
1030
|
+
winston.debug("(wab) message type: ", whatsappChannelMessage.type)
|
|
1031
|
+
tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname);
|
|
1032
|
+
}
|
|
935
1033
|
|
|
936
1034
|
if (tiledeskJsonMessage) {
|
|
937
1035
|
winston.verbose("(wab) tiledeskJsonMessage: ", tiledeskJsonMessage);
|
|
@@ -944,6 +1042,29 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
944
1042
|
|
|
945
1043
|
}
|
|
946
1044
|
}
|
|
1045
|
+
|
|
1046
|
+
if (req.body.entry &&
|
|
1047
|
+
req.body.entry[0].changes &&
|
|
1048
|
+
req.body.entry[0].changes[0] &&
|
|
1049
|
+
req.body.entry[0].changes[0].value.statuses &&
|
|
1050
|
+
req.body.entry[0].changes[0].value.statuses[0]
|
|
1051
|
+
) {
|
|
1052
|
+
let whatsappStatusMessage = req.body.entry[0].changes[0].value.statuses[0];
|
|
1053
|
+
winston.verbose("(wab) whatsappStatusMessage: ", whatsappStatusMessage);
|
|
1054
|
+
|
|
1055
|
+
let message_id = whatsappStatusMessage.id;
|
|
1056
|
+
let status = whatsappStatusMessage.status;
|
|
1057
|
+
let error = null;
|
|
1058
|
+
if (whatsappStatusMessage.errors) {
|
|
1059
|
+
error = whatsappStatusMessage.errors[0].title;
|
|
1060
|
+
}
|
|
1061
|
+
winston.verbose("(wab) update status in " + status+ " for message_id " + message_id);
|
|
1062
|
+
|
|
1063
|
+
const wl = new WhatsappLogger();
|
|
1064
|
+
wl.updateMessageStatus(message_id, status, error);
|
|
1065
|
+
|
|
1066
|
+
}
|
|
1067
|
+
|
|
947
1068
|
res.sendStatus(200);
|
|
948
1069
|
|
|
949
1070
|
} else {
|
|
@@ -1230,6 +1351,18 @@ async function startApp(settings, callback) {
|
|
|
1230
1351
|
winston.info("(wab) Missing redis parameters --> Test it out on WhatsApp disabled");
|
|
1231
1352
|
}
|
|
1232
1353
|
|
|
1354
|
+
if (!settings.AMQP_MANAGER_URL) {
|
|
1355
|
+
winston.error("(wab api) AMQP_MANAGER_URL is mandatory (?). Exit...");
|
|
1356
|
+
} else {
|
|
1357
|
+
AMQP_MANAGER_URL = settings.AMQP_MANAGER_URL;
|
|
1358
|
+
winston.info("(wab api) AMQP_MANAGER_URL is present");
|
|
1359
|
+
}
|
|
1360
|
+
|
|
1361
|
+
mongoose.connect(process.env.MONGODB_URL)
|
|
1362
|
+
.then(() => { winston.info("Mongoose DB Connected") })
|
|
1363
|
+
.catch((err) => { winston.error("(Mongoose) Unable to connect with MongoDB ", err)
|
|
1364
|
+
})
|
|
1365
|
+
|
|
1233
1366
|
db.connect(settings.MONGODB_URL, () => {
|
|
1234
1367
|
winston.info("(wab) KVBaseMongo successfully connected.");
|
|
1235
1368
|
|
|
@@ -1242,7 +1375,8 @@ async function startApp(settings, callback) {
|
|
|
1242
1375
|
DB: db,
|
|
1243
1376
|
API_URL: API_URL,
|
|
1244
1377
|
GRAPH_URL: GRAPH_URL,
|
|
1245
|
-
BASE_FILE_URL: BASE_FILE_URL
|
|
1378
|
+
BASE_FILE_URL: BASE_FILE_URL,
|
|
1379
|
+
AMQP_MANAGER_URL: AMQP_MANAGER_URL
|
|
1246
1380
|
}, (err) => {
|
|
1247
1381
|
if (!err) {
|
|
1248
1382
|
winston.info("(wab) API route successfully started.")
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const mongoose = require('mongoose');
|
|
2
|
+
|
|
3
|
+
const MessageLogSchema = mongoose.Schema({
|
|
4
|
+
json_message: {
|
|
5
|
+
type: Object,
|
|
6
|
+
required: true
|
|
7
|
+
},
|
|
8
|
+
transaction_id: {
|
|
9
|
+
type: String,
|
|
10
|
+
required: true
|
|
11
|
+
},
|
|
12
|
+
message_id: {
|
|
13
|
+
type: String,
|
|
14
|
+
required: false
|
|
15
|
+
},
|
|
16
|
+
status: {
|
|
17
|
+
type: String,
|
|
18
|
+
required: true
|
|
19
|
+
},
|
|
20
|
+
status_code: {
|
|
21
|
+
type: Number,
|
|
22
|
+
required: true
|
|
23
|
+
},
|
|
24
|
+
error: {
|
|
25
|
+
type: String
|
|
26
|
+
},
|
|
27
|
+
timestamp: {
|
|
28
|
+
type: Date,
|
|
29
|
+
default: Date.now
|
|
30
|
+
}
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
const MessageLog = mongoose.model("MessageLog", MessageLogSchema);
|
|
35
|
+
|
|
36
|
+
module.exports = { MessageLog };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tiledesk/tiledesk-whatsapp-connector",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.54",
|
|
4
4
|
"description": "Tiledesk Whatsapp connector",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
"express": "^4.18.1",
|
|
20
20
|
"form-data": "^4.0.0",
|
|
21
21
|
"handlebars": "^4.7.7",
|
|
22
|
+
"jobs-worker-queued": "^0.0.5",
|
|
22
23
|
"jsonwebtoken": "^8.5.1",
|
|
23
24
|
"mongoose": "^6.5.0",
|
|
24
25
|
"mongodb": "^3.5.5",
|
package/routes/api.js
CHANGED
|
@@ -6,11 +6,13 @@ const router = express.Router({ mergeParams: true });
|
|
|
6
6
|
const { TemplateManager } = require('../tiledesk/TemplateManager');
|
|
7
7
|
const { TiledeskWhatsappTranslator } = require('../tiledesk/TiledeskWhatsappTranslator');
|
|
8
8
|
const { TiledeskWhatsapp } = require('../tiledesk/TiledeskWhatsapp');
|
|
9
|
+
const { Scheduler } = require('../tiledesk/Scheduler');
|
|
9
10
|
|
|
10
11
|
let db = null;
|
|
11
12
|
let API_URL = null;
|
|
12
13
|
let GRAPH_URL = null;
|
|
13
14
|
let BASE_FILE_URL = null;
|
|
15
|
+
let AMQP_MANAGER_URL = null;
|
|
14
16
|
|
|
15
17
|
router.get('/', async (req, res) => {
|
|
16
18
|
res.status(200).send({ message: "API route works"})
|
|
@@ -125,14 +127,19 @@ router.get("/templates/:project_id", async (req, res) => {
|
|
|
125
127
|
|
|
126
128
|
router.post('/tiledesk/broadcast', async (req, res) => {
|
|
127
129
|
winston.verbose("(wab) Action received from Tiledesk (Broadcast)");
|
|
128
|
-
winston.debug("Body (broadcast): ",JSON.stringify(req.body, null, 2));
|
|
130
|
+
winston.debug("Body (broadcast): ", JSON.stringify(req.body, null, 2));
|
|
129
131
|
|
|
130
132
|
let body = req.body;
|
|
131
133
|
|
|
132
134
|
let project_id = body.id_project;
|
|
133
135
|
let receiver_list = body.receiver_list;
|
|
134
|
-
let phone_number_id = body.
|
|
136
|
+
let phone_number_id = body.phone_number_id;
|
|
135
137
|
let template = body.template;
|
|
138
|
+
let transaction_id = body.transaction_id;
|
|
139
|
+
|
|
140
|
+
if (!transaction_id) {
|
|
141
|
+
transaction_id = "tiledesk-broadcast-" + Date.now();
|
|
142
|
+
}
|
|
136
143
|
|
|
137
144
|
let CONTENT_KEY = "whatsapp-" + project_id;
|
|
138
145
|
let settings = await db.get(CONTENT_KEY);
|
|
@@ -145,92 +152,68 @@ router.post('/tiledesk/broadcast', async (req, res) => {
|
|
|
145
152
|
return res.status(400).send({ success: false, error: "Missing parameter 'WhatsApp Business Account ID'. Please update your app."})
|
|
146
153
|
}
|
|
147
154
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
155
|
+
let scheduler = new Scheduler({ AMQP_MANAGER_URL: AMQP_MANAGER_URL });
|
|
156
|
+
let data_To_scheduler = {
|
|
157
|
+
project_id: project_id,
|
|
158
|
+
receiver_list: receiver_list,
|
|
159
|
+
phone_number_id: phone_number_id,
|
|
160
|
+
transaction_id: transaction_id,
|
|
161
|
+
template: template,
|
|
162
|
+
settings: settings
|
|
163
|
+
};
|
|
157
164
|
|
|
158
|
-
|
|
159
|
-
let messages_sent = 0;
|
|
160
|
-
let errors = [];
|
|
161
|
-
let error_count = 0;
|
|
165
|
+
winston.debug('(wab) data_To_scheduler: ', data_To_scheduler);
|
|
162
166
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
async function execute(receiver) {
|
|
166
|
-
let whatsappJsonMessage = await tlr.createTemplateMessage(selected_template, receiver, params_object);
|
|
167
|
-
winston.debug("(wab) whatsappJsonMessage: ", JSON.stringify(whatsappJsonMessage, null, 2));
|
|
167
|
+
let schedulerResult = await scheduler.goSchedule(data_To_scheduler);
|
|
168
|
+
winston.verbose('(wab) schedulerResult: ', schedulerResult);
|
|
168
169
|
|
|
169
|
-
|
|
170
|
-
winston.verbose("(wab) Message sent to WhatsApp! " + response.status + " " + response.statusText);
|
|
171
|
-
messages_sent += 1;
|
|
172
|
-
i += 1;
|
|
173
|
-
if (i < receiver_list.length) {
|
|
174
|
-
execute(receiver_list[i])
|
|
175
|
-
} else {
|
|
176
|
-
winston.debug("(wab) End of list")
|
|
177
|
-
return res.status(200).send({ success: true, message: "Broadcast terminated", messages_sent: messages_sent, errors: errors });
|
|
178
|
-
}
|
|
179
|
-
}).catch((err) => {
|
|
180
|
-
winston.error("(wab) error send message: " + err.response.data.error.message);
|
|
181
|
-
errors.push({ receiver: receiver.phone_number, error: err.response.data.error.message });
|
|
182
|
-
i += 1;
|
|
183
|
-
if (i < receiver_list.length) {
|
|
184
|
-
execute(receiver_list[i])
|
|
185
|
-
} else {
|
|
186
|
-
winston.debug("(wab) End of list")
|
|
187
|
-
return res.status(200).send({ success: true, message: "Broadcast terminated", messages_sent: messages_sent, errors: errors });
|
|
188
|
-
}
|
|
189
|
-
})
|
|
190
|
-
}
|
|
191
|
-
execute(receiver_list[0]);
|
|
192
|
-
}
|
|
170
|
+
res.status(200).send({ success: true, message: "Job started. Send messages in queue." })
|
|
193
171
|
|
|
194
172
|
})
|
|
195
173
|
|
|
196
174
|
|
|
197
175
|
// start api route from whatsappRoute
|
|
198
176
|
async function startRoute(settings, callback) {
|
|
199
|
-
winston.info("(wab) Starting api route", settings);
|
|
177
|
+
winston.info("(wab api) Starting api route", settings);
|
|
200
178
|
|
|
201
179
|
if (!settings.DB) {
|
|
202
|
-
winston.error("(wab) db id mandatory. Exit...");
|
|
180
|
+
winston.error("(wab api) db id mandatory. Exit...");
|
|
203
181
|
return callback('Missing parameter: db');
|
|
204
182
|
} else {
|
|
205
183
|
db = settings.DB;
|
|
206
|
-
winston.info("(wab) db " + db);
|
|
184
|
+
//winston.info("(wab) db " + db);
|
|
207
185
|
}
|
|
208
186
|
|
|
209
187
|
if (!settings.GRAPH_URL) {
|
|
210
|
-
winston.error("(wab) GRAPH_URL is mandatory. Exit...");
|
|
188
|
+
winston.error("(wab api) GRAPH_URL is mandatory. Exit...");
|
|
211
189
|
return callback('Missing parameter: GRAPH_URL');
|
|
212
190
|
} else {
|
|
213
191
|
GRAPH_URL = settings.GRAPH_URL;
|
|
214
|
-
winston.info("(wab) GRAPH_URL: " + GRAPH_URL);
|
|
192
|
+
winston.info("(wab api) GRAPH_URL: " + GRAPH_URL);
|
|
215
193
|
}
|
|
216
194
|
|
|
217
195
|
if (!settings.API_URL) {
|
|
218
|
-
winston.error("(wab) API_URL is mandatory. Exit...");
|
|
196
|
+
winston.error("(wab api) API_URL is mandatory. Exit...");
|
|
219
197
|
return callback('Missing parameter: API_URL');
|
|
220
198
|
} else {
|
|
221
199
|
API_URL = settings.API_URL;
|
|
222
|
-
winston.info("(wab) API_URL: " + API_URL);
|
|
200
|
+
winston.info("(wab api) API_URL: " + API_URL);
|
|
223
201
|
}
|
|
224
202
|
|
|
225
203
|
if (!settings.BASE_FILE_URL) {
|
|
226
|
-
winston.error("(wab) BASE_FILE_URL is mandatory. Exit...");
|
|
204
|
+
winston.error("(wab api) BASE_FILE_URL is mandatory. Exit...");
|
|
227
205
|
return callback('Missing parameter: BASE_FILE_URL');
|
|
228
206
|
} else {
|
|
229
207
|
BASE_FILE_URL = settings.BASE_FILE_URL;
|
|
230
|
-
winston.info("(wab) BASE_FILE_URL: " + BASE_FILE_URL);
|
|
208
|
+
winston.info("(wab api) BASE_FILE_URL: " + BASE_FILE_URL);
|
|
231
209
|
}
|
|
232
210
|
|
|
233
|
-
|
|
211
|
+
if (!settings.AMQP_MANAGER_URL) {
|
|
212
|
+
winston.error("(wab api) AMQP_MANAGER_URL is mandatory (?). Exit...");
|
|
213
|
+
} else {
|
|
214
|
+
AMQP_MANAGER_URL = settings.AMQP_MANAGER_URL;
|
|
215
|
+
winston.info("(wab api) AMQP_MANAGER_URL is present");
|
|
216
|
+
}
|
|
234
217
|
|
|
235
218
|
}
|
|
236
219
|
|
package/template/detail.html
CHANGED
|
@@ -216,36 +216,36 @@
|
|
|
216
216
|
|
|
217
217
|
<div style="width: 100%; display: flex; flex-direction: row; justify-content: space-between; margin-bottom: 15px;">
|
|
218
218
|
<div>
|
|
219
|
+
<p>Added Messages Logs for WhatsApp broadcast</p>
|
|
220
|
+
<p>Added Queue Manager for Whatsapp broadcast</p>
|
|
219
221
|
<p>Bug fixes and performance improvements</p>
|
|
220
222
|
</div>
|
|
221
|
-
<p style="font-size: 12px; color: #676767; margin-bottom: 0px;">Version <b>0.1.
|
|
223
|
+
<p style="font-size: 12px; color: #676767; margin-bottom: 0px;">Version <b>0.1.54</b></p>
|
|
222
224
|
</div>
|
|
223
|
-
|
|
225
|
+
|
|
224
226
|
<div style="width: 100%; display: flex; flex-direction: row; justify-content: space-between; margin-bottom: 15px;">
|
|
225
227
|
<div>
|
|
226
|
-
<p
|
|
227
|
-
<!--<p>Bug fixes and performance improvements</p>-->
|
|
228
|
+
<p>Bug fixes and performance improvements</p>
|
|
228
229
|
</div>
|
|
229
|
-
<p style="font-size: 12px; color: #676767; margin-bottom: 0px;">Version <b>0.1.
|
|
230
|
+
<p style="font-size: 12px; color: #676767; margin-bottom: 0px;">Version <b>0.1.53</b></p>
|
|
230
231
|
</div>
|
|
231
232
|
|
|
232
233
|
<div style="width: 100%; display: flex; flex-direction: row; justify-content: space-between; margin-bottom: 15px;">
|
|
233
234
|
<div>
|
|
234
|
-
<p
|
|
235
|
-
<p style="margin-bottom: 0px">Bug fixes and minor improvements</p>
|
|
236
|
-
<!--<p>Bug fixes and performance improvements</p>-->
|
|
235
|
+
<p>Bug fixes and performance improvements</p>
|
|
237
236
|
</div>
|
|
238
|
-
<p style="font-size: 12px; color: #676767; margin-bottom: 0px;">Version <b>0.1.
|
|
237
|
+
<p style="font-size: 12px; color: #676767; margin-bottom: 0px;">Version <b>0.1.52</b></p>
|
|
239
238
|
</div>
|
|
240
239
|
|
|
241
240
|
<div style="width: 100%; display: flex; flex-direction: row; justify-content: space-between; margin-bottom: 15px;">
|
|
242
241
|
<div>
|
|
243
|
-
<p style="margin-bottom: 0px">
|
|
242
|
+
<p style="margin-bottom: 0px">Bug fixes</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.
|
|
245
|
+
<p style="font-size: 12px; color: #676767; margin-bottom: 0px;">Version <b>0.1.50</b></p>
|
|
247
246
|
</div>
|
|
248
247
|
|
|
248
|
+
|
|
249
249
|
<hr style="margin-top: 50px; margin-bottom: 20px">
|
|
250
250
|
|
|
251
251
|
<script>
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
var JobManager = require("jobs-worker-queued");
|
|
2
|
+
var winston = require('../winston');
|
|
3
|
+
var jobManager;
|
|
4
|
+
|
|
5
|
+
class Scheduler {
|
|
6
|
+
|
|
7
|
+
constructor(config) {
|
|
8
|
+
if (!config) {
|
|
9
|
+
throw new Error('config is mandatory');
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (!config.AMQP_MANAGER_URL) {
|
|
13
|
+
throw new Error('config.AMQP_MANAGER_URL is mandatory');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
this.AMQP_MANAGER_URL = config.AMQP_MANAGER_URL;
|
|
17
|
+
this.log = false;
|
|
18
|
+
if (config.log) {
|
|
19
|
+
this.log = config.log;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
jobManager = new JobManager(this.AMQP_MANAGER_URL, {
|
|
23
|
+
debug: false,
|
|
24
|
+
topic: "tiledesk-whatsapp_test",
|
|
25
|
+
exchange: "tiledesk-whatsapp_test"
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async goSchedule(mydata) {
|
|
30
|
+
mydata.date = new Date();
|
|
31
|
+
try {
|
|
32
|
+
winston.debug("(wab) Data arrived to scheduler: ", mydata);
|
|
33
|
+
jobManager.publish(mydata);
|
|
34
|
+
return { success: true, msg: 'scheduling success!' };
|
|
35
|
+
} catch (err) {
|
|
36
|
+
winston.error('(wab) scheduling error ', err);
|
|
37
|
+
return { success: false, msg: 'Error in scheduling' };
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
module.exports = { Scheduler };
|
|
@@ -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 ===
|
|
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(
|
|
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.
|
|
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
|
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
const winston = require("../winston");
|
|
2
|
+
const { MessageLog } = require("../models/WhatsappLog");
|
|
3
|
+
|
|
4
|
+
class WhatsappLogger {
|
|
5
|
+
|
|
6
|
+
constructor() {}
|
|
7
|
+
|
|
8
|
+
async updateMessageStatus(message_id, status, error) {
|
|
9
|
+
let status_code = this.getCodeFromStatus(status);
|
|
10
|
+
winston.debug("(wab) getCodeFromStatus result " + status_code);
|
|
11
|
+
|
|
12
|
+
MessageLog.findOneAndUpdate({ message_id: message_id }, { $set: {status: status, status_code: status_code, error: error }}, { new: true }).then((messageLog) => {
|
|
13
|
+
winston.verbose("(wab) status of message_id " + message_id + " updated to " + status);
|
|
14
|
+
winston.debug("(wab) messageLog updated ", messageLog);
|
|
15
|
+
}).catch((err) => {
|
|
16
|
+
winston.error("(wab) findOneAndUpdate error: ", err);
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
getCodeFromStatus(status) {
|
|
22
|
+
let code = null;
|
|
23
|
+
switch (status) {
|
|
24
|
+
case "rejected":
|
|
25
|
+
code = -1;
|
|
26
|
+
break;
|
|
27
|
+
case "accepted":
|
|
28
|
+
code = 0;
|
|
29
|
+
break;
|
|
30
|
+
case "sent":
|
|
31
|
+
code = 1;
|
|
32
|
+
break;
|
|
33
|
+
case "delivered":
|
|
34
|
+
code = 2;
|
|
35
|
+
break;
|
|
36
|
+
case "read":
|
|
37
|
+
code = 3;
|
|
38
|
+
break;
|
|
39
|
+
default:
|
|
40
|
+
code = -2;
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
return code;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
module.exports = { WhatsappLogger };
|