Skip to content

Commit 46bed84

Browse files
committed
### 0.8.37
- removed "\" from directive name - added directive-prefix. New template to have a directive parsed_ \_td{DIRECTIVE NAME}. This will reduce the necessity to escape the directive names because of their name uniqueness in the Tiledesk domain. - "\agent" legacy directive is still supported
1 parent 88af54c commit 46bed84

File tree

4 files changed

+178
-113
lines changed

4 files changed

+178
-113
lines changed

tiledesk-chatbot-util/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
This library is on npm: https://www.npmjs.com/package/@tiledesk/tiledesk-chatbot-util
44

5+
### 0.8.37
6+
- removed "\" from directive name
7+
- added directive-prefix. New template to have a directive parsed_ \_td{DIRECTIVE NAME}. This will reduce the necessity to escape the directive names because of their name uniqueness in the Tiledesk domain.
8+
- "\agent" legacy directive is still supported
9+
510
### 0.8.36
611
- splitPars() - bug fix
712

tiledesk-chatbot-util/index.js

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class TiledeskChatbotUtil {
3232
return null
3333
}
3434

35-
/**
35+
/** DEPRECATED
3636
* it checks if the message text contains the \agent command
3737
* @param {*} msg The message
3838
*/
@@ -88,26 +88,40 @@ static is_agent_handoff_command(msg) {
8888
if (!text) {
8989
return parsed;
9090
}
91-
9291
let final_msg_text = text;
93-
const directive_pattern = /(\\{1}[a-zA-Z_]*)[ ]*(.*)[\r\n]*/m;
92+
//const directive_pattern = new RegExp("((\\{1}" + this.DIRECTIVE_PREFIX + "[a-zA-Z_]*)|(\\agent))[ ]*(.*)[\r\n]*", "m");
93+
//const directive_pattern = new RegExp("((\\{1}_td[a-zA-Z_]*)|(\\agent))[ ]*(.*)[\r\n]*", "m");
94+
//const directive_pattern = /(\\{1}_td[a-zA-Z_]*)[ ]*(.*)[\r\n]*/m;
95+
const directive_pattern = /((\\{1}_td[a-zA-Z_0-9]*)|(\\agent))[ ]*(.*)[\r\n]*/m;
9496
let match = null;
9597
let directives = [];
9698
while ((match = directive_pattern.exec(final_msg_text)) != null) {
97-
// console.log("match: ", match);
98-
// console.log("match.index: ", match.index);
99-
// console.log("match[0].length: ", match[0].length);
100-
// console.log("match.length:", match.length);
99+
console.log("match: ", match);
100+
console.log("match.index: ", match.index);
101+
console.log("match[0].length: ", match[0].length);
102+
console.log("match.length:", match.length);
103+
const AGENT_DIRECTIVE_CMD = "\\agent"
101104
if (match.length >= 1) {
102105
final_msg_text = final_msg_text.substring(0, match.index) + final_msg_text.substring(match.index + match[0].length);
103106
// console.log("partial final_msg_text", final_msg_text);
104107
if (match.length >= 2) {
108+
let directive_name = match[1];
109+
if (directive_name !== AGENT_DIRECTIVE_CMD) {
110+
// REMOVES THE "DIRECTIVE_PREFIX" from the directive name
111+
directive_name = match[1].substring(this.DIRECTIVE_PREFIX.length + 1)
112+
}
113+
else if (directive_name === AGENT_DIRECTIVE_CMD) {
114+
directive_name = this.AGENT_DIRECTIVE
115+
}
105116
let directive = {
106-
name: match[1]
117+
name: directive_name
107118
};
108-
if (match.length >= 3 && match[2].trim().length > 0) {
109-
directive.parameter = match[2];
119+
console.log("match[3]::", match[4])
120+
if (match[1] !== AGENT_DIRECTIVE_CMD && match.length >= 5 && match[4] && match[4].trim().length > 0) {
121+
directive.parameter = match[4];
122+
console.log("directive.parameter = match[3]", directive.parameter)
110123
}
124+
console.log("directive:", directive);
111125
directives.push(directive);
112126
}
113127
}
@@ -170,6 +184,7 @@ static is_agent_handoff_command(msg) {
170184
}
171185

172186
/* Splits a message in multiple commands using the microlanguage
187+
DEPRECATED
173188
\split:TIME
174189
command \split:TIME must stand on a line of his own as in the following example
175190
Ex.
@@ -309,14 +324,15 @@ static is_agent_handoff_command(msg) {
309324
static INTENT_NO_ECHO_TAG = 'tdIntentNoEcho:';
310325

311326
// other
312-
static AGENT_DIRECTIVE = '\\agent';
313-
static CLOSE_DIRECTIVE = '\\close';
314-
static DEPARTMENT_DIRECTIVE = '\\department';
315-
static JSONMESSAGE_DIRECTIVE = '\\jsonmessage';
316-
static MESSAGE_DIRECTIVE = '\\message';
317-
static HIDDEN_MESSAGE_DIRECTIVE = '\\hmessage';
318-
static INTENT_DIRECTIVE = '\\intent';
319-
327+
static DIRECTIVE_PREFIX = '_td';
328+
static AGENT_DIRECTIVE = 'agent';
329+
static CLOSE_DIRECTIVE = 'close';
330+
static DEPARTMENT_DIRECTIVE = 'department';
331+
static JSONMESSAGE_DIRECTIVE = 'jsonmessage';
332+
static MESSAGE_DIRECTIVE = 'message';
333+
static HIDDEN_MESSAGE_DIRECTIVE = 'hmessage';
334+
static INTENT_DIRECTIVE = 'intent';
335+
320336
static parseReply(text) {
321337
let reply = {
322338
"message": {}

tiledesk-chatbot-util/package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@tiledesk/tiledesk-chatbot-util",
3-
"version": "0.8.36",
4-
"description": "This Library implements Tiledesk Microlanguage",
3+
"version": "0.8.37",
4+
"description": "This Library implements Tiledesk Markbot",
55
"main": "index.js",
66
"scripts": {
77
"test": "mocha"
@@ -19,15 +19,18 @@
1919
"chat",
2020
"support",
2121
"helpdesk",
22-
"chatbot-microlanguage"
22+
"markbot",
23+
"customer",
24+
"service",
25+
"customerservice"
2326
],
2427
"author": "Andrea Sponziello",
2528
"license": "MIT",
2629
"bugs": {
2730
"url": "https://github.com/Tiledesk/tiledesk-nodejs-libs/issues"
2831
},
2932
"homepage": "https://github.com/Tiledesk/tiledesk-nodejs-libs#readme",
30-
"dependencies": {
33+
"devDependencies": {
3134
"mocha": "^8.2.1"
3235
}
3336
}

tiledesk-chatbot-util/test/test_parse_directives.js

Lines changed: 132 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -6,106 +6,147 @@ const { TiledeskChatbotUtil } = require('..');
66
************************/
77

88
describe('_test directives_', function() {
9-
// describe('A message and the \\agent directive', function() {
10-
// it('should return a \\agent directive', function() {
11-
// const msg = 'Moving you to an agent\n\\agent';
12-
// const result = TiledeskChatbotUtil.parseDirectives(msg);
13-
// console.log("result:", JSON.stringify(result));
14-
// console.log("directives:", JSON.stringify(result.directives));
15-
// assert(result != null);
16-
// assert(result.directives != null);
17-
// assert.strictEqual(result.directives.length, 1);
18-
// assert(result.directives[0]);
19-
// assert(result.directives[0].name === TiledeskChatbotUtil.AGENT_DIRECTIVE);
20-
// assert(result.text === "Moving you to an agent");
21-
// console.log("result.findDirective(\"\\agent\"):", result.findDirective(TiledeskChatbotUtil.AGENT_DIRECTIVE))
22-
// assert(result.findDirective("\\agent").name === TiledeskChatbotUtil.AGENT_DIRECTIVE);
23-
// });
24-
// });
9+
10+
it('A message and the \\agent directive. Should return a \\agent directive', function() {
11+
const msg = 'Moving you to an agent\n\\agent';
12+
const result = TiledeskChatbotUtil.parseDirectives(msg);
13+
console.log("result:", JSON.stringify(result));
14+
console.log("directives:", JSON.stringify(result.directives));
15+
assert(result != null);
16+
assert(result.directives != null);
17+
assert.strictEqual(result.directives.length, 1);
18+
assert(result.directives[0]);
19+
assert(result.directives[0].name === TiledeskChatbotUtil.AGENT_DIRECTIVE);
20+
assert(result.text === "Moving you to an agent");
21+
assert(result.findDirective(TiledeskChatbotUtil.AGENT_DIRECTIVE).name != null);
22+
});
2523

26-
// describe('A message and the \\close directive', function() {
27-
// it('should return a \\close directive', function() {
28-
// const msg = 'Closing conversation\n\\close';
29-
// const result = TiledeskChatbotUtil.parseDirectives(msg);
30-
// console.log("result:", JSON.stringify(result));
31-
// console.log("directives:", JSON.stringify(result.directives));
32-
// assert(result != null);
33-
// assert(result.directives != null);
34-
// assert.strictEqual(result.directives.length, 1);
35-
// assert(result.directives[0]);
36-
// assert(result.directives[0].name === TiledeskChatbotUtil.CLOSE_DIRECTIVE);
37-
// assert(result.text === "Closing conversation");
38-
// console.log("result.findDirective(\"\\close\"):", result.findDirective(TiledeskChatbotUtil.CLOSE_DIRECTIVE))
39-
// assert(result.findDirective("\\close").name === TiledeskChatbotUtil.CLOSE_DIRECTIVE);
40-
// });
41-
// });
24+
it('Should return a \\close directive. A message and the \\close directive', function() {
25+
const msg = 'Closing conversation\n\\_tdclose';
26+
const result = TiledeskChatbotUtil.parseDirectives(msg);
27+
console.log("result:", JSON.stringify(result));
28+
console.log("directives:", JSON.stringify(result.directives));
29+
assert(result != null);
30+
assert(result.directives != null);
31+
assert.strictEqual(result.directives.length, 1);
32+
assert(result.directives[0]);
33+
assert(result.directives[0].name === TiledeskChatbotUtil.CLOSE_DIRECTIVE);
34+
assert(result.text === "Closing conversation");
35+
console.log("result.findDirective(\"\\close\"):", result.findDirective(TiledeskChatbotUtil.CLOSE_DIRECTIVE))
36+
assert(result.findDirective(TiledeskChatbotUtil.CLOSE_DIRECTIVE).name != null);
37+
});
38+
39+
it('A message, \\department and \\jsonmessage. Should return a \\department followed by a \\jsonmessage directives', function() {
40+
const msg =
41+
`Moving to main menu
42+
\\_tddepartment Default Department
43+
\\_tdjsonmessage {"attributes":{"subtype":"info"}}`;
44+
const result = TiledeskChatbotUtil.parseDirectives(msg);
45+
console.log("result:", JSON.stringify(result));
46+
console.log("directives:", JSON.stringify(result.directives));
47+
assert(result != null);
48+
assert(result.directives != null);
49+
assert.strictEqual(result.directives.length, 2);
50+
assert(result.directives[0]);
51+
assert(result.directives[1]);
52+
assert(result.directives[0].name === TiledeskChatbotUtil.DEPARTMENT_DIRECTIVE);
53+
assert(result.directives[0].parameter === 'Default Department');
54+
assert(result.directives[1].name === TiledeskChatbotUtil.JSONMESSAGE_DIRECTIVE);
55+
assert(result.directives[1].parameter === '{"attributes":{"subtype":"info"}}');
56+
const json_param = JSON.parse(result.directives[1].parameter);
57+
assert(json_param.attributes.subtype === "info");
58+
console.log("result.directives[1].parameter:", result.directives[1].parameter);
59+
assert(result.text === "Moving to main menu");
60+
console.log("result.findDirective(\"\\department\"):", result.findDirective(TiledeskChatbotUtil.DEPARTMENT_DIRECTIVE))
61+
assert(result.findDirective(TiledeskChatbotUtil.DEPARTMENT_DIRECTIVE).name === TiledeskChatbotUtil.DEPARTMENT_DIRECTIVE);
62+
assert(result.findDirective(TiledeskChatbotUtil.DEPARTMENT_DIRECTIVE).parameter === 'Default Department');
63+
console.log("result.findDirective(\"\\jsonmessage\"):", result.findDirective(TiledeskChatbotUtil.JSONMESSAGE_DIRECTIVE))
64+
assert(result.findDirective(TiledeskChatbotUtil.JSONMESSAGE_DIRECTIVE).name === TiledeskChatbotUtil.JSONMESSAGE_DIRECTIVE);
65+
assert(result.findDirective(TiledeskChatbotUtil.JSONMESSAGE_DIRECTIVE).parameter === '{"attributes":{"subtype":"info"}}');
66+
});
67+
68+
it('Only \\agent directive. Should return a \\agent and no text.', function() {
69+
const msg = '\\agent';
70+
const result = TiledeskChatbotUtil.parseDirectives(msg);
71+
console.log("result:", JSON.stringify(result));
72+
console.log("directives:", JSON.stringify(result.directives));
73+
assert(result != null);
74+
assert(result.directives != null);
75+
assert.strictEqual(result.directives.length, 1);
76+
assert(result.directives[0]);
77+
assert(result.directives[0].name === TiledeskChatbotUtil.AGENT_DIRECTIVE);
78+
assert(result.text === "");
79+
assert(result.findDirective(TiledeskChatbotUtil.AGENT_DIRECTIVE).name != null);
80+
});
4281

43-
// describe('A message, \\department and \\jsonmessage', function() {
44-
// it('should return a \\department followed by a \\jsonmessage directives', function() {
45-
// const msg = 'Moving to main menu\n\\department Default Department\n\\jsonmessage {"attributes":{"subtype":"info"}}';
46-
// const result = TiledeskChatbotUtil.parseDirectives(msg);
47-
// console.log("result:", JSON.stringify(result));
48-
// console.log("directives:", JSON.stringify(result.directives));
49-
// assert(result != null);
50-
// assert(result.directives != null);
51-
// assert.strictEqual(result.directives.length, 2);
52-
// assert(result.directives[0]);
53-
// assert(result.directives[1]);
54-
// assert(result.directives[0].name === TiledeskChatbotUtil.DEPARTMENT_DIRECTIVE);
55-
// assert(result.directives[0].parameter === 'Default Department');
56-
// assert(result.directives[1].name === TiledeskChatbotUtil.JSONMESSAGE_DIRECTIVE);
57-
// assert(result.directives[1].parameter === '{"attributes":{"subtype":"info"}}');
58-
// const json_param = JSON.parse(result.directives[1].parameter);
59-
// console.log("json_param:", json_param);
60-
// console.log("result.directives[1].parameter:", result.directives[1].parameter);
61-
// assert(result.text === "Moving to main menu");
62-
// console.log("result.findDirective(\"\\department\"):", result.findDirective(TiledeskChatbotUtil.DEPARTMENT_DIRECTIVE))
63-
// assert(result.findDirective(TiledeskChatbotUtil.DEPARTMENT_DIRECTIVE).name === TiledeskChatbotUtil.DEPARTMENT_DIRECTIVE);
64-
// assert(result.findDirective(TiledeskChatbotUtil.DEPARTMENT_DIRECTIVE).parameter === 'Default Department');
65-
// console.log("result.findDirective(\"\\jsonmessage\"):", result.findDirective(TiledeskChatbotUtil.JSONMESSAGE_DIRECTIVE))
66-
// assert(result.findDirective(TiledeskChatbotUtil.JSONMESSAGE_DIRECTIVE).name === TiledeskChatbotUtil.JSONMESSAGE_DIRECTIVE);
67-
// assert(result.findDirective(TiledeskChatbotUtil.JSONMESSAGE_DIRECTIVE).parameter === '{"attributes":{"subtype":"info"}}');
68-
// });
69-
// });
82+
it('should return a \\agent with text. One more only \\agent directive', function() {
83+
const msg =
84+
`We are looking for an operator..
85+
-
86+
JUST WAIT A MOMENT
87+
\\agent`;
88+
const result = TiledeskChatbotUtil.parseDirectives(msg);
89+
console.log("result:", JSON.stringify(result));
90+
console.log("directives:", JSON.stringify(result.directives));
91+
assert(result != null);
92+
assert(result.directives != null);
93+
assert.strictEqual(result.directives.length, 1);
94+
assert(result.directives[0]);
95+
assert(result.directives[0].name === TiledeskChatbotUtil.AGENT_DIRECTIVE);
96+
assert(result.text ===
97+
`We are looking for an operator..
98+
-
99+
JUST WAIT A MOMENT`);
100+
assert(result.findDirective(TiledeskChatbotUtil.AGENT_DIRECTIVE).name != null);
101+
});
70102

71-
// describe('Only \\agent directive', function() {
72-
// it('should return a \\agent and no text', function() {
73-
// const msg = '\\agent';
74-
// const result = TiledeskChatbotUtil.parseDirectives(msg);
75-
// console.log("result:", JSON.stringify(result));
76-
// console.log("directives:", JSON.stringify(result.directives));
77-
// assert(result != null);
78-
// assert(result.directives != null);
79-
// assert.strictEqual(result.directives.length, 1);
80-
// assert(result.directives[0]);
81-
// assert(result.directives[0].name === TiledeskChatbotUtil.AGENT_DIRECTIVE);
82-
// assert(result.text === "");
83-
// console.log("result.findDirective(\"\\agent\"):", result.findDirective(TiledeskChatbotUtil.AGENT_DIRECTIVE))
84-
// assert(result.findDirective("\\agent").name === TiledeskChatbotUtil.AGENT_DIRECTIVE);
85-
// });
86-
// });
103+
it('Finds no directives', function() {
104+
const msg =
105+
`We are looking for an operator..
106+
-
107+
\\not_a_directive
108+
JUST WAIT A MOMENT
109+
\\not_a_directive`;
110+
const result = TiledeskChatbotUtil.parseDirectives(msg);
111+
console.log("result:", JSON.stringify(result));
112+
console.log("directives:", JSON.stringify(result.directives));
113+
assert(result != null);
114+
assert(result.directives != null);
115+
assert.strictEqual(result.directives.length, 0);
116+
assert(result.text === msg);
117+
});
87118

88-
describe('One more only \\agent directive', function() {
89-
it('should return a \\agent with text', function() {
90-
const msg =
119+
it('Finds 3 directives', function() {
120+
const msg =
91121
`We are looking for an operator..
92122
-
93123
JUST WAIT A MOMENT
94-
\\agent`;
95-
const result = TiledeskChatbotUtil.parseDirectives(msg);
96-
console.log("result:", JSON.stringify(result));
97-
console.log("directives:", JSON.stringify(result.directives));
98-
assert(result != null);
99-
assert(result.directives != null);
100-
assert.strictEqual(result.directives.length, 1);
101-
assert(result.directives[0]);
102-
assert(result.directives[0].name === TiledeskChatbotUtil.AGENT_DIRECTIVE);
103-
assert(result.text ===
124+
\\_tdDir1 with params
125+
\\_tdDir2NoParam
126+
\\_tdDir3 one_param`;
127+
const result = TiledeskChatbotUtil.parseDirectives(msg);
128+
console.log("result:", JSON.stringify(result));
129+
console.log("directives:", JSON.stringify(result.directives));
130+
assert(result != null);
131+
assert(result.directives != null);
132+
assert.strictEqual(result.directives.length, 3);
133+
assert(result.directives[0]);
134+
assert(result.directives[1]);
135+
assert(result.directives[2]);
136+
137+
assert(result.directives[0].name === "Dir1");
138+
assert(result.directives[0].parameter === "with params");
139+
assert(result.directives[1].name === "Dir2NoParam");
140+
assert(result.directives[1].parameter == null);
141+
assert(result.directives[2].name === "Dir3");
142+
assert(result.directives[2].parameter === "one_param");
143+
144+
assert(result.text.trim() ===
104145
`We are looking for an operator..
105146
-
106147
JUST WAIT A MOMENT`);
107-
console.log("result.findDirective(\"\\agent\"):", result.findDirective(TiledeskChatbotUtil.AGENT_DIRECTIVE))
108-
assert(result.findDirective("\\agent").name === TiledeskChatbotUtil.AGENT_DIRECTIVE);
109-
});
148+
assert(result.findDirective("Dir1").name === "Dir1");
149+
assert(result.findDirective("Dir2NoParam").name === "Dir2NoParam");
150+
assert(result.findDirective("Dir3").name === "Dir3");
110151
});
111152
});

0 commit comments

Comments
 (0)