@tiledesk/tiledesk-whatsapp-connector 0.1.48 → 0.1.49
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 +90 -7
- package/package.json +1 -1
- package/template/detail.html +9 -0
- package/tiledesk/TemplateManager.js +0 -2
- package/tiledesk/TiledeskWhatsapp.js +18 -3
- package/tiledesk/TiledeskWhatsappTranslator.js +72 -2
- package/tmp/media-2b5o74.ogg +0 -0
- package/tmp/media-2b5ofr.ogg +0 -0
- package/tmp/media-296tga.ogg +0 -0
- package/tmp/media-296tsi.ogg +0 -0
- package/tmp/media-296tuv.ogg +0 -0
package/index.js
CHANGED
|
@@ -49,6 +49,7 @@ let APPS_API_URL = null;
|
|
|
49
49
|
let REDIS_HOST = null;
|
|
50
50
|
let REDIS_PORT = null;
|
|
51
51
|
let REDIS_PASSWORD = null;
|
|
52
|
+
let BASE_FILE_URL = null;
|
|
52
53
|
|
|
53
54
|
// Handlebars register helpers
|
|
54
55
|
handlebars.registerHelper('isEqual', (a, b) => {
|
|
@@ -448,7 +449,7 @@ router.get('/direct/tiledesk', async (req, res) => {
|
|
|
448
449
|
}
|
|
449
450
|
}
|
|
450
451
|
const tlr = new TiledeskWhatsappTranslator();
|
|
451
|
-
const twClient = new TiledeskWhatsapp({ token: settings.wab_token, GRAPH_URL: GRAPH_URL });
|
|
452
|
+
const twClient = new TiledeskWhatsapp({ token: settings.wab_token, GRAPH_URL: GRAPH_URL, API_URL: API_URL, BASE_FILE_URL: BASE_FILE_URL });
|
|
452
453
|
|
|
453
454
|
let whatsappJsonMessage = await tlr.toWhatsapp(tiledeskChannelMessage, whatsapp_receiver);
|
|
454
455
|
winston.verbose("(wab) whatsappJsonMessage", whatsappJsonMessage)
|
|
@@ -462,6 +463,83 @@ router.get('/direct/tiledesk', async (req, res) => {
|
|
|
462
463
|
|
|
463
464
|
res.status(200).send("Message sent");
|
|
464
465
|
|
|
466
|
+
})
|
|
467
|
+
|
|
468
|
+
router.post('/tiledesk/broadcast', async (req, res) => {
|
|
469
|
+
|
|
470
|
+
winston.verbose("(wab) Message received from Tiledesk (Broadcast)");
|
|
471
|
+
|
|
472
|
+
let tiledeskChannelMessage = req.body.payload;
|
|
473
|
+
winston.verbose("(wab) tiledeskChannelMessage: ", tiledeskChannelMessage)
|
|
474
|
+
let project_id = req.body.payload.id_project;
|
|
475
|
+
|
|
476
|
+
let CONTENT_KEY = "whatsapp-" + project_id;
|
|
477
|
+
let settings = await db.get(CONTENT_KEY);
|
|
478
|
+
|
|
479
|
+
if (!settings) {
|
|
480
|
+
return res.status(400).send({ success: false, error: "WhatsApp is not installed for the project_id: " + project_id});
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
let receiver_list = req.body.receiver_list;
|
|
484
|
+
console.log("receiver_list: ", receiver_list);
|
|
485
|
+
|
|
486
|
+
let phone_number_id = req.body.phone_number_id;
|
|
487
|
+
console.log("phone_number_id: ", phone_number_id);
|
|
488
|
+
|
|
489
|
+
if (!tiledeskChannelMessage.attributes.attachment.template) {
|
|
490
|
+
return res.status(400).send({ success: false, error: "Message must contain a template."})
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
const tlr = new TiledeskWhatsappTranslator();
|
|
494
|
+
const twClient = new TiledeskWhatsapp({ token: settings.wab_token, GRAPH_URL: GRAPH_URL, API_URL: API_URL, BASE_FILE_URL: BASE_FILE_URL });
|
|
495
|
+
|
|
496
|
+
let messages_sent = 0;
|
|
497
|
+
let errors = [];
|
|
498
|
+
let error_count = 0;
|
|
499
|
+
|
|
500
|
+
if (receiver_list) {
|
|
501
|
+
let i = 0;
|
|
502
|
+
async function execute(whatsapp_receiver) {
|
|
503
|
+
|
|
504
|
+
let customTiledeskJsonMessage = await tlr.sanitizeTiledeskMessage(tiledeskChannelMessage, whatsapp_receiver)
|
|
505
|
+
winston.info("customTiledeskJsonMessage: ", customTiledeskJsonMessage)
|
|
506
|
+
|
|
507
|
+
let whatsappJsonMessage = await tlr.toWhatsapp(customTiledeskJsonMessage, whatsapp_receiver.phone_number);
|
|
508
|
+
winston.verbose("(wab) message created for receiver: " + whatsapp_receiver.phone_number);
|
|
509
|
+
winston.verbose("(wab) whatsappJsonMessage", whatsappJsonMessage);
|
|
510
|
+
console.log("(wab) whatsappJsonMessage", JSON.stringify(whatsappJsonMessage, null, 2));
|
|
511
|
+
|
|
512
|
+
//winston.debug("(wab) End of list")
|
|
513
|
+
//return res.sendStatus(200);
|
|
514
|
+
|
|
515
|
+
await twClient.sendMessage(phone_number_id, whatsappJsonMessage).then((response) => {
|
|
516
|
+
winston.verbose("(wab) Message sent to WhatsApp! " + response.status + " " + response.statusText);
|
|
517
|
+
messages_sent += 1;
|
|
518
|
+
i += 1;
|
|
519
|
+
if (i < receiver_list.length) {
|
|
520
|
+
execute(receiver_list[i])
|
|
521
|
+
} else {
|
|
522
|
+
return res.status(200).send({ success: true, message: "Broadcast terminated", messages_sent: messages_sent, errors: errors});
|
|
523
|
+
winston.debug("(wab) End of list")
|
|
524
|
+
}
|
|
525
|
+
}).catch((err) => {
|
|
526
|
+
winston.error("(wab) error send message: " + err.response.data.error.message);
|
|
527
|
+
errors.push({receiver: whatsapp_receiver.phone_number,error: err.response.data.error.message});
|
|
528
|
+
i += 1;
|
|
529
|
+
if (i < receiver_list.length) {
|
|
530
|
+
execute(receiver_list[i])
|
|
531
|
+
} else {
|
|
532
|
+
winston.debug("(wab) End of list")
|
|
533
|
+
console.log("errors: ", JSON.stringify(errors, null, 2))
|
|
534
|
+
return res.status(200).send({ success: true, message: "Broadcast terminated", messages_sent: messages_sent, errors: errors});
|
|
535
|
+
}
|
|
536
|
+
})
|
|
537
|
+
}
|
|
538
|
+
execute(receiver_list[0]);
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
|
|
542
|
+
|
|
465
543
|
})
|
|
466
544
|
|
|
467
545
|
router.post('/tiledesk', async (req, res) => {
|
|
@@ -565,7 +643,7 @@ router.post('/tiledesk', async (req, res) => {
|
|
|
565
643
|
winston.verbose("(wab) whatsappJsonMessage", whatsappJsonMessage)
|
|
566
644
|
|
|
567
645
|
if (whatsappJsonMessage) {
|
|
568
|
-
const twClient = new TiledeskWhatsapp({ token: settings.wab_token, GRAPH_URL: GRAPH_URL });
|
|
646
|
+
const twClient = new TiledeskWhatsapp({ token: settings.wab_token, GRAPH_URL: GRAPH_URL, API_URL: API_URL, BASE_FILE_URL: BASE_FILE_URL });
|
|
569
647
|
twClient.sendMessage(phone_number_id, whatsappJsonMessage).then((response) => {
|
|
570
648
|
winston.verbose("(wab) Message sent to WhatsApp! " + response.status + " " + response.statusText);
|
|
571
649
|
i += 1;
|
|
@@ -604,7 +682,7 @@ router.post('/tiledesk', async (req, res) => {
|
|
|
604
682
|
winston.verbose("(wab) whatsappJsonMessage", whatsappJsonMessage)
|
|
605
683
|
|
|
606
684
|
if (whatsappJsonMessage) {
|
|
607
|
-
const twClient = new TiledeskWhatsapp({ token: settings.wab_token, GRAPH_URL: GRAPH_URL });
|
|
685
|
+
const twClient = new TiledeskWhatsapp({ token: settings.wab_token, GRAPH_URL: GRAPH_URL, API_URL: API_URL, BASE_FILE_URL: BASE_FILE_URL });
|
|
608
686
|
|
|
609
687
|
twClient.sendMessage(phone_number_id, whatsappJsonMessage).then((response) => {
|
|
610
688
|
winston.verbose("(wab) Message sent to WhatsApp! " + response.status + " " + response.statusText);
|
|
@@ -705,7 +783,7 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
705
783
|
|
|
706
784
|
else if ((whatsappChannelMessage.type == 'image') || (whatsappChannelMessage.type == 'video') || (whatsappChannelMessage.type == 'document') || (whatsappChannelMessage.type == 'audio')) {
|
|
707
785
|
let media;
|
|
708
|
-
const util = new TiledeskWhatsapp({ token: settings.wab_token, GRAPH_URL: GRAPH_URL })
|
|
786
|
+
const util = new TiledeskWhatsapp({ token: settings.wab_token, GRAPH_URL: GRAPH_URL, API_URL: API_URL, BASE_FILE_URL: BASE_FILE_URL })
|
|
709
787
|
|
|
710
788
|
if (whatsappChannelMessage.type == 'image') {
|
|
711
789
|
media = whatsappChannelMessage.image;
|
|
@@ -976,9 +1054,6 @@ router.get("/ext/templates/:project_id", async (req, res) => {
|
|
|
976
1054
|
if (settings.business_account_id) {
|
|
977
1055
|
let tm = new TemplateManager({ token: settings.wab_token, business_account_id: settings.business_account_id, GRAPH_URL: GRAPH_URL })
|
|
978
1056
|
let templates = await tm.getTemplates();
|
|
979
|
-
|
|
980
|
-
winston.info("(wab) ext templates: ", templates);
|
|
981
|
-
|
|
982
1057
|
if (templates) {
|
|
983
1058
|
res.status(200).send(templates.data);
|
|
984
1059
|
} else {
|
|
@@ -1015,6 +1090,14 @@ async function startApp(settings, callback) {
|
|
|
1015
1090
|
winston.info("(wab) API_URL: " + API_URL);
|
|
1016
1091
|
}
|
|
1017
1092
|
|
|
1093
|
+
if (!settings.BASE_FILE_URL) {
|
|
1094
|
+
winston.error("(wab) BASE_FILE_URL is mandatory. Exit...");
|
|
1095
|
+
return callback('Missing parameter: BASE_FILE_URL');
|
|
1096
|
+
} else {
|
|
1097
|
+
BASE_FILE_URL = settings.BASE_FILE_URL;
|
|
1098
|
+
winston.info("(wab) BASE_FILE_URL: " + BASE_FILE_URL);
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1018
1101
|
if (!settings.BASE_URL) {
|
|
1019
1102
|
winston.error("(wab) BASE_URL is mandatory. Exit...");
|
|
1020
1103
|
return callback('Missing parameter: BASE_URL');
|
package/package.json
CHANGED
package/template/detail.html
CHANGED
|
@@ -213,6 +213,15 @@
|
|
|
213
213
|
|
|
214
214
|
<p style="font-size: 18px;"><b>What's new?</b></p>
|
|
215
215
|
|
|
216
|
+
<div style="width: 100%; display: flex; flex-direction: row; justify-content: space-between; margin-bottom: 15px;">
|
|
217
|
+
<div>
|
|
218
|
+
<p style="margin-bottom: 0px">Added support for WhatsApp voice messages</p>
|
|
219
|
+
<p style="margin-bottom: 0px">Bug fixes and minor improvements</p>
|
|
220
|
+
<!--<p>Bug fixes and performance improvements</p>-->
|
|
221
|
+
</div>
|
|
222
|
+
<p style="font-size: 12px; color: #676767; margin-bottom: 0px;">Version <b>0.1.49</b></p>
|
|
223
|
+
</div>
|
|
224
|
+
|
|
216
225
|
<div style="width: 100%; display: flex; flex-direction: row; justify-content: space-between; margin-bottom: 15px;">
|
|
217
226
|
<div>
|
|
218
227
|
<p style="margin-bottom: 0px">Added support for WhatsApp templates</p>
|
|
@@ -28,8 +28,6 @@ class TemplateManager {
|
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
async getTemplates() {
|
|
31
|
-
// business account id: 110354305066769
|
|
32
|
-
// business phone number id: 104777398965560
|
|
33
31
|
return await axios({
|
|
34
32
|
url: this.graph_url + this.business_account_id + "/message_templates?access_token=" + this.token,
|
|
35
33
|
headers: {
|
|
@@ -31,9 +31,20 @@ class TiledeskWhatsapp {
|
|
|
31
31
|
if (!config.GRAPH_URL) {
|
|
32
32
|
throw new Error('config.GRAPH_URL is mandatory');
|
|
33
33
|
}
|
|
34
|
+
|
|
35
|
+
if (!config.API_URL) {
|
|
36
|
+
throw new Error('config.API_URL is mandatory');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (!config.BASE_FILE_URL) {
|
|
40
|
+
throw new Error('config.BASE_FILE_URL is mandatory')
|
|
41
|
+
}
|
|
42
|
+
|
|
34
43
|
// this.media = config.channelMedia;
|
|
35
44
|
this.token = config.token;
|
|
36
45
|
this.GRAPH_URL = config.GRAPH_URL
|
|
46
|
+
this.api_url = config.API_URL;
|
|
47
|
+
this.base_file_url = config.BASE_FILE_URL;
|
|
37
48
|
|
|
38
49
|
this.log = false;
|
|
39
50
|
if (config.log) {
|
|
@@ -115,7 +126,8 @@ class TiledeskWhatsapp {
|
|
|
115
126
|
}
|
|
116
127
|
|
|
117
128
|
async uploadMedia(path, type) {
|
|
118
|
-
let url = "
|
|
129
|
+
let url = this.base_file_url + "/" + type + "/public/";
|
|
130
|
+
console.log("upload media url: ", url);
|
|
119
131
|
winston.debug("(wab) [TiledeskWhatsapp] Uploading file...");
|
|
120
132
|
|
|
121
133
|
const form = new FormData();
|
|
@@ -131,11 +143,14 @@ class TiledeskWhatsapp {
|
|
|
131
143
|
return await axios.post(url, form, request_config).then((response) => {
|
|
132
144
|
|
|
133
145
|
winston.debug("(wab) [TiledeskWhatsapp] upload response: ", response.data);
|
|
146
|
+
console.log("\n\nresponse.data: ", response.data)
|
|
134
147
|
if (type == "images") {
|
|
135
|
-
let image_url = "https://tiledesk-server-pre.herokuapp.com/images/?path=" + response.data.filename;
|
|
148
|
+
//let image_url = "https://tiledesk-server-pre.herokuapp.com/images/?path=" + response.data.filename;
|
|
149
|
+
let image_url = this.base_file_url + "/images/?path=" + response.data.filename;
|
|
136
150
|
return image_url;
|
|
137
151
|
} else {
|
|
138
|
-
let file_url = "https://tiledesk-server-pre.herokuapp.com/files/download?path=" + response.data.filename;
|
|
152
|
+
//let file_url = "https://tiledesk-server-pre.herokuapp.com/files/download?path=" + response.data.filename;
|
|
153
|
+
let file_url = this.base_file_url + "/files/download?path=" + response.data.filename;
|
|
139
154
|
return file_url;
|
|
140
155
|
}
|
|
141
156
|
|
|
@@ -286,12 +286,35 @@ const path = require('path');
|
|
|
286
286
|
}
|
|
287
287
|
components.push(component);
|
|
288
288
|
}
|
|
289
|
+
if (template.params && template.params.buttons) {
|
|
290
|
+
|
|
291
|
+
let component = {
|
|
292
|
+
type: "button",
|
|
293
|
+
sub_type: "url",
|
|
294
|
+
index: "0",
|
|
295
|
+
parameters: template.params.buttons
|
|
296
|
+
}
|
|
297
|
+
components.push(component);
|
|
298
|
+
|
|
299
|
+
/*
|
|
300
|
+
template.params.buttons.forEach((button) => {
|
|
301
|
+
let component = button;
|
|
302
|
+
components.push(component);
|
|
303
|
+
})
|
|
304
|
+
|
|
305
|
+
let component = {
|
|
306
|
+
type: "button",
|
|
307
|
+
sub_type: "url",
|
|
308
|
+
parmeters: template.params.buttons
|
|
309
|
+
}
|
|
310
|
+
*/
|
|
311
|
+
}
|
|
289
312
|
|
|
290
313
|
if (components.length > 0) {
|
|
291
314
|
whatsapp_message.template.components = components;
|
|
292
315
|
}
|
|
293
316
|
|
|
294
|
-
winston.
|
|
317
|
+
winston.verbose("(wab) [Translator] whatsapp_message: ", whatsapp_message);
|
|
295
318
|
return whatsapp_message;
|
|
296
319
|
|
|
297
320
|
}
|
|
@@ -462,7 +485,7 @@ const path = require('path');
|
|
|
462
485
|
}*/
|
|
463
486
|
|
|
464
487
|
var tiledeskMessage = {
|
|
465
|
-
text: "
|
|
488
|
+
text: "",
|
|
466
489
|
senderFullname: from,
|
|
467
490
|
channel: { name: TiledeskWhatsappTranslator.CHANNEL_NAME },
|
|
468
491
|
type: "file",
|
|
@@ -475,6 +498,53 @@ const path = require('path');
|
|
|
475
498
|
return tiledeskMessage;
|
|
476
499
|
}
|
|
477
500
|
}
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
async sanitizeTiledeskMessage(tiledeskJsonMessage, rcv_option) {
|
|
504
|
+
console.log("tiledeskJsonMessage: ", JSON.stringify(tiledeskJsonMessage, null, 2))
|
|
505
|
+
//console.log("rcv_option: ", rcv_option)
|
|
506
|
+
console.log("Sanitizing...")
|
|
507
|
+
|
|
508
|
+
if (!tiledeskJsonMessage.attributes.attachment.template.params) {
|
|
509
|
+
return tiledeskJsonMessage;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
let header_params = tiledeskJsonMessage.attributes.attachment.template.params.header;
|
|
513
|
+
if (header_params) {
|
|
514
|
+
header_params.forEach((param, i) => {
|
|
515
|
+
if (param.type === "text") {
|
|
516
|
+
param.text = rcv_option.header_params[i];
|
|
517
|
+
}
|
|
518
|
+
if (param.type === "image") {
|
|
519
|
+
param.image = {
|
|
520
|
+
link: rcv_option.header_params[i]
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
})
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
let body_params = tiledeskJsonMessage.attributes.attachment.template.params.body;
|
|
527
|
+
if (body_params) {
|
|
528
|
+
body_params.forEach((param, i) => {
|
|
529
|
+
param.text = rcv_option.body_params[i];
|
|
530
|
+
})
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
let buttons_params = tiledeskJsonMessage.attributes.attachment.template.params.buttons;
|
|
534
|
+
if (buttons_params) {
|
|
535
|
+
/*
|
|
536
|
+
buttons_params.forEach((button, i) => {
|
|
537
|
+
button.parameters[0].text = rcv_option.buttons_params[i];
|
|
538
|
+
})
|
|
539
|
+
*/
|
|
540
|
+
buttons_params.forEach((param, i) => {
|
|
541
|
+
param.text = rcv_option.buttons_params[i];
|
|
542
|
+
})
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
console.log("tiledeskJsonMessage: ", JSON.stringify(tiledeskJsonMessage, null, 2))
|
|
546
|
+
return tiledeskJsonMessage;
|
|
547
|
+
}
|
|
478
548
|
/*
|
|
479
549
|
*************************************
|
|
480
550
|
********* WHATSAPP BUSINESS *********
|
|
Binary file
|
|
Binary file
|
package/tmp/media-296tga.ogg
DELETED
|
Binary file
|
package/tmp/media-296tsi.ogg
DELETED
|
Binary file
|
package/tmp/media-296tuv.ogg
DELETED
|
Binary file
|