@tiledesk/tiledesk-tybot-connector 0.1.23 → 0.1.26
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 +9 -0
- package/ExtApi.js +29 -29
- package/index.js +39 -8
- package/models/TiledeskChatbot.js +1 -0
- package/models/faq_kb.js +4 -0
- package/package.json +2 -2
- package/test/close_directive_test.js +1 -4
- package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +31 -6
- package/tiledeskChatbotPlugs/FillParamsChatbotPlug.js +44 -52
- package/tiledeskChatbotPlugs/MarkbotChatbotPlug.js +30 -7
- package/tiledeskChatbotPlugs/directives/DirIfAvailableAgents.js +108 -0
- package/tiledeskChatbotPlugs/directives/DirIntent.js +26 -17
- package/tiledeskChatbotPlugs/directives/Directives.js +2 -0
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,15 @@
|
|
|
5
5
|
available on:
|
|
6
6
|
▶️ https://www.npmjs.com/package/@tiledesk/tiledesk-tybot-connector
|
|
7
7
|
|
|
8
|
+
### 0.1.26 - online
|
|
9
|
+
- no message is sent when there is no message for markbot out of the pipeline. Only directives are admitted.
|
|
10
|
+
|
|
11
|
+
### 0.1.25
|
|
12
|
+
- introduced new \_tdIfNoAgents \_tdIfAgents (DirIfAvailableAgents)
|
|
13
|
+
|
|
14
|
+
### 0.1.24
|
|
15
|
+
- @tiledesk/tiledesk-client => 0.8.39
|
|
16
|
+
|
|
8
17
|
### 0.1.23
|
|
9
18
|
- introduced new DirClose \_tdClose
|
|
10
19
|
- introduced new DirWhenOpen \_tdWhenOpen \_tdWhenClosed
|
package/ExtApi.js
CHANGED
|
@@ -74,35 +74,35 @@ class ExtApi {
|
|
|
74
74
|
* @param {string} botId. Tiledesk botId
|
|
75
75
|
* @param {string} token. User token
|
|
76
76
|
*/
|
|
77
|
-
sendMessageToBot(message, botId, token, callback) {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
}
|
|
77
|
+
// sendMessageToBot(message, botId, token, callback) {
|
|
78
|
+
// const jwt_token = this.fixToken(token);
|
|
79
|
+
// const url = `${this.ENDPOINT}/ext/${botId}`;
|
|
80
|
+
// if (this.log) {console.log("sendMessageToBot URL", url);}
|
|
81
|
+
// const HTTPREQUEST = {
|
|
82
|
+
// url: url,
|
|
83
|
+
// headers: {
|
|
84
|
+
// 'Content-Type' : 'application/json',
|
|
85
|
+
// 'Authorization': jwt_token
|
|
86
|
+
// },
|
|
87
|
+
// json: message,
|
|
88
|
+
// method: 'POST'
|
|
89
|
+
// };
|
|
90
|
+
// this.myrequest(
|
|
91
|
+
// HTTPREQUEST,
|
|
92
|
+
// function(err, resbody) {
|
|
93
|
+
// if (err) {
|
|
94
|
+
// if (callback) {
|
|
95
|
+
// callback(err);
|
|
96
|
+
// }
|
|
97
|
+
// }
|
|
98
|
+
// else {
|
|
99
|
+
// if (callback) {
|
|
100
|
+
// callback(null, resbody);
|
|
101
|
+
// }
|
|
102
|
+
// }
|
|
103
|
+
// }, this.log
|
|
104
|
+
// );
|
|
105
|
+
// }
|
|
106
106
|
|
|
107
107
|
myrequest(options, callback, log) {
|
|
108
108
|
if (this.log) {
|
package/index.js
CHANGED
|
@@ -46,7 +46,7 @@ let connection;
|
|
|
46
46
|
let APIURL = null;
|
|
47
47
|
|
|
48
48
|
router.post('/ext/:botid', async (req, res) => {
|
|
49
|
-
console.log("REQUEST BODY:", JSON.stringify(req.body));
|
|
49
|
+
if (log) {console.log("REQUEST BODY:", JSON.stringify(req.body));}
|
|
50
50
|
res.status(200).send({"success":true});
|
|
51
51
|
|
|
52
52
|
const botId = req.params.botid;
|
|
@@ -107,13 +107,11 @@ router.post('/ext/:botid', async (req, res) => {
|
|
|
107
107
|
tdcache: tdcache,
|
|
108
108
|
requestId: requestId,
|
|
109
109
|
projectId: projectId,
|
|
110
|
-
log:
|
|
110
|
+
log: log
|
|
111
111
|
});
|
|
112
112
|
|
|
113
113
|
const parameters_key = "tilebot:requests:" + requestId + ":parameters";
|
|
114
114
|
await chatbot.addParameter(requestId, "tdMessageId", messageId);
|
|
115
|
-
//all_params = await chatbot.allParameters(requestId);
|
|
116
|
-
//console.log("Allparams", all_params);
|
|
117
115
|
let reply = await chatbot.replyToMessage(message);
|
|
118
116
|
if (!reply) {
|
|
119
117
|
reply = {
|
|
@@ -172,11 +170,44 @@ router.post('/ext/:projectId/requests/:requestId/messages', async (req, res) =>
|
|
|
172
170
|
// PIPELINE-EXT
|
|
173
171
|
const bot_answer = await ExtUtil.execPipelineExt(request, answer, directivesPlug, tdcache, log);
|
|
174
172
|
//const bot_answer = answer;
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
173
|
+
console.log("bot_answer to send:", bot_answer);
|
|
174
|
+
// empty answer
|
|
175
|
+
// let b = {
|
|
176
|
+
// text: '',
|
|
177
|
+
// attributes: {
|
|
178
|
+
// clienttimestamp: 1670571497092,
|
|
179
|
+
// _answerid: '638c7b0c1db44900351104b1',
|
|
180
|
+
// intent_info: {
|
|
181
|
+
// intent_name: 'wantagent',
|
|
182
|
+
// is_fallback: false,
|
|
183
|
+
// question_payload: [Object],
|
|
184
|
+
// botId: '638c78d71db44900351101c2',
|
|
185
|
+
// bot: [Object]
|
|
186
|
+
// },
|
|
187
|
+
// directives: true,
|
|
188
|
+
// splits: true,
|
|
189
|
+
// markbot: true,
|
|
190
|
+
// fillParams: true,
|
|
191
|
+
// webhook: false
|
|
192
|
+
// },
|
|
193
|
+
// triggeredByMessageId: '6392e5e8408e0000437aa383'
|
|
194
|
+
// }
|
|
195
|
+
if (bot_answer) {
|
|
196
|
+
tdclient.sendSupportMessage(requestId, bot_answer, (err, response) => {
|
|
197
|
+
if (err) {
|
|
198
|
+
console.error("Error sending message", err);
|
|
199
|
+
}
|
|
200
|
+
directivesPlug.processDirectives( () => {
|
|
201
|
+
if (log) {console.log("After message - Directives executed.");}
|
|
202
|
+
});
|
|
178
203
|
});
|
|
179
|
-
}
|
|
204
|
+
}
|
|
205
|
+
else {
|
|
206
|
+
directivesPlug.processDirectives( () => {
|
|
207
|
+
if (log) {console.log("Directives executed.");}
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
|
|
180
211
|
});
|
|
181
212
|
|
|
182
213
|
router.get('/message/context/:messageid', async (req, res) => {
|
package/models/faq_kb.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tiledesk/tiledesk-tybot-connector",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.26",
|
|
4
4
|
"description": "Tiledesk Tybot connector",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"@tiledesk/helpcenter-query-client": "^0.1.8",
|
|
15
15
|
"@tiledesk/tiledesk-chatbot-client": "^0.5.30",
|
|
16
16
|
"@tiledesk/tiledesk-chatbot-util": "^0.8.38",
|
|
17
|
-
"@tiledesk/tiledesk-client": "^0.8.
|
|
17
|
+
"@tiledesk/tiledesk-client": "^0.8.39",
|
|
18
18
|
"axios": "^0.27.2",
|
|
19
19
|
"body-parser": "^1.19.0",
|
|
20
20
|
"cors": "^2.8.5",
|
|
@@ -34,10 +34,7 @@ describe('Directive DirClose', function() {
|
|
|
34
34
|
}
|
|
35
35
|
let directivesPlug = new DirectivesChatbotPlug({supportRequest: supportRequest, TILEDESK_API_ENDPOINT: "APIURL", token: "token", log: false, HELP_CENTER_API_ENDPOINT: "HELP_CENTER_API_ENDPOINT"});
|
|
36
36
|
const bot_answer = await ExtUtil.execPipelineExt(supportRequest, answer, directivesPlug, null, false);
|
|
37
|
-
|
|
38
|
-
// console.log("directivesPlug.directives:", directivesPlug.directives);
|
|
39
|
-
assert.strictEqual(bot_answer.text, "");
|
|
40
|
-
assert(bot_answer.attributes.commands == null);
|
|
37
|
+
assert(bot_answer == null);
|
|
41
38
|
assert(directivesPlug.directives != null);
|
|
42
39
|
assert(directivesPlug.directives.length == 1);
|
|
43
40
|
assert(directivesPlug.directives[0].name === "close");
|
|
@@ -13,6 +13,7 @@ const { DirIntent } = require('./directives/DirIntent');
|
|
|
13
13
|
const { DirWhenOpen } = require('./directives/DirWhenOpen');
|
|
14
14
|
const { DirDisableInputText } = require('./directives/DirDisableInputText');
|
|
15
15
|
const { DirClose } = require('./directives/DirClose');
|
|
16
|
+
const { DirIfAvailableAgents } = require('./directives/DirIfAvailableAgents');
|
|
16
17
|
const { Directives } = require('./directives/Directives');
|
|
17
18
|
// const { ExtApi } = require('../ExtApi.js');
|
|
18
19
|
|
|
@@ -68,8 +69,8 @@ class DirectivesChatbotPlug {
|
|
|
68
69
|
|
|
69
70
|
}
|
|
70
71
|
|
|
71
|
-
processDirectives(
|
|
72
|
-
if (this.log) { console.log("Directives on message:", message); }
|
|
72
|
+
processDirectives(theend) {
|
|
73
|
+
// if (this.log) { console.log("Directives on message:", JSON.stringify(message)); }
|
|
73
74
|
const directives = this.directives;
|
|
74
75
|
if (!directives || directives.length === 0) {
|
|
75
76
|
if (this.log) { console.log("No directives to process."); }
|
|
@@ -143,7 +144,7 @@ class DirectivesChatbotPlug {
|
|
|
143
144
|
log: false
|
|
144
145
|
}
|
|
145
146
|
);
|
|
146
|
-
intentDir.execute(directive,
|
|
147
|
+
intentDir.execute(directive, projectId, requestId, token, () => {
|
|
147
148
|
process(nextDirective());
|
|
148
149
|
});
|
|
149
150
|
}
|
|
@@ -152,7 +153,7 @@ class DirectivesChatbotPlug {
|
|
|
152
153
|
{
|
|
153
154
|
API_ENDPOINT: API_URL,
|
|
154
155
|
TILEBOT_ENDPOINT:TILEBOT_ENDPOINT,
|
|
155
|
-
log:
|
|
156
|
+
log: false
|
|
156
157
|
}
|
|
157
158
|
);
|
|
158
159
|
messageDir.execute(directive, projectId, requestId, token, () => {
|
|
@@ -162,7 +163,8 @@ class DirectivesChatbotPlug {
|
|
|
162
163
|
else if (directive_name === Directives.WHEN_OPEN) {
|
|
163
164
|
const whenOpenDir = new DirWhenOpen(
|
|
164
165
|
{
|
|
165
|
-
tdclient: tdclient // matches open hours
|
|
166
|
+
tdclient: tdclient, // matches open hours
|
|
167
|
+
log: true
|
|
166
168
|
});
|
|
167
169
|
whenOpenDir.execute(directive, directives, curr_directive_index, () => {
|
|
168
170
|
process(nextDirective());
|
|
@@ -172,12 +174,35 @@ class DirectivesChatbotPlug {
|
|
|
172
174
|
const whenOpenDir = new DirWhenOpen(
|
|
173
175
|
{
|
|
174
176
|
tdclient: tdclient,
|
|
175
|
-
checkOpen: false // matches closed hours
|
|
177
|
+
checkOpen: false, // matches closed hours
|
|
178
|
+
log: true
|
|
176
179
|
});
|
|
177
180
|
whenOpenDir.execute(directive, directives, curr_directive_index, () => {
|
|
178
181
|
process(nextDirective());
|
|
179
182
|
});
|
|
180
183
|
}
|
|
184
|
+
else if (directive_name === Directives.IF_AGENTS) {
|
|
185
|
+
const ifNoAgentsDir = new DirIfAvailableAgents(
|
|
186
|
+
{
|
|
187
|
+
tdclient: tdclient,
|
|
188
|
+
checkAgents: true, // check available agents > 0
|
|
189
|
+
log: false
|
|
190
|
+
});
|
|
191
|
+
ifNoAgentsDir.execute(directive, directives, curr_directive_index, () => {
|
|
192
|
+
process(nextDirective());
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
else if (directive_name === Directives.IF_NO_AGENTS) {
|
|
196
|
+
const ifNoAgentsDir = new DirIfAvailableAgents(
|
|
197
|
+
{
|
|
198
|
+
tdclient: tdclient,
|
|
199
|
+
checkAgents: false, // check no available agents
|
|
200
|
+
log: false
|
|
201
|
+
});
|
|
202
|
+
ifNoAgentsDir.execute(directive, directives, curr_directive_index, () => {
|
|
203
|
+
process(nextDirective());
|
|
204
|
+
});
|
|
205
|
+
}
|
|
181
206
|
else if (directive_name === Directives.AGENT) {
|
|
182
207
|
const agentDir = new DirMoveToAgent(tdclient);
|
|
183
208
|
directive.whenOnlineOnly = false;
|
|
@@ -16,64 +16,56 @@ class FillParamsChatbotPlug {
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
async exec(pipeline) {
|
|
19
|
-
if (this.log) {console.log("fillParams...")}
|
|
20
19
|
let message = pipeline.message;
|
|
21
|
-
if (
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
let commands = message.attributes.commands;
|
|
62
|
-
if (this.log) {console.log("commands for fillMessage:", commands);}
|
|
63
|
-
if (commands.length > 1) {
|
|
64
|
-
for (let i = 0; i < commands.length; i++) {
|
|
65
|
-
if (commands[i].type === 'message' && commands[i].message && commands[i].message.text) {
|
|
66
|
-
let filled_reply = this.fillWithRequestParams(commands[i].message.text, all_parameters);
|
|
67
|
-
commands[i].message.text = filled_reply;
|
|
20
|
+
if (message) {
|
|
21
|
+
if (message.attributes && (message.attributes.fillParams == undefined || message.attributes.fillParams == false)) { // defaults to disabled
|
|
22
|
+
if (this.log) {console.log("fillParams disabled.");}
|
|
23
|
+
pipeline.nextplug();
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
if (this.log) {
|
|
27
|
+
console.log("fillParams: true");
|
|
28
|
+
}
|
|
29
|
+
const requestId = this.request.request_id;
|
|
30
|
+
if (this.log) {console.log("all_parameters of requestId:", requestId)}
|
|
31
|
+
const all_parameters = await TiledeskChatbot.allParametersStatic(this.tdcache, requestId);
|
|
32
|
+
if (this.log) {console.log("--got parameters", all_parameters);}
|
|
33
|
+
if (!all_parameters) {
|
|
34
|
+
pipeline.nextplug();
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const filled_message_text = this.fillWithRequestParams(message.text, all_parameters);
|
|
38
|
+
message.text = filled_message_text;
|
|
39
|
+
//console.log("message filled_message_text:", message)
|
|
40
|
+
if (!message.attributes) {
|
|
41
|
+
message.attributes = {}
|
|
42
|
+
}
|
|
43
|
+
// Reserved names: userEmail, userFullname (and firstMessage)
|
|
44
|
+
if (all_parameters['userEmail']) {
|
|
45
|
+
message.attributes.updateUserEmail = all_parameters['userEmail'];
|
|
46
|
+
}
|
|
47
|
+
if (all_parameters['userFullname']) {
|
|
48
|
+
message.attributes.updateUserFullname = all_parameters['userFullname'];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (message.attributes && message.attributes.commands) {
|
|
52
|
+
let commands = message.attributes.commands;
|
|
53
|
+
// if (this.log) {console.log("commands for fillMessage:", JSON.stringify(commands));}
|
|
54
|
+
if (commands.length > 1) {
|
|
55
|
+
for (let i = 0; i < commands.length; i++) {
|
|
56
|
+
if (commands[i].type === 'message' && commands[i].message && commands[i].message.text) {
|
|
57
|
+
let filled_reply = this.fillWithRequestParams(commands[i].message.text, all_parameters);
|
|
58
|
+
commands[i].message.text = filled_reply;
|
|
59
|
+
}
|
|
68
60
|
}
|
|
69
61
|
}
|
|
70
62
|
}
|
|
63
|
+
pipeline.nextplug();
|
|
71
64
|
}
|
|
72
|
-
|
|
73
|
-
console.log("
|
|
65
|
+
else {
|
|
66
|
+
if (this.log) {console.log("Fillparams. No message.");}
|
|
67
|
+
pipeline.nextplug();
|
|
74
68
|
}
|
|
75
|
-
pipeline.nextplug();
|
|
76
|
-
//});
|
|
77
69
|
|
|
78
70
|
}
|
|
79
71
|
|
|
@@ -15,20 +15,35 @@ class MarkbotChatbotPlug {
|
|
|
15
15
|
|
|
16
16
|
exec(pipeline) {
|
|
17
17
|
let message = pipeline.message;
|
|
18
|
-
|
|
18
|
+
console.log("markbot on message", message)
|
|
19
19
|
if (message.attributes && (message.attributes.markbot == undefined || message.attributes.markbot == false)) { // defaults to disabled
|
|
20
20
|
if (this.log) {console.log("markbot disabled")}
|
|
21
21
|
pipeline.nextplug();
|
|
22
22
|
return;
|
|
23
23
|
}
|
|
24
|
-
|
|
24
|
+
|
|
25
|
+
let incoming_message_text = message.text.trim();
|
|
26
|
+
|
|
27
|
+
let commands = null;
|
|
28
|
+
if (message.attributes && message.attributes.commands) {
|
|
29
|
+
commands = message.attributes.commands;
|
|
30
|
+
}
|
|
31
|
+
console.log("before taking decision:");
|
|
32
|
+
console.log("message.text:", incoming_message_text);
|
|
33
|
+
console.log("message commands:", commands)
|
|
34
|
+
if (incoming_message_text === "" && !commands) {
|
|
35
|
+
console.log("message with no content. Ignoring");
|
|
36
|
+
pipeline.message = null;
|
|
37
|
+
pipeline.nextplug();
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
|
|
25
41
|
if (!message.attributes) {
|
|
26
42
|
message.attributes = {}
|
|
27
43
|
}
|
|
28
|
-
|
|
29
|
-
if (message.text) {
|
|
44
|
+
if (incoming_message_text !== "") {
|
|
30
45
|
if (this.log) {console.log("markbotting main message...");}
|
|
31
|
-
let parsed_reply = TiledeskChatbotUtil.parseReply(
|
|
46
|
+
let parsed_reply = TiledeskChatbotUtil.parseReply(incoming_message_text);
|
|
32
47
|
if (this.log) {console.log("parsed", JSON.stringify(parsed_reply));}
|
|
33
48
|
if (parsed_reply) {
|
|
34
49
|
message.text = parsed_reply.message.text;
|
|
@@ -49,9 +64,14 @@ class MarkbotChatbotPlug {
|
|
|
49
64
|
}
|
|
50
65
|
}
|
|
51
66
|
}
|
|
67
|
+
else {
|
|
68
|
+
console.log("no message text:", message.text);
|
|
69
|
+
}
|
|
52
70
|
|
|
53
|
-
|
|
54
|
-
|
|
71
|
+
// let commands = null;
|
|
72
|
+
// if (message.attributes && message.attributes.commands) {
|
|
73
|
+
// commands = message.attributes.commands;
|
|
74
|
+
if (commands) {
|
|
55
75
|
if (this.log) {console.log("commands for markbot:", commands);}
|
|
56
76
|
if (commands.length > 1) {
|
|
57
77
|
for (let i = 0; i < commands.length; i++) {
|
|
@@ -63,6 +83,9 @@ class MarkbotChatbotPlug {
|
|
|
63
83
|
}
|
|
64
84
|
}
|
|
65
85
|
}
|
|
86
|
+
else if (message.attributes && !message.attributes.commands) {
|
|
87
|
+
console.log("no message commands.");
|
|
88
|
+
}
|
|
66
89
|
if (this.log) {console.log("Message out of Markbot:", JSON.stringify(message));}
|
|
67
90
|
pipeline.nextplug();
|
|
68
91
|
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
const { param } = require("express/lib/request");
|
|
2
|
+
|
|
3
|
+
class DirIfAvailableAgents {
|
|
4
|
+
|
|
5
|
+
constructor(config) {
|
|
6
|
+
if (!config.tdclient) {
|
|
7
|
+
throw new Error('config.tdclient (TiledeskClient) object is mandatory.');
|
|
8
|
+
}
|
|
9
|
+
this.tdclient = config.tdclient;
|
|
10
|
+
if (config.checkAgents == null || config.checkAgents === true) {
|
|
11
|
+
// null => defaults to checkNoAgents
|
|
12
|
+
this.checkAgents = true;
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
this.checkAgents = false;
|
|
16
|
+
}
|
|
17
|
+
this.log = config.log;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
execute(directive, directives, current_directive_index, callback) {
|
|
21
|
+
this.tdclient.openNow((err, result) => {
|
|
22
|
+
if (this.log) {console.log("openNow():", result);}
|
|
23
|
+
if (err) {
|
|
24
|
+
console.error("Agent in DirWhenOpen Error:", err);
|
|
25
|
+
callback();
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
if (directive.parameter) {
|
|
30
|
+
if (result && result.isopen) {
|
|
31
|
+
this.tdclient.getProjectAvailableAgents((err, agents) => {
|
|
32
|
+
if (this.log) {console.log("Agents on 'open'", agents);}
|
|
33
|
+
if (this.log) {console.log("Checking agents:", this.checkAgents);}
|
|
34
|
+
if (err || !agents) {
|
|
35
|
+
console.error("Error getting available agents in DirWhenAvailableAgents", err);
|
|
36
|
+
callback();
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
if (this.log) {console.log("Agents count:", agents.length);}
|
|
40
|
+
if (agents.length === 0 && !this.checkAgents) { // check no agents
|
|
41
|
+
let directive_to_execute = this.directiveFromParameter(directive.parameter);
|
|
42
|
+
if (this.log) {console.log("directive_to_execute:", directive_to_execute);}
|
|
43
|
+
if (directive_to_execute) {
|
|
44
|
+
directives.splice(current_directive_index + 1, 0, directive_to_execute);
|
|
45
|
+
}
|
|
46
|
+
callback();
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
else if (agents.length > 0 && this.checkAgents) { // check agents
|
|
50
|
+
let directive_to_execute = this.directiveFromParameter(directive.parameter);
|
|
51
|
+
if (this.log) {console.log("directive_to_execute:", directive_to_execute);}
|
|
52
|
+
if (directive_to_execute) {
|
|
53
|
+
directives.splice(current_directive_index + 1, 0, directive_to_execute);
|
|
54
|
+
}
|
|
55
|
+
callback();
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
callback();
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
callback();
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
if (this.log) {console.log("no directive to execute.");}
|
|
72
|
+
callback();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
directiveFromParameter(parameter) {
|
|
79
|
+
const DIRECTIVE_PREFIX = "_td";
|
|
80
|
+
const AGENT_DIRECTIVE_CMD = "\\agent"
|
|
81
|
+
const directive_pattern = /((\\{1}_td[a-zA-Z_0-9]*)|(\\agent))[ ]*(.*)[\r\n]*/m;
|
|
82
|
+
let match = null;
|
|
83
|
+
let directive = null;
|
|
84
|
+
match = directive_pattern.exec(parameter);
|
|
85
|
+
if (match && match.length >= 1) {
|
|
86
|
+
let final_msg_text = parameter.substring(0, match.index) + parameter.substring(match.index + match[0].length);
|
|
87
|
+
if (match.length >= 2) {
|
|
88
|
+
let directive_name = match[1];
|
|
89
|
+
if (directive_name !== AGENT_DIRECTIVE_CMD) {
|
|
90
|
+
// REMOVES THE "DIRECTIVE_PREFIX" from the directive name
|
|
91
|
+
directive_name = match[1].substring(DIRECTIVE_PREFIX.length + 1)
|
|
92
|
+
}
|
|
93
|
+
else if (directive_name === AGENT_DIRECTIVE_CMD) {
|
|
94
|
+
directive_name = this.AGENT_DIRECTIVE
|
|
95
|
+
}
|
|
96
|
+
directive = {
|
|
97
|
+
name: directive_name
|
|
98
|
+
};
|
|
99
|
+
if (match[1] !== AGENT_DIRECTIVE_CMD && match.length >= 5 && match[4] && match[4].trim().length > 0) {
|
|
100
|
+
directive.parameter = match[4];
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return directive;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
module.exports = { DirIfAvailableAgents };
|
|
@@ -8,38 +8,47 @@ class DirIntent {
|
|
|
8
8
|
}
|
|
9
9
|
this.API_ENDPOINT = settings.API_ENDPOINT;
|
|
10
10
|
this.TILEBOT_ENDPOINT = settings.TILEBOT_ENDPOINT;
|
|
11
|
+
this.log = settings.log;
|
|
11
12
|
}
|
|
12
13
|
|
|
13
|
-
execute(directive,
|
|
14
|
+
execute(directive, projectId, requestId, token, callback) {
|
|
14
15
|
if (directive.parameter) {
|
|
15
16
|
let intent_name = directive.parameter.trim();
|
|
16
|
-
let
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
17
|
+
let intent_command = "/" + intent_name;
|
|
18
|
+
let intent_command_message = {
|
|
19
|
+
sender: "_tdsender", // bot doesn't reply to "himself" and "system"
|
|
20
|
+
text: intent_command,
|
|
21
|
+
attributes: {
|
|
22
|
+
subtype: "info"
|
|
23
|
+
}
|
|
24
|
+
// request: {
|
|
25
|
+
// request_id: requestId
|
|
26
|
+
// },
|
|
27
|
+
// id_project: projectId
|
|
23
28
|
};
|
|
24
29
|
// send message to /ext/botId
|
|
25
|
-
const req_body = {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
30
|
+
// const req_body = {
|
|
31
|
+
// payload: message_to_bot,
|
|
32
|
+
// token: token
|
|
33
|
+
// }
|
|
29
34
|
let extEndpoint = `${this.API_ENDPOINT}/modules/tilebot`;
|
|
30
35
|
if (this.TILEBOT_ENDPOINT) {
|
|
31
36
|
extEndpoint = `${this.TILEBOT_ENDPOINT}`;
|
|
32
37
|
}
|
|
33
38
|
const extapi = new ExtApi({
|
|
34
39
|
ENDPOINT: extEndpoint,
|
|
35
|
-
log:
|
|
40
|
+
log: this.log
|
|
36
41
|
});
|
|
37
|
-
console.log("
|
|
38
|
-
console.log("(sending to bot) the req_body:", req_body);
|
|
39
|
-
extapi.
|
|
40
|
-
console.log("
|
|
42
|
+
if (this.log) {console.log("move to intent message:", intent_command_message);}
|
|
43
|
+
// console.log("(sending to bot) the req_body:", req_body);
|
|
44
|
+
extapi.sendSupportMessageExt(intent_command_message, projectId, requestId, token, () => {
|
|
45
|
+
if (this.log) {console.log("command " + intent_command + " sent.");}
|
|
41
46
|
callback();
|
|
42
47
|
});
|
|
48
|
+
// extapi.sendMessageToBot(req_body, message.attributes.intent_info.botId, token, () => {
|
|
49
|
+
// console.log("sendMessageToBot() req_body sent:", req_body);
|
|
50
|
+
// callback();
|
|
51
|
+
// });
|
|
43
52
|
}
|
|
44
53
|
else {
|
|
45
54
|
callback();
|
|
@@ -15,6 +15,8 @@ class Directives {
|
|
|
15
15
|
static DISABLE_INPUT_TEXT = "disableinputtext";
|
|
16
16
|
static WHEN_OPEN = "whenopen";
|
|
17
17
|
static WHEN_CLOSED = "whenclosed";
|
|
18
|
+
static IF_NO_AGENTS = "ifnoagents";
|
|
19
|
+
static IF_AGENTS = "ifagents";
|
|
18
20
|
static DEFLECT_TO_HELP_CENTER = "deflecttohelpcenter";
|
|
19
21
|
static WAIT = "wait";
|
|
20
22
|
static LOCK_INTENT = "lockintent";
|