@tiledesk/tiledesk-whatsapp-connector 1.0.9 → 1.0.10
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 +51 -122
- package/package.json +1 -1
- package/tiledesk/TiledeskBotTester.js +6 -1
package/index.js
CHANGED
|
@@ -185,7 +185,13 @@ router.get("/configure", async (req, res) => {
|
|
|
185
185
|
|
|
186
186
|
router.get('/onboarding/callback', async (req, res) => {
|
|
187
187
|
const { code, waba_id, phone_number_id, business_id } = req.query;
|
|
188
|
-
|
|
188
|
+
let state;
|
|
189
|
+
try {
|
|
190
|
+
state = JSON.parse(req.query.state)
|
|
191
|
+
} catch(err) {
|
|
192
|
+
winston.error("(wab) Error parsing query state");
|
|
193
|
+
return res.status(500).send({ success: false, message: "Error parsing query state" });
|
|
194
|
+
}
|
|
189
195
|
const project_id = state.project_id;
|
|
190
196
|
const token = state.token;
|
|
191
197
|
|
|
@@ -700,9 +706,11 @@ router.post("/tiledesk", async (req, res) => {
|
|
|
700
706
|
}
|
|
701
707
|
}).catch((err) => {
|
|
702
708
|
winston.error("(wab) send message error: ", err.response.data);
|
|
709
|
+
return res.status(500).send({ success: false, error: "Send message error" });
|
|
703
710
|
});
|
|
704
711
|
} else {
|
|
705
712
|
winston.error("(wab) WhatsappJsonMessage is undefined!");
|
|
713
|
+
return res.status(500).send({ success: false, error: "Translation to whatsapp message failed" });
|
|
706
714
|
}
|
|
707
715
|
}
|
|
708
716
|
|
|
@@ -993,105 +1001,21 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
993
1001
|
|
|
994
1002
|
// Standard message
|
|
995
1003
|
} else {
|
|
996
|
-
|
|
1004
|
+
const value = req.body?.entry?.[0]?.changes?.[0]?.value;
|
|
997
1005
|
|
|
998
1006
|
let message_info = {
|
|
999
1007
|
channel: "whatsapp",
|
|
1000
1008
|
whatsapp: {
|
|
1001
|
-
phone_number_id:
|
|
1002
|
-
from:
|
|
1003
|
-
firstname:
|
|
1009
|
+
phone_number_id: value?.metadata?.phone_number_id || null,
|
|
1010
|
+
from: value?.messages?.[0]?.from || null,
|
|
1011
|
+
firstname: value?.contacts?.[0]?.profile?.name || "Unknown",
|
|
1004
1012
|
lastname: " "
|
|
1005
1013
|
},
|
|
1006
1014
|
};
|
|
1007
1015
|
|
|
1008
|
-
let
|
|
1009
|
-
|
|
1010
|
-
/*
|
|
1011
|
-
if ((whatsappChannelMessage.type == 'text')) {
|
|
1012
|
-
winston.debug("(wab) message type: text")
|
|
1013
|
-
tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname);
|
|
1014
|
-
}
|
|
1015
|
-
|
|
1016
|
-
else if (whatsappChannelMessage.type == 'interactive') {
|
|
1017
|
-
winston.debug("(wab) message type: interactive")
|
|
1018
|
-
tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname);
|
|
1019
|
-
}
|
|
1020
|
-
|
|
1021
|
-
else if ((whatsappChannelMessage.type == 'image') || (whatsappChannelMessage.type == 'video') || (whatsappChannelMessage.type == 'document') || (whatsappChannelMessage.type == 'audio')) {
|
|
1022
|
-
let media;
|
|
1023
|
-
const util = new TiledeskWhatsapp({ token: settings.wab_token, GRAPH_URL: GRAPH_URL, API_URL: API_URL, BASE_FILE_URL: BASE_FILE_URL })
|
|
1024
|
-
|
|
1025
|
-
if (whatsappChannelMessage.type == 'image') {
|
|
1026
|
-
media = whatsappChannelMessage.image;
|
|
1027
|
-
const filename = await util.downloadMedia(media.id);
|
|
1028
|
-
if (!filename) {
|
|
1029
|
-
winston.debug("(wab) Unable to download media with id " + media.id + ". Message not sent.");
|
|
1030
|
-
return res.status(500).send({ success: false, error: "unable to download media" })
|
|
1031
|
-
}
|
|
1032
|
-
let file_path = path.join(__dirname, 'tmp', filename);
|
|
1033
|
-
|
|
1034
|
-
const image_url = await util.uploadMedia(file_path, "images");
|
|
1035
|
-
winston.debug("(wab) image_url: " + image_url)
|
|
1036
|
-
|
|
1037
|
-
tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname, image_url);
|
|
1038
|
-
}
|
|
1039
|
-
|
|
1040
|
-
if (whatsappChannelMessage.type == 'video') {
|
|
1041
|
-
media = whatsappChannelMessage.video;
|
|
1042
|
-
|
|
1043
|
-
const filename = await util.downloadMedia(media.id);
|
|
1044
|
-
if (!filename) {
|
|
1045
|
-
winston.debug("(wab) Unable to download media with id " + media.id + ". Message not sent.");
|
|
1046
|
-
return res.status(500).send({ success: false, error: "unable to download media" })
|
|
1047
|
-
}
|
|
1048
|
-
let file_path = path.join(__dirname, 'tmp', filename);
|
|
1049
|
-
|
|
1050
|
-
const media_url = await util.uploadMedia(file_path, "files");
|
|
1051
|
-
winston.debug("(wab) media_url: " + media_url)
|
|
1052
|
-
|
|
1053
|
-
tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname, media_url);
|
|
1054
|
-
}
|
|
1055
|
-
|
|
1056
|
-
if (whatsappChannelMessage.type == 'document') {
|
|
1057
|
-
media = whatsappChannelMessage.document;
|
|
1058
|
-
|
|
1059
|
-
const filename = await util.downloadMedia(media.id);
|
|
1060
|
-
if (!filename) {
|
|
1061
|
-
winston.debug("(wab) Unable to download media with id " + media.id + ". Message not sent.");
|
|
1062
|
-
return res.status(500).send({ success: false, error: "unable to download media" })
|
|
1063
|
-
}
|
|
1064
|
-
let file_path = path.join(__dirname, 'tmp', filename);
|
|
1065
|
-
|
|
1066
|
-
const media_url = await util.uploadMedia(file_path, "files");
|
|
1067
|
-
winston.debug("(wab) media_url: " + media_url)
|
|
1068
|
-
|
|
1069
|
-
tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname, media_url);
|
|
1070
|
-
}
|
|
1071
|
-
|
|
1072
|
-
if (whatsappChannelMessage.type == 'audio') {
|
|
1073
|
-
media = whatsappChannelMessage.audio;
|
|
1074
|
-
|
|
1075
|
-
const filename = await util.downloadMedia(media.id);
|
|
1076
|
-
if (!filename) {
|
|
1077
|
-
winston.debug("(wab) Unable to download media with id " + media.id + ". Message not sent.");
|
|
1078
|
-
return res.status(500).send({ success: false, error: "unable to download media" })
|
|
1079
|
-
}
|
|
1080
|
-
let file_path = path.join(__dirname, 'tmp', filename);
|
|
1081
|
-
|
|
1082
|
-
const media_url = await util.uploadMedia(file_path, "files");
|
|
1083
|
-
winston.debug("(wab) media_url: " + media_url)
|
|
1084
|
-
|
|
1085
|
-
tiledeskJsonMessage = await tlr.toTiledesk(whatsappChannelMessage, firstname, media_url);
|
|
1086
|
-
}
|
|
1016
|
+
let firstname = message_info.whatsapp.firstname;
|
|
1087
1017
|
|
|
1088
|
-
|
|
1089
|
-
// unsupported. Try anyway to send something.
|
|
1090
|
-
winston.debug("(wab) unsupported message")
|
|
1091
|
-
}
|
|
1092
|
-
*/
|
|
1093
|
-
|
|
1094
|
-
////////////////////////////////////////////////////
|
|
1018
|
+
let tiledeskJsonMessage;
|
|
1095
1019
|
|
|
1096
1020
|
if (
|
|
1097
1021
|
whatsappChannelMessage.type == "image" ||
|
|
@@ -1491,37 +1415,42 @@ router.get("/templates/detail", async (req, res) => {
|
|
|
1491
1415
|
let namespace = templates_info.message_template_namespace;
|
|
1492
1416
|
let template = await tm.getTemplateById(namespace);
|
|
1493
1417
|
*/
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1418
|
+
try {
|
|
1419
|
+
let tm = new TemplateManager({
|
|
1420
|
+
token: settings.wab_token,
|
|
1421
|
+
business_account_id: settings.business_account_id,
|
|
1422
|
+
GRAPH_URL: GRAPH_URL,
|
|
1423
|
+
});
|
|
1424
|
+
let templates = await tm.getTemplates();
|
|
1425
|
+
let template = JSON.parse(JSON.stringify(templates.data.find((t) => t.id === template_id)));
|
|
1426
|
+
let template_name = template.name;
|
|
1427
|
+
|
|
1428
|
+
let template_copy = {
|
|
1429
|
+
name: template.name,
|
|
1430
|
+
components: template.components,
|
|
1431
|
+
language: template.language,
|
|
1432
|
+
status: template.status,
|
|
1433
|
+
id: template.id,
|
|
1434
|
+
category: template.category,
|
|
1435
|
+
};
|
|
1436
|
+
|
|
1437
|
+
readHTMLFile("/template_detail.html", (err, html) => {
|
|
1438
|
+
var template = handlebars.compile(html);
|
|
1439
|
+
var replacements = {
|
|
1440
|
+
app_version: pjson.version,
|
|
1441
|
+
project_id: project_id,
|
|
1442
|
+
token: token,
|
|
1443
|
+
app_id: app_id,
|
|
1444
|
+
name: template_name,
|
|
1445
|
+
template: template_copy,
|
|
1446
|
+
};
|
|
1447
|
+
var html = template(replacements);
|
|
1448
|
+
res.send(html);
|
|
1449
|
+
});
|
|
1450
|
+
} catch (err) {
|
|
1451
|
+
winston.error("(wab) Error geting templates: ", err);
|
|
1452
|
+
return res.status(500).send({ success: false, error: err?.response?.data || "General error" });
|
|
1453
|
+
}
|
|
1525
1454
|
} else {
|
|
1526
1455
|
return res.send("whatsapp not installed on this project");
|
|
1527
1456
|
}
|
package/package.json
CHANGED
|
@@ -75,7 +75,12 @@ class TiledeskBotTester {
|
|
|
75
75
|
return;
|
|
76
76
|
|
|
77
77
|
} else {
|
|
78
|
-
|
|
78
|
+
try {
|
|
79
|
+
test_info = JSON.parse(value)
|
|
80
|
+
} catch(err) {
|
|
81
|
+
winston.error("(wab) Error parsing value: " + value);
|
|
82
|
+
reject(err);
|
|
83
|
+
}
|
|
79
84
|
let message_info = {
|
|
80
85
|
channel: "whatsapp",
|
|
81
86
|
whatsapp: {
|