@tiledesk/tiledesk-tybot-connector 0.2.62 → 0.2.63
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/CHANGELOG.md +4 -0
- package/index.js +87 -0
- package/models/TiledeskChatbotUtil.js +73 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,10 @@ available on:
|
|
|
6
6
|
▶️ https://www.npmjs.com/package/@tiledesk/tiledesk-tybot-connector
|
|
7
7
|
|
|
8
8
|
|
|
9
|
+
# v0.2.63
|
|
10
|
+
- BUG fix on /ext/parameters/requests/:requestid
|
|
11
|
+
- Moved getChatbotParameters function in TiledeskChatbotUtil
|
|
12
|
+
|
|
9
13
|
# v0.2.62
|
|
10
14
|
- Whatsapp fix (transcript error whitout "senderFullname")
|
|
11
15
|
|
package/index.js
CHANGED
|
@@ -358,8 +358,95 @@ router.get('/message/context/:messageid', async (req, res) => {
|
|
|
358
358
|
}
|
|
359
359
|
});
|
|
360
360
|
|
|
361
|
+
router.get('/ext/reserved/parameters/requests/:requestid', async (req, res) => {
|
|
362
|
+
const requestId = req.params.requestid;
|
|
363
|
+
const parameters = await TiledeskChatbot.allParametersStatic(tdcache, requestId);
|
|
364
|
+
if (parameters === null) {
|
|
365
|
+
res.send([]);
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
if (req.query.all != null) {
|
|
369
|
+
res.send(parameters);
|
|
370
|
+
}
|
|
371
|
+
else {
|
|
372
|
+
const RESERVED = [
|
|
373
|
+
TiledeskChatbotConst.REQ_CHATBOT_NAME_KEY,
|
|
374
|
+
TiledeskChatbotConst.REQ_CHAT_URL,
|
|
375
|
+
TiledeskChatbotConst.REQ_CITY_KEY,
|
|
376
|
+
TiledeskChatbotConst.REQ_COUNTRY_KEY,
|
|
377
|
+
TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY,
|
|
378
|
+
TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY,
|
|
379
|
+
TiledeskChatbotConst.REQ_END_USER_ID_KEY,
|
|
380
|
+
TiledeskChatbotConst.REQ_END_USER_IP_ADDRESS_KEY,
|
|
381
|
+
TiledeskChatbotConst.REQ_LAST_MESSAGE_ID_KEY,
|
|
382
|
+
TiledeskChatbotConst.REQ_LAST_USER_TEXT_KEY,
|
|
383
|
+
TiledeskChatbotConst.REQ_PROJECT_ID_KEY,
|
|
384
|
+
TiledeskChatbotConst.REQ_REQUEST_ID_KEY,
|
|
385
|
+
TiledeskChatbotConst.REQ_USER_AGENT_KEY,
|
|
386
|
+
TiledeskChatbotConst.REQ_USER_LANGUAGE_KEY,
|
|
387
|
+
TiledeskChatbotConst.REQ_USER_SOURCE_PAGE_KEY,
|
|
388
|
+
TiledeskChatbotConst.REQ_LAST_USER_MESSAGE_TYPE_KEY,
|
|
389
|
+
TiledeskChatbotConst.REQ_TRANSCRIPT_KEY,
|
|
390
|
+
TiledeskChatbotConst.REQ_LAST_USER_MESSAGE_KEY,
|
|
391
|
+
"lastUserImageURL", // image
|
|
392
|
+
"lastUserImageName", // image
|
|
393
|
+
"lastUserImageWidth", // image
|
|
394
|
+
"lastUserImageHeight", // image
|
|
395
|
+
"lastUserImageType", // image
|
|
396
|
+
"lastUserDocumentURL", // file
|
|
397
|
+
"lastUserDocumentName", // file
|
|
398
|
+
"lastUserDocumentType" // file
|
|
399
|
+
]
|
|
400
|
+
let userParams = {};
|
|
401
|
+
if (parameters) {
|
|
402
|
+
for (const [key, value] of Object.entries(parameters)) {
|
|
403
|
+
// console.log(key, value);
|
|
404
|
+
// There is a bug that moves the requestId as a key in request attributes, so: && !key.startsWith("support-group-")
|
|
405
|
+
if (!key.startsWith("_") && !RESERVED.some(e => e === key) && !key.startsWith("support-group-")) {
|
|
406
|
+
userParams[key] = value;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
res.send(userParams);
|
|
411
|
+
}
|
|
412
|
+
});
|
|
413
|
+
|
|
361
414
|
router.get('/ext/parameters/requests/:requestid', async (req, res) => {
|
|
415
|
+
// console.log("Checking authorization...");
|
|
416
|
+
// const authorization = req.headers["authorization"];
|
|
417
|
+
// if (!authorization) {
|
|
418
|
+
// console.log("No authorization header...");
|
|
419
|
+
// res.status(401).send("Unauthorized");
|
|
420
|
+
// return;
|
|
421
|
+
// }
|
|
422
|
+
// const token = req.headers["authorization"];
|
|
423
|
+
// const publicKey = process.env.GLOBAL_SECRET_OR_PUB_KEY;
|
|
424
|
+
// console.log("Got public key:", publicKey);
|
|
425
|
+
// const _decoded = null;
|
|
426
|
+
// jwt.verify(token, publicKey, function (err, decoded) {
|
|
427
|
+
// _decoded = decoded;
|
|
428
|
+
// });
|
|
429
|
+
// console.log("Authorization header field checking", req.headers["authorization"]);
|
|
430
|
+
|
|
431
|
+
|
|
362
432
|
const requestId = req.params.requestid;
|
|
433
|
+
if (!requestId) {
|
|
434
|
+
res.status(404).send("Not found");
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
const request_parts = requestId.split("-");
|
|
438
|
+
if (request_parts && request_parts.length >= 4) {
|
|
439
|
+
const project_id = request_parts[2];
|
|
440
|
+
// console.log("ProjectId:", project_id);
|
|
441
|
+
if (project_id !== "656054000410fa00132e5dcc") { //&& project_id !== "ANOTHER P_ID"
|
|
442
|
+
res.status(401).send("Unauthorized");
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
else if (!request_parts || ( request_parts && request_parts.length < 4) ) {
|
|
447
|
+
res.status(500).send("Invalid request ID");
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
363
450
|
const parameters = await TiledeskChatbot.allParametersStatic(tdcache, requestId);
|
|
364
451
|
if (parameters === null) {
|
|
365
452
|
res.send([]);
|
|
@@ -4,6 +4,8 @@ const { TiledeskChatbotConst } = require('./TiledeskChatbotConst');
|
|
|
4
4
|
const { TiledeskChatbot } = require('./TiledeskChatbot.js');
|
|
5
5
|
let parser = require('accept-language-parser');
|
|
6
6
|
const { Directives } = require('../tiledeskChatbotPlugs/directives/Directives.js');
|
|
7
|
+
require('dotenv').config();
|
|
8
|
+
let axios = require('axios');
|
|
7
9
|
|
|
8
10
|
class TiledeskChatbotUtil {
|
|
9
11
|
|
|
@@ -752,6 +754,77 @@ class TiledeskChatbotUtil {
|
|
|
752
754
|
return isValid;
|
|
753
755
|
}
|
|
754
756
|
|
|
757
|
+
/**
|
|
758
|
+
* A stub to get the request parameters, hosted by tilebot on:
|
|
759
|
+
* /${TILEBOT_ROUTE}/ext/parameters/requests/${requestId}?all
|
|
760
|
+
*
|
|
761
|
+
* @param {string} requestId. Tiledesk chatbot/requestId parameters
|
|
762
|
+
*/
|
|
763
|
+
getChatbotParameters(requestId, callback) {
|
|
764
|
+
// const jwt_token = this.fixToken(token);
|
|
765
|
+
const url = `${process.env.TYBOT_ENDPOINT}/ext/reserved/parameters/requests/${requestId}?all`;
|
|
766
|
+
const HTTPREQUEST = {
|
|
767
|
+
url: url,
|
|
768
|
+
headers: {
|
|
769
|
+
'Content-Type': 'application/json'
|
|
770
|
+
},
|
|
771
|
+
method: 'get'
|
|
772
|
+
};
|
|
773
|
+
this.myrequest(
|
|
774
|
+
HTTPREQUEST,
|
|
775
|
+
function (err, resbody) {
|
|
776
|
+
if (err) {
|
|
777
|
+
if (callback) {
|
|
778
|
+
callback(err);
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
else {
|
|
782
|
+
if (callback) {
|
|
783
|
+
callback(null, resbody);
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
}, false
|
|
787
|
+
);
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
myrequest(options, callback, log) {
|
|
791
|
+
if (log) {
|
|
792
|
+
console.log("API URL:", options.url);
|
|
793
|
+
console.log("** Options:", JSON.stringify(options));
|
|
794
|
+
}
|
|
795
|
+
axios(
|
|
796
|
+
{
|
|
797
|
+
url: options.url,
|
|
798
|
+
method: options.method,
|
|
799
|
+
data: options.json,
|
|
800
|
+
params: options.params,
|
|
801
|
+
headers: options.headers
|
|
802
|
+
})
|
|
803
|
+
.then((res) => {
|
|
804
|
+
if (log) {
|
|
805
|
+
console.log("Response for url:", options.url);
|
|
806
|
+
console.log("Response headers:\n", JSON.stringify(res.headers));
|
|
807
|
+
}
|
|
808
|
+
if (res && res.status == 200 && res.data) {
|
|
809
|
+
if (callback) {
|
|
810
|
+
callback(null, res.data);
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
else {
|
|
814
|
+
if (callback) {
|
|
815
|
+
callback(TiledeskClient.getErr({ message: "Response status not 200" }, options, res), null, null);
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
})
|
|
819
|
+
.catch((error) => {
|
|
820
|
+
// console.error("An error occurred:", error);
|
|
821
|
+
if (callback) {
|
|
822
|
+
callback(error, null, null);
|
|
823
|
+
}
|
|
824
|
+
});
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
|
|
755
828
|
}
|
|
756
829
|
|
|
757
830
|
module.exports = { TiledeskChatbotUtil };
|