@tiledesk/tiledesk-whatsapp-connector 0.1.28 → 0.1.30
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 +61 -44
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -898,18 +898,33 @@ router.post("/newtest", async (req, res) => {
|
|
|
898
898
|
|
|
899
899
|
//let short_uid = getRandomName();
|
|
900
900
|
let short_uid = uuidv4().substring(0, 8)
|
|
901
|
-
let
|
|
901
|
+
let key = "bottest:" + short_uid;
|
|
902
902
|
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
903
|
+
if (!redis_client) {
|
|
904
|
+
return res.status(500).send({ message: "Test it out on Whatsapp not available. Redis not ready."})
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
//console.log("redis_client: ", redis_client)
|
|
908
|
+
|
|
909
|
+
await redis_client.set(key, JSON.stringify(info));
|
|
910
|
+
//const value = await redis_client.get(key)
|
|
911
|
+
//console.log("get --> value: ", value)
|
|
912
|
+
redis_client.get(key, (err, value) => {
|
|
913
|
+
if (err) {
|
|
914
|
+
console.log("get err: ", err)
|
|
915
|
+
return res.status(500).send({success: "false", message: "No info found on redis"});
|
|
916
|
+
} else {
|
|
917
|
+
console.log("get value: ", value)
|
|
918
|
+
return res.status(200).send({short_uid: short_uid});
|
|
919
|
+
}
|
|
920
|
+
})
|
|
906
921
|
/*
|
|
907
|
-
const value = await redis_client.json.get(
|
|
922
|
+
const value = await redis_client.json.get(key, {
|
|
908
923
|
// JSON Path: .node = the element called 'node' at root level.
|
|
909
924
|
path: '.nodell',
|
|
910
925
|
});
|
|
911
926
|
*/
|
|
912
|
-
res.status(200).send({short_uid: short_uid});
|
|
927
|
+
//res.status(200).send({short_uid: short_uid});
|
|
913
928
|
})
|
|
914
929
|
|
|
915
930
|
router.post("/testitout/:project_id", async (req, res) => {
|
|
@@ -924,11 +939,23 @@ router.post("/testitout/:project_id", async (req, res) => {
|
|
|
924
939
|
|
|
925
940
|
let whatsappContact = req.body.entry[0].changes[0].value.contacts[0];
|
|
926
941
|
|
|
927
|
-
let
|
|
928
|
-
console.log("(testitout) key: ",
|
|
942
|
+
let key = "bottest:" + whatsappChannelMessage.text.body.substring(3);
|
|
943
|
+
console.log("(testitout) key: ", key);
|
|
929
944
|
|
|
945
|
+
/*
|
|
930
946
|
const info = await redis_client.json.get(REDIS_KEY);
|
|
931
947
|
console.log("(testitout) info: ", info);
|
|
948
|
+
*/
|
|
949
|
+
let test_info;
|
|
950
|
+
redis_client.get(key, (err, value) => {
|
|
951
|
+
if (err) {
|
|
952
|
+
console.log("No info found on redis. Exit..");
|
|
953
|
+
res.sendStatus(200);
|
|
954
|
+
} else {
|
|
955
|
+
test_info = JSON.parse(value)
|
|
956
|
+
console.log("test_info: ", test_info)
|
|
957
|
+
}
|
|
958
|
+
})
|
|
932
959
|
|
|
933
960
|
let message_info = {
|
|
934
961
|
channel: "whatsapp",
|
|
@@ -960,7 +987,7 @@ router.post("/testitout/:project_id", async (req, res) => {
|
|
|
960
987
|
console.log("(testitout) tiledeskJsonMessage: ", tiledeskJsonMessage)
|
|
961
988
|
|
|
962
989
|
const tdChannel = new TiledeskChannel({ settings: settings, API_URL: API_URL })
|
|
963
|
-
const response = await tdChannel.sendAndAddBot(tiledeskJsonMessage, message_info,
|
|
990
|
+
const response = await tdChannel.sendAndAddBot(tiledeskJsonMessage, message_info, test_info.bot_id)
|
|
964
991
|
console.log("testitout --> send response: ", response)
|
|
965
992
|
|
|
966
993
|
res.status(200).send("testitout")
|
|
@@ -972,7 +999,7 @@ router.post("/testitout/:project_id", async (req, res) => {
|
|
|
972
999
|
// *****************************
|
|
973
1000
|
|
|
974
1001
|
async function startApp(settings, callback) {
|
|
975
|
-
console.log("Starting Whatsapp App");
|
|
1002
|
+
console.log("(whatsapp) Starting Whatsapp App");
|
|
976
1003
|
|
|
977
1004
|
if (!settings.MONGODB_URL) {
|
|
978
1005
|
throw new Error("settings.MONGODB_URL is mandatory");
|
|
@@ -982,63 +1009,61 @@ async function startApp(settings, callback) {
|
|
|
982
1009
|
throw new Error("settings.API_URL is mandatory");
|
|
983
1010
|
} else {
|
|
984
1011
|
API_URL = settings.API_URL;
|
|
985
|
-
console.log("API_URL: ", API_URL);
|
|
1012
|
+
console.log("(whatsapp) API_URL: ", API_URL);
|
|
986
1013
|
}
|
|
987
1014
|
|
|
988
1015
|
if (!settings.BASE_URL) {
|
|
989
1016
|
throw new Error("settings.BASE_URL is mandatory");
|
|
990
1017
|
} else {
|
|
991
1018
|
BASE_URL = settings.BASE_URL;
|
|
992
|
-
console.log("BASE_URL: ", BASE_URL);
|
|
1019
|
+
console.log("(whatsapp) BASE_URL: ", BASE_URL);
|
|
993
1020
|
}
|
|
994
1021
|
|
|
995
1022
|
if (!settings.GRAPH_URL) {
|
|
996
1023
|
throw new Error("settings.GRAPH_URL is mandatory");
|
|
997
1024
|
} else {
|
|
998
1025
|
GRAPH_URL = settings.GRAPH_URL;
|
|
999
|
-
console.log("GRAPH_URL: ", GRAPH_URL);
|
|
1026
|
+
console.log("(whatsapp) GRAPH_URL: ", GRAPH_URL);
|
|
1000
1027
|
}
|
|
1001
1028
|
|
|
1002
1029
|
if (!settings.APPS_API_URL) {
|
|
1003
1030
|
throw new Error("settings.APPS_API_URL is mandatory");
|
|
1004
1031
|
} else {
|
|
1005
1032
|
APPS_API_URL = settings.APPS_API_URL;
|
|
1006
|
-
console.log("APPS_API_URL: ", APPS_API_URL);
|
|
1033
|
+
console.log("(whatsapp) APPS_API_URL: ", APPS_API_URL);
|
|
1007
1034
|
}
|
|
1008
1035
|
|
|
1009
|
-
if (
|
|
1010
|
-
throw new Error("!settings.REDIS_HOST is mandatory");
|
|
1011
|
-
} else {
|
|
1036
|
+
if (settings.REDIS_HOST && settings.REDIS_PORT && settings.REDIS_PASSWORD) {
|
|
1012
1037
|
REDIS_HOST = settings.REDIS_HOST;
|
|
1013
|
-
console.log("REDIS_HOST: ", REDIS_HOST);
|
|
1014
|
-
}
|
|
1015
|
-
|
|
1016
|
-
if (!settings.REDIS_PORT) {
|
|
1017
|
-
throw new Error("!settings.REDIS_PORT is mandatory");
|
|
1018
|
-
} else {
|
|
1019
1038
|
REDIS_PORT = settings.REDIS_PORT;
|
|
1020
|
-
console.log("REDIS_PORT: ", REDIS_PORT);
|
|
1021
|
-
}
|
|
1022
|
-
|
|
1023
|
-
if (!settings.REDIS_PASSWORD) {
|
|
1024
|
-
throw new Error("!settings.REDIS_PASSWORD is mandatory");
|
|
1025
|
-
} else {
|
|
1026
1039
|
REDIS_PASSWORD = settings.REDIS_PASSWORD;
|
|
1027
|
-
|
|
1040
|
+
connectRedis();
|
|
1041
|
+
} else {
|
|
1042
|
+
console.log("(whatsapp) Missing redis parameters --> Test it out on WhatsApp disabled");
|
|
1028
1043
|
}
|
|
1029
1044
|
|
|
1030
1045
|
if (settings.log) {
|
|
1031
1046
|
log = settings.log;
|
|
1032
1047
|
}
|
|
1033
1048
|
|
|
1049
|
+
db.connect(settings.MONGODB_URL, () => {
|
|
1050
|
+
console.log("(whatsapp) KVBaseMongo successfully connected.");
|
|
1051
|
+
|
|
1052
|
+
if (callback) {
|
|
1053
|
+
callback();
|
|
1054
|
+
}
|
|
1055
|
+
})
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
function connectRedis() {
|
|
1034
1059
|
redis_client = redis.createClient({
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
password: REDIS_PASSWORD
|
|
1060
|
+
host: REDIS_HOST,
|
|
1061
|
+
port: REDIS_PORT,
|
|
1062
|
+
password: REDIS_PASSWORD
|
|
1038
1063
|
});
|
|
1039
1064
|
|
|
1040
1065
|
redis_client.on('error', err => {
|
|
1041
|
-
console.log('Error ' + err);
|
|
1066
|
+
console.log('(whatsapp) Connect Redis Error ' + err);
|
|
1042
1067
|
})
|
|
1043
1068
|
/*
|
|
1044
1069
|
redis_client.on('connect', () => {
|
|
@@ -1046,22 +1071,14 @@ async function startApp(settings, callback) {
|
|
|
1046
1071
|
});
|
|
1047
1072
|
*/
|
|
1048
1073
|
redis_client.on('ready', () => {
|
|
1049
|
-
console.log("Redis ready")
|
|
1074
|
+
console.log("(whatsapp) Redis ready!")
|
|
1050
1075
|
})
|
|
1051
1076
|
//await redis_client.connect(); // only for v4
|
|
1052
|
-
|
|
1053
1077
|
|
|
1054
|
-
db.connect(settings.MONGODB_URL, () => {
|
|
1055
|
-
console.log("KVBaseMongo successfully connected.");
|
|
1056
|
-
|
|
1057
|
-
if (callback) {
|
|
1058
|
-
callback();
|
|
1059
|
-
}
|
|
1060
|
-
})
|
|
1061
1078
|
}
|
|
1062
1079
|
|
|
1063
1080
|
function readHTMLFile(templateName, callback) {
|
|
1064
|
-
console.log("Reading file: ", templateName)
|
|
1081
|
+
//console.log("Reading file: ", templateName)
|
|
1065
1082
|
fs.readFile(__dirname + '/template' + templateName, { encoding: 'utf-8' },
|
|
1066
1083
|
function(err, html) {
|
|
1067
1084
|
if (err) {
|