@tiledesk/tiledesk-tybot-connector 0.1.21 → 0.1.23

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.
Files changed (36) hide show
  1. package/CHANGELOG.md +10 -1
  2. package/ExtApi.js +37 -0
  3. package/index backup.js +656 -0
  4. package/index.js +58 -461
  5. package/models/IntentForm.js +12 -13
  6. package/models/MockBotsDataSource.js +74 -0
  7. package/models/MockIntentsMachine.js +39 -0
  8. package/models/MongodbBotsDataSource.js +73 -0
  9. package/models/MongodbIntentsMachine.js +55 -0
  10. package/models/{TiledeskChatbot_Intents_Adapter.js → TiledeskChatbot backup.js } +0 -1
  11. package/models/TiledeskChatbot.js +206 -180
  12. package/models/TiledeskIntentsMachine.js +90 -0
  13. package/models/faqKbService.js +27 -0
  14. package/models/faqService.js +22 -0
  15. package/models/faq_kb.js +11 -28
  16. package/package.json +2 -2
  17. package/test/close_directive_test.js +49 -0
  18. package/test/directives_test.js +24 -26
  19. package/test/disable_input_text_directive_test.js +88 -0
  20. package/test/mock_query_test.js +276 -0
  21. package/test/single_test.sh +4 -0
  22. package/test/when_open_directive_test.js +167 -0
  23. package/tiledeskChatbotPlugs/CHANGELOG.md +2 -0
  24. package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +66 -46
  25. package/tiledeskChatbotPlugs/directives/DirClose.js +25 -0
  26. package/tiledeskChatbotPlugs/directives/DirDeflectToHelpCenter.js +6 -6
  27. package/tiledeskChatbotPlugs/directives/DirDepartment.js +52 -0
  28. package/tiledeskChatbotPlugs/directives/DirDisableInputText.js +45 -0
  29. package/tiledeskChatbotPlugs/directives/DirIntent.js +50 -0
  30. package/tiledeskChatbotPlugs/directives/DirMessage.js +5 -4
  31. package/tiledeskChatbotPlugs/directives/DirWhenOpen.js +95 -0
  32. package/tiledeskChatbotPlugs/directives/Directives.js +3 -0
  33. package/tiledeskChatbotPlugs/package.json +1 -1
  34. package/models/MongoDBIntentsDataSource.js +0 -22
  35. package/models/StaticIntentsDataSource.js +0 -110
  36. package/test/chatbot_query_test.js_ +0 -41
