@tiledesk/tiledesk-tybot-connector 0.5.0 → 2.0.0

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 (100) hide show
  1. package/ExtApi.js +4 -83
  2. package/ExtUtil.js +0 -1
  3. package/Logger.js +105 -0
  4. package/TdCache.js +5 -3
  5. package/TiledeskClientTest.js +5 -28
  6. package/TiledeskExpression.js +6 -47
  7. package/{models → engine}/IntentForm.js +30 -44
  8. package/{models → engine}/IntentsMachineFactory.js +5 -4
  9. package/{models → engine}/MongodbBotsDataSource.js +31 -55
  10. package/{models → engine}/MongodbIntentsMachine.js +6 -5
  11. package/{models → engine}/TiledeskChatbot.js +82 -259
  12. package/{models → engine}/TiledeskChatbotConst.js +9 -0
  13. package/{models → engine}/TiledeskIntentsMachine.js +5 -15
  14. package/{models → engine/mock}/MockBotsDataSource.js +3 -19
  15. package/{models → engine/mock}/MockTdCache.js +0 -9
  16. package/index.js +103 -380
  17. package/logs/app.log +12723 -277
  18. package/logs/app1.log +62258 -0
  19. package/logs/app2.log +46280 -0
  20. package/logs/app3.log +31686 -0
  21. package/logs/app4.log +46163 -0
  22. package/logs/app5.log +33173 -0
  23. package/models/faq.js +2 -5
  24. package/package.json +5 -2
  25. package/{TiledeskServices → services}/AIService.js +4 -3
  26. package/{models/faqKbService.js → services/FaqKbService.js} +1 -2
  27. package/{models/faqService.js → services/FaqService.js} +4 -3
  28. package/services/IntegrationService.js +43 -0
  29. package/services/TilebotService.js +47 -0
  30. package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +49 -135
  31. package/tiledeskChatbotPlugs/FillParamsChatbotPlug.js +8 -14
  32. package/tiledeskChatbotPlugs/Filler.js +0 -1
  33. package/tiledeskChatbotPlugs/MarkbotChatbotPlug.js +12 -20
  34. package/tiledeskChatbotPlugs/MessagePipeline.js +5 -15
  35. package/tiledeskChatbotPlugs/SplitsChatbotPlug.js +4 -13
  36. package/tiledeskChatbotPlugs/TiledeskRequestVariables.js +1 -1
  37. package/tiledeskChatbotPlugs/WebhookChatbotPlug.js +21 -89
  38. package/tiledeskChatbotPlugs/directives/DEPRECATED_DirSetAttribute.js +1 -1
  39. package/tiledeskChatbotPlugs/directives/DirAddTags.js +23 -140
  40. package/tiledeskChatbotPlugs/directives/DirAiPrompt.js +42 -128
  41. package/tiledeskChatbotPlugs/directives/DirAskGPT.js +37 -127
  42. package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +52 -145
  43. package/tiledeskChatbotPlugs/directives/DirAssign.js +12 -23
  44. package/tiledeskChatbotPlugs/directives/DirAssignFromFunction.js +16 -12
  45. package/tiledeskChatbotPlugs/directives/DirAssistant.js +56 -198
  46. package/tiledeskChatbotPlugs/directives/DirBrevo.js +41 -101
  47. package/tiledeskChatbotPlugs/directives/DirCaptureUserReply.js +9 -19
  48. package/tiledeskChatbotPlugs/directives/DirClearTranscript.js +1 -1
  49. package/tiledeskChatbotPlugs/directives/DirClose.js +4 -2
  50. package/tiledeskChatbotPlugs/directives/DirCode.js +16 -23
  51. package/tiledeskChatbotPlugs/directives/DirCondition.js +19 -25
  52. package/tiledeskChatbotPlugs/directives/DirConnectBlock.js +9 -82
  53. package/tiledeskChatbotPlugs/directives/DirContactUpdate.js +10 -22
  54. package/tiledeskChatbotPlugs/directives/DirCustomerio.js +34 -89
  55. package/tiledeskChatbotPlugs/directives/DirDeflectToHelpCenter.js +17 -22
  56. package/tiledeskChatbotPlugs/directives/DirDeleteVariable.js +8 -8
  57. package/tiledeskChatbotPlugs/directives/DirDepartment.js +13 -12
  58. package/tiledeskChatbotPlugs/directives/DirDisableInputText.js +2 -6
  59. package/tiledeskChatbotPlugs/directives/DirFireTiledeskEvent.js +4 -4
  60. package/tiledeskChatbotPlugs/directives/DirForm.js +17 -24
  61. package/tiledeskChatbotPlugs/directives/DirGptTask.js +49 -139
  62. package/tiledeskChatbotPlugs/directives/DirGptTask_OLD.js +8 -53
  63. package/tiledeskChatbotPlugs/directives/DirHubspot.js +31 -91
  64. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgents.js +19 -26
  65. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgentsV2.js +24 -78
  66. package/tiledeskChatbotPlugs/directives/DirIfOpenHours.js +17 -68
  67. package/tiledeskChatbotPlugs/directives/DirIntent.js +6 -100
  68. package/tiledeskChatbotPlugs/directives/DirJSONCondition.js +15 -14
  69. package/tiledeskChatbotPlugs/directives/DirLockIntent.js +7 -12
  70. package/tiledeskChatbotPlugs/directives/DirMake.js +21 -53
  71. package/tiledeskChatbotPlugs/directives/DirMessage.js +35 -28
  72. package/tiledeskChatbotPlugs/directives/DirMessageToBot.js +8 -87
  73. package/tiledeskChatbotPlugs/directives/DirMoveToAgent.js +6 -4
  74. package/tiledeskChatbotPlugs/directives/DirMoveToUnassigned.js +6 -4
  75. package/tiledeskChatbotPlugs/directives/DirQapla.js +26 -120
  76. package/tiledeskChatbotPlugs/directives/DirRandomReply.js +11 -19
  77. package/tiledeskChatbotPlugs/directives/DirRemoveCurrentBot.js +4 -0
  78. package/tiledeskChatbotPlugs/directives/DirReplaceBot.js +5 -2
  79. package/tiledeskChatbotPlugs/directives/DirReplaceBotV2.js +12 -58
  80. package/tiledeskChatbotPlugs/directives/DirReplaceBotV3.js +11 -57
  81. package/tiledeskChatbotPlugs/directives/DirReply.js +36 -30
  82. package/tiledeskChatbotPlugs/directives/DirReplyV2.js +30 -72
  83. package/tiledeskChatbotPlugs/directives/DirSendEmail.js +9 -10
  84. package/tiledeskChatbotPlugs/directives/DirSendWhatsapp.js +15 -40
  85. package/tiledeskChatbotPlugs/directives/DirSetAttribute.js +8 -5
  86. package/tiledeskChatbotPlugs/directives/DirSetAttributeV2.js +25 -113
  87. package/tiledeskChatbotPlugs/directives/DirSetConversationTags.js +14 -13
  88. package/tiledeskChatbotPlugs/directives/DirUnlockIntent.js +3 -3
  89. package/tiledeskChatbotPlugs/directives/DirWait.js +5 -6
  90. package/tiledeskChatbotPlugs/directives/DirWebRequest.js +14 -49
  91. package/tiledeskChatbotPlugs/directives/DirWebRequestV2.js +22 -77
  92. package/tiledeskChatbotPlugs/directives/DirWebRequestV2_old.js +1 -1
  93. package/tiledeskChatbotPlugs/directives/DirWebResponse.js +11 -14
  94. package/tiledeskChatbotPlugs/directives/DirWhatsappByAttribute.js +13 -42
  95. package/tiledeskChatbotPlugs/directives/Directives.js +0 -3
  96. package/utils/HttpUtils.js +128 -0
  97. package/{models → utils}/TiledeskChatbotUtil.js +164 -234
  98. package/utils/winston.js +42 -0
  99. package/TiledeskServices/utils.js +0 -99
  100. /package/{models → engine/mock}/MockIntentsMachine.js +0 -0
