@tiledesk/tiledesk-tybot-connector 0.1.48 → 0.1.49
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 +5 -0
- package/TdCache.js +15 -0
- package/index.js +6 -4
- package/models/MockTdCache.js +26 -2
- package/models/TiledeskChatbot.js +57 -19
- package/models/TiledeskChatbotUtil.js +16 -0
- package/package.json +1 -1
- package/test/{anomaly-detection-test.js_ → anomaly-detection-test.js} +6 -6
- package/test/check_steps_test.js +1 -4
- package/test/conversation-actions_bot.js +7 -5
- package/test/conversation-locked-intent-test.js +3 -3
- package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +279 -260
- package/tiledeskChatbotPlugs/Filler.js +4 -0
- package/tiledeskChatbotPlugs/directives/DirAssign.js +11 -5
- package/tiledeskChatbotPlugs/directives/DirCondition.js +16 -7
- package/tiledeskChatbotPlugs/directives/DirDeleteVariable.js +1 -3
- package/tiledeskChatbotPlugs/directives/DirDepartment.js +1 -3
- package/tiledeskChatbotPlugs/directives/DirIfOnlineAgents.js +2 -4
- package/tiledeskChatbotPlugs/directives/DirIntent.js +1 -3
- package/tiledeskChatbotPlugs/directives/DirMessage.js +7 -2
- package/tiledeskChatbotPlugs/directives/DirMoveToAgent.js +8 -9
- package/tiledeskChatbotPlugs/directives/DirReplaceBot.js +1 -3
- package/tiledeskChatbotPlugs/directives/DirReply.js +15 -4
- package/tiledeskChatbotPlugs/directives/DirSendEmail.js +4 -6
- package/tiledeskChatbotPlugs/directives/DirWait.js +4 -0
- package/tiledeskChatbotPlugs/directives/Directives.js +5 -5
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,9 +5,14 @@
|
|
|
5
5
|
available on:
|
|
6
6
|
▶️ https://www.npmjs.com/package/@tiledesk/tiledesk-tybot-connector
|
|
7
7
|
|
|
8
|
+
### 0.1.49
|
|
9
|
+
- anomaly detection
|
|
10
|
+
- bug fix
|
|
11
|
+
|
|
8
12
|
### 0.1.48
|
|
9
13
|
- fixed ReplaceBot "log" bug
|
|
10
14
|
- ReplaceBot fill-variables botName
|
|
15
|
+
- fixing
|
|
11
16
|
|
|
12
17
|
### 0.1.47
|
|
13
18
|
- Introduced _tdIfOnlineAgents --trueIntent -- falseIntent
|
package/TdCache.js
CHANGED
|
@@ -72,6 +72,21 @@ class TdCache {
|
|
|
72
72
|
});
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
+
async incr(key) {
|
|
76
|
+
// console.log("incr key:", key)
|
|
77
|
+
return new Promise( async (resolve, reject) => {
|
|
78
|
+
try {
|
|
79
|
+
// console.log("incr here...key", key)
|
|
80
|
+
await this.client.incr(key);
|
|
81
|
+
}
|
|
82
|
+
catch(error) {
|
|
83
|
+
console.error("Error on incr:", error);
|
|
84
|
+
reject(error)
|
|
85
|
+
}
|
|
86
|
+
return resolve();
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
75
90
|
async hset(dict_key, key, value, options) {
|
|
76
91
|
//console.log("hsetting dict_key key value", dict_key, key, value)
|
|
77
92
|
return new Promise( async (resolve, reject) => {
|
package/index.js
CHANGED
|
@@ -212,10 +212,12 @@ async function updateRequestVariables(chatbot, message, projectId, requestId) {
|
|
|
212
212
|
if (message.request && message.request.location && message.request.location.city) {
|
|
213
213
|
await chatbot.addParameter("_tdCity", message.request.location.city);
|
|
214
214
|
}
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
215
|
+
// console.log("message.request.language", message.request["language"])
|
|
216
|
+
if (message.request) {
|
|
217
|
+
await chatbot.addParameter("_tdUserSourcePage", message.request.sourcePage);
|
|
218
|
+
await chatbot.addParameter("_tdUserLanguage", message.request["language"]);
|
|
219
|
+
await chatbot.addParameter("_tdUserAgent", message.request.userAgent);
|
|
220
|
+
}
|
|
219
221
|
if (message.attributes) {
|
|
220
222
|
await chatbot.addParameter("_tdRequestDepartmentId", message.attributes.departmentId);
|
|
221
223
|
await chatbot.addParameter("_tdRequestDepartmentName", message.attributes.departmentName);
|
package/models/MockTdCache.js
CHANGED
|
@@ -7,14 +7,38 @@ class MockTdCache {
|
|
|
7
7
|
this.db = new Map();
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
set(k, v) {
|
|
10
|
+
async set(k, v) {
|
|
11
11
|
return new Promise( (resolve, reject) => {
|
|
12
12
|
this.db.set(k, "" + v) // saves as string
|
|
13
13
|
resolve();
|
|
14
14
|
});
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
async incr(k) {
|
|
18
|
+
// console.log("incr...", k)
|
|
19
|
+
// return new Promise( (resolve, reject) => {
|
|
20
|
+
// console.log("Promise incr...", k)
|
|
21
|
+
let value = await this.get(k);
|
|
22
|
+
// console.log("value.............", value)
|
|
23
|
+
if (value == undefined || value == null) {
|
|
24
|
+
value = 0;
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
value = Number(value);
|
|
28
|
+
}
|
|
29
|
+
catch(error) {
|
|
30
|
+
// console.error("Error on value = Number(value);", error);
|
|
31
|
+
value = 0
|
|
32
|
+
}
|
|
33
|
+
// console.log("got", k, value)
|
|
34
|
+
let v_incr = Number(value) + 1;
|
|
35
|
+
this.db.set(k, "" + v_incr) // saves as string
|
|
36
|
+
// resolve();
|
|
37
|
+
|
|
38
|
+
// });
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async get(k) {
|
|
18
42
|
return new Promise( (resolve, reject) => {
|
|
19
43
|
const v = this.db.get(k);
|
|
20
44
|
resolve(v);
|
|
@@ -64,8 +64,23 @@ class TiledeskChatbot {
|
|
|
64
64
|
}
|
|
65
65
|
|
|
66
66
|
// any external invocation restarts the steps counter
|
|
67
|
-
|
|
68
|
-
|
|
67
|
+
if (message.sender != "_tdinternal") {
|
|
68
|
+
if (this.log) {
|
|
69
|
+
console.log("Resetting current step by request message:", message.text);
|
|
70
|
+
}
|
|
71
|
+
await TiledeskChatbot.resetStep(this.tdcache, this.requestId);
|
|
72
|
+
if (this.log) {
|
|
73
|
+
if (this.tdcache) {
|
|
74
|
+
let currentStep =
|
|
75
|
+
await TiledeskChatbot.currentStep(this.tdcache, this.requestId);
|
|
76
|
+
if (this.log) {console.log("after reset currentStep:", currentStep)}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
// Emergency stop :)
|
|
81
|
+
// if (message.text === "/anomaly") {
|
|
82
|
+
// console.log(".................stop on /anomaly!");
|
|
83
|
+
// resolve(null);
|
|
69
84
|
// }
|
|
70
85
|
|
|
71
86
|
// Checking locked intent (for non-internal intents)
|
|
@@ -132,6 +147,7 @@ class TiledeskChatbot {
|
|
|
132
147
|
if (this.log) {console.log("Processing explicit intent:", explicit_intent_name)}
|
|
133
148
|
// look for parameters
|
|
134
149
|
const intent = TiledeskChatbotUtil.parseIntent(explicit_intent_name);
|
|
150
|
+
if (this.log) {console.log("parsed intent:", intent);}
|
|
135
151
|
let reply;
|
|
136
152
|
if (!intent || (intent && !intent.name)) {
|
|
137
153
|
if (this.log) {console.log("Invalid intent:", explicit_intent_name);}
|
|
@@ -217,7 +233,6 @@ class TiledeskChatbot {
|
|
|
217
233
|
else {
|
|
218
234
|
// fallback
|
|
219
235
|
let fallbackIntent = await this.botsDataSource.getByIntentDisplayName(this.botId, "defaultFallback");
|
|
220
|
-
console.log("fallbackIntent found", fallbackIntent);
|
|
221
236
|
if (!fallbackIntent) {
|
|
222
237
|
console.log("No defaultFallback found!");
|
|
223
238
|
resolve(null);
|
|
@@ -459,33 +474,56 @@ class TiledeskChatbot {
|
|
|
459
474
|
}
|
|
460
475
|
|
|
461
476
|
static async checkStep(_tdcache, requestId, max_steps) {
|
|
462
|
-
|
|
477
|
+
// console.log("CHECKING ON MAX_STEPS:", max_steps);
|
|
478
|
+
let go_on = true; // continue
|
|
463
479
|
const parameter_key = TiledeskChatbot.requestCacheKey(requestId) + ":step";
|
|
464
|
-
console.log("
|
|
480
|
+
// console.log("__parameter_key:", parameter_key);
|
|
481
|
+
await _tdcache.incr(parameter_key);
|
|
482
|
+
// console.log("incr-ed");
|
|
465
483
|
let _current_step = await _tdcache.get(parameter_key);
|
|
466
|
-
if (!_current_step) { // this shouldn't be happening
|
|
467
|
-
|
|
468
|
-
}
|
|
469
|
-
console.log("_current_step:", _current_step);
|
|
484
|
+
// if (!_current_step) { // this shouldn't be happening
|
|
485
|
+
// _current_step = 0;
|
|
486
|
+
// }
|
|
470
487
|
let current_step = Number(_current_step);
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
488
|
+
// current_step += 1;
|
|
489
|
+
// await _tdcache.set(parameter_key, current_step); // increment step
|
|
490
|
+
// console.log("CURRENT-STEP:", current_step);
|
|
491
|
+
if (current_step > max_steps) { // max_steps limit just violated
|
|
492
|
+
// console.log("CURRENT-STEP > MAX_STEPS!", current_step);
|
|
493
|
+
// await TiledeskChatbot.resetStep(_tdcache, requestId);
|
|
494
|
+
// go_on = 1; // stop execution, send error message
|
|
495
|
+
go_on = false
|
|
476
496
|
}
|
|
497
|
+
// else if (current_step > max_steps + 1) { // max_steps limit already violated
|
|
498
|
+
// console.log("CURRENT-STEP > MAX_STEPS!", current_step);
|
|
499
|
+
// // await TiledeskChatbot.resetStep(_tdcache, requestId);
|
|
500
|
+
// go_on = 2; // stop execution, don't send error message (already sent with go_on = 1)
|
|
501
|
+
// }
|
|
477
502
|
else {
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
await _tdcache.set(parameter_key, current_step); // increment step
|
|
481
|
-
console.log("current_step from cache:", await _tdcache.get(parameter_key));
|
|
503
|
+
// go_on = 0;
|
|
504
|
+
go_on = true;
|
|
482
505
|
}
|
|
506
|
+
// else {
|
|
507
|
+
// console.log("CURRENT-STEP UNDER MAX_STEPS THRESHOLD:)", current_step);
|
|
508
|
+
// current_step += 1;
|
|
509
|
+
// await _tdcache.set(parameter_key, current_step); // increment step
|
|
510
|
+
// console.log("current_step from cache:", await _tdcache.get(parameter_key));
|
|
511
|
+
// }
|
|
483
512
|
return go_on;
|
|
484
513
|
}
|
|
485
514
|
|
|
486
515
|
static async resetStep(_tdcache, requestId) {
|
|
487
516
|
const parameter_key = TiledeskChatbot.requestCacheKey(requestId) + ":step";
|
|
488
|
-
|
|
517
|
+
// console.log("resetStep() parameter_key:", parameter_key);
|
|
518
|
+
if (_tdcache) {
|
|
519
|
+
await _tdcache.set(parameter_key, 0);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
static async currentStep(_tdcache, requestId) {
|
|
524
|
+
const parameter_key = TiledeskChatbot.requestCacheKey(requestId) + ":step";
|
|
525
|
+
// console.log("currentStep() parameter_key:", parameter_key);
|
|
526
|
+
return await _tdcache.get(parameter_key);
|
|
489
527
|
}
|
|
490
528
|
|
|
491
529
|
async allParameters() {
|
|
@@ -28,6 +28,22 @@ class TiledeskChatbotUtil {
|
|
|
28
28
|
return intent;
|
|
29
29
|
|
|
30
30
|
}
|
|
31
|
+
|
|
32
|
+
// static errorMessage(message) {
|
|
33
|
+
// return {
|
|
34
|
+
// name: "message",
|
|
35
|
+
// action: {
|
|
36
|
+
// "_tdThenStop": true,
|
|
37
|
+
// text: message,
|
|
38
|
+
// attributes: {
|
|
39
|
+
// runtimeError: {
|
|
40
|
+
// message: message
|
|
41
|
+
// }
|
|
42
|
+
// }
|
|
43
|
+
// }
|
|
44
|
+
// }
|
|
45
|
+
// }
|
|
46
|
+
|
|
31
47
|
}
|
|
32
48
|
|
|
33
49
|
module.exports = { TiledeskChatbotUtil };
|
package/package.json
CHANGED
|
@@ -47,25 +47,25 @@ describe('Conversation for anomaly detection test', async () => {
|
|
|
47
47
|
|
|
48
48
|
after(function (done) {
|
|
49
49
|
app_listener.close(() => {
|
|
50
|
-
// console.log('ACTIONS app_listener closed.');
|
|
51
50
|
done();
|
|
52
51
|
});
|
|
53
52
|
});
|
|
54
53
|
|
|
55
54
|
it('/anomaly', (done) => {
|
|
56
|
-
console.log("/anomaly story...");
|
|
55
|
+
// console.log("/anomaly story...");
|
|
57
56
|
let message_id = uuidv4();
|
|
58
57
|
let listener;
|
|
59
58
|
let endpointServer = express();
|
|
60
59
|
endpointServer.use(bodyParser.json());
|
|
61
60
|
endpointServer.post('/:projectId/requests/:requestId/messages', function (req, res) {
|
|
62
|
-
console.log("...req.body:", JSON.stringify(req.body));
|
|
61
|
+
// console.log("...req.body:", JSON.stringify(req.body));
|
|
63
62
|
res.send({ success: true });
|
|
64
63
|
const message = req.body;
|
|
65
64
|
assert(message.attributes.error !== null);
|
|
66
|
-
assert(message.attributes.
|
|
67
|
-
|
|
65
|
+
assert(message.attributes.runtimeError.message === "Request error: anomaly detection. MAX ACTIONS exeeded.");
|
|
66
|
+
// console.log("/anomaly test success");
|
|
68
67
|
listener.close(() => {
|
|
68
|
+
// console.log("/anomaly lister test closed");
|
|
69
69
|
done();
|
|
70
70
|
});
|
|
71
71
|
});
|
|
@@ -89,7 +89,7 @@ describe('Conversation for anomaly detection test', async () => {
|
|
|
89
89
|
"token": CHATBOT_TOKEN
|
|
90
90
|
}
|
|
91
91
|
sendMessageToBot(request, BOT_ID, () => {
|
|
92
|
-
console.log("Message sent:\n", request);
|
|
92
|
+
// console.log("Message sent:\n", request);
|
|
93
93
|
});
|
|
94
94
|
});
|
|
95
95
|
|
package/test/check_steps_test.js
CHANGED
|
@@ -12,15 +12,12 @@ describe('checkStep()', function() {
|
|
|
12
12
|
let i;
|
|
13
13
|
// trying to brute-pass MAX_STEPS limit by doubling it
|
|
14
14
|
for (i = 0; i < MAX_STEPS * 2; i++) {
|
|
15
|
-
console.log("i: " + i);
|
|
16
15
|
let go_on = await TiledeskChatbot.checkStep(tdcache, requestId, MAX_STEPS);
|
|
17
|
-
console.log("go on?", go_on);
|
|
18
16
|
if (!go_on) {
|
|
19
17
|
break;
|
|
20
18
|
}
|
|
21
19
|
}
|
|
22
|
-
|
|
23
|
-
assert(i === MAX_STEPS + 1);
|
|
20
|
+
assert(i === MAX_STEPS);
|
|
24
21
|
});
|
|
25
22
|
|
|
26
23
|
});
|
|
@@ -206,11 +206,13 @@ const bot = {
|
|
|
206
206
|
"webhook_enabled": false,
|
|
207
207
|
"enabled": true,
|
|
208
208
|
"question": "***",
|
|
209
|
-
"
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
209
|
+
"actions": [
|
|
210
|
+
|
|
211
|
+
{
|
|
212
|
+
"_tdActionType": "intent",
|
|
213
|
+
"intentName": "anomaly"
|
|
214
|
+
}
|
|
215
|
+
],
|
|
214
216
|
"language": "en",
|
|
215
217
|
"intent_display_name": "anomaly"
|
|
216
218
|
}]
|
|
@@ -65,7 +65,7 @@ describe('Conversation1 - Form filling', async () => {
|
|
|
65
65
|
res.send({ success: true });
|
|
66
66
|
const message = req.body;
|
|
67
67
|
if (message.text.startsWith("Hi welcome to this dialog.")) {
|
|
68
|
-
console.log("got #0 sending #1", message.text);
|
|
68
|
+
// console.log("got #0 sending #1", message.text);
|
|
69
69
|
let request = {
|
|
70
70
|
"payload": {
|
|
71
71
|
"_id": request1_uuid,
|
|
@@ -87,7 +87,7 @@ describe('Conversation1 - Form filling', async () => {
|
|
|
87
87
|
});
|
|
88
88
|
}
|
|
89
89
|
else if (message.text.startsWith("As I told you,")) {
|
|
90
|
-
console.log("got #1 sending #2", message.text);
|
|
90
|
+
// console.log("got #1 sending #2", message.text);
|
|
91
91
|
let request = {
|
|
92
92
|
"payload": {
|
|
93
93
|
"_id": request2_uuid,
|
|
@@ -109,7 +109,7 @@ describe('Conversation1 - Form filling', async () => {
|
|
|
109
109
|
});
|
|
110
110
|
}
|
|
111
111
|
else if (message.text.startsWith("And now tell me,")) {
|
|
112
|
-
console.log("got #2 sending #3", message.text);
|
|
112
|
+
// console.log("got #2 sending #3", message.text);
|
|
113
113
|
let request = {
|
|
114
114
|
"payload": {
|
|
115
115
|
"_id": request3_uuid,
|
|
@@ -83,12 +83,13 @@ class DirectivesChatbotPlug {
|
|
|
83
83
|
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
-
processDirectives(theend) {
|
|
86
|
+
async processDirectives(theend) {
|
|
87
87
|
// console.log("Directives to process:", JSON.stringify(this.directives));
|
|
88
|
+
this.theend = theend;
|
|
88
89
|
const directives = this.directives;
|
|
89
90
|
if (!directives || directives.length === 0) {
|
|
90
91
|
if (this.log) { console.log("No directives to process."); }
|
|
91
|
-
theend();
|
|
92
|
+
this.theend();
|
|
92
93
|
return;
|
|
93
94
|
}
|
|
94
95
|
const supportRequest = this.supportRequest;
|
|
@@ -96,7 +97,7 @@ class DirectivesChatbotPlug {
|
|
|
96
97
|
const API_URL = this.API_URL;
|
|
97
98
|
const TILEBOT_ENDPOINT = this.TILEBOT_ENDPOINT;
|
|
98
99
|
|
|
99
|
-
const requestId = supportRequest.request_id
|
|
100
|
+
// const requestId = supportRequest.request_id
|
|
100
101
|
let depId;
|
|
101
102
|
if (supportRequest.department && supportRequest.department._id) {
|
|
102
103
|
depId = supportRequest.department._id;
|
|
@@ -111,7 +112,7 @@ class DirectivesChatbotPlug {
|
|
|
111
112
|
log: this.log
|
|
112
113
|
});
|
|
113
114
|
|
|
114
|
-
|
|
115
|
+
this.context = {
|
|
115
116
|
projectId: projectId,
|
|
116
117
|
token: token,
|
|
117
118
|
supportRequest: supportRequest,
|
|
@@ -124,273 +125,291 @@ class DirectivesChatbotPlug {
|
|
|
124
125
|
log: this.log
|
|
125
126
|
}
|
|
126
127
|
|
|
127
|
-
|
|
128
|
-
if (this.log) { console.log("processing directives
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
let directive_name = null;
|
|
135
|
-
if (directive && directive.name) {
|
|
136
|
-
directive_name = directive.name.toLowerCase();
|
|
137
|
-
}
|
|
138
|
-
if (directive == null) {
|
|
139
|
-
theend();
|
|
140
|
-
}
|
|
141
|
-
else if (directive_name === Directives.DEPARTMENT) {
|
|
142
|
-
// let dep_name = "default department";
|
|
143
|
-
// if (directive.parameter) {
|
|
144
|
-
// dep_name = directive.parameter;
|
|
145
|
-
// }
|
|
146
|
-
const departmentDir = new DirDepartment(context);
|
|
147
|
-
departmentDir.execute(directive, () => {
|
|
148
|
-
console.log("departmentDir.executed", directive);
|
|
149
|
-
process(nextDirective());
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
else if (directive_name === Directives.HMESSAGE) {
|
|
153
|
-
const messageDir = new DirMessage(
|
|
154
|
-
{
|
|
155
|
-
API_ENDPOINT: API_URL,
|
|
156
|
-
TILEBOT_ENDPOINT:TILEBOT_ENDPOINT,
|
|
157
|
-
log: false,
|
|
158
|
-
projectId: projectId,
|
|
159
|
-
requestId: requestId,
|
|
160
|
-
token: token
|
|
161
|
-
}
|
|
162
|
-
);
|
|
163
|
-
messageDir.execute(directive, async () => {
|
|
164
|
-
process(nextDirective());
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
// if (directive.parameter) {
|
|
168
|
-
// let text = directive.parameter.trim();
|
|
169
|
-
// let message = {
|
|
170
|
-
// sender: "system22", // bot doesn't reply to himself
|
|
171
|
-
// text: text,
|
|
172
|
-
// attributes: {
|
|
173
|
-
// subtype: "info"
|
|
174
|
-
// }
|
|
175
|
-
// };
|
|
176
|
-
// tdclient.sendSupportMessage(requestId, message, () => {
|
|
177
|
-
// process(nextDirective());
|
|
178
|
-
// });
|
|
179
|
-
// }
|
|
128
|
+
this.curr_directive_index = -1;
|
|
129
|
+
if (this.log) { console.log("processing directives...");}
|
|
130
|
+
|
|
131
|
+
const next_dir = await this.nextDirective(directives);
|
|
132
|
+
if (this.log) { console.log("next_dir:", JSON.stringify(next_dir));}
|
|
133
|
+
await this.process(next_dir);
|
|
134
|
+
}
|
|
180
135
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
// tdclient: tdclient,
|
|
219
|
-
// intentDir: intentDir,
|
|
220
|
-
// log: false
|
|
221
|
-
// });
|
|
222
|
-
ifOnlineAgents.execute(directive, () => {
|
|
223
|
-
process(nextDirective());
|
|
224
|
-
});
|
|
225
|
-
}
|
|
226
|
-
else if (directive_name === Directives.FUNCTION_VALUE) {
|
|
227
|
-
// console.log("...DirAssignFromFunction")
|
|
228
|
-
const assign_dir = new DirAssignFromFunction(context);
|
|
229
|
-
assign_dir.execute(directive, () => {
|
|
230
|
-
process(nextDirective());
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
else if (directive_name === Directives.CONDITION) {
|
|
234
|
-
// console.log("...DirCondition");
|
|
235
|
-
const condition_dir = new DirCondition(context);
|
|
236
|
-
condition_dir.execute(directive, () => {
|
|
237
|
-
process(nextDirective());
|
|
238
|
-
});
|
|
239
|
-
}
|
|
240
|
-
else if (directive_name === Directives.ASSIGN) {
|
|
241
|
-
// console.log("...DirAssign", context.log);
|
|
242
|
-
const assign_dir = new DirAssign(context);
|
|
243
|
-
assign_dir.execute(directive, () => {
|
|
244
|
-
process(nextDirective());
|
|
245
|
-
});
|
|
246
|
-
}
|
|
247
|
-
else if (directive_name === Directives.WHEN_OPEN) {
|
|
248
|
-
// DEPRECATED
|
|
249
|
-
const whenOpenDir = new DirWhenOpen(
|
|
250
|
-
{
|
|
251
|
-
tdclient: tdclient, // matches open hours
|
|
252
|
-
log: false
|
|
253
|
-
});
|
|
254
|
-
whenOpenDir.execute(directive, directives, curr_directive_index, () => {
|
|
255
|
-
process(nextDirective());
|
|
256
|
-
});
|
|
257
|
-
}
|
|
258
|
-
else if (directive_name === Directives.WHEN_CLOSED) {
|
|
259
|
-
// DEPRECATED
|
|
260
|
-
const whenOpenDir = new DirWhenOpen(
|
|
261
|
-
{
|
|
262
|
-
tdclient: tdclient,
|
|
263
|
-
checkOpen: false, // matches closed hours
|
|
264
|
-
log: false
|
|
265
|
-
});
|
|
266
|
-
whenOpenDir.execute(directive, directives, curr_directive_index, () => {
|
|
267
|
-
process(nextDirective());
|
|
268
|
-
});
|
|
269
|
-
}
|
|
270
|
-
else if (directive_name === Directives.IF_AGENTS) {
|
|
271
|
-
// DEPRECATED
|
|
272
|
-
const ifNoAgentsDir = new DirIfAvailableAgents(
|
|
273
|
-
{
|
|
274
|
-
tdclient: tdclient,
|
|
275
|
-
checkAgents: true, // check available agents > 0
|
|
276
|
-
log: false
|
|
277
|
-
});
|
|
278
|
-
ifNoAgentsDir.execute(directive, directives, curr_directive_index, () => {
|
|
279
|
-
process(nextDirective());
|
|
280
|
-
});
|
|
281
|
-
}
|
|
282
|
-
else if (directive_name === Directives.IF_NO_AGENTS) {
|
|
283
|
-
// DEPRECATED
|
|
284
|
-
const ifNoAgentsDir = new DirIfAvailableAgents(
|
|
285
|
-
{
|
|
286
|
-
tdclient: tdclient,
|
|
287
|
-
checkAgents: false, // check no available agents
|
|
288
|
-
log: false
|
|
289
|
-
});
|
|
290
|
-
ifNoAgentsDir.execute(directive, directives, curr_directive_index, () => {
|
|
291
|
-
process(nextDirective());
|
|
292
|
-
});
|
|
293
|
-
}
|
|
294
|
-
else if (directive_name === Directives.AGENT) {
|
|
295
|
-
console.log("...DirMoveToAgent");
|
|
296
|
-
const agentDir = new DirMoveToAgent(context);
|
|
297
|
-
agentDir.execute(directive, () => {
|
|
298
|
-
process(nextDirective());
|
|
299
|
-
});
|
|
300
|
-
}
|
|
301
|
-
else if (directive_name === Directives.WHEN_ONLINE_MOVE_TO_AGENT) { // DEPRECATED?
|
|
302
|
-
if (depId) {
|
|
303
|
-
const agentDir = new DirMoveToAgent(
|
|
304
|
-
{
|
|
305
|
-
tdclient: tdclient,
|
|
306
|
-
requestId: requestId,
|
|
307
|
-
depId: depId
|
|
308
|
-
}
|
|
309
|
-
);
|
|
310
|
-
if (!directive.body) {
|
|
311
|
-
directive.action = {}
|
|
312
|
-
directive.action.body = {
|
|
313
|
-
whenOnlineOnly: true
|
|
314
|
-
}
|
|
136
|
+
async nextDirective(directives) {
|
|
137
|
+
// console.log("....nextDirective() checkStep():");
|
|
138
|
+
const go_on = await TiledeskChatbot.checkStep(
|
|
139
|
+
this.context.tdcache, this.context.requestId, TiledeskChatbot.MAX_STEPS
|
|
140
|
+
);
|
|
141
|
+
// const current_step = await TiledeskChatbot.currentStep(this.context.tdcache, this.context.requestId);
|
|
142
|
+
// console.log("........nextDirective() currentStep:", current_step);
|
|
143
|
+
if (go_on == false) {
|
|
144
|
+
// console.log("..nextDirective() Stopped!");
|
|
145
|
+
return this.errorMessage("Request error: anomaly detection. MAX ACTIONS exeeded.");
|
|
146
|
+
}
|
|
147
|
+
// else if (go_on == 2) {
|
|
148
|
+
// return null;
|
|
149
|
+
// }
|
|
150
|
+
else { // continue with the next directive
|
|
151
|
+
// console.log("Go on!");
|
|
152
|
+
}
|
|
153
|
+
this.curr_directive_index += 1;
|
|
154
|
+
if (this.curr_directive_index < directives.length) {
|
|
155
|
+
let nextd = directives[this.curr_directive_index];
|
|
156
|
+
// console.log("nextd:", nextd);
|
|
157
|
+
return nextd;
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
errorMessage(message) {
|
|
165
|
+
return {
|
|
166
|
+
name: "message",
|
|
167
|
+
action: {
|
|
168
|
+
"_tdThenStop": true,
|
|
169
|
+
text: message,
|
|
170
|
+
attributes: {
|
|
171
|
+
runtimeError: {
|
|
172
|
+
message: message
|
|
315
173
|
}
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async process(directive) {
|
|
180
|
+
// console.log("process(directive):", JSON.stringify(directive));
|
|
181
|
+
let context = this.context;
|
|
182
|
+
if (directive) {
|
|
183
|
+
// if (context.log) { console.log("process(directive):", JSON.stringify(directive));}
|
|
184
|
+
}
|
|
185
|
+
let directive_name = null;
|
|
186
|
+
if (directive && directive.name) {
|
|
187
|
+
directive_name = directive.name.toLowerCase();
|
|
188
|
+
}
|
|
189
|
+
if (directive == null || (directive !== null && directive["name"] === undefined)) {
|
|
190
|
+
if (context.log) { console.log("stop process(). directive is (null?):", directive);}
|
|
191
|
+
this.theend();
|
|
192
|
+
}
|
|
193
|
+
else if (directive_name === Directives.DEPARTMENT) {
|
|
194
|
+
new DirDepartment(context).execute(directive, async () => {
|
|
195
|
+
let next_dir = await this.nextDirective(this.directives);
|
|
196
|
+
this.process(next_dir);
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
else if (directive_name === Directives.HMESSAGE) {
|
|
200
|
+
new DirMessage(context).execute(directive, async () => {
|
|
201
|
+
let next_dir = await this.nextDirective(this.directives);
|
|
202
|
+
this.process(next_dir);
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
else if (directive_name === Directives.INTENT) {
|
|
206
|
+
// console.log(".....DirIntent")
|
|
207
|
+
new DirIntent(context).execute(directive, async () => {
|
|
208
|
+
let next_dir = await this.nextDirective(this.directives);
|
|
209
|
+
this.process(next_dir);
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
else if (directive_name === Directives.MESSAGE) {
|
|
213
|
+
new DirMessage(context).execute(directive, async (stop) => {
|
|
214
|
+
if (stop) {
|
|
215
|
+
if (context.log) { console.log("Stopping Actions on:", directive);}
|
|
216
|
+
this.theend();
|
|
319
217
|
}
|
|
320
218
|
else {
|
|
321
|
-
|
|
322
|
-
process(
|
|
219
|
+
let next_dir = await this.nextDirective(this.directives);
|
|
220
|
+
this.process(next_dir);
|
|
323
221
|
}
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
else if (directive_name === Directives.REPLY) {
|
|
225
|
+
console.log("...DirReply");
|
|
226
|
+
new DirReply(context).execute(directive, async () => {
|
|
227
|
+
let next_dir = await this.nextDirective(this.directives);
|
|
228
|
+
this.process(next_dir);
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
else if (directive_name === Directives.IF_OPEN_HOURS) {
|
|
232
|
+
new DirIfOpenHours(context).execute(directive, async () => {
|
|
233
|
+
let next_dir = await this.nextDirective(this.directives);
|
|
234
|
+
this.process(next_dir);
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
else if (directive_name === Directives.IF_ONLINE_AGENTS) {
|
|
238
|
+
new DirIfOnlineAgents(context).execute(directive, async () => {
|
|
239
|
+
let next_dir = await this.nextDirective(this.directives);
|
|
240
|
+
this.process(next_dir);
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
else if (directive_name === Directives.FUNCTION_VALUE) {
|
|
244
|
+
// console.log("...DirAssignFromFunction")
|
|
245
|
+
new DirAssignFromFunction(context).execute(directive, async () => {
|
|
246
|
+
let next_dir = await this.nextDirective(this.directives);
|
|
247
|
+
this.process(next_dir);
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
else if (directive_name === Directives.CONDITION) {
|
|
251
|
+
// console.log("...DirCondition");
|
|
252
|
+
new DirCondition(context).execute(directive, async () => {
|
|
253
|
+
let next_dir = await this.nextDirective(this.directives);
|
|
254
|
+
this.process(next_dir);
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
else if (directive_name === Directives.ASSIGN) {
|
|
258
|
+
// console.log("...DirAssign", context.log);
|
|
259
|
+
new DirAssign(context).execute(directive, async () => {
|
|
260
|
+
let next_dir = await this.nextDirective(this.directives);
|
|
261
|
+
this.process(next_dir);
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
else if (directive_name === Directives.WHEN_OPEN) {
|
|
265
|
+
// DEPRECATED
|
|
266
|
+
const whenOpenDir = new DirWhenOpen(
|
|
267
|
+
{
|
|
268
|
+
tdclient: tdclient, // matches open hours
|
|
269
|
+
log: false
|
|
349
270
|
});
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
271
|
+
whenOpenDir.execute(directive, directives, curr_directive_index, async () => {
|
|
272
|
+
let next_dir = await this.nextDirective(this.directives);
|
|
273
|
+
this.process(next_dir);
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
else if (directive_name === Directives.WHEN_CLOSED) {
|
|
277
|
+
// DEPRECATED
|
|
278
|
+
const whenOpenDir = new DirWhenOpen(
|
|
279
|
+
{
|
|
280
|
+
tdclient: tdclient,
|
|
281
|
+
checkOpen: false, // matches closed hours
|
|
282
|
+
log: false
|
|
354
283
|
});
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
284
|
+
whenOpenDir.execute(directive, directives, curr_directive_index, async () => {
|
|
285
|
+
let next_dir = await this.nextDirective(this.directives);
|
|
286
|
+
this.process(next_dir);
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
else if (directive_name === Directives.IF_AGENTS) {
|
|
290
|
+
// DEPRECATED
|
|
291
|
+
const ifNoAgentsDir = new DirIfAvailableAgents(
|
|
292
|
+
{
|
|
293
|
+
tdclient: tdclient,
|
|
294
|
+
checkAgents: true, // check available agents > 0
|
|
295
|
+
log: false
|
|
359
296
|
});
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
297
|
+
ifNoAgentsDir.execute(directive, directives, curr_directive_index, async () => {
|
|
298
|
+
let next_dir = await this.nextDirective(this.directives);
|
|
299
|
+
this.process(next_dir);
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
else if (directive_name === Directives.IF_NO_AGENTS) {
|
|
303
|
+
// DEPRECATED
|
|
304
|
+
const ifNoAgentsDir = new DirIfAvailableAgents(
|
|
305
|
+
{
|
|
306
|
+
tdclient: tdclient,
|
|
307
|
+
checkAgents: false, // check no available agents
|
|
308
|
+
log: false
|
|
365
309
|
});
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
310
|
+
ifNoAgentsDir.execute(directive, directives, curr_directive_index, async () => {
|
|
311
|
+
let next_dir = await this.nextDirective(this.directives);
|
|
312
|
+
this.process(next_dir);
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
else if (directive_name === Directives.AGENT) {
|
|
316
|
+
console.log("...DirMoveToAgent");
|
|
317
|
+
new DirMoveToAgent(context).execute(directive, async () => {
|
|
318
|
+
let next_dir = await this.nextDirective(this.directives);
|
|
319
|
+
this.process(next_dir);
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
else if (directive_name === Directives.WHEN_ONLINE_MOVE_TO_AGENT) { // DEPRECATED?
|
|
323
|
+
// let depId;
|
|
324
|
+
// if (context.supportRequest && context.supportRequest.department && context.supportRequest.department._id) {
|
|
325
|
+
// depId = context.supportRequest.department._id;
|
|
326
|
+
// console.log("context.supportRequest", JSON.stringify(context.supportRequest));
|
|
327
|
+
// const agentDir = new DirMoveToAgent(
|
|
328
|
+
// {
|
|
329
|
+
// tdclient: context.tdclient,
|
|
330
|
+
// requestId: context.requestId,
|
|
331
|
+
// depId: depId
|
|
332
|
+
// }
|
|
333
|
+
// );
|
|
334
|
+
// if (!directive.action) {
|
|
335
|
+
// directive.action = {}
|
|
336
|
+
// directive.action = {
|
|
337
|
+
// whenOnlineOnly: true
|
|
338
|
+
// }
|
|
339
|
+
// }
|
|
340
|
+
new DirMoveToAgent(context).execute(directive, async () => {
|
|
341
|
+
let next_dir = await this.nextDirective(this.directives);
|
|
342
|
+
this.process(next_dir);
|
|
376
343
|
});
|
|
377
|
-
}
|
|
378
|
-
else {
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
344
|
+
// }
|
|
345
|
+
// else {
|
|
346
|
+
// console.log("Warning. DepId null while calling 'WHEN_ONLINE_MOVE_TO_AGENT' directive")
|
|
347
|
+
// let next_dir = await this.nextDirective(this.directives);
|
|
348
|
+
// this.process(next_dir);
|
|
349
|
+
// }
|
|
382
350
|
}
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
351
|
+
else if (directive_name === Directives.CLOSE) {
|
|
352
|
+
// console.log("Exec close()")
|
|
353
|
+
new DirClose(context).execute(directive, async () => {
|
|
354
|
+
let next_dir = await this.nextDirective(this.directives);
|
|
355
|
+
this.process(next_dir);
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
else if (directive_name === Directives.REMOVE_CURRENT_BOT) {
|
|
359
|
+
new DirRemoveCurrentBot(context).execute(directive, async () => {
|
|
360
|
+
let next_dir = await this.nextDirective(this.directives);
|
|
361
|
+
this.process(next_dir);
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
else if (directive_name === Directives.REPLACE_BOT) {
|
|
365
|
+
new DirReplaceBot(context).execute(directive, async () => {
|
|
366
|
+
let next_dir = await this.nextDirective(this.directives);
|
|
367
|
+
this.process(next_dir);
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
else if (directive_name === Directives.WAIT) {
|
|
371
|
+
console.log("........ DirWait");
|
|
372
|
+
new DirWait(context).execute(directive, async () => {
|
|
373
|
+
let next_dir = await this.nextDirective(this.directives);
|
|
374
|
+
this.process(next_dir);
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
else if (directive_name === Directives.LOCK_INTENT) {
|
|
378
|
+
new DirLockIntent(context).execute(directive, async () => {
|
|
379
|
+
let next_dir = await this.nextDirective(this.directives);
|
|
380
|
+
this.process(next_dir);
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
else if (directive_name === Directives.UNLOCK_INTENT) {
|
|
384
|
+
new DirUnlockIntent(context).execute(directive, async () => {
|
|
385
|
+
let next_dir = await this.nextDirective(this.directives);
|
|
386
|
+
this.process(next_dir);
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
else if (directive_name === Directives.FIRE_TILEDESK_EVENT) {
|
|
390
|
+
new DirFireTiledeskEvent(context).execute(directive, async () => {
|
|
391
|
+
let next_dir = await this.nextDirective(this.directives);
|
|
392
|
+
this.process(next_dir);
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
else if (directive_name === Directives.SEND_EMAIL) {
|
|
396
|
+
// console.log("...DirSendEmail");
|
|
397
|
+
new DirSendEmail(context).execute(directive, async () => {
|
|
398
|
+
let next_dir = await this.nextDirective(this.directives);
|
|
399
|
+
this.process(next_dir);
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
else if (directive_name === Directives.DELETE) {
|
|
403
|
+
// console.log("got delete dir...")
|
|
404
|
+
new DirDeleteVariable(context).execute(directive, async () => {
|
|
405
|
+
let next_dir = await this.nextDirective(this.directives);
|
|
406
|
+
this.process(next_dir);
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
else {
|
|
410
|
+
//console.log("Unhandled Post-message Directive:", directive_name);
|
|
411
|
+
let next_dir = await this.nextDirective(this.directives);
|
|
412
|
+
this.process(next_dir);
|
|
394
413
|
}
|
|
395
414
|
}
|
|
396
415
|
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
class Filler {
|
|
2
2
|
|
|
3
3
|
fill(text, parameters) {
|
|
4
|
+
if (text == null || text == undefined) {
|
|
5
|
+
console.log("Skip filling. 'text' is null");
|
|
6
|
+
return text;
|
|
7
|
+
}
|
|
4
8
|
if (parameters) {
|
|
5
9
|
for (const [key, value] of Object.entries(parameters)) {
|
|
6
10
|
text = text.replace(new RegExp("(\\$\\{" + key + "\\})", 'i'), value); //parameters[key]);
|
|
@@ -35,10 +35,8 @@ class DirAssign {
|
|
|
35
35
|
params = this.parseParams(directive.parameter);
|
|
36
36
|
// console.log("DirAssign params:", params);
|
|
37
37
|
action = {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
assignTo: params.assignTo
|
|
41
|
-
// }
|
|
38
|
+
expression: params.expression,
|
|
39
|
+
assignTo: params.assignTo
|
|
42
40
|
}
|
|
43
41
|
// console.log("DirAssign action:", action);
|
|
44
42
|
}
|
|
@@ -80,7 +78,15 @@ class DirAssign {
|
|
|
80
78
|
const value = await new TiledeskExpression().evaluateExpression(expression, variables);
|
|
81
79
|
if (this.log) {console.log("(DirAssign) executed expression:", expression, "value:", value);}
|
|
82
80
|
await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, variableName, value);
|
|
83
|
-
if (this.log) {
|
|
81
|
+
if (this.log) {
|
|
82
|
+
console.log("(DirAssign) Assigned:", variableName, "=", value);
|
|
83
|
+
const all_parameters = await TiledeskChatbot.allParametersStatic(this.context.tdcache, this.context.requestId);
|
|
84
|
+
for (const [key, value] of Object.entries(all_parameters)) {
|
|
85
|
+
const value_type = typeof value;
|
|
86
|
+
if (this.log) {console.log("(DirAssign) request parameter:", key, "value:", value, "type:", value_type)}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
84
90
|
callback();
|
|
85
91
|
}
|
|
86
92
|
else {
|
|
@@ -53,11 +53,9 @@ class DirCondition {
|
|
|
53
53
|
return;
|
|
54
54
|
}
|
|
55
55
|
action = {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
falseIntent: params.falseIntent
|
|
60
|
-
// }
|
|
56
|
+
condition: params.condition,
|
|
57
|
+
trueIntent: params.trueIntent,
|
|
58
|
+
falseIntent: params.falseIntent
|
|
61
59
|
}
|
|
62
60
|
}
|
|
63
61
|
else {
|
|
@@ -75,14 +73,25 @@ class DirCondition {
|
|
|
75
73
|
// const trueIntent = action.body.trueIntent;
|
|
76
74
|
// const falseIntent = action.body.falseIntent;
|
|
77
75
|
const condition = action.condition;
|
|
78
|
-
|
|
79
|
-
|
|
76
|
+
let trueIntent = action.trueIntent;
|
|
77
|
+
let falseIntent = action.falseIntent;
|
|
78
|
+
if (trueIntent && trueIntent.trim() === "") {
|
|
79
|
+
trueIntent = null;
|
|
80
|
+
}
|
|
81
|
+
if (falseIntent && falseIntent.trim() === "") {
|
|
82
|
+
falseIntent = null;
|
|
83
|
+
}
|
|
80
84
|
if (this.log) {console.log("condition action:", action);}
|
|
81
85
|
if (!trueIntent && !falseIntent) {
|
|
82
86
|
if (this.log) {console.log("Invalid condition, no intents specified");}
|
|
83
87
|
callback();
|
|
84
88
|
return;
|
|
85
89
|
}
|
|
90
|
+
if (condition === null || (condition !== null && condition.trim === "")) {
|
|
91
|
+
if (this.log) {console.log("Invalid condition, empty or null:", condition);}
|
|
92
|
+
callback();
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
86
95
|
let trueIntentDirective = null;
|
|
87
96
|
if (trueIntent) {
|
|
88
97
|
trueIntentDirective = DirIntent.intentDirectiveFor(trueIntent);
|
|
@@ -75,8 +75,13 @@ class DirMessage {
|
|
|
75
75
|
callback();
|
|
76
76
|
return;
|
|
77
77
|
}
|
|
78
|
-
this.go(action, () => {
|
|
79
|
-
|
|
78
|
+
this.go(action, async () => {
|
|
79
|
+
if (action["_tdThenStop"] == true) {
|
|
80
|
+
callback(true); // stopping the action flow
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
callback();
|
|
84
|
+
}
|
|
80
85
|
});
|
|
81
86
|
}
|
|
82
87
|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const { TiledeskClient } = require('@tiledesk/tiledesk-client');
|
|
2
|
+
const { Directives } = require('./Directives');
|
|
2
3
|
|
|
3
4
|
class DirMoveToAgent {
|
|
4
5
|
|
|
@@ -33,18 +34,16 @@ class DirMoveToAgent {
|
|
|
33
34
|
}
|
|
34
35
|
|
|
35
36
|
execute(directive, callback) {
|
|
36
|
-
|
|
37
|
-
if (directive.
|
|
38
|
-
action =
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
});
|
|
37
|
+
directive.action = {};
|
|
38
|
+
if (directive.name === Directives.WHEN_ONLINE_MOVE_TO_AGENT) { // TEMP
|
|
39
|
+
directive.action = {
|
|
40
|
+
whenOnlineOnly: true
|
|
41
|
+
}
|
|
42
42
|
}
|
|
43
|
-
|
|
43
|
+
this.go(directive.action, () => {
|
|
44
44
|
callback();
|
|
45
|
-
}
|
|
45
|
+
});
|
|
46
46
|
}
|
|
47
|
-
|
|
48
47
|
go(action, callback) {
|
|
49
48
|
if (action.whenOnlineOnly === true) {
|
|
50
49
|
this.tdclient.openNow((err, result) => {
|
|
@@ -12,10 +12,11 @@ class DirReply {
|
|
|
12
12
|
this.requestId = context.requestId;
|
|
13
13
|
this.token = context.token;
|
|
14
14
|
this.tdcache = context.tdcache;
|
|
15
|
+
this.log = context.log;
|
|
15
16
|
}
|
|
16
17
|
|
|
17
18
|
execute(directive, callback) {
|
|
18
|
-
console.log("Reply directive:", directive);
|
|
19
|
+
console.log("Reply directive:", JSON.stringify(directive));
|
|
19
20
|
let action;
|
|
20
21
|
if (directive.action) {
|
|
21
22
|
action = directive.action;
|
|
@@ -44,16 +45,26 @@ class DirReply {
|
|
|
44
45
|
await TiledeskChatbot.allParametersStatic(
|
|
45
46
|
this.tdcache, this.requestId
|
|
46
47
|
);
|
|
48
|
+
if (this.log) {
|
|
49
|
+
for (const [key, value] of Object.entries(requestVariables)) {
|
|
50
|
+
const value_type = typeof value;
|
|
51
|
+
if (this.log) {console.log("(DirReply) request parameter:", key, "value:", value, "type:", value_type)}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
47
54
|
const filler = new Filler();
|
|
48
55
|
// fill text attribute
|
|
49
56
|
message.text = filler.fill(message.text, requestVariables);
|
|
50
|
-
|
|
57
|
+
if (this.log) {console.log("filling commands'. Message:", JSON.stringify(message));}
|
|
51
58
|
if (message.attributes && message.attributes.commands) {
|
|
59
|
+
if (this.log) {console.log("filling commands'. commands found.");}
|
|
52
60
|
let commands = message.attributes.commands;
|
|
53
|
-
if (
|
|
61
|
+
if (this.log) {console.log("commands:", JSON.stringify(commands), commands.length);}
|
|
62
|
+
if (commands.length > 0) {
|
|
63
|
+
if (this.log) {console.log("commands' found");}
|
|
54
64
|
for (let i = 0; i < commands.length; i++) {
|
|
55
65
|
if (commands[i].type === 'message' && commands[i].message && commands[i].message.text) {
|
|
56
|
-
commands[i].message.text =
|
|
66
|
+
commands[i].message.text = filler.fill(commands[i].message.text, requestVariables);
|
|
67
|
+
if (this.log) {console.log("command filled:", commands[i].message.text);}
|
|
57
68
|
}
|
|
58
69
|
}
|
|
59
70
|
}
|
|
@@ -3,7 +3,7 @@ const { param } = require('express/lib/request');
|
|
|
3
3
|
const ms = require('minimist-string');
|
|
4
4
|
const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
|
|
5
5
|
const { Filler } = require('../Filler');
|
|
6
|
-
const { TiledeskClient } = require('@tiledesk/tiledesk-client');
|
|
6
|
+
// const { TiledeskClient } = require('@tiledesk/tiledesk-client');
|
|
7
7
|
|
|
8
8
|
class DirSendEmail {
|
|
9
9
|
|
|
@@ -46,11 +46,9 @@ class DirSendEmail {
|
|
|
46
46
|
else if (directive.parameter && directive.parameter.trim() !== "") {
|
|
47
47
|
const params = this.parseParams(directive.parameter);
|
|
48
48
|
action = {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
to: params.to
|
|
53
|
-
// }
|
|
49
|
+
subject: params.subject,
|
|
50
|
+
text: params.text,
|
|
51
|
+
to: params.to
|
|
54
52
|
}
|
|
55
53
|
}
|
|
56
54
|
else {
|
|
@@ -31,13 +31,17 @@ class DirWait {
|
|
|
31
31
|
millis: 500
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
|
+
console.log("____-----_", action)
|
|
34
35
|
this.go(action, () => {
|
|
36
|
+
console.log("YES", callback)
|
|
35
37
|
callback();
|
|
36
38
|
})
|
|
37
39
|
}
|
|
38
40
|
|
|
39
41
|
go(action, callback) {
|
|
42
|
+
console.log(">>>>__", callback)
|
|
40
43
|
setTimeout(() => {
|
|
44
|
+
console.log("QUINO....__")
|
|
41
45
|
callback();
|
|
42
46
|
}, action.millis);
|
|
43
47
|
}
|
|
@@ -23,11 +23,11 @@ class Directives {
|
|
|
23
23
|
static LOCK_INTENT = "lockintent";
|
|
24
24
|
static UNLOCK_INTENT = "unlockintent";
|
|
25
25
|
static FIRE_TILEDESK_EVENT = "firetiledeskevent";
|
|
26
|
-
static SEND_EMAIL = "
|
|
26
|
+
static SEND_EMAIL = "email";
|
|
27
27
|
static DELETE = "delete";
|
|
28
28
|
static IF_OPEN_HOURS = "ifopenhours";
|
|
29
29
|
static IF_ONLINE_AGENTS = "ifonlineagents";
|
|
30
|
-
static IF_NOT_OPEN_HOURS = "ifnotopenhours";
|
|
30
|
+
static IF_NOT_OPEN_HOURS = "ifnotopenhours"; // DEPRECATED
|
|
31
31
|
static FUNCTION_VALUE = "functionvalue";
|
|
32
32
|
static CONDITION = "condition";
|
|
33
33
|
static ASSIGN = "assign";
|
|
@@ -36,13 +36,13 @@ class Directives {
|
|
|
36
36
|
static REPLY = 'reply';
|
|
37
37
|
|
|
38
38
|
static actionToDirective(action) {
|
|
39
|
-
console.log("actionToDirective:", action);
|
|
39
|
+
// console.log("actionToDirective:", action);
|
|
40
40
|
let directive = {
|
|
41
|
-
name: action["_tdActionType"]
|
|
41
|
+
name: action["_tdActionType"],
|
|
42
42
|
action: action
|
|
43
43
|
}
|
|
44
44
|
// delete directive.action["_tdActionType"];
|
|
45
|
-
console.log("Directive out:", directive);
|
|
45
|
+
// console.log("Directive out:", directive);
|
|
46
46
|
return directive;
|
|
47
47
|
}
|
|
48
48
|
}
|