@@ -0,0 +1,74 @@
1
+ class MockBotsDataSource {
2
+
3
+ constructor(bots) {
4
+ if (!bots) {
5
+ throw new Error("bots is mandatory")
6
+ }
7
+ this.data = bots;
8
+ }
9
+
10
+ // let faqs = await this.intentsDataSource.getByExactMatch(message.text);
11
+ // let faq = await this.intentsDataSource.getByIntentDisplayName(display_name);
12
+ // let intents = await this.intentsFinder.find(message.text);
13
+
14
+ async getBotById(botId) {
15
+ const bot = this.data.bots[botId];
16
+ if (bot) {
17
+ return bot;
18
+ }
19
+ return null;
20
+ }
21
+
22
+ /**
23
+ *
24
+ * @param {String} text
25
+ * @returns an Array of matches
26
+ */
27
+ async getByExactMatch(botId, text) {
28
+ const intent_display_name = this.data.bots[botId].questions_intent[text];
29
+ if (intent_display_name) {
30
+ return [this.data.bots[botId].intents[intent_display_name]];
31
+ }
32
+ return null;
33
+ }
34
+
35
+ /**
36
+ *
37
+ * @param {String} intentName
38
+ * @returns a single Intent
39
+ */
40
+ async getByIntentDisplayName(botId, intentName) {
41
+ const intent = this.data.bots[botId].intents[intentName];
42
+ return intent;
43
+ }
44
+
45
+ /**
46
+ * intentsFinder Adapter
47
+ * @param {String} text
48
+ * @returns the matching intents' names array
49
+ */
50
+ async decode(botId, text) {
51
+ if (this.data.bots[botId].intents_nlp[text]) {
52
+ return [ this.data.bots[botId].intents_nlp[text] ];
53
+ }
54
+ else {
55
+ return [];
56
+ }
57
+ }
58
+
59
+ /**
60
+ * intentsFinder Adapter
61
+ * @param {String} botId
62
+ * @param {Object} json
63
+ * @returns true if the train task started
64
+ */
65
+ async train(botId, json) {
66
+ if (!this.data) {
67
+ throw new Error("Can't train empty data");
68
+ }
69
+ return true
70
+ }
71
+
72
+ }
73
+
74
+ module.exports = { MockBotsDataSource }
@@ -0,0 +1,39 @@
1
+ class MockIntentsMachine {
2
+
3
+ constructor(bots) {
4
+ if (!bots) {
5
+ throw new Error("bots is mandatory")
6
+ }
7
+ this.data = bots;
8
+ }
9
+
10
+ /**
11
+ * intentsFinder Adapter
12
+ * @param {String} text
13
+ * @returns the matching intents' names array
14
+ */
15
+ async decode(botId, text) {
16
+ if (this.data.bots[botId].intents_nlp[text]) {
17
+ return [ this.data.bots[botId].intents_nlp[text] ];
18
+ }
19
+ else {
20
+ return [];
21
+ }
22
+ }
23
+
24
+ /**
25
+ * intentsFinder Adapter
26
+ * @param {String} botId
27
+ * @param {Object} json
28
+ * @returns true if the train task started
29
+ */
30
+ async train(botId, json) {
31
+ if (!this.data) {
32
+ throw new Error("Can't train empty data");
33
+ }
34
+ return true
35
+ }
36
+
37
+ }
38
+
39
+ module.exports = { MockIntentsMachine }
@@ -0,0 +1,73 @@
1
+ let Faq = require('./faq');
2
+ let Faq_kb = require('./faq_kb');
3
+
4
+ class MongodbBotsDataSource {
5
+
6
+ constructor(config) {
7
+ if (!config.projectId) {
8
+ throw new Error("config.projectId is mandatory");
9
+ }
10
+ this.projectId = config.projectId;
11
+ }
12
+
13
+ async getBotById(botId) {
14
+ const bot = await Faq_kb.findById(botId).select('+secret').exec();
15
+ if (bot) {
16
+ return bot;
17
+ }
18
+ return null;
19
+ }
20
+
21
+ /**
22
+ *
23
+ * @param {String} text
24
+ * @returns an Array of matches
25
+ */
26
+ async getByExactMatch(botId, text) {
27
+ // SEARCH INTENTS
28
+ return new Promise( (resolve, reject) => {
29
+ let query = { "id_project": this.projectId, "id_faq_kb": botId, "question": text };
30
+ Faq.find(query).lean().exec(async (err, faqs) => {
31
+ if (err) {
32
+ console.error("Error getting faq object.", err);
33
+ reject(err);
34
+ }
35
+ else if (faqs && faqs.length > 0 && faqs[0].answer) {
36
+ if (this.log) {console.log("EXACT MATCH OR ACTION FAQ:", faqs);}
37
+ resolve(faqs);
38
+ }
39
+ else {
40
+ resolve(null);
41
+ }
42
+ });
43
+ });
44
+ }
45
+
46
+ /**
47
+ *
48
+ * @param {String} intentName
49
+ * @returns a single Intent
50
+ */
51
+ async getByIntentDisplayName(botId, name) {
52
+ return new Promise((resolve, reject) => {
53
+ var query = { "id_project": this.projectId, "id_faq_kb": botId, "intent_display_name": name};
54
+ if (this.log) {console.debug('query', query);}
55
+ Faq.find(query).lean().exec( (err, faqs) => {
56
+ if (err) {
57
+ return reject(err);
58
+ }
59
+ if (this.log) {console.debug("getByIntentDisplayName faqs", faqs);}
60
+ if (faqs && faqs.length > 0) {
61
+ const intent = faqs[0];
62
+ return resolve(intent);
63
+ }
64
+ else {
65
+ return resolve(null);
66
+ }
67
+ });
68
+ });
69
+ }
70
+
71
+ }
72
+
73
+ module.exports = { MongodbBotsDataSource }
@@ -0,0 +1,55 @@
1
+ let mongoose = require('mongoose');
2
+ let Faq = require('./faq');
3
+
4
+ class MongodbIntentsMachine {
5
+
6
+ constructor(config) {
7
+ if (!config.projectId) {
8
+ throw new Error("config.projectId is mandatory");
9
+ }
10
+ this.projectId = config.projectId;
11
+ this.language = config.language;
12
+ this.log = config.log;
13
+ }
14
+
15
+ /**
16
+ * intentsFinder Adapter
17
+ * @param {String} text
18
+ * @returns the matching intents' names array
19
+ */
20
+ async decode(botId, text) {
21
+ return new Promise( (resolve, reject) => {
22
+ if (this.log) {console.log("NLP decode intent...");}
23
+ let query = { "id_project": this.projectId, "id_faq_kb": botId };
24
+ var mongoproject = undefined;
25
+ var sort = undefined;
26
+ var search_obj = { "$search": text };
27
+
28
+ if (this.language) {
29
+ search_obj["$language"] = this.language;
30
+ }
31
+ query.$text = search_obj;
32
+ mongoproject = { score: { $meta: "textScore" } };
33
+ sort = { score: { $meta: "textScore" } }
34
+ // DA QUI RECUPERO LA RISPOSTA DATO (ID: SE EXT_AI) (QUERY FULLTEXT SE NATIVE-BASIC-AI)
35
+ Faq.find(query, mongoproject).sort(sort).lean().exec( (err, faqs) => {
36
+ if (this.log) {console.log("Found:", faqs);}
37
+ if (err) {
38
+ console.error("Error:", err);
39
+ }
40
+ if (faqs && faqs.length > 0 && faqs[0].answer) {
41
+ resolve(faqs);
42
+ }
43
+ else {
44
+ // fallback
45
+ //const fallbackIntent = await this.getIntentByDisplayName("defaultFallback", bot);
46
+ //const faqs = [fallbackIntent];
47
+ resolve([]);
48
+ }
49
+ });
50
+ })
51
+ }
52
+
53
+ }
54
+
55
+ module.exports = { MongodbIntentsMachine }
@@ -19,7 +19,6 @@ class TiledeskChatbot {
19
19
  this.requestId = config.requestId;
20
20
  this.projectId = config.projectId;
21
21
  this.log = config.log;
22
- this.intentsDataSource = config.intentsDataSource
23
22
  }
24
23
 
25
24
  async replyToMessage(message, callback) {