@tiledesk/tiledesk-whatsapp-connector 0.1.74 → 0.1.76
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 +201 -375
- package/logs/app.log +17 -0
- package/package.json +1 -1
- package/routes/api.js +0 -1
package/index.js
CHANGED
|
@@ -39,11 +39,6 @@ const { KVBaseMongo } = require("./tiledesk/KVBaseMongo");
|
|
|
39
39
|
const kvbase_collection = "kvstore";
|
|
40
40
|
const db = new KVBaseMongo(kvbase_collection);
|
|
41
41
|
|
|
42
|
-
// mongo old
|
|
43
|
-
//const { KVBaseMongo } = require('@tiledesk/tiledesk-kvbasemongo')
|
|
44
|
-
//const kvbase_collection = 'kvstore';
|
|
45
|
-
//const db = new KVBaseMongo(kvbase_collection);
|
|
46
|
-
|
|
47
42
|
// redis
|
|
48
43
|
var redis = require("redis");
|
|
49
44
|
var redis_client;
|
|
@@ -52,7 +47,6 @@ router.use(bodyParser.json());
|
|
|
52
47
|
router.use(bodyParser.urlencoded({ extended: true }));
|
|
53
48
|
router.use(express.static(path.join(__dirname, "template")));
|
|
54
49
|
router.use(cors());
|
|
55
|
-
|
|
56
50
|
router.use("/ext", apiRoute);
|
|
57
51
|
router.use("/api", apiRoute);
|
|
58
52
|
|
|
@@ -99,11 +93,7 @@ router.get("/detail", async (req, res) => {
|
|
|
99
93
|
let isAvailable = await tdChannel.getProjectDetail();
|
|
100
94
|
winston.debug("(wab) app is available: ", isAvailable);
|
|
101
95
|
if (!project_id || !token || !app_id) {
|
|
102
|
-
return res
|
|
103
|
-
.status(500)
|
|
104
|
-
.send(
|
|
105
|
-
"<p>Ops! An error occured.</p><p>Missing query params! project_id, token and app_id are required.</p>"
|
|
106
|
-
);
|
|
96
|
+
return res.status(500).send("<p>Ops! An error occured.</p><p>Missing query params! project_id, token and app_id are required.</p>");
|
|
107
97
|
}
|
|
108
98
|
|
|
109
99
|
const appClient = new TiledeskAppsClient({ APPS_API_URL: APPS_API_URL });
|
|
@@ -136,9 +126,7 @@ router.post("/install", async (req, res) => {
|
|
|
136
126
|
let app_id = req.body.app_id;
|
|
137
127
|
let token = req.body.token;
|
|
138
128
|
|
|
139
|
-
winston.debug(
|
|
140
|
-
"(wab) Install app " + app_id + " for project id " + project_id
|
|
141
|
-
);
|
|
129
|
+
winston.debug("(wab) Install app " + app_id + " for project id " + project_id);
|
|
142
130
|
let installation_info = {
|
|
143
131
|
project_id: project_id,
|
|
144
132
|
app_id: app_id,
|
|
@@ -175,26 +163,22 @@ router.post("/uninstall", async (req, res) => {
|
|
|
175
163
|
let token = req.body.token;
|
|
176
164
|
|
|
177
165
|
const appClient = new TiledeskAppsClient({ APPS_API_URL: APPS_API_URL });
|
|
178
|
-
appClient
|
|
179
|
-
.
|
|
180
|
-
.
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
.catch((err) => {
|
|
195
|
-
winston.error("(wab) uninstallation error: " + err.data);
|
|
196
|
-
res.send("An error occurred during the uninstallation");
|
|
197
|
-
});
|
|
166
|
+
appClient.uninstall(project_id, app_id).then((response) => {
|
|
167
|
+
winston.debug("(wab) uninstallation response: ", response);
|
|
168
|
+
res.redirect(
|
|
169
|
+
url.format({
|
|
170
|
+
pathname: BASE_URL + "/detail",
|
|
171
|
+
query: {
|
|
172
|
+
project_id: project_id,
|
|
173
|
+
app_id: app_id,
|
|
174
|
+
token: token,
|
|
175
|
+
},
|
|
176
|
+
})
|
|
177
|
+
);
|
|
178
|
+
}).catch((err) => {
|
|
179
|
+
winston.error("(wab) uninstallation error: " + err.data);
|
|
180
|
+
res.send("An error occurred during the uninstallation");
|
|
181
|
+
});
|
|
198
182
|
});
|
|
199
183
|
|
|
200
184
|
router.get("/configure", async (req, res) => {
|
|
@@ -257,9 +241,7 @@ router.get("/configure", async (req, res) => {
|
|
|
257
241
|
});
|
|
258
242
|
} else {
|
|
259
243
|
readHTMLFile("/configure.html", (err, html) => {
|
|
260
|
-
if (err) {
|
|
261
|
-
winston.error("(wab) error read html file: " + err);
|
|
262
|
-
}
|
|
244
|
+
if (err) { winston.error("(wab) error read html file: " + err); }
|
|
263
245
|
|
|
264
246
|
var template = handlebars.compile(html);
|
|
265
247
|
var replacements = {
|
|
@@ -347,63 +329,60 @@ router.post("/update", async (req, res) => {
|
|
|
347
329
|
*/
|
|
348
330
|
|
|
349
331
|
// promise
|
|
350
|
-
tdClient
|
|
351
|
-
|
|
352
|
-
.
|
|
353
|
-
|
|
354
|
-
|
|
332
|
+
tdClient.subscribe(subscription_info).then((data) => {
|
|
333
|
+
let subscription = data;
|
|
334
|
+
winston.debug("\n(wab) Subscription: ", subscription);
|
|
335
|
+
|
|
336
|
+
let settings = {
|
|
337
|
+
project_id: project_id,
|
|
338
|
+
token: token,
|
|
339
|
+
proxy_url: proxy_url,
|
|
340
|
+
subscriptionId: subscription._id,
|
|
341
|
+
secret: subscription.secret,
|
|
342
|
+
wab_token: wab_token,
|
|
343
|
+
verify_token: verify_token,
|
|
344
|
+
business_account_id: business_account_id,
|
|
345
|
+
department_id: department_id,
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
db.set(CONTENT_KEY, settings);
|
|
349
|
+
//let cnt = db.get(CONTENT_KEY);
|
|
355
350
|
|
|
356
|
-
|
|
351
|
+
readHTMLFile("/configure.html", (err, html) => {
|
|
352
|
+
var template = handlebars.compile(html);
|
|
353
|
+
var replacements = {
|
|
354
|
+
app_version: pjson.version,
|
|
357
355
|
project_id: project_id,
|
|
358
356
|
token: token,
|
|
359
357
|
proxy_url: proxy_url,
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
department_id: department_id,
|
|
358
|
+
show_success_modal: true,
|
|
359
|
+
wab_token: settings.wab_token,
|
|
360
|
+
verify_token: settings.verify_token,
|
|
361
|
+
business_account_id: settings.business_account_id,
|
|
362
|
+
subscription_id: settings.subscriptionId,
|
|
363
|
+
department_id: settings.department_id,
|
|
364
|
+
departments: departments,
|
|
365
|
+
brand_name: BRAND_NAME
|
|
366
366
|
};
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
departments: departments,
|
|
385
|
-
brand_name: BRAND_NAME
|
|
386
|
-
};
|
|
387
|
-
var html = template(replacements);
|
|
388
|
-
res.send(html);
|
|
389
|
-
});
|
|
390
|
-
})
|
|
391
|
-
.catch((err) => {
|
|
392
|
-
readHTMLFile("/configure.html", (err, html) => {
|
|
393
|
-
var template = handlebars.compile(html);
|
|
394
|
-
var replacements = {
|
|
395
|
-
app_version: pjson.version,
|
|
396
|
-
project_id: project_id,
|
|
397
|
-
token: token,
|
|
398
|
-
proxy_url: proxy_url,
|
|
399
|
-
departments: departments,
|
|
400
|
-
show_error_modal: true,
|
|
401
|
-
brand_name: BRAND_NAME
|
|
402
|
-
};
|
|
403
|
-
var html = template(replacements);
|
|
404
|
-
res.send(html);
|
|
405
|
-
});
|
|
367
|
+
var html = template(replacements);
|
|
368
|
+
res.send(html);
|
|
369
|
+
});
|
|
370
|
+
}).catch((err) => {
|
|
371
|
+
readHTMLFile("/configure.html", (err, html) => {
|
|
372
|
+
var template = handlebars.compile(html);
|
|
373
|
+
var replacements = {
|
|
374
|
+
app_version: pjson.version,
|
|
375
|
+
project_id: project_id,
|
|
376
|
+
token: token,
|
|
377
|
+
proxy_url: proxy_url,
|
|
378
|
+
departments: departments,
|
|
379
|
+
show_error_modal: true,
|
|
380
|
+
brand_name: BRAND_NAME
|
|
381
|
+
};
|
|
382
|
+
var html = template(replacements);
|
|
383
|
+
res.send(html);
|
|
406
384
|
});
|
|
385
|
+
});
|
|
407
386
|
}
|
|
408
387
|
});
|
|
409
388
|
|
|
@@ -439,26 +418,23 @@ router.post("/disconnect", async (req, res) => {
|
|
|
439
418
|
})
|
|
440
419
|
*/
|
|
441
420
|
|
|
442
|
-
tdClient
|
|
443
|
-
.
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
var html = template(replacements);
|
|
456
|
-
res.send(html);
|
|
457
|
-
});
|
|
458
|
-
})
|
|
459
|
-
.catch((err) => {
|
|
460
|
-
winston.error("(wab) unsubscribe error: " + err);
|
|
421
|
+
tdClient.unsubscribe(subscriptionId).then((data) => {
|
|
422
|
+
readHTMLFile("/configure.html", (err, html) => {
|
|
423
|
+
var template = handlebars.compile(html);
|
|
424
|
+
var replacements = {
|
|
425
|
+
app_version: pjson.version,
|
|
426
|
+
project_id: project_id,
|
|
427
|
+
token: token,
|
|
428
|
+
proxy_url: proxy_url,
|
|
429
|
+
departments: departments,
|
|
430
|
+
brand_name: BRAND_NAME
|
|
431
|
+
};
|
|
432
|
+
var html = template(replacements);
|
|
433
|
+
res.send(html);
|
|
461
434
|
});
|
|
435
|
+
}).catch((err) => {
|
|
436
|
+
winston.error("(wab) unsubscribe error: " + err);
|
|
437
|
+
});
|
|
462
438
|
});
|
|
463
439
|
|
|
464
440
|
router.get("/direct/tiledesk", async (req, res) => {
|
|
@@ -479,9 +455,9 @@ router.get("/direct/tiledesk", async (req, res) => {
|
|
|
479
455
|
template: {
|
|
480
456
|
language: "en_US",
|
|
481
457
|
name: "hello_world",
|
|
482
|
-
}
|
|
483
|
-
}
|
|
484
|
-
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
485
461
|
};
|
|
486
462
|
const tlr = new TiledeskWhatsappTranslator();
|
|
487
463
|
const twClient = new TiledeskWhatsapp({
|
|
@@ -497,20 +473,17 @@ router.get("/direct/tiledesk", async (req, res) => {
|
|
|
497
473
|
);
|
|
498
474
|
winston.verbose("(wab) whatsappJsonMessage", whatsappJsonMessage);
|
|
499
475
|
|
|
500
|
-
twClient
|
|
501
|
-
.
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
})
|
|
510
|
-
|
|
511
|
-
//winston.error("(wab) error send message: ", err);
|
|
512
|
-
return res.status(400).send({ success: false, error: err });
|
|
513
|
-
});
|
|
476
|
+
twClient.sendMessage(phone_number_id, whatsappJsonMessage).then((response) => {
|
|
477
|
+
winston.verbose(
|
|
478
|
+
"(wab) Message sent to WhatsApp! " +
|
|
479
|
+
response.status +
|
|
480
|
+
" " +
|
|
481
|
+
response.statusText
|
|
482
|
+
);
|
|
483
|
+
}).catch((err) => {
|
|
484
|
+
//winston.error("(wab) error send message: ", err);
|
|
485
|
+
return res.status(400).send({ success: false, error: err });
|
|
486
|
+
});
|
|
514
487
|
|
|
515
488
|
res.status(200).send("Message sent");
|
|
516
489
|
});
|
|
@@ -528,18 +501,11 @@ router.post("/tiledesk/broadcast", async (req, res) => {
|
|
|
528
501
|
let settings = await db.get(CONTENT_KEY);
|
|
529
502
|
|
|
530
503
|
if (!settings) {
|
|
531
|
-
return res.status(400).send({
|
|
532
|
-
success: false,
|
|
533
|
-
error: "WhatsApp is not installed for the project_id: " + project_id,
|
|
534
|
-
});
|
|
504
|
+
return res.status(400).send({ success: false, error: "WhatsApp is not installed for the project_id: " + project_id });
|
|
535
505
|
}
|
|
536
506
|
|
|
537
507
|
if (!settings.business_account_id) {
|
|
538
|
-
return res.status(400).send({
|
|
539
|
-
success: false,
|
|
540
|
-
error:
|
|
541
|
-
"Missing parameter 'WhatsApp Business Account ID'. Please update your app.",
|
|
542
|
-
});
|
|
508
|
+
return res.status(400).send({ success: false, error: "Missing parameter 'WhatsApp Business Account ID'. Please update your app." });
|
|
543
509
|
}
|
|
544
510
|
|
|
545
511
|
let receiver_list = req.body.receiver_list;
|
|
@@ -577,65 +543,34 @@ router.post("/tiledesk/broadcast", async (req, res) => {
|
|
|
577
543
|
if (receiver_list) {
|
|
578
544
|
let i = 0;
|
|
579
545
|
async function execute(whatsapp_receiver) {
|
|
580
|
-
let customTiledeskJsonMessage = await tlr.sanitizeTiledeskMessage(
|
|
581
|
-
tiledeskChannelMessage,
|
|
582
|
-
whatsapp_receiver
|
|
583
|
-
);
|
|
546
|
+
let customTiledeskJsonMessage = await tlr.sanitizeTiledeskMessage(tiledeskChannelMessage, whatsapp_receiver);
|
|
584
547
|
winston.verbose("customTiledeskJsonMessage: ", customTiledeskJsonMessage);
|
|
585
548
|
|
|
586
|
-
let whatsappJsonMessage = await tlr.toWhatsapp(
|
|
587
|
-
|
|
588
|
-
whatsapp_receiver.phone_number
|
|
589
|
-
);
|
|
590
|
-
winston.verbose(
|
|
591
|
-
"(wab) message created for receiver: " + whatsapp_receiver.phone_number
|
|
592
|
-
);
|
|
549
|
+
let whatsappJsonMessage = await tlr.toWhatsapp(customTiledeskJsonMessage, whatsapp_receiver.phone_number);
|
|
550
|
+
winston.verbose("(wab) message created for receiver: " + whatsapp_receiver.phone_number);
|
|
593
551
|
winston.verbose("(wab) whatsappJsonMessage", whatsappJsonMessage);
|
|
594
552
|
|
|
595
|
-
await twClient
|
|
596
|
-
.
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
);
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
}
|
|
617
|
-
})
|
|
618
|
-
.catch((err) => {
|
|
619
|
-
winston.error(
|
|
620
|
-
"(wab) error send message: " + err.response.data.error.message
|
|
621
|
-
);
|
|
622
|
-
errors.push({
|
|
623
|
-
receiver: whatsapp_receiver.phone_number,
|
|
624
|
-
error: err.response.data.error.message,
|
|
625
|
-
});
|
|
626
|
-
i += 1;
|
|
627
|
-
if (i < receiver_list.length) {
|
|
628
|
-
execute(receiver_list[i]);
|
|
629
|
-
} else {
|
|
630
|
-
winston.debug("(wab) End of list");
|
|
631
|
-
return res.status(200).send({
|
|
632
|
-
success: true,
|
|
633
|
-
message: "Broadcast terminated",
|
|
634
|
-
messages_sent: messages_sent,
|
|
635
|
-
errors: errors,
|
|
636
|
-
});
|
|
637
|
-
}
|
|
638
|
-
});
|
|
553
|
+
await twClient.sendMessage(phone_number_id, whatsappJsonMessage).then((response) => {
|
|
554
|
+
winston.verbose("(wab) Message sent to WhatsApp! " + response.status + " " + response.statusText);
|
|
555
|
+
messages_sent += 1;
|
|
556
|
+
i += 1;
|
|
557
|
+
if (i < receiver_list.length) {
|
|
558
|
+
execute(receiver_list[i]);
|
|
559
|
+
} else {
|
|
560
|
+
winston.debug("(wab) End of list");
|
|
561
|
+
return res.status(200).send({ success: true, message: "Broadcast terminated", messages_sent: messages_sent, errors: errors });
|
|
562
|
+
}
|
|
563
|
+
}).catch((err) => {
|
|
564
|
+
winston.error("(wab) error send message: " + err.response.data.error.message);
|
|
565
|
+
errors.push({ receiver: whatsapp_receiver.phone_number, error: err.response.data.error.message });
|
|
566
|
+
i += 1;
|
|
567
|
+
if (i < receiver_list.length) {
|
|
568
|
+
execute(receiver_list[i]);
|
|
569
|
+
} else {
|
|
570
|
+
winston.debug("(wab) End of list");
|
|
571
|
+
return res.status(200).send({ success: true, message: "Broadcast terminated", messages_sent: messages_sent, errors: errors });
|
|
572
|
+
}
|
|
573
|
+
});
|
|
639
574
|
}
|
|
640
575
|
execute(receiver_list[0]);
|
|
641
576
|
}
|
|
@@ -643,7 +578,6 @@ router.post("/tiledesk/broadcast", async (req, res) => {
|
|
|
643
578
|
|
|
644
579
|
router.post("/tiledesk", async (req, res) => {
|
|
645
580
|
winston.verbose("(wab) Message received from Tiledesk");
|
|
646
|
-
console.log("req.body: ", JSON.stringify(req.body));
|
|
647
581
|
|
|
648
582
|
var tiledeskChannelMessage = req.body.payload;
|
|
649
583
|
winston.debug("(wab) tiledeskChannelMessage: ", tiledeskChannelMessage);
|
|
@@ -685,24 +619,17 @@ router.post("/tiledesk", async (req, res) => {
|
|
|
685
619
|
|
|
686
620
|
let recipient_id = tiledeskChannelMessage.recipient;
|
|
687
621
|
let sender = tiledeskChannelMessage.sender;
|
|
688
|
-
let whatsapp_receiver = recipient_id.substring(
|
|
689
|
-
recipient_id.lastIndexOf("-") + 1
|
|
690
|
-
);
|
|
622
|
+
let whatsapp_receiver = recipient_id.substring(recipient_id.lastIndexOf("-") + 1);
|
|
691
623
|
|
|
692
624
|
let phone_number_id;
|
|
693
625
|
if (attributes && attributes.whatsapp_phone_number_id) {
|
|
694
626
|
phone_number_id = attributes.whatsapp_phone_number_id;
|
|
695
627
|
} else {
|
|
696
|
-
phone_number_id = recipient_id.substring(
|
|
697
|
-
recipient_id.lastIndexOf("wab-") + 4,
|
|
698
|
-
recipient_id.lastIndexOf("-")
|
|
699
|
-
);
|
|
628
|
+
phone_number_id = recipient_id.substring(recipient_id.lastIndexOf("wab-") + 4, recipient_id.lastIndexOf("-"));
|
|
700
629
|
}
|
|
701
630
|
|
|
702
631
|
if (!phone_number_id) {
|
|
703
|
-
return res
|
|
704
|
-
.status(400)
|
|
705
|
-
.send({ success: false, message: "Phone number id undefined" });
|
|
632
|
+
return res.status(400).send({ success: false, message: "Phone number id undefined" });
|
|
706
633
|
}
|
|
707
634
|
|
|
708
635
|
/*
|
|
@@ -758,7 +685,7 @@ router.post("/tiledesk", async (req, res) => {
|
|
|
758
685
|
});
|
|
759
686
|
const tlr = new TiledeskWhatsappTranslator();
|
|
760
687
|
|
|
761
|
-
if (commands) {
|
|
688
|
+
if (commands && commands.length > 0) {
|
|
762
689
|
let i = 0;
|
|
763
690
|
async function execute(command) {
|
|
764
691
|
// message
|
|
@@ -766,15 +693,9 @@ router.post("/tiledesk", async (req, res) => {
|
|
|
766
693
|
let tiledeskCommandMessage = await messageHandler.generateMessageObject(
|
|
767
694
|
command
|
|
768
695
|
);
|
|
769
|
-
winston.debug(
|
|
770
|
-
"(wab) message generated from command: ",
|
|
771
|
-
tiledeskCommandMessage
|
|
772
|
-
);
|
|
696
|
+
winston.debug("(wab) message generated from command: ", tiledeskCommandMessage);
|
|
773
697
|
|
|
774
|
-
let whatsappJsonMessage = await tlr.toWhatsapp(
|
|
775
|
-
tiledeskCommandMessage,
|
|
776
|
-
whatsapp_receiver
|
|
777
|
-
);
|
|
698
|
+
let whatsappJsonMessage = await tlr.toWhatsapp(tiledeskCommandMessage, whatsapp_receiver);
|
|
778
699
|
winston.verbose("(wab) whatsappJsonMessage", whatsappJsonMessage);
|
|
779
700
|
|
|
780
701
|
if (whatsappJsonMessage) {
|
|
@@ -784,26 +705,23 @@ router.post("/tiledesk", async (req, res) => {
|
|
|
784
705
|
API_URL: API_URL,
|
|
785
706
|
BASE_FILE_URL: BASE_FILE_URL,
|
|
786
707
|
});
|
|
787
|
-
twClient
|
|
788
|
-
.
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
i
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
.catch((err) => {
|
|
805
|
-
winston.error("(wab) send message error: ", err);
|
|
806
|
-
});
|
|
708
|
+
twClient.sendMessage(phone_number_id, whatsappJsonMessage).then((response) => {
|
|
709
|
+
winston.verbose(
|
|
710
|
+
"(wab) Message sent to WhatsApp! " +
|
|
711
|
+
response.status +
|
|
712
|
+
" " +
|
|
713
|
+
response.statusText
|
|
714
|
+
);
|
|
715
|
+
i += 1;
|
|
716
|
+
if (i < commands.length) {
|
|
717
|
+
execute(commands[i]);
|
|
718
|
+
} else {
|
|
719
|
+
winston.debug("(wab) End of commands");
|
|
720
|
+
return res.sendStatus(200);
|
|
721
|
+
}
|
|
722
|
+
}).catch((err) => {
|
|
723
|
+
winston.error("(wab) send message error: ", err);
|
|
724
|
+
});
|
|
807
725
|
} else {
|
|
808
726
|
winston.error("(wab) WhatsappJsonMessage is undefined!");
|
|
809
727
|
}
|
|
@@ -838,40 +756,25 @@ router.post("/tiledesk", async (req, res) => {
|
|
|
838
756
|
BASE_FILE_URL: BASE_FILE_URL,
|
|
839
757
|
});
|
|
840
758
|
|
|
841
|
-
twClient
|
|
842
|
-
.
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
})
|
|
852
|
-
|
|
853
|
-
//winston.error("(wab) error send message: ", err);
|
|
854
|
-
return res
|
|
855
|
-
.status(400)
|
|
856
|
-
.send({ success: false, error: "Template not existing" });
|
|
857
|
-
});
|
|
759
|
+
twClient.sendMessage(phone_number_id, whatsappJsonMessage).then((response) => {
|
|
760
|
+
winston.verbose(
|
|
761
|
+
"(wab) Message sent to WhatsApp! " +
|
|
762
|
+
response.status +
|
|
763
|
+
" " +
|
|
764
|
+
response.statusText
|
|
765
|
+
);
|
|
766
|
+
return res.sendStatus(200);
|
|
767
|
+
}).catch((err) => {
|
|
768
|
+
//winston.error("(wab) error send message: ", err);
|
|
769
|
+
return res.status(400).send({ success: false, error: "Template not existing" });
|
|
770
|
+
});
|
|
858
771
|
} else {
|
|
859
772
|
winston.error("(wab) Whatsapp Json Message is undefined!");
|
|
860
|
-
return res
|
|
861
|
-
.status(400)
|
|
862
|
-
.send({
|
|
863
|
-
success: false,
|
|
864
|
-
error: "An error occurred during message translation",
|
|
865
|
-
});
|
|
773
|
+
return res.status(400).send({ success: false, error: "An error occurred during message translation" });
|
|
866
774
|
}
|
|
867
775
|
} else {
|
|
868
776
|
winston.debug("(wab) no command, no text --> skip");
|
|
869
|
-
return res
|
|
870
|
-
.sendStatus(400)
|
|
871
|
-
.send({
|
|
872
|
-
success: false,
|
|
873
|
-
error: "No command or text specified. Skip message.",
|
|
874
|
-
});
|
|
777
|
+
return res.status(400).send({ success: false, error: "No command or text specified. Skip message." });
|
|
875
778
|
}
|
|
876
779
|
});
|
|
877
780
|
|
|
@@ -919,15 +822,12 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
919
822
|
}
|
|
920
823
|
|
|
921
824
|
if (req.body.entry[0].id !== settings.business_account_id) {
|
|
922
|
-
winston.verbose(
|
|
923
|
-
"(wab) Skip message with waba that not belong to Tiledesk project id"
|
|
924
|
-
);
|
|
825
|
+
winston.verbose("(wab) Skip message with waba that not belong to Tiledesk project id");
|
|
925
826
|
return res.sendStatus(200);
|
|
926
827
|
}
|
|
927
828
|
|
|
928
|
-
let whatsappChannelMessage =
|
|
929
|
-
req.body.entry[0].changes[0].value.messages[0];
|
|
930
829
|
|
|
830
|
+
let whatsappChannelMessage = req.body.entry[0].changes[0].value.messages[0];
|
|
931
831
|
/*
|
|
932
832
|
let CONTENT_KEY = "whatsapp-" + project_id;
|
|
933
833
|
let settings = await db.get(CONTENT_KEY);
|
|
@@ -946,10 +846,7 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
946
846
|
});
|
|
947
847
|
|
|
948
848
|
// Initialize conversation with chatbot
|
|
949
|
-
if (
|
|
950
|
-
whatsappChannelMessage.text &&
|
|
951
|
-
whatsappChannelMessage.text.body.startsWith("#td")
|
|
952
|
-
) {
|
|
849
|
+
if (whatsappChannelMessage.text && whatsappChannelMessage.text.body.startsWith("#td")) {
|
|
953
850
|
let code = whatsappChannelMessage.text.body.split(" ")[0];
|
|
954
851
|
|
|
955
852
|
const bottester = new TiledeskBotTester({
|
|
@@ -959,30 +856,24 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
959
856
|
tdChannel: tdChannel,
|
|
960
857
|
tlr: tlr,
|
|
961
858
|
});
|
|
962
|
-
bottester
|
|
963
|
-
.
|
|
964
|
-
.
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
.catch((err) => {
|
|
969
|
-
winston.error("(wab) start test onversation error: ", err);
|
|
970
|
-
});
|
|
859
|
+
bottester.startBotConversation(req.body, code).then((result) => {
|
|
860
|
+
winston.verbose("(wab) test conversation started");
|
|
861
|
+
winston.debug("(wab) startBotConversation result: ", result);
|
|
862
|
+
}).catch((err) => {
|
|
863
|
+
winston.error("(wab) start test onversation error: ", err);
|
|
864
|
+
});
|
|
971
865
|
|
|
972
866
|
// Standard message
|
|
973
867
|
} else {
|
|
974
|
-
let firstname =
|
|
975
|
-
req.body.entry[0].changes[0].value.contacts[0].profile.name;
|
|
868
|
+
let firstname = req.body.entry[0].changes[0].value.contacts[0].profile.name;
|
|
976
869
|
|
|
977
870
|
let message_info = {
|
|
978
871
|
channel: "whatsapp",
|
|
979
872
|
whatsapp: {
|
|
980
|
-
phone_number_id:
|
|
981
|
-
req.body.entry[0].changes[0].value.metadata.phone_number_id,
|
|
873
|
+
phone_number_id: req.body.entry[0].changes[0].value.metadata.phone_number_id,
|
|
982
874
|
from: req.body.entry[0].changes[0].value.messages[0].from,
|
|
983
|
-
firstname:
|
|
984
|
-
|
|
985
|
-
lastname: " ",
|
|
875
|
+
firstname: req.body.entry[0].changes[0].value.contacts[0].profile.name,
|
|
876
|
+
lastname: " "
|
|
986
877
|
},
|
|
987
878
|
};
|
|
988
879
|
|
|
@@ -1092,25 +983,15 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
1092
983
|
media = whatsappChannelMessage.image;
|
|
1093
984
|
const filename = await util.downloadMedia(media.id);
|
|
1094
985
|
if (!filename) {
|
|
1095
|
-
winston.debug(
|
|
1096
|
-
|
|
1097
|
-
media.id +
|
|
1098
|
-
". Message not sent."
|
|
1099
|
-
);
|
|
1100
|
-
return res
|
|
1101
|
-
.status(500)
|
|
1102
|
-
.send({ success: false, error: "unable to download media" });
|
|
986
|
+
winston.debug("(wab) Unable to download media with id " + media.id + ". Message not sent.");
|
|
987
|
+
return res.status(500).send({ success: false, error: "unable to download media" });
|
|
1103
988
|
}
|
|
1104
|
-
let file_path = path.join(__dirname, "tmp", filename);
|
|
1105
989
|
|
|
990
|
+
let file_path = path.join(__dirname, "tmp", filename);
|
|
1106
991
|
const image_url = await util.uploadMedia(file_path, "images");
|
|
1107
992
|
winston.debug("(wab) image_url: " + image_url);
|
|
1108
993
|
|
|
1109
|
-
tiledeskJsonMessage = await tlr.toTiledesk(
|
|
1110
|
-
whatsappChannelMessage,
|
|
1111
|
-
firstname,
|
|
1112
|
-
image_url
|
|
1113
|
-
);
|
|
994
|
+
tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname, image_url);
|
|
1114
995
|
}
|
|
1115
996
|
|
|
1116
997
|
if (whatsappChannelMessage.type == "video") {
|
|
@@ -1118,25 +999,15 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
1118
999
|
|
|
1119
1000
|
const filename = await util.downloadMedia(media.id);
|
|
1120
1001
|
if (!filename) {
|
|
1121
|
-
winston.debug(
|
|
1122
|
-
|
|
1123
|
-
media.id +
|
|
1124
|
-
". Message not sent."
|
|
1125
|
-
);
|
|
1126
|
-
return res
|
|
1127
|
-
.status(500)
|
|
1128
|
-
.send({ success: false, error: "unable to download media" });
|
|
1002
|
+
winston.debug("(wab) Unable to download media with id " + media.id + ". Message not sent.");
|
|
1003
|
+
return res.status(500).send({ success: false, error: "unable to download media" });
|
|
1129
1004
|
}
|
|
1130
1005
|
let file_path = path.join(__dirname, "tmp", filename);
|
|
1131
1006
|
|
|
1132
1007
|
const media_url = await util.uploadMedia(file_path, "files");
|
|
1133
1008
|
winston.debug("(wab) media_url: " + media_url);
|
|
1134
1009
|
|
|
1135
|
-
tiledeskJsonMessage = await tlr.toTiledesk(
|
|
1136
|
-
whatsappChannelMessage,
|
|
1137
|
-
firstname,
|
|
1138
|
-
media_url
|
|
1139
|
-
);
|
|
1010
|
+
tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname, media_url);
|
|
1140
1011
|
}
|
|
1141
1012
|
|
|
1142
1013
|
if (whatsappChannelMessage.type == "document") {
|
|
@@ -1144,25 +1015,15 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
1144
1015
|
|
|
1145
1016
|
const filename = await util.downloadMedia(media.id);
|
|
1146
1017
|
if (!filename) {
|
|
1147
|
-
winston.debug(
|
|
1148
|
-
|
|
1149
|
-
media.id +
|
|
1150
|
-
". Message not sent."
|
|
1151
|
-
);
|
|
1152
|
-
return res
|
|
1153
|
-
.status(500)
|
|
1154
|
-
.send({ success: false, error: "unable to download media" });
|
|
1018
|
+
winston.debug("(wab) Unable to download media with id " + media.id + ". Message not sent.");
|
|
1019
|
+
return res.status(500).send({ success: false, error: "unable to download media" });
|
|
1155
1020
|
}
|
|
1156
1021
|
let file_path = path.join(__dirname, "tmp", filename);
|
|
1157
1022
|
|
|
1158
1023
|
const media_url = await util.uploadMedia(file_path, "files");
|
|
1159
1024
|
winston.debug("(wab) media_url: " + media_url);
|
|
1160
1025
|
|
|
1161
|
-
tiledeskJsonMessage = await tlr.toTiledesk(
|
|
1162
|
-
whatsappChannelMessage,
|
|
1163
|
-
firstname,
|
|
1164
|
-
media_url
|
|
1165
|
-
);
|
|
1026
|
+
tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname, media_url);
|
|
1166
1027
|
}
|
|
1167
1028
|
|
|
1168
1029
|
if (whatsappChannelMessage.type == "audio") {
|
|
@@ -1170,41 +1031,24 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
1170
1031
|
|
|
1171
1032
|
const filename = await util.downloadMedia(media.id);
|
|
1172
1033
|
if (!filename) {
|
|
1173
|
-
winston.debug(
|
|
1174
|
-
|
|
1175
|
-
media.id +
|
|
1176
|
-
". Message not sent."
|
|
1177
|
-
);
|
|
1178
|
-
return res
|
|
1179
|
-
.status(500)
|
|
1180
|
-
.send({ success: false, error: "unable to download media" });
|
|
1034
|
+
winston.debug("(wab) Unable to download media with id " + media.id + ". Message not sent.");
|
|
1035
|
+
return res.status(500).send({ success: false, error: "unable to download media" });
|
|
1181
1036
|
}
|
|
1182
1037
|
let file_path = path.join(__dirname, "tmp", filename);
|
|
1183
1038
|
|
|
1184
1039
|
const media_url = await util.uploadMedia(file_path, "files");
|
|
1185
1040
|
winston.debug("(wab) media_url: " + media_url);
|
|
1186
1041
|
|
|
1187
|
-
tiledeskJsonMessage = await tlr.toTiledesk(
|
|
1188
|
-
whatsappChannelMessage,
|
|
1189
|
-
firstname,
|
|
1190
|
-
media_url
|
|
1191
|
-
);
|
|
1042
|
+
tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname, media_url);
|
|
1192
1043
|
}
|
|
1193
1044
|
} else {
|
|
1194
1045
|
winston.debug("(wab) message type: ", whatsappChannelMessage.type);
|
|
1195
|
-
tiledeskJsonMessage = await tlr.toTiledesk(
|
|
1196
|
-
whatsappChannelMessage,
|
|
1197
|
-
firstname
|
|
1198
|
-
);
|
|
1046
|
+
tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname);
|
|
1199
1047
|
}
|
|
1200
1048
|
|
|
1201
1049
|
if (tiledeskJsonMessage) {
|
|
1202
1050
|
winston.verbose("(wab) tiledeskJsonMessage: ", tiledeskJsonMessage);
|
|
1203
|
-
const response = await tdChannel.send(
|
|
1204
|
-
tiledeskJsonMessage,
|
|
1205
|
-
message_info,
|
|
1206
|
-
settings.department_id
|
|
1207
|
-
);
|
|
1051
|
+
const response = await tdChannel.send(tiledeskJsonMessage, message_info, settings.department_id);
|
|
1208
1052
|
winston.verbose("(wab) Message sent to Tiledesk!");
|
|
1209
1053
|
winston.debug("(wab) response: ", response);
|
|
1210
1054
|
} else {
|
|
@@ -1220,8 +1064,7 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
1220
1064
|
req.body.entry[0].changes[0].value.statuses &&
|
|
1221
1065
|
req.body.entry[0].changes[0].value.statuses[0]
|
|
1222
1066
|
) {
|
|
1223
|
-
let whatsappStatusMessage =
|
|
1224
|
-
req.body.entry[0].changes[0].value.statuses[0];
|
|
1067
|
+
let whatsappStatusMessage = req.body.entry[0].changes[0].value.statuses[0];
|
|
1225
1068
|
winston.verbose("(wab) whatsappStatusMessage: ", whatsappStatusMessage);
|
|
1226
1069
|
|
|
1227
1070
|
let message_id = whatsappStatusMessage.id;
|
|
@@ -1230,9 +1073,7 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
1230
1073
|
if (whatsappStatusMessage.errors) {
|
|
1231
1074
|
error = whatsappStatusMessage.errors[0].title;
|
|
1232
1075
|
}
|
|
1233
|
-
winston.verbose(
|
|
1234
|
-
"(wab) update status in " + status + " for message_id " + message_id
|
|
1235
|
-
);
|
|
1076
|
+
winston.verbose("(wab) update status in " + status + " for message_id " + message_id);
|
|
1236
1077
|
|
|
1237
1078
|
/*
|
|
1238
1079
|
let CONTENT_KEY = "whatsapp-" + project_id;
|
|
@@ -1299,9 +1140,7 @@ router.get("/webhook/:project_id", async (req, res) => {
|
|
|
1299
1140
|
}
|
|
1300
1141
|
} else {
|
|
1301
1142
|
winston.error("(wab) mode or token undefined");
|
|
1302
|
-
res
|
|
1303
|
-
.status(400)
|
|
1304
|
-
.send("impossible to verify the webhook: mode or token undefined.");
|
|
1143
|
+
res.status(400).send("impossible to verify the webhook: mode or token undefined.");
|
|
1305
1144
|
}
|
|
1306
1145
|
}
|
|
1307
1146
|
});
|
|
@@ -1321,18 +1160,14 @@ router.post("/newtest", async (req, res) => {
|
|
|
1321
1160
|
let key = "bottest:" + short_uid;
|
|
1322
1161
|
|
|
1323
1162
|
if (!redis_client) {
|
|
1324
|
-
return res.status(500).send({
|
|
1325
|
-
message: "Test it out on Whatsapp not available. Redis not ready.",
|
|
1326
|
-
});
|
|
1163
|
+
return res.status(500).send({ message: "Test it out on Whatsapp not available. Redis not ready." });
|
|
1327
1164
|
}
|
|
1328
1165
|
|
|
1329
1166
|
await redis_client.set(key, JSON.stringify(info), "EX", 604800);
|
|
1330
1167
|
redis_client.get(key, (err, value) => {
|
|
1331
1168
|
if (err) {
|
|
1332
1169
|
winston.error("(wab) redis get err: ", err);
|
|
1333
|
-
return res
|
|
1334
|
-
.status(500)
|
|
1335
|
-
.send({ success: "false", message: "Testing info could not be saved" });
|
|
1170
|
+
return res.status(500).send({ success: "false", message: "Testing info could not be saved" });
|
|
1336
1171
|
} else {
|
|
1337
1172
|
winston.debug("(wab) new test initialized with id: " + short_uid);
|
|
1338
1173
|
return res.status(200).send({ short_uid: short_uid });
|
|
@@ -1365,9 +1200,7 @@ router.get("/templates/detail", async (req, res) => {
|
|
|
1365
1200
|
GRAPH_URL: GRAPH_URL,
|
|
1366
1201
|
});
|
|
1367
1202
|
let templates = await tm.getTemplates();
|
|
1368
|
-
let template = JSON.parse(
|
|
1369
|
-
JSON.stringify(templates.data.find((t) => t.id === template_id))
|
|
1370
|
-
);
|
|
1203
|
+
let template = JSON.parse(JSON.stringify(templates.data.find((t) => t.id === template_id)));
|
|
1371
1204
|
let template_name = template.name;
|
|
1372
1205
|
|
|
1373
1206
|
let template_copy = {
|
|
@@ -1547,9 +1380,7 @@ async function startApp(settings, callback) {
|
|
|
1547
1380
|
REDIS_PASSWORD = settings.REDIS_PASSWORD;
|
|
1548
1381
|
connectRedis();
|
|
1549
1382
|
} else {
|
|
1550
|
-
winston.info(
|
|
1551
|
-
"(wab) Missing redis parameters --> Test it out on WhatsApp disabled"
|
|
1552
|
-
);
|
|
1383
|
+
winston.info("(wab) Missing redis parameters --> Test it out on WhatsApp disabled");
|
|
1553
1384
|
}
|
|
1554
1385
|
|
|
1555
1386
|
if (!settings.AMQP_MANAGER_URL) {
|
|
@@ -1560,9 +1391,7 @@ async function startApp(settings, callback) {
|
|
|
1560
1391
|
}
|
|
1561
1392
|
|
|
1562
1393
|
if (!settings.JOB_TOPIC_EXCHANGE) {
|
|
1563
|
-
winston.info(
|
|
1564
|
-
"(wab) JOB_TOPIC_EXCHANGE should be present. Using default value"
|
|
1565
|
-
);
|
|
1394
|
+
winston.info("(wab) JOB_TOPIC_EXCHANGE should be present. Using default value");
|
|
1566
1395
|
JOB_TOPIC_EXCHANGE = "tiledesk-whatsapp";
|
|
1567
1396
|
} else {
|
|
1568
1397
|
JOB_TOPIC_EXCHANGE = settings.JOB_TOPIC_EXCHANGE;
|
|
@@ -1595,14 +1424,11 @@ async function startApp(settings, callback) {
|
|
|
1595
1424
|
LOG_MONGODB_URL = settings.LOG_MONGODB_URL;
|
|
1596
1425
|
}
|
|
1597
1426
|
|
|
1598
|
-
mongoose
|
|
1599
|
-
.
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
.catch((err) => {
|
|
1604
|
-
winston.error("(Mongoose) Unable to connect with MongoDB ", err);
|
|
1605
|
-
});
|
|
1427
|
+
mongoose.connect(LOG_MONGODB_URL).then(() => {
|
|
1428
|
+
winston.info("Mongoose DB Connected");
|
|
1429
|
+
}).catch((err) => {
|
|
1430
|
+
winston.error("(Mongoose) Unable to connect with MongoDB ", err);
|
|
1431
|
+
});
|
|
1606
1432
|
|
|
1607
1433
|
if (settings.dbconnection) {
|
|
1608
1434
|
db.reuseConnection(settings.dbconnection, () => {
|
package/logs/app.log
CHANGED
|
@@ -603,3 +603,20 @@ info: (wab) Connect Redis Error Error: Redis connection to redis-19030.c226.eu-w
|
|
|
603
603
|
info: (wab) Redis ready!
|
|
604
604
|
info: (wab) Connect Redis Error Error: Redis connection to redis-19030.c226.eu-west-1-3.ec2.cloud.redislabs.com:19030 failed - read ETIMEDOUT
|
|
605
605
|
info: (wab) Redis ready!
|
|
606
|
+
info: (wab) info Starting Whatsapp App
|
|
607
|
+
info: (wab) API_URL: https://tiledesk-server-pre.herokuapp.com
|
|
608
|
+
info: (wab) BASE_FILE_URL: https://tiledesk-server-pre.herokuapp.com
|
|
609
|
+
info: (wab) BASE_URL: https://tiledesk-whatsapp-connector-dev.glitch.me
|
|
610
|
+
info: (wab) GRAPH_URL: https://graph.facebook.com/v14.0/
|
|
611
|
+
info: (wab) APPS_API_URL: https://tiledesk-apps.herokuapp.com
|
|
612
|
+
info: (wab) AMQP_MANAGER_URL is present
|
|
613
|
+
info: (wab) JOB_TOPIC_EXCHANGE is present
|
|
614
|
+
info: (wab api) Starting api route
|
|
615
|
+
info: (wab api) GRAPH_URL: https://graph.facebook.com/v14.0/
|
|
616
|
+
info: (wab api) API_URL: https://tiledesk-server-pre.herokuapp.com
|
|
617
|
+
info: (wab api) BASE_FILE_URL: https://tiledesk-server-pre.herokuapp.com
|
|
618
|
+
info: (wab api) AMQP_MANAGER_URL is present
|
|
619
|
+
info: (wab api) JOB_TOPIC_EXCHANGE is present
|
|
620
|
+
info: (wab) Redis ready!
|
|
621
|
+
info: Mongoose DB Connected
|
|
622
|
+
info: (wab) KVBaseMongo successfully connected.
|
package/package.json
CHANGED
package/routes/api.js
CHANGED
|
@@ -147,7 +147,6 @@ router.post('/tiledesk/broadcast', async (req, res) => {
|
|
|
147
147
|
let CONTENT_KEY = "whatsapp-" + project_id;
|
|
148
148
|
let settings = await db.get(CONTENT_KEY);
|
|
149
149
|
|
|
150
|
-
console.log("Settings: ", settings);
|
|
151
150
|
if (!settings) {
|
|
152
151
|
return res.status(400).send({ success: false, error: "WhatsApp is not installed for the project_id: " + project_id });
|
|
153
152
|
}
|