@tiledesk/tiledesk-tybot-connector 0.1.72 → 0.1.74
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/.env +1 -1
- package/CHANGELOG.md +7 -0
- package/ExtUtil.js +1 -0
- package/index.js +19 -15
- package/package.json +2 -1
- package/test/web_request-bot.js +11 -2
- package/test/web_request-conversation_test.js +9 -2
- package/tiledeskChatbotPlugs/directives/DirWebRequest.js +12 -7
package/.env
CHANGED
|
@@ -6,4 +6,4 @@ TYBOT_ENDPOINT=http://localhost:10001
|
|
|
6
6
|
_TYBOT_ENDPOINT=http://localhost:3000
|
|
7
7
|
CHATBOT_TOKEN=JWT eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ3ZWJob29rX2VuYWJsZWQiOmZhbHNlLCJ0eXBlIjoiZXh0ZXJuYWwiLCJsYW5ndWFnZSI6ImVuIiwicHVibGljIjpmYWxzZSwiX2lkIjoiNjM4Yzc5MDQxZGI0NDkwMDM1MTEwMjYwIiwibmFtZSI6IlRoZSBGb3JtIHYyIC0gZXh0IiwidXJsIjoiaHR0cHM6Ly90aWxlYm90LWRldi5oZXJva3VhcHAuY29tL2V4dC82MzhjNzhkNzFkYjQ0OTAwMzUxMTAxYzIiLCJpZF9wcm9qZWN0IjoiNjM4Yzc4YTYxZGI0NDkwMDM1MTBmZjkxIiwidHJhc2hlZCI6ZmFsc2UsImNyZWF0ZWRCeSI6IjVlMDlkMTZkNGQzNjExMDAxNzUwNmQ3ZiIsImNyZWF0ZWRBdCI6IjIwMjItMTItMDRUMTA6NDA6MDQuMjA3WiIsInVwZGF0ZWRBdCI6IjIwMjItMTItMDVUMDc6MjE6MDIuOTIxWiIsIl9fdiI6MCwiZGVzY3JpcHRpb24iOiJPbiBIZXJva3UiLCJpYXQiOjE2NzA2NzE4ODksImF1ZCI6Imh0dHBzOi8vdGlsZWRlc2suY29tL2JvdHMvNjM4Yzc5MDQxZGI0NDkwMDM1MTEwMjYwIiwiaXNzIjoiaHR0cHM6Ly90aWxlZGVzay5jb20iLCJzdWIiOiJib3QiLCJqdGkiOiJmNDVlZGIwYS0zNzVhLTQ0NjMtYjFhZi1jM2ZiZDg4YmE3ZGQifQ.FbW3csHl1sQgSyRz5Jg0qaTvvpXWXgWHlJ1JWoVbv3s
|
|
8
8
|
_CHATBOT_ENDPOINT=http://localhost:10001
|
|
9
|
-
|
|
9
|
+
API_LOG=1
|
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,13 @@
|
|
|
5
5
|
available on:
|
|
6
6
|
▶️ https://www.npmjs.com/package/@tiledesk/tiledesk-tybot-connector
|
|
7
7
|
|
|
8
|
+
### 0.1.74
|
|
9
|
+
- DEBUG VERSION
|
|
10
|
+
- Added if (log || projectId === "64218dfecdb804001380b9ba")
|
|
11
|
+
|
|
12
|
+
### 0.1.73
|
|
13
|
+
- Bug fixing: WebRequest headersString now is a JSON
|
|
14
|
+
|
|
8
15
|
### 0.1.72
|
|
9
16
|
- Removed error-prone request caching.
|
|
10
17
|
- Added botId caching on requestId with key: request_botId_key
|
package/ExtUtil.js
CHANGED
|
@@ -19,6 +19,7 @@ class ExtUtil {
|
|
|
19
19
|
const messagePipeline = new MessagePipeline(static_bot_answer, null);
|
|
20
20
|
// /// const webhookurl = bot.webhook_url;
|
|
21
21
|
// /// messagePipeline.addPlug(new WebhookChatbotPlug(message.request, webhookurl, token));
|
|
22
|
+
console.log("111")
|
|
22
23
|
messagePipeline.addPlug(directivesPlug);
|
|
23
24
|
messagePipeline.addPlug(new FillParamsChatbotPlug(request, tdcache, log)); // in original message
|
|
24
25
|
messagePipeline.addPlug(new SplitsChatbotPlug(log));
|
package/index.js
CHANGED
|
@@ -353,8 +353,13 @@ router.post('/ext/:projectId/requests/:requestId/messages', async (req, res) =>
|
|
|
353
353
|
const projectId = req.params.projectId;
|
|
354
354
|
const requestId = req.params.requestId;
|
|
355
355
|
const token = req.headers["authorization"];
|
|
356
|
+
if (log || projectId === "64218dfecdb804001380b9ba") {console.log("64218dfecdb804001380b9ba => projectId:", projectId);}
|
|
357
|
+
if (log || projectId === "64218dfecdb804001380b9ba") {console.log("64218dfecdb804001380b9ba => requestId:", requestId);}
|
|
358
|
+
if (log || projectId === "64218dfecdb804001380b9ba") {console.log("64218dfecdb804001380b9ba => req.headers:", req.headers);}
|
|
359
|
+
if (log || projectId === "64218dfecdb804001380b9ba") {console.log("64218dfecdb804001380b9ba => token:", token);}
|
|
360
|
+
|
|
356
361
|
let answer = req.body;
|
|
357
|
-
|
|
362
|
+
if (log || projectId === "64218dfecdb804001380b9ba") {console.log("64218dfecdb804001380b9ba => answer on sendSupportMessageExt:", JSON.stringify(answer));}
|
|
358
363
|
const tdclient = new TiledeskClient({
|
|
359
364
|
projectId: projectId,
|
|
360
365
|
token: token,
|
|
@@ -386,34 +391,35 @@ router.post('/ext/:projectId/requests/:requestId/messages', async (req, res) =>
|
|
|
386
391
|
// console.log("Cache request found.");
|
|
387
392
|
}
|
|
388
393
|
catch(err) {
|
|
389
|
-
console.error("Request not found:", requestId);
|
|
394
|
+
console.error("64218dfecdb804001380b9ba => Request not found:", requestId);
|
|
390
395
|
}
|
|
391
396
|
// if (log) {console.log("(No tdcache) Got request with APIs");}
|
|
392
397
|
// }
|
|
393
398
|
if (!request) {
|
|
394
|
-
if (log) {console.log("
|
|
399
|
+
if (log || projectId === "64218dfecdb804001380b9ba") {console.log("64218dfecdb804001380b9ba => Creating new Request. Chatbot-pure directives still work. Tiledesk specific directives don't");}
|
|
395
400
|
const request_botId_key = "tilebot:botId_requests:" + requestId;
|
|
396
401
|
const botId = await tdcache.get(request_botId_key);
|
|
397
|
-
if (log) {console.log("current botId [" + request_botId_key + "]:", botId);}
|
|
402
|
+
if (log || projectId === "64218dfecdb804001380b9ba") {console.log("64218dfecdb804001380b9ba => current botId [" + request_botId_key + "]:", botId);}
|
|
398
403
|
request = {
|
|
399
404
|
request_id: requestId,
|
|
400
405
|
id_project: projectId,
|
|
401
406
|
bot_id: botId
|
|
402
407
|
}
|
|
403
408
|
}
|
|
404
|
-
if (log) {
|
|
405
|
-
console.log("/ext request....", JSON.stringify(request));
|
|
406
|
-
console.log("/ext APIURL....", APIURL);
|
|
407
|
-
console.log("/ext process.env.TYBOT_ENDPOINT....", process.env.TYBOT_ENDPOINT);
|
|
409
|
+
if (log || projectId === "64218dfecdb804001380b9ba") {
|
|
410
|
+
console.log("64218dfecdb804001380b9ba => /ext request....", JSON.stringify(request));
|
|
411
|
+
console.log("64218dfecdb804001380b9ba => /ext APIURL....", APIURL);
|
|
412
|
+
console.log("64218dfecdb804001380b9ba => /ext process.env.TYBOT_ENDPOINT....", process.env.TYBOT_ENDPOINT);
|
|
408
413
|
}
|
|
409
414
|
let directivesPlug = new DirectivesChatbotPlug({supportRequest: request, TILEDESK_API_ENDPOINT: APIURL, TILEBOT_ENDPOINT:process.env.TYBOT_ENDPOINT, token: token, log: log, HELP_CENTER_API_ENDPOINT: process.env.HELP_CENTER_API_ENDPOINT, cache: tdcache});
|
|
415
|
+
// let directivesPlug = null;
|
|
410
416
|
// PIPELINE-EXT
|
|
411
417
|
// if (log) {console.log("answer to process:", JSON.stringify(answer));}
|
|
412
418
|
const original_answer_text = answer.text;
|
|
413
419
|
const bot_answer = await ExtUtil.execPipelineExt(request, answer, directivesPlug, tdcache, log);
|
|
414
|
-
|
|
420
|
+
if (log || projectId === "64218dfecdb804001380b9ba") {console.log("64218dfecdb804001380b9ba => bot_answer", JSON.stringify(bot_answer))}
|
|
415
421
|
if (bot_answer) {
|
|
416
|
-
if (log) {console.log("adding to bot_answer original_answer_text:", JSON.stringify(original_answer_text));}
|
|
422
|
+
if (log || projectId === "64218dfecdb804001380b9ba") {console.log("64218dfecdb804001380b9ba => adding to bot_answer original_answer_text:", JSON.stringify(original_answer_text));}
|
|
417
423
|
if (!bot_answer.attributes) {
|
|
418
424
|
bot_answer.attributes = {};
|
|
419
425
|
}
|
|
@@ -423,8 +429,9 @@ router.post('/ext/:projectId/requests/:requestId/messages', async (req, res) =>
|
|
|
423
429
|
bot_answer.attributes["_raw_message"] = original_answer_text;
|
|
424
430
|
// if (log) {console.log("bot_answer", JSON.stringify(bot_answer));}
|
|
425
431
|
tdclient.sendSupportMessage(requestId, bot_answer, (err, response) => {
|
|
432
|
+
if (log || projectId === "64218dfecdb804001380b9ba") {console.log("64218dfecdb804001380b9ba => bot_answer sent:", JSON.stringify(bot_answer));}
|
|
426
433
|
if (err) {
|
|
427
|
-
console.error("Error sending message", err);
|
|
434
|
+
console.error("64218dfecdb804001380b9ba => Error sending message", err);
|
|
428
435
|
}
|
|
429
436
|
directivesPlug.processDirectives( () => {
|
|
430
437
|
if (log) {console.log("After message - Directives executed.");}
|
|
@@ -432,6 +439,7 @@ router.post('/ext/:projectId/requests/:requestId/messages', async (req, res) =>
|
|
|
432
439
|
});
|
|
433
440
|
}
|
|
434
441
|
else {
|
|
442
|
+
if (log || projectId === "64218dfecdb804001380b9ba") {console.log("64218dfecdb804001380b9ba => !bot_answer");}
|
|
435
443
|
directivesPlug.processDirectives( () => {
|
|
436
444
|
if (log) {console.log("Directives executed.");}
|
|
437
445
|
});
|
|
@@ -606,10 +614,6 @@ async function connectRedis() {
|
|
|
606
614
|
console.log("(Tilebot) Redis connected.");
|
|
607
615
|
}
|
|
608
616
|
return;
|
|
609
|
-
// console.info("Tilebot started.");
|
|
610
|
-
// if (completionCallback) {
|
|
611
|
-
// completionCallback();
|
|
612
|
-
// }
|
|
613
617
|
}
|
|
614
618
|
|
|
615
619
|
module.exports = { router: router, startApp: startApp};
|
package/package.json
CHANGED
package/test/web_request-bot.js
CHANGED
|
@@ -66,7 +66,11 @@ const bot = {
|
|
|
66
66
|
"actions": [{
|
|
67
67
|
"_tdActionTitle": "var1",
|
|
68
68
|
"url": "http://localhost:10002/test/webrequest/get/plain",
|
|
69
|
-
"headersString":
|
|
69
|
+
"headersString": {
|
|
70
|
+
"Content-Type": "application/json",
|
|
71
|
+
"Cache-Control": "no-cache",
|
|
72
|
+
"User-Agent": "TiledeskBotRuntime"
|
|
73
|
+
},
|
|
70
74
|
"jsonBody": "{}",
|
|
71
75
|
"assignTo": "var1",
|
|
72
76
|
"method": "GET",
|
|
@@ -74,7 +78,12 @@ const bot = {
|
|
|
74
78
|
}, {
|
|
75
79
|
"_tdActionTitle": "service_reply",
|
|
76
80
|
"url": "http://localhost:10002/test/webrequest/post/plain",
|
|
77
|
-
"headersString":
|
|
81
|
+
"headersString": {
|
|
82
|
+
"Content-Type": "application/json",
|
|
83
|
+
"Cache-Control": "no-cache",
|
|
84
|
+
"User-Agent": "TiledeskBotRuntime",
|
|
85
|
+
"Accept": "*/*"
|
|
86
|
+
},
|
|
78
87
|
"jsonBody": "{\n\t\"name\": \"Andrea\"\n}",
|
|
79
88
|
"assignTo": "service_reply",
|
|
80
89
|
"method": "POST",
|
|
@@ -59,11 +59,18 @@ describe('Conversation for JSONCondition test', async () => {
|
|
|
59
59
|
let endpointServer = express();
|
|
60
60
|
endpointServer.use(bodyParser.json());
|
|
61
61
|
endpointServer.get('/test/webrequest/get/plain', async (req, res) => {
|
|
62
|
-
|
|
62
|
+
console.log("/webrequest GET req.headers:", req.headers);
|
|
63
|
+
assert(req.headers["user-agent"] === "TiledeskBotRuntime");
|
|
64
|
+
assert(req.headers["content-type"] === "application/json");
|
|
65
|
+
assert(req.headers["cache-control"] === "no-cache");
|
|
63
66
|
res.send("Application var");
|
|
64
67
|
});
|
|
65
68
|
endpointServer.post('/test/webrequest/post/plain', async (req, res) => {
|
|
66
|
-
|
|
69
|
+
console.log("/webrequest POST req.headers:", req.headers);
|
|
70
|
+
assert(req.headers["user-agent"] === "TiledeskBotRuntime");
|
|
71
|
+
assert(req.headers["content-type"] === "application/json");
|
|
72
|
+
assert(req.headers["cache-control"] === "no-cache");
|
|
73
|
+
assert(req.headers["accept"] === "*/*");
|
|
67
74
|
if (req && req.body && req.body.name) {
|
|
68
75
|
res.send("Your name is " + req.body.name);
|
|
69
76
|
}
|
|
@@ -42,15 +42,20 @@ class DirWebRequest {
|
|
|
42
42
|
const filler = new Filler();
|
|
43
43
|
const url = filler.fill(action.url, requestVariables);
|
|
44
44
|
|
|
45
|
-
let headers =
|
|
45
|
+
let headers = {};
|
|
46
46
|
if (action.headersString) {
|
|
47
|
-
let
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
console.error("Error parsing webRequest headersString as JSON:", headersString);
|
|
47
|
+
let headersDict = action.headersString
|
|
48
|
+
for (const [key, value] of Object.entries(headersDict)) {
|
|
49
|
+
if (this.log) {console.log("header:", key, "value:", value)}
|
|
50
|
+
let filled_value = filler.fill(value, requestVariables);
|
|
51
|
+
headers[key] = filled_value;
|
|
53
52
|
}
|
|
53
|
+
// try {
|
|
54
|
+
// headers = JSON.parse(headersString);
|
|
55
|
+
// }
|
|
56
|
+
// catch(err) {
|
|
57
|
+
// console.error("Error parsing webRequest headersString as JSON:", headersString);
|
|
58
|
+
// }
|
|
54
59
|
// for (const [key, value] of Object.entries(action.headers)) {
|
|
55
60
|
// action.headers[key] = filler.fill(value, requestVariables);
|
|
56
61
|
// }
|