@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,9 +1,12 @@
1
1
  const axios = require("axios").default;
2
- const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
2
+ const { TiledeskChatbot } = require('../../engine/TiledeskChatbot');
3
3
  const { Filler } = require('../Filler');
4
4
  let https = require("https");
5
5
  const { DirIntent } = require("./DirIntent");
6
6
  require('dotenv').config();
7
+ const winston = require('../../utils/winston');
8
+ const httpUtils = require("../../utils/HttpUtils");
9
+ const integrationService = require("../../services/IntegrationService");
7
10
 
8
11
  class DirAskGPT {
9
12
 
@@ -14,19 +17,21 @@ class DirAskGPT {
14
17
  this.context = context;
15
18
  this.tdcache = this.context.tdcache;
16
19
  this.requestId = this.context.requestId;
20
+ this.projectId = this.context.projectId;
21
+ this.token = this.context.token;
17
22
  this.intentDir = new DirIntent(context);
18
23
  this.API_ENDPOINT = this.context.API_ENDPOINT;
19
24
  this.log = context.log;
20
25
  }
21
26
 
22
27
  execute(directive, callback) {
23
- if (this.log) { console.log("AskGPT directive: ", directive); }
28
+ winston.verbose("Execute AskGPT directive");
24
29
  let action;
25
30
  if (directive.action) {
26
31
  action = directive.action;
27
32
  }
28
33
  else {
29
- console.error("Incorrect directive: ", JSON.stringify(directive));
34
+ winston.warn("DirAskGPT Incorrect directive: ", directive);
30
35
  callback();
31
36
  return;
32
37
  }
@@ -36,9 +41,9 @@ class DirAskGPT {
36
41
  }
37
42
 
38
43
  async go(action, callback) {
39
- if (this.log) { console.log("DirAskGPT action:", JSON.stringify(action)); }
44
+ winston.debug("(DirAskGPT) Action: ", action);
40
45
  if (!this.tdcache) {
41
- console.error("Error: DirAskGPT tdcache is mandatory");
46
+ winston.error("Error: DirAskGPT tdcache is mandatory");
42
47
  callback();
43
48
  return;
44
49
  }
@@ -49,19 +54,17 @@ class DirAskGPT {
49
54
  let trueIntentAttributes = action.trueIntentAttributes;
50
55
  let falseIntentAttributes = action.falseIntentAttributes;
51
56
 
52
- if (this.log) {
53
- console.log("DirAskGPT trueIntent", trueIntent)
54
- console.log("DirAskGPT falseIntent", falseIntent)
55
- console.log("DirAskGPT trueIntentAttributes", trueIntentAttributes)
56
- console.log("DirAskGPT falseIntentAttributes", falseIntentAttributes)
57
- }
57
+ winston.debug("(DirAskGPT) trueIntent " + trueIntent)
58
+ winston.debug("(DirAskGPT) falseIntent " + falseIntent)
59
+ winston.debug("(DirAskGPT) trueIntentAttributes " + trueIntentAttributes)
60
+ winston.debug("(DirAskGPT) falseIntentAttributes " + falseIntentAttributes)
58
61
 
59
62
  // default values
60
63
  let answer = "No answers";
61
64
  let source = null;
62
65
 
63
66
  if (!action.question || action.question === '') {
64
- console.error("Error: DirAskGPT question attribute is mandatory. Executing condition false...");
67
+ winston.error("(DirAskGPT) Error: question attribute is mandatory. Executing condition false...");
65
68
  await this.#assignAttributes(action, answer, source);
66
69
  if (falseIntent) {
67
70
  await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
@@ -71,7 +74,7 @@ class DirAskGPT {
71
74
  }
72
75
 
73
76
  if (!action.kbid) {
74
- console.error("Error: DirAskGPT kbid attribute is mandatory. Executing condition false...");
77
+ winston.error("(DirAskGPT) Error: kbid attribute is mandatory. Executing condition false...");
75
78
  await this.#assignAttributes(action, answer, source);
76
79
  if (falseIntent) {
77
80
  await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes)
@@ -90,22 +93,22 @@ class DirAskGPT {
90
93
  const filled_question = filler.fill(action.question, requestVariables);
91
94
 
92
95
  const kb_endpoint = process.env.KB_ENDPOINT;
93
- if (this.log) { console.log("DirAskGPT KbEndpoint URL: ", kb_endpoint); }
96
+ winston.verbose("DirAskGPT KbEndpoint URL: ", kb_endpoint);
94
97
 
95
- let key = await this.getKeyFromIntegrations();
98
+ let key = await integrationService.getKeyFromIntegrations(this.projectId, 'openai', this.token);
96
99
  if (!key) {
97
- if (this.log) { console.log("DirAskGPT - Key not found in Integrations. Searching in kb settings..."); }
100
+ winston.debug("(DirAskGPT) - Key not found in Integrations. Searching in kb settings...");
98
101
  key = await this.getKeyFromKbSettings();
99
102
  }
100
103
 
101
104
  if (!key) {
102
- if (this.log) { console.log("DirAskGPT - Retrieve public gptkey")}
105
+ winston.debug("(DirAskGPT) - Retrieve public gptkey")
103
106
  key = process.env.GPTKEY;
104
107
  publicKey = true;
105
108
  }
106
109
 
107
110
  if (!key) {
108
- console.error("Error: DirAskGPT gptkey is mandatory");
111
+ winston.error("(DirAskGPT) Error: gptkey is mandatory");
109
112
  await this.#assignAttributes(action, answer);
110
113
  if (falseIntent) {
111
114
  await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
@@ -119,7 +122,7 @@ class DirAskGPT {
119
122
  if (publicKey === true) {
120
123
  let keep_going = await this.checkQuoteAvailability();
121
124
  if (keep_going === false) {
122
- if (this.log) { console.log("DirAskGPT - Quota exceeded for tokens. Skip the action")}
125
+ winston.debug("(DirAskGPT) - Quota exceeded for tokens. Skip the action")
123
126
  callback();
124
127
  return;
125
128
  }
@@ -130,26 +133,25 @@ class DirAskGPT {
130
133
  kbid: action.kbid,
131
134
  gptkey: key
132
135
  };
133
- if (this.log) { console.log("DirAskGPT json:", json); }
136
+ winston.debug("(DirAskGPT)DirAskGPT json:", json);
134
137
 
135
138
  const HTTPREQUEST = {
136
139
  url: kb_endpoint + "/qa",
137
140
  json: json,
138
141
  method: "POST"
139
142
  }
140
- if (this.log) { console.log("DirAskGPT HTTPREQUEST", HTTPREQUEST); }
143
+ winston.debug("(DirAskGPT) HttpRequest", HTTPREQUEST);
141
144
 
142
- this.#myrequest(
145
+ httpUtils.request(
143
146
  HTTPREQUEST, async (err, resbody) => {
144
- if (this.log && err) {
145
- console.log("DirAskGPT error: ", err);
146
- }
147
- if (this.log) { console.log("DirAskGPT resbody:", resbody); }
147
+
148
+ winston.debug("(DirAskGPT) resbody:", resbody);
148
149
  let answer = resbody.answer;
149
150
  let source = resbody.source_url;
150
151
  await this.#assignAttributes(action, answer, source);
151
-
152
+
152
153
  if (err) {
154
+ winston.error("(DirAskGPT) error: ", err);
153
155
  if (callback) {
154
156
  if (falseIntent) {
155
157
  await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
@@ -205,7 +207,7 @@ class DirAskGPT {
205
207
  })
206
208
  }
207
209
  else {
208
- if (this.log) { console.log("No trueIntentDirective specified"); }
210
+ winston.debug("(DirAskGPT) No trueIntentDirective specified");
209
211
  if (callback) {
210
212
  callback();
211
213
  }
@@ -220,7 +222,7 @@ class DirAskGPT {
220
222
  });
221
223
  }
222
224
  else {
223
- if (this.log) { console.log("No falseIntentDirective specified"); }
225
+ winston.debug("(DirAskGPT) No falseIntentDirective specified");
224
226
  if (callback) {
225
227
  callback();
226
228
  }
@@ -229,109 +231,17 @@ class DirAskGPT {
229
231
  }
230
232
 
231
233
  async #assignAttributes(action, answer, source) {
232
- if (this.log) {
233
- console.log("assignAttributes action:", action)
234
- console.log("assignAttributes answer:", answer)
235
- console.log("assignAttributes source:", source)
236
- }
234
+ winston.debug("(DirAskGPT) assignAttributes action:", action)
235
+ winston.debug("(DirAskGPT) assignAttributes answer:", answer)
236
+ winston.debug("(DirAskGPT) assignAttributes source:", source)
237
237
  if (this.context.tdcache) {
238
238
  if (action.assignReplyTo && answer) {
239
239
  await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignReplyTo, answer);
240
240
  }
241
- // console.log("--> action.assignSourceTo: ", action.assignSourceTo)
242
- // console.log("--> source: ", source)
243
241
  if (action.assignSourceTo && source) {
244
- // console.log("--> source: ", source)
245
242
  await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignSourceTo, source);
246
243
  }
247
- // Debug log
248
- if (this.log) {
249
- const all_parameters = await TiledeskChatbot.allParametersStatic(this.context.tdcache, this.context.requestId);
250
- for (const [key, value] of Object.entries(all_parameters)) {
251
- if (this.log) { console.log("(askgpt) request parameter:", key, "value:", value, "type:", typeof value) }
252
- }
253
- }
254
- }
255
- }
256
-
257
- #myrequest(options, callback) {
258
- if (this.log) {
259
- console.log("API URL:", options.url);
260
- console.log("** Options:", JSON.stringify(options));
261
- }
262
- let axios_options = {
263
- url: options.url,
264
- method: options.method,
265
- params: options.params,
266
- headers: options.headers
267
244
  }
268
- if (options.json !== null) {
269
- axios_options.data = options.json
270
- }
271
- if (this.log) {
272
- console.log("axios_options:", JSON.stringify(axios_options));
273
- }
274
- if (options.url.startsWith("https:")) {
275
- const httpsAgent = new https.Agent({
276
- rejectUnauthorized: false,
277
- });
278
- axios_options.httpsAgent = httpsAgent;
279
- }
280
- axios(axios_options)
281
- .then((res) => {
282
- if (this.log) {
283
- console.log("Response for url:", options.url);
284
- console.log("Response headers:\n", JSON.stringify(res.headers));
285
- }
286
- if (res && res.status == 200 && res.data) {
287
- if (callback) {
288
- callback(null, res.data);
289
- }
290
- }
291
- else {
292
- if (callback) {
293
- callback(new Error("Response status is not 200"), null);
294
- }
295
- }
296
- })
297
- .catch((error) => {
298
- console.error("(DirAskGPT) Axios error: ", JSON.stringify(error));
299
- if (callback) {
300
- callback(error, null);
301
- }
302
- });
303
- }
304
-
305
- async getKeyFromIntegrations() {
306
- return new Promise((resolve) => {
307
-
308
- const INTEGRATIONS_HTTPREQUEST = {
309
- url: this.API_ENDPOINT + "/" + this.context.projectId + "/integration/name/openai",
310
- headers: {
311
- 'Content-Type': 'application/json',
312
- 'Authorization': 'JWT ' + this.context.token
313
- },
314
- method: "GET"
315
- }
316
- if (this.log) { console.log("DirAskGPT INTEGRATIONS_HTTPREQUEST ", INTEGRATIONS_HTTPREQUEST) }
317
-
318
- this.#myrequest(
319
- INTEGRATIONS_HTTPREQUEST, async (err, integration) => {
320
- if (err) {
321
- if (this.log) { console.error("DirAskGPT Get integrations error ", err); }
322
- resolve(null);
323
- } else {
324
-
325
- if (integration &&
326
- integration.value) {
327
- resolve(integration.value.apikey)
328
- }
329
- else {
330
- resolve(null)
331
- }
332
- }
333
- })
334
- })
335
245
  }
336
246
 
337
247
  async getKeyFromKbSettings() {
@@ -345,12 +255,12 @@ class DirAskGPT {
345
255
  },
346
256
  method: "GET"
347
257
  }
348
- if (this.log) { console.log("DirAskGPT KB_HTTPREQUEST", KB_HTTPREQUEST); }
258
+ winston.debug("(DirAskGPT) KB HttpRequest ", KB_HTTPREQUEST);
349
259
 
350
- this.#myrequest(
260
+ httpUtils.request(
351
261
  KB_HTTPREQUEST, async (err, resbody) => {
352
262
  if (err) {
353
- if (this.log) { console.error("DirAskGPT Get kb settings error ", err); }
263
+ winston.error("DirAskGPT Get kb settings error ", err?.response?.data);
354
264
  resolve(null);
355
265
  } else {
356
266
  if (!resbody.gptkey) {