@@ -1,5 +1,6 @@
1
1
  //const CURRENT_FIELD_K = "tilebot:requests:forms:currentField"; // form field index
2
2
  const CURRENT_FORM_K = "CURRENT_FORM"; // form json
3
+ const winston = require('../utils/winston');
3
4
 
4
5
  class IntentForm {
5
6
 
@@ -18,10 +19,8 @@ class IntentForm {
18
19
  if (!this.requestParameters) {
19
20
  return null;
20
21
  }
21
- if (this.log) {
22
- console.log("this.requestParameters:", JSON.stringify(this.requestParameters));
23
- console.log("this.requestParameters[" + paramKey + "]:", this.requestParameters[paramKey]);
24
- }
22
+ winston.debug("(IntentForm) requestParameters: ", this.requestParameters);
23
+ winston.debug("(IntentForm) requestParameters[" + paramKey + "]:" + this.requestParameters[paramKey]);
25
24
  return this.requestParameters[paramKey];
26
25
  }
27
26
 
@@ -61,13 +60,11 @@ class IntentForm {
61
60
  }
62
61
  */
63
62
  async getMessage(user_text) {
64
- //console.log("get message:", user_text)
65
63
  let current_form = null;
66
64
  const _current_form = await this.getValue(this.CURRENT_FORM_KEY);
67
65
  if (_current_form) {
68
66
  current_form = JSON.parse(_current_form);
69
67
  }
70
- //console.log("CURRENT FORM IS", current_form);
71
68
  if (
72
69
  current_form &&
73
70
  this.form &&
@@ -90,7 +87,7 @@ class IntentForm {
90
87
  }
91
88
 
92
89
  if (current_field == null) {
93
- if (this.log) {console.log("current_field is undefined")}
90
+ winston.debug("(IntentForm) current_field is undefined");
94
91
  current_field = 0;
95
92
 
96
93
  // first "freeze" the currente form, so that eventual form modifications
@@ -105,58 +102,48 @@ class IntentForm {
105
102
  // set the first field under the "await the response-value" state (=0)
106
103
  await this.setValue(this.CURRENT_FIELD_INDEX_KEY, current_field);
107
104
  // now look for an already set value for this field in request parameters
108
- if (this.log) {console.log("IntentForm parameters", JSON.stringify(this.requestParameters));}
109
- if (this.log) {console.log("checking field:", this.form.fields[current_field].name);}
105
+ winston.debug("(IntentForm) parameters ", this.requestParameters);
106
+ winston.debug("(IntentForm) checking field: " + this.form.fields[current_field].name);
110
107
  const is_current_value = this.getParam(this.form.fields[current_field].name);
111
108
  if (is_current_value) {
112
- if (this.log) {console.log("is_current_value!", is_current_value);}
109
+ winston.debug("(IntentForm) is_current_value: " + is_current_value);
113
110
  return await this.getMessage(is_current_value);
114
111
  }
115
- if (this.log) {console.log("Form asking fist value. No 'is_current_value' for first form field", is_current_value);}
116
- // if (this.log) {console.log("INTENT_FORM:", this.form);}
117
- if (this.log) {console.log("CURRENT FIELD:", current_field);}
112
+ winston.debug("(IntentForm) asking fist value. No 'is_current_value' for first form field" + is_current_value);
113
+ winston.debug("(IntentForm) current_field: " + current_field);
118
114
  let message = {
119
115
  text: this.form.fields[current_field].label
120
116
  }
121
- if (this.log) {console.log("form reply message:", message);}
117
+ winston.debug("(IntentForm)form reply message: ", message);
122
118
  return {
123
119
  message: message
124
120
  }
125
121
  }
126
122
  else {
127
- // = 0
128
- // current++ (1) y? =>
129
- //
130
- //inc(current_field)=1 getMessage(null)
131
- // == 1?
132
- // n => continue =>
133
- // param[1] n? => continue
134
- // == 2? => set fields[2].name => user_text
135
- //console.log("current_form:", current_form);
136
- if (this.log) {console.log("current_field:", current_field);}
137
-
123
+
124
+ winston.debug("(IntentForm) current_field: " + current_field);
138
125
 
139
126
  if (current_form.fields[current_field].regex) {
140
127
  if (!this.validate(user_text, current_form.fields[current_field].regex)) {
141
- if (this.log) {console.log("text is invalid");}
128
+ winston.verbose("(IntentForm) text is invalid");
142
129
  // send error message
143
130
  let error_reply_text = this.form.fields[current_field].label;
144
- if (this.log) {console.log("text is invalid label", error_reply_text);}
131
+ winston.debug("(IntentForm) text is invalid label: " + error_reply_text);
145
132
  if (current_form.fields[current_field].errorLabel) {
146
- if (this.log) {console.log("text is invalid errorLabel", current_form.fields[current_field].errorLabel);}
133
+ winston.debug("(IntentForm) text is invalid errorLabel: " + current_form.fields[current_field].errorLabel);
147
134
  error_reply_text = current_form.fields[current_field].errorLabel;
148
135
  }
149
136
  let message = {
150
137
  text: error_reply_text // Error
151
138
  }
152
- if (this.log) {console.log("IntentForm error message:", message);}
139
+ winston.debug("(IntentForm) error message: ", message);
153
140
  return {
154
141
  message: message
155
142
  };
156
143
  }
157
144
  }
158
145
  else {
159
- if (this.log) {console.log("no regex validation requested. next field...")}
146
+ winston.verbose("(IntentForm) no regex validation requested. next field...")
160
147
  }
161
148
 
162
149
  // text ok?
@@ -169,12 +156,12 @@ class IntentForm {
169
156
  // persist parameter
170
157
  const parameter_name = current_form.fields[current_field].name;
171
158
  const parameter_value = user_text;
172
- if (this.log) {console.log("adding parameters, name:", parameter_name, "value:", parameter_value)}
159
+ winston.debug("(IntentForm) adding parameters, name: " + parameter_name + " value: " + parameter_value)
173
160
  await this.chatbot.addParameter(parameter_name, parameter_value);
174
161
  if (current_form.fields[current_field].type) { // adding type
175
162
  await this.chatbot.addParameter("_tdTypeOf:" + parameter_name, current_form.fields[current_field].type);
176
163
  }
177
- if (this.log) {console.log("next field...");}
164
+ winston.verbose("(IntentForm) next field...");
178
165
 
179
166
  current_field += 1;
180
167
  if (current_field === current_form.fields.length) {
@@ -186,23 +173,22 @@ class IntentForm {
186
173
  };
187
174
  }
188
175
  else {
189
- if (this.log) {console.log("Processing next field:", current_field)}
176
+ winston.debug("(IntentForm) Processing next field: " + current_field);
190
177
  await this.setValue(this.CURRENT_FIELD_INDEX_KEY, current_field);
191
178
 
192
- // if (this.log) {console.log("params", this.requestParameters);}
193
- if (this.log) {console.log("checking field:", this.form.fields[current_field].name);}
179
+ winston.debug("(IntentForm)checking field:", this.form.fields[current_field].name);
194
180
 
195
181
  const is_current_value = this.getParam(this.form.fields[current_field].name);
196
182
  if (is_current_value) {
197
- if (this.log) {console.log("is_current_value!", is_current_value);}
198
- return await this.getMessage(is_current_value);
183
+ winston.debug("(IntentForm) is_current_value! " + is_current_value);
184
+ return await this.getMessage(is_current_value);
199
185
  }
200
186
  else {
201
- return {
202
- message: {
203
- text: current_form.fields[current_field].label
204
- }
205
- };
187
+ return {
188
+ message: {
189
+ text: current_form.fields[current_field].label
190
+ }
191
+ };
206
192
  }
207
193
 
208
194
  }
@@ -215,13 +201,13 @@ class IntentForm {
215
201
  // removing leading and trailing / if regex is sorrounded by (legacy support, to be removed)
216
202
  _regex = regex.substring(1, regex.length-1);
217
203
  }
218
- if (this.log) {console.log("Validating using regex:", _regex);}
204
+ winston.debug("(IntentForm)Validating using regex: " + _regex);
219
205
  try {
220
206
  const rg = new RegExp(_regex, "g");
221
207
  return rg.test(text);
222
208
  }
223
209
  catch(error) {
224
- console.error("Error, invalid regex:", _regex);
210
+ winston.error("(IntentForm) Error, invalid regex: " + _regex);
225
211
  return true;
226
212
  }
227
213
 
@@ -1,30 +1,31 @@
1
1
  const { MongodbIntentsMachine } = require('./MongodbIntentsMachine.js');
2
2
  const { TiledeskIntentsMachine } = require("./TiledeskIntentsMachine.js");
3
+ const winston = require('../utils/winston.js')
3
4
 
4
5
  class IntentsMachineFactory {
5
6
 
6
7
  static getMachine(bot, botId, projectId, log) {
7
8
  let machine;
8
9
  if (bot && bot.intentsEngine === "tiledesk-ai") {
9
- console.log("bot.intentsEngine is tiledesk-ai");
10
+ winston.verbose("(IntentsMachineFactory) bot.intentsEngine is tiledesk-ai");
10
11
  machine = new TiledeskIntentsMachine(
11
12
  {
12
13
  botId: botId
13
14
  });
14
15
  }
15
16
  else if (bot) {
16
- if (log) {console.log("Setting MongodbIntentsMachine with bot:", JSON.stringify(bot));}
17
+ winston.verbose("(IntentsMachineFactory) Setting MongodbIntentsMachine with bot:", JSON.stringify(bot));
17
18
  machine = new MongodbIntentsMachine({projectId: projectId, language: bot.language, log});
18
19
  }
19
20
  else {
20
- console.error("bot is null for:", botId, "on projectId:", projectId);
21
+ winston.error("bot is null for: " + botId + " on projectId: " + projectId);
21
22
  }
22
23
  return machine;
23
24
  }
24
25
 
25
26
  static getBackupMachine(bot, botId, projectId, log) {
26
27
  let machine;
27
- if (log) {console.log("Setting MongodbIntentsMachine as Backup Intents Machine on bot:", JSON.stringify(bot));}
28
+ winston.verbose("(IntentsMachineFactory) Setting MongodbIntentsMachine as Backup Intents Machine on bot:", JSON.stringify(bot));
28
29
  machine = new MongodbIntentsMachine({projectId: projectId, language: bot.language, log});
29
30
  return machine;
30
31
  }
@@ -1,5 +1,6 @@
1
- let Faq = require('./faq');
2
- let Faq_kb = require('./faq_kb');
1
+ let Faq = require('../models/faq');
2
+ let Faq_kb = require('../models/faq_kb');
3
+ const winston = require('../utils/winston');
3
4
 
4
5
  class MongodbBotsDataSource {
5
6
 
@@ -22,51 +23,33 @@ class MongodbBotsDataSource {
22
23
  async getBotByIdCache(botId, tdcache) {
23
24
  let bot = null;
24
25
  if (tdcache) {
25
- // console.log("getBotByIdCache cache ok");
26
26
  let botCacheKey = "cacheman:cachegoose-cache:faq_kbs:id:" + botId;
27
27
  try {
28
28
  let _bot_as_string = await tdcache.get(botCacheKey);
29
29
  const value_type = typeof _bot_as_string;
30
- // console.log("__bot_as_string found in chache:", _bot_as_string);
31
- if (this.log) {
32
- console.log("__bot_as_string found in chache:", _bot_as_string);
33
- console.log("value_type:", value_type);
34
- }
30
+ winston.debug("(MongodbBotsDataSource) _bot_as_string found in chache: " + _bot_as_string);
31
+ winston.debug("(MongodbBotsDataSource) value_type: " + value_type);
35
32
  if (_bot_as_string) {
36
33
  bot = JSON.parse(_bot_as_string);
37
- // console.log("got bot from cache:", JSON.stringify(bot));
38
- if (this.log) {
39
- console.log("got bot from cache:", JSON.stringify(bot));
40
- }
34
+ winston.debug("(MongodbBotsDataSource) got bot from cache: ", bot);
41
35
  }
42
36
  else {
43
- // console.log("bot not found, getting from datasource...");
44
- if (this.log) {
45
- console.log("bot not found, getting from datasource...");
46
- }
37
+ winston.debug("(MongodbBotsDataSource) bot not found, getting from datasource...");
47
38
  bot = await this.getBotById(botId);
48
- // console.log("bot found in datasource:", JSON.stringify(bot));
49
- if (this.log) {
50
- console.log("bot found in datasource:", JSON.stringify(bot));
51
- }
39
+ winston.debug("(MongodbBotsDataSource) bot found in datasource: ", bot);
52
40
  await tdcache.set(botCacheKey, JSON.stringify(bot));
53
41
  // DEBUG CODE REMOVE
54
42
  // let bot_ = await tdcache.get(botCacheKey);
55
- // console.log("_bot_as_string from cache debug:", bot_)
56
43
  }
57
44
  }
58
45
  catch(err) {
59
- console.error("error getting bot by id:", err);
46
+ winston.error("(MongodbBotsDataSource) Error getting bot by id: ", err);
60
47
  }
61
48
  }
62
49
  else {
63
- // if (this.log) {
64
- console.log("no chache. getting bot from datasource...");
65
- // }
50
+ winston.verbose("(MongodbBotsDataSource) No chache. getting bot from datasource...");
66
51
  bot = await this.getBotById(botId);
67
- // if (this.log) {
68
- console.log("bot found in datasource:", JSON.stringify(bot));
69
- // }
52
+ winston.debug("(MongodbBotsDataSource) bot found in datasource: ", bot);
70
53
  }
71
54
  return bot;
72
55
  }
@@ -83,11 +66,11 @@ class MongodbBotsDataSource {
83
66
  let query = { "id_faq_kb": botId, "question": text };
84
67
  Faq.find(query).lean().exec(async (err, faqs) => {
85
68
  if (err) {
86
- console.error("Error getting faq object.", err);
69
+ winston.error("(MongodbBotsDataSource) Error getting faq object: ", err);
87
70
  reject(err);
88
71
  }
89
72
  else if (faqs && faqs.length > 0 && faqs[0].answer) {
90
- if (this.log) {console.log("EXACT MATCH OR ACTION FAQ:", faqs);}
73
+ winston.debug("(MongodbBotsDataSource) Exact match or action Faq: ", faqs);
91
74
  resolve(faqs);
92
75
  }
93
76
  else {
@@ -103,34 +86,34 @@ class MongodbBotsDataSource {
103
86
  * @returns a single Intent
104
87
  */
105
88
  async getByIntentDisplayName(botId, key) {
106
- if (this.log) {console.log("Quering intent by botId:", botId, "key:", key );}
89
+ winston.debug("(MongodbBotsDataSource) Quering intent by botId: " + botId + " key: " + key );
107
90
  return new Promise((resolve, reject) => {
108
91
  // var query = { "id_project": this.projectId, "id_faq_kb": botId, "intent_display_name": name};
109
92
  let query = null;
110
93
  key = key.trim();
111
94
  if (key.startsWith("#")) {
112
95
  let intent_id = key.substring(key.indexOf("#") + 1);
113
- if (this.log) {console.log("Query by intent_id:", intent_id );}
96
+ winston.debug("(MongodbBotsDataSource)Query by intent_id: " + intent_id );
114
97
  query = { "id_faq_kb": botId, "intent_id": intent_id };
115
98
  }
116
99
  else {
117
- if (this.log) {console.log("Query by intent name:", key);}
100
+ winston.debug("(MongodbBotsDataSource) Query by intent name: " + key);
118
101
  query = { "id_faq_kb": botId, "intent_display_name": key };
119
102
  }
120
- if (this.log) {console.debug('query', query);}
103
+ winston.debug("(MongodbBotsDataSource) query", query);
121
104
  Faq.find(query).lean().exec( (err, faqs) => {
122
105
  if (err) {
123
- console.error("error getting faqs", err);
106
+ winston.error("(MongodbBotsDataSource) Error getting faqs ", err);
124
107
  return reject(err);
125
108
  }
126
- if (this.log) {console.debug("getByIntentDisplayName faqs", JSON.stringify(faqs));}
109
+ winston.debug("(MongodbBotsDataSource) getByIntentDisplayName faqs ", faqs);
127
110
  if (faqs && faqs.length > 0) {
128
111
  const intent = faqs[0];
129
- if (this.log) {console.debug("intent found:", JSON.stringify(intent));}
112
+ winston.debug("(MongodbBotsDataSource) intent found: ", intent);
130
113
  return resolve(intent);
131
114
  }
132
115
  else {
133
- if (this.log) {console.debug("No intent found");}
116
+ winston.verbose("(MongodbBotsDataSource) No intent found");
134
117
  return resolve(null);
135
118
  }
136
119
  });
@@ -139,29 +122,23 @@ class MongodbBotsDataSource {
139
122
 
140
123
  async getByIntentDisplayNameCache(botId, key, tdcache) {
141
124
  let faq = null;
142
- if (this.log) {
143
- console.log("botID: -" + botId + "-");
144
- console.log("key: -" + key + "-");
145
- }
125
+ winston.verbose("(MongodbBotsDataSource) botID: -" + botId + "-");
126
+ winston.verbose("(MongodbBotsDataSource) key: -" + key + "-");
146
127
  if (tdcache) {
147
- if (this.log) {console.log("in tdcache");}
148
128
  let faqCacheKey = "cacheman:cachegoose-cache:faqs:botid:"+ botId + ":faq:id:" + key;
149
- if (this.log) {console.log("Looking in cache for: -" + faqCacheKey + "-");}
129
+ winston.debug("(MongodbBotsDataSource) Looking in cache for: -" + faqCacheKey + "-");
150
130
  try {
151
131
  let _faq_as_string = await tdcache.get(faqCacheKey);
152
132
  const value_type = typeof _faq_as_string;
153
- if (this.log) {
154
- console.log("_faq_as_string found in chache:", _faq_as_string);
155
- console.log("value_type:", value_type);
156
- }
133
+ winston.debug("(MongodbBotsDataSource)_faq_as_string found in chache: " + _faq_as_string);
134
+ winston.debug("(MongodbBotsDataSource)value_type: " + value_type);
157
135
  if (_faq_as_string) {
158
136
  faq = JSON.parse(_faq_as_string);
159
- if (this.log) {console.log("got faq from cache:", JSON.stringify(faq));}
137
+ winston.debug("(MongodbBotsDataSource) Got faq from cache: ", faq);
160
138
  }
161
139
  else {
162
- //console.log("faq not found, getting from datasource...");
163
140
  faq = await this.getByIntentDisplayName(botId, key);
164
- if (this.log) {console.log(".faq found in datasource:", JSON.stringify(faq));}
141
+ winston.debug("(MongodbBotsDataSource) faq found in datasource:", faq);
165
142
  await tdcache.set(
166
143
  faqCacheKey,
167
144
  JSON.stringify(faq),
@@ -169,17 +146,16 @@ class MongodbBotsDataSource {
169
146
  );
170
147
  // DEBUG CODE REMOVE
171
148
  // let faq_ = await tdcache.get(faqCacheKey);
172
- // console.log("_faq_as_string from cache debug:", faq_)
173
149
  }
174
150
  }
175
151
  catch(err) {
176
- console.error("error getting faq by id:", err);
152
+ winston.error("(MongodbBotsDataSource) Error getting faq by id: ", err);
177
153
  }
178
154
  }
179
155
  else {
180
- if (this.log) {console.log("no chache. getting faq from datasource...");}
156
+ winston.debug("(MongodbBotsDataSource) No chache. Getting faq from datasource...");
181
157
  faq = await this.getByIntentDisplayName(botId, key);
182
- if (this.log) {console.log("faq found in datasource (no-cache):", JSON.stringify(faq));}
158
+ winston.debug("(MongodbBotsDataSource) Faq found in datasource (no-cache): ", faq);
183
159
  }
184
160
  return faq;
185
161
  }
@@ -1,5 +1,6 @@
1
1
  let mongoose = require('mongoose');
2
- let Faq = require('./faq');
2
+ let Faq = require('../models/faq');
3
+ const winston = require('../utils/winston');
3
4
 
4
5
  class MongodbIntentsMachine {
5
6
 
@@ -19,14 +20,14 @@ class MongodbIntentsMachine {
19
20
  */
20
21
  async decode(botId, text) {
21
22
  return new Promise( (resolve, reject) => {
22
- if (this.log) {console.log("Mongodb NLP decode intent...");}
23
+ winston.debug("(MongodbIntentsMachine) Mongodb NLP decode intent...");
23
24
  // let query = { "id_project": this.projectId, "id_faq_kb": botId };
24
25
  let query = { "id_faq_kb": botId };
25
26
  var mongoproject = undefined;
26
27
  var sort = undefined;
27
28
  var search_obj = { "$search": text };
28
29
 
29
- if (this.log) {console.log("chatbot lang:", this.language);}
30
+ winston.debug("(MongodbIntentsMachine) chatbot lang: " + this.language);
30
31
  if (this.language) {
31
32
  search_obj["$language"] = this.language;
32
33
  }
@@ -35,9 +36,9 @@ class MongodbIntentsMachine {
35
36
  sort = { score: { $meta: "textScore" } }
36
37
  // DA QUI RECUPERO LA RISPOSTA DATO (ID: SE EXT_AI) (QUERY FULLTEXT SE NATIVE-BASIC-AI)
37
38
  Faq.find(query, mongoproject).sort(sort).lean().exec( (err, faqs) => {
38
- if (this.log) {console.log("Found:", faqs);}
39
+ winston.debug("(MongodbIntentsMachine) Found: ", faqs);
39
40
  if (err) {
40
- console.error("Error:", err);
41
+ winston.error("(MongodbIntentsMachine) Error:", err);
41
42
  }
42
43
  if (faqs && faqs.length > 0) {
43
44
  resolve(faqs);