@tiledesk/tiledesk-tybot-connector 0.1.16 → 0.1.18
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 +8 -1
- package/README.md +2 -0
- package/index.js +10 -7
- package/models/IntentForm.js +1 -1
- package/models/MongoDBIntentsDataSource.js +22 -0
- package/models/StaticIntentsDataSource.js +110 -0
- package/models/TiledeskChatbot.js +23 -16
- package/models/TiledeskChatbot_Intents_Adapter.js +406 -0
- package/package.json +1 -1
- package/test/{chatbot_query_test.js → chatbot_query_test.js_} +0 -0
- package/index (copy).js +0 -718
- package/index_compare.js +0 -471
package/CHANGELOG.md
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
# Tiledesk tybotRoute
|
|
2
2
|
|
|
3
|
-
**npm @tiledesk/tiledesk-tybot-connector@0.1.
|
|
3
|
+
**npm @tiledesk/tiledesk-tybot-connector@0.1.18**
|
|
4
4
|
|
|
5
5
|
available on:
|
|
6
6
|
▶️ https://www.npmjs.com/package/@tiledesk/tiledesk-tybot-connector
|
|
7
7
|
|
|
8
|
+
### 0.1.18
|
|
9
|
+
- removed support for CACHE_ENABLED.
|
|
10
|
+
- Redis is now mandatory: settings.REDIS_PORT, settings.REDIS_HOST are mandatory. settings.REDIS_PASSWORD is optional
|
|
11
|
+
|
|
12
|
+
### 0.1.17
|
|
13
|
+
- removed deprecated files
|
|
14
|
+
|
|
8
15
|
### 0.1.16
|
|
9
16
|
- added Redis support to share some parameters (message.requestId, message.projectId) with Apps
|
|
10
17
|
|
package/README.md
ADDED
package/index.js
CHANGED
|
@@ -85,11 +85,11 @@ router.post('/ext/:botid', async (req, res) => {
|
|
|
85
85
|
|
|
86
86
|
const parameters_key = "tilebot:requests:" + requestId + ":parameters";
|
|
87
87
|
await chatbot.addParameter(requestId, "tdMessageId", messageId);
|
|
88
|
-
all_params = await chatbot.allParameters(requestId);
|
|
89
|
-
console.log("Allparams", all_params);
|
|
90
|
-
let reply = await chatbot.
|
|
88
|
+
//all_params = await chatbot.allParameters(requestId);
|
|
89
|
+
//console.log("Allparams", all_params);
|
|
90
|
+
let reply = await chatbot.replyToMessage(message);
|
|
91
91
|
reply.triggeredByMessageId = messageId;
|
|
92
|
-
console.log("reply ok", reply);
|
|
92
|
+
//console.log("reply ok", reply);
|
|
93
93
|
let extEndpoint = `${APIURL}/modules/tilebot/`;
|
|
94
94
|
if (process.env.TYBOT_ENDPOINT) {
|
|
95
95
|
extEndpoint = `${process.env.TYBOT_ENDPOINT}`;
|
|
@@ -582,16 +582,19 @@ function startApp(settings, completionCallback) {
|
|
|
582
582
|
console.log("Starting Tybot with Settings:", settings);
|
|
583
583
|
|
|
584
584
|
if (!settings.MONGODB_URI) {
|
|
585
|
-
throw new Error("settings.MONGODB_URI is mandatory
|
|
585
|
+
throw new Error("settings.MONGODB_URI is mandatory");
|
|
586
586
|
}
|
|
587
587
|
if (!settings.API_ENDPOINT) {
|
|
588
|
-
throw new Error("settings.API_ENDPOINT is mandatory
|
|
588
|
+
throw new Error("settings.API_ENDPOINT is mandatory");
|
|
589
589
|
}
|
|
590
590
|
else {
|
|
591
591
|
APIURL = settings.API_ENDPOINT;
|
|
592
592
|
console.log("(Tybot) settings.API_ENDPOINT:", APIURL);
|
|
593
593
|
}
|
|
594
|
-
if (settings.REDIS_HOST && settings.REDIS_PORT
|
|
594
|
+
if (!settings.REDIS_HOST && !settings.REDIS_PORT) {
|
|
595
|
+
throw new Error("settings.REDIS_HOST && settings.REDIS_PORT are mandatory");
|
|
596
|
+
}
|
|
597
|
+
else { // (settings.REDIS_HOST && settings.REDIS_PORT) { // && settings.CACHE_ENABLED) {
|
|
595
598
|
tdcache = new TdCache({
|
|
596
599
|
host: settings.REDIS_HOST,
|
|
597
600
|
port: settings.REDIS_PORT,
|
package/models/IntentForm.js
CHANGED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
class StaticIntentsQueryAdapter {
|
|
2
|
+
|
|
3
|
+
constructor() {
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
async getByExactMatch(text) {
|
|
7
|
+
const intent_display_name = questions_intent[text];
|
|
8
|
+
if (intent_display_name) {
|
|
9
|
+
return intents[intent_display_name];
|
|
10
|
+
}
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async getByNLP(text) {
|
|
15
|
+
return getByExactMatch(text);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async getByIntentName(intentName) {
|
|
19
|
+
return questions_intent[text];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
class StaticIntentsDataSource {
|
|
2
|
+
|
|
3
|
+
constructor(intentsDataSource) {
|
|
4
|
+
if (intentsDataSource) {
|
|
5
|
+
this.intentsDataSource = intentsDataSource;
|
|
6
|
+
}
|
|
7
|
+
else {
|
|
8
|
+
this.intentsDataSource = defaultIntentsSataSource;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async getByExactMatch(text) {
|
|
13
|
+
const intent_display_name = questions_intent[text];
|
|
14
|
+
if (intent_display_name) {
|
|
15
|
+
return intents[intent_display_name];
|
|
16
|
+
}
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async getByNLP(text) {
|
|
21
|
+
return getByExactMatch(text);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async getByIntentName(intentName) {
|
|
25
|
+
return questions_intent[text];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const defaultIntentsSataSource = {
|
|
31
|
+
"intents" = {
|
|
32
|
+
"intent1": {
|
|
33
|
+
intent_display_name: "intent1",
|
|
34
|
+
questions: [
|
|
35
|
+
"intent1 question1",
|
|
36
|
+
"intent1 question2"
|
|
37
|
+
],
|
|
38
|
+
answer: "reply to intent1"
|
|
39
|
+
},
|
|
40
|
+
"intent2": {
|
|
41
|
+
intent_display_name: "intent1",
|
|
42
|
+
questions: [
|
|
43
|
+
"intent2 question1",
|
|
44
|
+
"intent2 question2"
|
|
45
|
+
],
|
|
46
|
+
answer: "reply to intent2"
|
|
47
|
+
},
|
|
48
|
+
"intent3": {
|
|
49
|
+
intent_display_name: "intent3",
|
|
50
|
+
questions: [
|
|
51
|
+
"intent3 question1",
|
|
52
|
+
"intent3 question2"
|
|
53
|
+
],
|
|
54
|
+
answer: "reply to intent3"
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
"questions_intent": {
|
|
58
|
+
|
|
59
|
+
"intent1 question1": "intent1",
|
|
60
|
+
"intent1 question2": "intent1",
|
|
61
|
+
|
|
62
|
+
"intent2 question1": "intent2",
|
|
63
|
+
"intent2 question2": "intent2",
|
|
64
|
+
|
|
65
|
+
"intent3 question1": "intent3",
|
|
66
|
+
"intent3 question2": "intent3"
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
const intents = {
|
|
72
|
+
"intent1": {
|
|
73
|
+
intent_display_name: "intent1",
|
|
74
|
+
questions: [
|
|
75
|
+
"intent1 question1",
|
|
76
|
+
"intent1 question2"
|
|
77
|
+
],
|
|
78
|
+
answer: "reply to intent1"
|
|
79
|
+
},
|
|
80
|
+
"intent2": {
|
|
81
|
+
intent_display_name: "intent1",
|
|
82
|
+
questions: [
|
|
83
|
+
"intent2 question1",
|
|
84
|
+
"intent2 question2"
|
|
85
|
+
],
|
|
86
|
+
answer: "reply to intent2"
|
|
87
|
+
},
|
|
88
|
+
"intent3": {
|
|
89
|
+
intent_display_name: "intent3",
|
|
90
|
+
questions: [
|
|
91
|
+
"intent3 question1",
|
|
92
|
+
"intent3 question2"
|
|
93
|
+
],
|
|
94
|
+
answer: "reply to intent3"
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const questions_intent = {
|
|
99
|
+
|
|
100
|
+
"intent1 question1": "intent1",
|
|
101
|
+
"intent1 question2": "intent1",
|
|
102
|
+
|
|
103
|
+
"intent2 question1": "intent2",
|
|
104
|
+
"intent2 question2": "intent2",
|
|
105
|
+
|
|
106
|
+
"intent3 question1": "intent3",
|
|
107
|
+
"intent3 question2": "intent3"
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
module.exports = { StaticIntentsDataSource }
|
|
@@ -21,7 +21,7 @@ class TiledeskChatbot {
|
|
|
21
21
|
this.log = config.log;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
async
|
|
24
|
+
async replyToMessage(message, callback) {
|
|
25
25
|
return new Promise( async (resolve, reject) => {
|
|
26
26
|
|
|
27
27
|
const bot = await Faq_kb.findById(this.botId).select('+secret').exec();
|
|
@@ -39,7 +39,8 @@ class TiledeskChatbot {
|
|
|
39
39
|
});
|
|
40
40
|
const faqs = await tdclient.getIntents(this.botId, locked_intent, 0, 0, null);
|
|
41
41
|
if (this.log) {console.log("locked intent. got faqs", faqs)}
|
|
42
|
-
|
|
42
|
+
let reply = await this.execIntent(faqs, this.botId, message, bot);
|
|
43
|
+
resolve(reply);
|
|
43
44
|
return;
|
|
44
45
|
}
|
|
45
46
|
// CREATE TOKEN
|
|
@@ -74,8 +75,10 @@ class TiledeskChatbot {
|
|
|
74
75
|
return console.error("Error getting faq object.", err);
|
|
75
76
|
}
|
|
76
77
|
if (faqs && faqs.length > 0 && faqs[0].answer) {
|
|
77
|
-
if (this.log) {console.log("EXACT MATCH FAQ:", faqs[0]);}
|
|
78
|
-
|
|
78
|
+
if (this.log) {console.log("EXACT MATCH OR ACTION FAQ:", faqs[0]);}
|
|
79
|
+
let reply = await this.execIntent(faqs, this.botId, message, bot);
|
|
80
|
+
resolve(reply);
|
|
81
|
+
//resolve(this.execIntent(faqs, this.botId, message, bot)); // bot_token
|
|
79
82
|
}
|
|
80
83
|
else { // FULL TEXT
|
|
81
84
|
if (this.log) {console.log("NLP decode intent...");}
|
|
@@ -99,13 +102,15 @@ class TiledeskChatbot {
|
|
|
99
102
|
console.error("Error:", err);
|
|
100
103
|
}
|
|
101
104
|
if (faqs && faqs.length > 0 && faqs[0].answer) {
|
|
102
|
-
|
|
105
|
+
let reply = await this.execIntent(faqs, this.botId, message, bot);
|
|
106
|
+
resolve(reply);
|
|
103
107
|
}
|
|
104
108
|
else {
|
|
105
109
|
// fallback
|
|
106
110
|
const fallbackIntent = await this.getIntentByDisplayName("defaultFallback", bot);
|
|
107
111
|
const faqs = [fallbackIntent];
|
|
108
|
-
|
|
112
|
+
let reply = await this.execIntent(faqs, this.botId, message, bot);
|
|
113
|
+
resolve(reply); // bot_token
|
|
109
114
|
}
|
|
110
115
|
});
|
|
111
116
|
}
|
|
@@ -133,7 +138,7 @@ class TiledeskChatbot {
|
|
|
133
138
|
});
|
|
134
139
|
}
|
|
135
140
|
|
|
136
|
-
async
|
|
141
|
+
async execIntent(faqs, botId, message, bot) {
|
|
137
142
|
answerObj = faqs[0];
|
|
138
143
|
let sender = 'bot_' + botId;
|
|
139
144
|
var answerObj;
|
|
@@ -164,7 +169,7 @@ class TiledeskChatbot {
|
|
|
164
169
|
ENDPOINT: extEndpoint,
|
|
165
170
|
log: this.log
|
|
166
171
|
});
|
|
167
|
-
console.log("the form...")
|
|
172
|
+
//console.log("the form...")
|
|
168
173
|
|
|
169
174
|
// THE FORM
|
|
170
175
|
|
|
@@ -200,10 +205,10 @@ class TiledeskChatbot {
|
|
|
200
205
|
const user_reply = message.text;
|
|
201
206
|
//const intent_answer = answerObj.answer; //req.body.payload.text;
|
|
202
207
|
let form_reply = await this.execIntentForm(user_reply, intent_form);
|
|
203
|
-
console.log("got form reply", form_reply)
|
|
208
|
+
//console.log("got form reply", form_reply)
|
|
204
209
|
//if (form_reply_message) {
|
|
205
210
|
if (!form_reply.canceled && form_reply.message) {
|
|
206
|
-
console.log("Form replying for next field...");
|
|
211
|
+
//console.log("Form replying for next field...");
|
|
207
212
|
if (this.log) {console.log("Sending form reply...", form_reply.message)}
|
|
208
213
|
// reply with this message (ex. please enter your fullname)
|
|
209
214
|
if (!form_reply.message.attributes) {
|
|
@@ -219,8 +224,11 @@ class TiledeskChatbot {
|
|
|
219
224
|
//return;
|
|
220
225
|
}
|
|
221
226
|
else if (form_reply.end) {
|
|
222
|
-
console.log("Form end.");
|
|
223
|
-
if (this.log) {
|
|
227
|
+
//console.log("Form end.");
|
|
228
|
+
if (this.log) {
|
|
229
|
+
console.log("Form end.");
|
|
230
|
+
console.log("Unlocking intent for request", requestId);
|
|
231
|
+
}
|
|
224
232
|
this.unlockIntent(requestId);
|
|
225
233
|
this.populatePrechatFormAndLead(message);
|
|
226
234
|
}
|
|
@@ -241,7 +249,7 @@ class TiledeskChatbot {
|
|
|
241
249
|
//});
|
|
242
250
|
//return;
|
|
243
251
|
}
|
|
244
|
-
console.log("form_reply is", form_reply)
|
|
252
|
+
//console.log("form_reply is", form_reply)
|
|
245
253
|
}
|
|
246
254
|
|
|
247
255
|
// FORM END
|
|
@@ -256,7 +264,7 @@ class TiledeskChatbot {
|
|
|
256
264
|
token: this.token
|
|
257
265
|
};
|
|
258
266
|
|
|
259
|
-
console.log("the static_bot_answer...")
|
|
267
|
+
//console.log("the static_bot_answer...")
|
|
260
268
|
const static_bot_answer = { // static design of the chatbot reply
|
|
261
269
|
//type: answerObj.type,
|
|
262
270
|
text: answerObj.answer,
|
|
@@ -299,12 +307,11 @@ class TiledeskChatbot {
|
|
|
299
307
|
static_bot_answer.attributes.markbot = true;
|
|
300
308
|
static_bot_answer.attributes.fillParams = true;
|
|
301
309
|
static_bot_answer.attributes.webhook = answerObj.webhook_enabled;
|
|
302
|
-
|
|
310
|
+
|
|
303
311
|
// exec webhook (only)
|
|
304
312
|
const bot_answer = await this.execPipeline(static_bot_answer, message, bot, context, this.token);
|
|
305
313
|
|
|
306
314
|
//bot_answer.text = await fillWithRequestParams(bot_answer.text, requestId); // move to "ext" pipeline
|
|
307
|
-
console.log("returning answer", bot_answer)
|
|
308
315
|
return bot_answer;
|
|
309
316
|
|
|
310
317
|
/*apiext.sendSupportMessageExt(bot_answer, projectId, requestId, token, () => {
|