@tiledesk/tiledesk-tybot-connector 0.1.40 → 0.1.42
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 +7 -0
- package/models/IntentForm.js +56 -6
- package/models/{IntentFormStudy.js → IntentForm_no_prefill.js} +6 -32
- package/models/TiledeskChatbot.js +19 -5
- package/package.json +4 -3
- package/test/intent_form_pre_filled_test.js +260 -0
- package/test/intent_form_test.js +0 -1
- package/test/send_email_directive_test.js +28 -24
- package/test/single_test.sh +3 -2
- package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +7 -2
- package/tiledeskChatbotPlugs/Filler.js +14 -0
- package/tiledeskChatbotPlugs/directives/DirSendEmail.js +17 -20
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.42
|
|
9
|
+
- added IntentForm pre-set variables skipping
|
|
10
|
+
|
|
11
|
+
### 0.1.41
|
|
12
|
+
- added Filler
|
|
13
|
+
- added Filler to email .to .subject .text attributes
|
|
14
|
+
|
|
8
15
|
### 0.1.40
|
|
9
16
|
- tiledesk-client => 0.9.5
|
|
10
17
|
- added _tdSendEmail directive
|
package/models/IntentForm.js
CHANGED
|
@@ -11,6 +11,18 @@ class IntentForm {
|
|
|
11
11
|
this.CURRENT_FIELD_INDEX_KEY = "tilebot:requests:" + this.requestId + ":currentFieldIndex"
|
|
12
12
|
this.CURRENT_FORM_KEY = "tilebot:requests:" + this.requestId + ":currentForm"
|
|
13
13
|
this.log = options.log;
|
|
14
|
+
this.requestParameters = options.requestParameters;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
getParam(paramKey) {
|
|
18
|
+
if (!this.requestParameters) {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
if (this.log) {
|
|
22
|
+
console.log("this.requestParameters:", this.requestParameters);
|
|
23
|
+
console.log("this.requestParameters[" + paramKey + "]:", this.requestParameters[paramKey]);
|
|
24
|
+
}
|
|
25
|
+
return this.requestParameters[paramKey];
|
|
14
26
|
}
|
|
15
27
|
|
|
16
28
|
async setValue(key, value) {
|
|
@@ -78,9 +90,27 @@ class IntentForm {
|
|
|
78
90
|
if (current_field == null) {
|
|
79
91
|
if (this.log) {console.log("current_field is undefined")}
|
|
80
92
|
current_field = 0;
|
|
93
|
+
|
|
94
|
+
// first "freeze" the currente form, so that eventual form modifications
|
|
95
|
+
// do not compromise this form processing.
|
|
81
96
|
await this.setValue(this.CURRENT_FORM_KEY, JSON.stringify(this.form));
|
|
97
|
+
|
|
98
|
+
// if (getParam(this.form.fields[current_field].name)) {
|
|
99
|
+
// //current_field++;
|
|
100
|
+
// await this.setValue(this.CURRENT_FIELD_INDEX_KEY, current_field); //=0
|
|
101
|
+
// return this.getMessage(current_value)
|
|
102
|
+
// }
|
|
103
|
+
// set the first field under the "await the response-value" state (=0)
|
|
82
104
|
await this.setValue(this.CURRENT_FIELD_INDEX_KEY, current_field);
|
|
83
|
-
//this
|
|
105
|
+
// now look for an already set value for this field in request parameters
|
|
106
|
+
if (this.log) {console.log("params", this.requestParameters);}
|
|
107
|
+
if (this.log) {console.log("checking field:", this.form.fields[current_field].name);}
|
|
108
|
+
const is_current_value = this.getParam(this.form.fields[current_field].name);
|
|
109
|
+
if (is_current_value) {
|
|
110
|
+
if (this.log) {console.log("is_current_value!", is_current_value);}
|
|
111
|
+
return await this.getMessage(is_current_value);
|
|
112
|
+
}
|
|
113
|
+
if (this.log) {console.log("Form asking fist value. No 'is_current_value' for first form field", is_current_value);}
|
|
84
114
|
if (this.log) {console.log("INTENT_FORM:", this.form);}
|
|
85
115
|
if (this.log) {console.log("CURRENT FIELD:", current_field);}
|
|
86
116
|
let message = {
|
|
@@ -92,9 +122,18 @@ class IntentForm {
|
|
|
92
122
|
}
|
|
93
123
|
}
|
|
94
124
|
else {
|
|
125
|
+
// = 0
|
|
126
|
+
// current++ (1) y? =>
|
|
127
|
+
//
|
|
128
|
+
//inc(current_field)=1 getMessage(null)
|
|
129
|
+
// == 1?
|
|
130
|
+
// n => continue =>
|
|
131
|
+
// param[1] n? => continue
|
|
132
|
+
// == 2? => set fields[2].name => user_text
|
|
95
133
|
//console.log("current_form:", current_form);
|
|
96
134
|
if (this.log) {console.log("current_field:", current_field);}
|
|
97
135
|
|
|
136
|
+
|
|
98
137
|
if (current_form.fields[current_field].regex) {
|
|
99
138
|
if (!this.validate(user_text, current_form.fields[current_field].regex)) {
|
|
100
139
|
if (this.log) {console.log("text is invalid");}
|
|
@@ -147,12 +186,23 @@ class IntentForm {
|
|
|
147
186
|
else {
|
|
148
187
|
if (this.log) {console.log("Processing next field:", current_field)}
|
|
149
188
|
await this.setValue(this.CURRENT_FIELD_INDEX_KEY, current_field);
|
|
150
|
-
|
|
151
|
-
|
|
189
|
+
|
|
190
|
+
if (this.log) {console.log("params", this.requestParameters);}
|
|
191
|
+
if (this.log) {console.log("checking field:", this.form.fields[current_field].name);}
|
|
192
|
+
|
|
193
|
+
const is_current_value = this.getParam(this.form.fields[current_field].name);
|
|
194
|
+
if (is_current_value) {
|
|
195
|
+
if (this.log) {console.log("is_current_value!", is_current_value);}
|
|
196
|
+
return await this.getMessage(is_current_value);
|
|
152
197
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
198
|
+
else {
|
|
199
|
+
return {
|
|
200
|
+
message: {
|
|
201
|
+
text: current_form.fields[current_field].label
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
|
|
156
206
|
}
|
|
157
207
|
}
|
|
158
208
|
}
|
|
@@ -78,19 +78,9 @@ class IntentForm {
|
|
|
78
78
|
if (current_field == null) {
|
|
79
79
|
if (this.log) {console.log("current_field is undefined")}
|
|
80
80
|
current_field = 0;
|
|
81
|
-
|
|
82
81
|
await this.setValue(this.CURRENT_FORM_KEY, JSON.stringify(this.form));
|
|
83
|
-
// if (getParam(this.form.fields[current_field].name)) {
|
|
84
|
-
// //current_field++;
|
|
85
|
-
// await this.setValue(this.CURRENT_FIELD_INDEX_KEY, current_field); //=0
|
|
86
|
-
// return this.getMessage(current_value)
|
|
87
|
-
// }
|
|
88
|
-
|
|
89
82
|
await this.setValue(this.CURRENT_FIELD_INDEX_KEY, current_field);
|
|
90
|
-
|
|
91
|
-
if (is_current_value) {
|
|
92
|
-
return await this.getMessage(is_current_value);
|
|
93
|
-
}
|
|
83
|
+
//this.printdb();
|
|
94
84
|
if (this.log) {console.log("INTENT_FORM:", this.form);}
|
|
95
85
|
if (this.log) {console.log("CURRENT FIELD:", current_field);}
|
|
96
86
|
let message = {
|
|
@@ -102,18 +92,9 @@ class IntentForm {
|
|
|
102
92
|
}
|
|
103
93
|
}
|
|
104
94
|
else {
|
|
105
|
-
// = 0
|
|
106
|
-
// current++ (1) y? =>
|
|
107
|
-
//
|
|
108
|
-
//inc(current_field)=1 getMessage(null)
|
|
109
|
-
// == 1?
|
|
110
|
-
// n => continue =>
|
|
111
|
-
// param[1] n? => continue
|
|
112
|
-
// == 2? => set fields[2].name => user_text
|
|
113
95
|
//console.log("current_form:", current_form);
|
|
114
96
|
if (this.log) {console.log("current_field:", current_field);}
|
|
115
97
|
|
|
116
|
-
|
|
117
98
|
if (current_form.fields[current_field].regex) {
|
|
118
99
|
if (!this.validate(user_text, current_form.fields[current_field].regex)) {
|
|
119
100
|
if (this.log) {console.log("text is invalid");}
|
|
@@ -166,19 +147,12 @@ class IntentForm {
|
|
|
166
147
|
else {
|
|
167
148
|
if (this.log) {console.log("Processing next field:", current_field)}
|
|
168
149
|
await this.setValue(this.CURRENT_FIELD_INDEX_KEY, current_field);
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
return await this.getMessage(is_current_value);
|
|
150
|
+
let message = {
|
|
151
|
+
text: current_form.fields[current_field].label
|
|
172
152
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
}
|
|
177
|
-
return {
|
|
178
|
-
message: message
|
|
179
|
-
};
|
|
180
|
-
}
|
|
181
|
-
|
|
153
|
+
return {
|
|
154
|
+
message: message
|
|
155
|
+
};
|
|
182
156
|
}
|
|
183
157
|
}
|
|
184
158
|
}
|
|
@@ -413,9 +413,14 @@ class TiledeskChatbot {
|
|
|
413
413
|
}
|
|
414
414
|
|
|
415
415
|
static async allParametersStatic(_tdcache, requestId) {
|
|
416
|
-
const parameters_key = "tilebot:requests:" + requestId + ":parameters";
|
|
417
|
-
|
|
418
|
-
|
|
416
|
+
// const parameters_key = "tilebot:requests:" + requestId + ":parameters";
|
|
417
|
+
return await _tdcache.hgetall(
|
|
418
|
+
TiledeskChatbot.requestCacheKey(requestId) + ":parameters");
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
static requestCacheKey(requestId) {
|
|
422
|
+
const request_key = "tilebot:requests:" + requestId;
|
|
423
|
+
return request_key;
|
|
419
424
|
}
|
|
420
425
|
|
|
421
426
|
async execWebhook(static_bot_answer, message, bot, context) {
|
|
@@ -431,8 +436,17 @@ class TiledeskChatbot {
|
|
|
431
436
|
}
|
|
432
437
|
|
|
433
438
|
async execIntentForm(userInputReply, form) {
|
|
434
|
-
|
|
435
|
-
let
|
|
439
|
+
if (this.log) {console.log("executing intent form...")}
|
|
440
|
+
let all_parameters = await this.allParameters();
|
|
441
|
+
if (this.log) {console.log("allParameters for IntentForm:", all_parameters)}
|
|
442
|
+
let intentForm = new IntentForm(
|
|
443
|
+
{
|
|
444
|
+
form: form,
|
|
445
|
+
requestId: this.requestId,
|
|
446
|
+
chatbot: this,
|
|
447
|
+
requestParameters: all_parameters,
|
|
448
|
+
log: this.log
|
|
449
|
+
});
|
|
436
450
|
let message = await intentForm.getMessage(userInputReply);
|
|
437
451
|
return message;
|
|
438
452
|
}
|
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.42",
|
|
4
4
|
"description": "Tiledesk Tybot connector",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -18,13 +18,14 @@
|
|
|
18
18
|
"axios": "^0.27.2",
|
|
19
19
|
"body-parser": "^1.19.0",
|
|
20
20
|
"cors": "^2.8.5",
|
|
21
|
+
"dotenv": "^16.0.3",
|
|
21
22
|
"express": "^4.17.1",
|
|
22
23
|
"jsonwebtoken": "^8.5.1",
|
|
23
24
|
"minimist-string": "^1.0.2",
|
|
25
|
+
"mocha": "^10.2.0",
|
|
24
26
|
"mongoose": "^6.3.5",
|
|
25
27
|
"nanoid": "^3.1.25",
|
|
26
28
|
"redis": "^3.1.2",
|
|
27
|
-
"uuid": "^3.3.3"
|
|
28
|
-
"dotenv": "^16.0.3"
|
|
29
|
+
"uuid": "^3.3.3"
|
|
29
30
|
}
|
|
30
31
|
}
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
var assert = require('assert');
|
|
2
|
+
const { ExtUtil } = require('../ExtUtil');
|
|
3
|
+
const { IntentForm } = require('../models/IntentForm');
|
|
4
|
+
const { MockTdCache } = require('../models/MockTdCache');
|
|
5
|
+
const { v4: uuidv4 } = require('uuid');
|
|
6
|
+
|
|
7
|
+
const TYPE_PREFIX = "_tdTypeOf:";
|
|
8
|
+
|
|
9
|
+
describe('IntentForm', function() {
|
|
10
|
+
|
|
11
|
+
it('basic form with 1 field, pre-filled', async () => {
|
|
12
|
+
const form = {
|
|
13
|
+
"fields": [
|
|
14
|
+
{
|
|
15
|
+
"name": "userFullname",
|
|
16
|
+
"type": "string",
|
|
17
|
+
"label": "What is your name?"
|
|
18
|
+
}
|
|
19
|
+
]
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const REQUEST_ID = "support-group-PROJECT_X-" + uuidv4().replace(/-/g, "");
|
|
23
|
+
|
|
24
|
+
const chatbot = new Chatbot();
|
|
25
|
+
await chatbot.addParameter('userFullname', "Already Andrea");
|
|
26
|
+
let all_parameters = await chatbot.allParameters();
|
|
27
|
+
// console.log("executing intent form...")
|
|
28
|
+
let intentForm = new IntentForm(
|
|
29
|
+
{
|
|
30
|
+
form: form,
|
|
31
|
+
requestId: REQUEST_ID,
|
|
32
|
+
chatbot: chatbot,
|
|
33
|
+
requestParameters: all_parameters,
|
|
34
|
+
log: false
|
|
35
|
+
}
|
|
36
|
+
);
|
|
37
|
+
let form_reply1 = await intentForm.getMessage("Trigger message");
|
|
38
|
+
// console.log("Got form first field label:", form_reply1)
|
|
39
|
+
assert(form_reply1 !== null);
|
|
40
|
+
assert(!form_reply1.canceled);
|
|
41
|
+
assert(form_reply1.end);
|
|
42
|
+
assert(all_parameters[form.fields[0].name] === "Already Andrea");
|
|
43
|
+
assert(all_parameters[TYPE_PREFIX + form.fields[0].name] === form.fields[0].type);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('basic form with 2 fields, both pre-filled', async () => {
|
|
47
|
+
const form = {
|
|
48
|
+
"fields": [
|
|
49
|
+
{
|
|
50
|
+
"name": "userFullname",
|
|
51
|
+
"type": "string",
|
|
52
|
+
"label": "What is your name?"
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
"name": "userEmail",
|
|
56
|
+
"type": "string",
|
|
57
|
+
"label": "What is your email?"
|
|
58
|
+
}
|
|
59
|
+
]
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const REQUEST_ID = "support-group-PROJECT_X-" + uuidv4().replace(/-/g, "");
|
|
63
|
+
|
|
64
|
+
const chatbot = new Chatbot();
|
|
65
|
+
await chatbot.addParameter('userFullname', "Already Andrea");
|
|
66
|
+
await chatbot.addParameter('userEmail', "Already Email");
|
|
67
|
+
let all_parameters = await chatbot.allParameters();
|
|
68
|
+
// console.log("executing intent form...")
|
|
69
|
+
let intentForm = new IntentForm(
|
|
70
|
+
{
|
|
71
|
+
form: form,
|
|
72
|
+
requestId: REQUEST_ID,
|
|
73
|
+
chatbot: chatbot,
|
|
74
|
+
requestParameters: all_parameters,
|
|
75
|
+
log: false
|
|
76
|
+
}
|
|
77
|
+
);
|
|
78
|
+
let form_reply1 = await intentForm.getMessage("Trigger message");
|
|
79
|
+
// console.log("Got form first field label:", form_reply1)
|
|
80
|
+
assert(form_reply1 !== null);
|
|
81
|
+
assert(!form_reply1.canceled);
|
|
82
|
+
assert(form_reply1.end);
|
|
83
|
+
assert(all_parameters[form.fields[0].name] === "Already Andrea");
|
|
84
|
+
assert(all_parameters[TYPE_PREFIX + form.fields[0].name] === form.fields[0].type);
|
|
85
|
+
assert(all_parameters[form.fields[1].name] === "Already Email");
|
|
86
|
+
assert(all_parameters[TYPE_PREFIX + form.fields[1].name] === form.fields[1].type);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('basic form with 3 fields, only 1st & 2nd pre-filled', async () => {
|
|
90
|
+
const form = {
|
|
91
|
+
"fields": [
|
|
92
|
+
{
|
|
93
|
+
"name": "userFullname",
|
|
94
|
+
"type": "string",
|
|
95
|
+
"label": "What is your name?"
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
"name": "userEmail",
|
|
99
|
+
"type": "string",
|
|
100
|
+
"label": "What is your email?"
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
"name": "companyName",
|
|
104
|
+
"type": "longString",
|
|
105
|
+
"label": "Company name?"
|
|
106
|
+
}
|
|
107
|
+
]
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const REQUEST_ID = "support-group-PROJECT_X-" + uuidv4().replace(/-/g, "");
|
|
111
|
+
|
|
112
|
+
const chatbot = new Chatbot();
|
|
113
|
+
await chatbot.addParameter('userFullname', "Already Andrea");
|
|
114
|
+
await chatbot.addParameter('userEmail', "Already Email");
|
|
115
|
+
let all_parameters = await chatbot.allParameters();
|
|
116
|
+
// console.log("executing intent form...")
|
|
117
|
+
let intentForm = new IntentForm(
|
|
118
|
+
{
|
|
119
|
+
form: form,
|
|
120
|
+
requestId: REQUEST_ID,
|
|
121
|
+
chatbot: chatbot,
|
|
122
|
+
requestParameters: all_parameters,
|
|
123
|
+
log: false
|
|
124
|
+
}
|
|
125
|
+
);
|
|
126
|
+
let form_reply1 = await intentForm.getMessage("Trigger message");
|
|
127
|
+
// console.log("Got form first field label:", form_reply1)
|
|
128
|
+
assert(form_reply1 !== null);
|
|
129
|
+
assert(!form_reply1.canceled);
|
|
130
|
+
assert(form_reply1.message.text === form.fields[2].label);
|
|
131
|
+
|
|
132
|
+
let form_reply2 = await intentForm.getMessage("Tiledesk");
|
|
133
|
+
// console.log("Got form second field label:", form_reply2)
|
|
134
|
+
assert(form_reply2 !== null);
|
|
135
|
+
assert(!form_reply2.canceled);
|
|
136
|
+
assert(form_reply2.end);
|
|
137
|
+
assert(all_parameters[form.fields[2].name] === "Tiledesk");
|
|
138
|
+
assert(all_parameters[TYPE_PREFIX + form.fields[2].name] === form.fields[2].type);
|
|
139
|
+
|
|
140
|
+
assert(all_parameters[form.fields[0].name] === "Already Andrea");
|
|
141
|
+
assert(all_parameters[TYPE_PREFIX + form.fields[0].name] === form.fields[0].type);
|
|
142
|
+
assert(all_parameters[form.fields[1].name] === "Already Email");
|
|
143
|
+
assert(all_parameters[TYPE_PREFIX + form.fields[1].name] === form.fields[1].type);
|
|
144
|
+
assert(all_parameters[form.fields[2].name] === "Tiledesk");
|
|
145
|
+
assert(all_parameters[TYPE_PREFIX + form.fields[2].name] === form.fields[2].type);
|
|
146
|
+
// console.log("All params:", all_parameters);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('basic form with 6 fields, 1st & 2nd pre-filled, 3rd not pre-filled, 4th (tel) pre-filled, 5th not pre-filled, 6th (age) pre-filled', async () => {
|
|
150
|
+
const form = {
|
|
151
|
+
"fields": [
|
|
152
|
+
{
|
|
153
|
+
"name": "userFullname",
|
|
154
|
+
"type": "string",
|
|
155
|
+
"label": "What is your name?"
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
"name": "userEmail",
|
|
159
|
+
"type": "string",
|
|
160
|
+
"label": "What is your email?"
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
"name": "companyName",
|
|
164
|
+
"type": "string",
|
|
165
|
+
"label": "Company name?"
|
|
166
|
+
},
|
|
167
|
+
{
|
|
168
|
+
"name": "tel",
|
|
169
|
+
"type": "longString",
|
|
170
|
+
"label": "Your phone number?"
|
|
171
|
+
},
|
|
172
|
+
{
|
|
173
|
+
"name": "SSN",
|
|
174
|
+
"type": "longString",
|
|
175
|
+
"label": "Your SSN?"
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
"name": "age",
|
|
179
|
+
"type": "number",
|
|
180
|
+
"label": "Your age?"
|
|
181
|
+
}
|
|
182
|
+
]
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
const REQUEST_ID = "support-group-PROJECT_X-" + uuidv4().replace(/-/g, "");
|
|
186
|
+
|
|
187
|
+
const chatbot = new Chatbot();
|
|
188
|
+
await chatbot.addParameter('userFullname', "Already Andrea");
|
|
189
|
+
await chatbot.addParameter('userEmail', "Already Email");
|
|
190
|
+
await chatbot.addParameter('tel', "0000");
|
|
191
|
+
await chatbot.addParameter('age', "49");
|
|
192
|
+
|
|
193
|
+
let all_parameters = await chatbot.allParameters();
|
|
194
|
+
// console.log("executing intent form...")
|
|
195
|
+
let intentForm = new IntentForm(
|
|
196
|
+
{
|
|
197
|
+
form: form,
|
|
198
|
+
requestId: REQUEST_ID,
|
|
199
|
+
chatbot: chatbot,
|
|
200
|
+
requestParameters: all_parameters,
|
|
201
|
+
log: false
|
|
202
|
+
}
|
|
203
|
+
);
|
|
204
|
+
let form_reply1 = await intentForm.getMessage("Trigger message");
|
|
205
|
+
// console.log("Got form first field label:", form_reply1)
|
|
206
|
+
assert(form_reply1 !== null);
|
|
207
|
+
assert(!form_reply1.canceled);
|
|
208
|
+
assert(form_reply1.message.text === form.fields[2].label);
|
|
209
|
+
|
|
210
|
+
let form_reply2 = await intentForm.getMessage("Tiledesk");
|
|
211
|
+
// console.log("Got form 'companyName' field label:", form_reply2);
|
|
212
|
+
assert(form_reply2 !== null);
|
|
213
|
+
assert(!form_reply2.canceled);
|
|
214
|
+
assert(form_reply2.message);
|
|
215
|
+
assert(form_reply2.message.text === form.fields[4].label) // asks the next "editable" field's label (SSN)
|
|
216
|
+
assert(all_parameters[form.fields[2].name] === "Tiledesk");
|
|
217
|
+
assert(all_parameters[TYPE_PREFIX + form.fields[2].name] === form.fields[2].type);
|
|
218
|
+
|
|
219
|
+
let form_reply3 = await intentForm.getMessage("SPNNDR0000");
|
|
220
|
+
// console.log("Got form 'SSN' field label:", form_reply3);
|
|
221
|
+
assert(form_reply3 !== null);
|
|
222
|
+
assert(!form_reply3.canceled);
|
|
223
|
+
assert(form_reply3.end);
|
|
224
|
+
|
|
225
|
+
assert(all_parameters[form.fields[0].name] === "Already Andrea");
|
|
226
|
+
assert(all_parameters[TYPE_PREFIX + form.fields[0].name] === form.fields[0].type);
|
|
227
|
+
assert(all_parameters[form.fields[1].name] === "Already Email");
|
|
228
|
+
assert(all_parameters[TYPE_PREFIX + form.fields[1].name] === form.fields[1].type);
|
|
229
|
+
assert(all_parameters[form.fields[2].name] === "Tiledesk");
|
|
230
|
+
assert(all_parameters[TYPE_PREFIX + form.fields[2].name] === form.fields[2].type);
|
|
231
|
+
assert(all_parameters[form.fields[3].name] === "0000");
|
|
232
|
+
assert(all_parameters[TYPE_PREFIX + form.fields[3].name] === form.fields[3].type);
|
|
233
|
+
assert(all_parameters[form.fields[4].name] === "SPNNDR0000");
|
|
234
|
+
assert(all_parameters[TYPE_PREFIX + form.fields[4].name] === form.fields[4].type);
|
|
235
|
+
assert(all_parameters[form.fields[5].name] === "49");
|
|
236
|
+
assert(all_parameters[TYPE_PREFIX + form.fields[5].name] === form.fields[5].type);
|
|
237
|
+
// console.log("All params:", all_parameters);
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
class Chatbot {
|
|
243
|
+
|
|
244
|
+
constructor() {
|
|
245
|
+
this.tdcache = new MockTdCache();
|
|
246
|
+
this.requestParameters = {};
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
async addParameter(parameter_name, parameter_value) {
|
|
250
|
+
this.requestParameters[parameter_name] = parameter_value;
|
|
251
|
+
return true;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
async allParameters() {
|
|
255
|
+
return new Promise( (resolve) => {
|
|
256
|
+
resolve(this.requestParameters);
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
}
|
package/test/intent_form_test.js
CHANGED
|
@@ -3,10 +3,11 @@ const { ExtUtil } = require('../ExtUtil');
|
|
|
3
3
|
const { DirectivesChatbotPlug } = require('../tiledeskChatbotPlugs/DirectivesChatbotPlug');
|
|
4
4
|
const { DirSendEmail } = require('../tiledeskChatbotPlugs/directives/DirSendEmail');
|
|
5
5
|
const supportRequest = require('./support_request.js').request;
|
|
6
|
+
const { TiledeskChatbot } = require('../models/TiledeskChatbot');
|
|
6
7
|
|
|
7
8
|
describe('Directive DirSendEmail', function() {
|
|
8
9
|
|
|
9
|
-
it('test
|
|
10
|
+
it('test DirSendEmail', async () => {
|
|
10
11
|
class MockTdClient {
|
|
11
12
|
async sendEmail(message, callback) {
|
|
12
13
|
if (callback) {
|
|
@@ -15,24 +16,37 @@ describe('Directive DirSendEmail', function() {
|
|
|
15
16
|
return message;
|
|
16
17
|
}
|
|
17
18
|
};
|
|
19
|
+
class MockTdCache {
|
|
20
|
+
async hgetall(request_parameters_key) {
|
|
21
|
+
// simulate we already saved sone request variables
|
|
22
|
+
// with a specific request id: request1
|
|
23
|
+
const request1_cache_id = TiledeskChatbot.requestCacheKey('request1') + ":parameters";
|
|
24
|
+
const variables = {}
|
|
25
|
+
variables[request1_cache_id] = {
|
|
26
|
+
"fullname": "John B.",
|
|
27
|
+
"email": "johnb@email.com"
|
|
28
|
+
}
|
|
29
|
+
return variables[request_parameters_key];
|
|
30
|
+
}
|
|
31
|
+
};
|
|
18
32
|
send_email_directive = {
|
|
19
33
|
name: "sendemail",
|
|
20
|
-
parameter: '--to "
|
|
34
|
+
parameter: '--to "${email}" --subject "Hello ${fullname}" --text "Welcome ${fullname}"'
|
|
21
35
|
};
|
|
36
|
+
let requestId = "request1";
|
|
22
37
|
let dir = new DirSendEmail({
|
|
23
|
-
tdclient: new MockTdClient()
|
|
38
|
+
tdclient: new MockTdClient(),
|
|
39
|
+
tdcache: new MockTdCache(),
|
|
40
|
+
requestId: requestId
|
|
24
41
|
});
|
|
25
|
-
|
|
26
|
-
const message = await dir.execute(send_email_directive, requestId) //, "err").to.be.null;
|
|
27
|
-
// console.error("Was expecting an error for the 'to' missing parameter");
|
|
28
|
-
console.log("message:", message);
|
|
42
|
+
const message = await dir.execute(send_email_directive);
|
|
29
43
|
assert(message);
|
|
30
|
-
assert(message.to);
|
|
31
|
-
assert(message.text);
|
|
32
|
-
assert(message.subject);
|
|
44
|
+
assert(message.to === 'johnb@email.com');
|
|
45
|
+
assert(message.text === 'Welcome John B.');
|
|
46
|
+
assert(message.subject === 'Hello John B.');
|
|
33
47
|
});
|
|
34
48
|
|
|
35
|
-
it('test
|
|
49
|
+
it('test DirSendEmail with missing "to"', async () => {
|
|
36
50
|
class MockTdClient {
|
|
37
51
|
async sendEmail(message, callback) {
|
|
38
52
|
if (callback) {
|
|
@@ -48,21 +62,11 @@ describe('Directive DirSendEmail', function() {
|
|
|
48
62
|
let dir = new DirSendEmail({
|
|
49
63
|
tdclient: new MockTdClient()
|
|
50
64
|
});
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
const message = await dir.execute(send_email_directive, requestId) //, "err").to.be.null;
|
|
54
|
-
// console.error("Was expecting an error for the 'to' missing parameter");
|
|
55
|
-
assert(message == null);
|
|
56
|
-
// }
|
|
57
|
-
// catch(err) {
|
|
58
|
-
// //console.log("Error is ok", err);
|
|
59
|
-
// if (!err.message.startsWith("sendEmail missing mandatory parameters")) {
|
|
60
|
-
// assert.ok(false);
|
|
61
|
-
// }
|
|
62
|
-
// }
|
|
65
|
+
const message = await dir.execute(send_email_directive)
|
|
66
|
+
assert(message == null);
|
|
63
67
|
});
|
|
64
68
|
|
|
65
|
-
it('test
|
|
69
|
+
it('test DirSendEmail in pipeline', async () => {
|
|
66
70
|
const message_text = `\\_tdsendemail --to "test@test" --subject "_sub" --text "_body"`;
|
|
67
71
|
const answer = {
|
|
68
72
|
text: message_text,
|
package/test/single_test.sh
CHANGED
|
@@ -3,5 +3,6 @@
|
|
|
3
3
|
#npx mocha ./test/disable_input_text_directive_test.js
|
|
4
4
|
#npx mocha ./test/close_directive_test.js
|
|
5
5
|
#npx mocha ./test/conversation1-test.js --exit
|
|
6
|
-
|
|
7
|
-
npx mocha ./test/send_email_directive_test.js --exit
|
|
6
|
+
npx mocha ./test/intent_form_test.js --exit
|
|
7
|
+
#npx mocha ./test/send_email_directive_test.js --exit
|
|
8
|
+
#npx mocha ./test/intent_form_pre_filled_test.js --exit
|
|
@@ -269,8 +269,13 @@ class DirectivesChatbotPlug {
|
|
|
269
269
|
});
|
|
270
270
|
}
|
|
271
271
|
else if (directive_name === Directives.SEND_EMAIL) {
|
|
272
|
-
new DirSendEmail(
|
|
273
|
-
|
|
272
|
+
new DirSendEmail(
|
|
273
|
+
{
|
|
274
|
+
tdclient: tdclient,
|
|
275
|
+
tdcache: tdcache,
|
|
276
|
+
requestId: requestId
|
|
277
|
+
}).execute(directive, () => {
|
|
278
|
+
process(nextDirective());
|
|
274
279
|
});
|
|
275
280
|
}
|
|
276
281
|
else {
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
class Filler {
|
|
2
|
+
|
|
3
|
+
fill(text, parameters) {
|
|
4
|
+
if (parameters) {
|
|
5
|
+
for (const [key, value] of Object.entries(parameters)) {
|
|
6
|
+
text = text.replace(new RegExp("(\\$\\{" + key + "\\})", 'i'), parameters[key]);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
return text;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
module.exports = { Filler };
|
|
@@ -1,47 +1,50 @@
|
|
|
1
|
-
//const { HelpCenter } = require('./HelpCenter');
|
|
2
1
|
const { HelpCenterQuery } = require('@tiledesk/helpcenter-query-client');
|
|
3
2
|
const { param } = require('express/lib/request');
|
|
4
3
|
const ms = require('minimist-string');
|
|
4
|
+
const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
|
|
5
|
+
const { Filler } = require('../Filler');
|
|
5
6
|
|
|
6
7
|
class DirSendEmail {
|
|
7
8
|
|
|
8
9
|
constructor(config) {
|
|
9
10
|
if (!config.tdclient) {
|
|
10
|
-
throw new Error('config.tdclient (TiledeskClient)
|
|
11
|
+
throw new Error('config.tdclient (TiledeskClient) is mandatory.');
|
|
11
12
|
}
|
|
12
13
|
this.tdclient = config.tdclient;
|
|
14
|
+
this.tdcache = config.tdcache;
|
|
15
|
+
this.requestId = config.requestId;
|
|
13
16
|
this.log = config.log;
|
|
14
17
|
}
|
|
15
18
|
|
|
16
|
-
async execute(directive,
|
|
17
|
-
// return new Promise( (resolve, reject) => {
|
|
19
|
+
async execute(directive, completion) {
|
|
18
20
|
let params = null;
|
|
19
21
|
if (directive.parameter) {
|
|
20
|
-
// console.log("processing sendEmail parameters");
|
|
21
22
|
params = this.parseParams(directive.parameter);
|
|
22
|
-
// console.log("parameters found", params);
|
|
23
23
|
}
|
|
24
24
|
else {
|
|
25
|
-
console.log("sendEmail missing parameter error. Skipping");
|
|
26
25
|
const error = new Error("sendEmail missing 'parameter' error. Skipping");
|
|
27
26
|
if (completion) {
|
|
28
27
|
completion(error);
|
|
29
28
|
}
|
|
30
|
-
// reject(error);
|
|
31
|
-
// throw error;
|
|
32
29
|
}
|
|
33
30
|
if (params.subject && params.text && params.to) {
|
|
34
31
|
try {
|
|
32
|
+
let requestVariables = null;
|
|
33
|
+
if (this.tdcache) {
|
|
34
|
+
requestVariables =
|
|
35
|
+
await TiledeskChatbot.allParametersStatic(
|
|
36
|
+
this.tdcache, this.requestId
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
const filler = new Filler();
|
|
35
40
|
const message_echo = await this.tdclient.sendEmail({
|
|
36
|
-
subject: params.subject,
|
|
37
|
-
text: params.text,
|
|
38
|
-
to: params.to
|
|
41
|
+
subject: filler.fill(params.subject, requestVariables),
|
|
42
|
+
text: filler.fill(params.text, requestVariables),
|
|
43
|
+
to: filler.fill(params.to, requestVariables)
|
|
39
44
|
});
|
|
40
|
-
// console.log("echo", message_echo)
|
|
41
45
|
if (completion) {
|
|
42
46
|
completion(null, message_echo);
|
|
43
47
|
}
|
|
44
|
-
// resolve(message_echo);
|
|
45
48
|
return message_echo;
|
|
46
49
|
}
|
|
47
50
|
catch(err) {
|
|
@@ -49,20 +52,14 @@ class DirSendEmail {
|
|
|
49
52
|
if (completion) {
|
|
50
53
|
completion(err);
|
|
51
54
|
}
|
|
52
|
-
// reject(error);
|
|
53
|
-
// throw err;
|
|
54
55
|
}
|
|
55
56
|
}
|
|
56
57
|
else {
|
|
57
|
-
// console.log("sendEmail missing mandatory parameters (to|subject|text):");
|
|
58
58
|
const error = new Error("sendEmail missing mandatory parameters (to|subject|text)");
|
|
59
59
|
if (completion) {
|
|
60
60
|
completion(error);
|
|
61
61
|
}
|
|
62
|
-
// reject(err);
|
|
63
|
-
// throw error;
|
|
64
62
|
}
|
|
65
|
-
// });
|
|
66
63
|
}
|
|
67
64
|
|
|
68
65
|
parseParams(directive_parameter) {
|