@tiledesk/tiledesk-tybot-connector 0.5.0 → 0.5.1-rc1

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 (102) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/ExtApi.js +4 -83
  3. package/ExtUtil.js +0 -1
  4. package/Logger.js +113 -0
  5. package/TdCache.js +5 -3
  6. package/TiledeskClientTest.js +5 -28
  7. package/TiledeskExpression.js +6 -47
  8. package/{models → engine}/IntentForm.js +30 -44
  9. package/{models → engine}/IntentsMachineFactory.js +5 -4
  10. package/{models → engine}/MongodbBotsDataSource.js +31 -55
  11. package/{models → engine}/MongodbIntentsMachine.js +6 -5
  12. package/{models → engine}/TiledeskChatbot.js +82 -259
  13. package/{models → engine}/TiledeskChatbotConst.js +9 -0
  14. package/{models → engine}/TiledeskIntentsMachine.js +5 -15
  15. package/{models → engine/mock}/MockBotsDataSource.js +2 -19
  16. package/{models → engine/mock}/MockTdCache.js +0 -9
  17. package/index.js +106 -380
  18. package/logs/app.log +11557 -277
  19. package/logs/app1.log +62258 -0
  20. package/logs/app2.log +46280 -0
  21. package/logs/app3.log +31686 -0
  22. package/logs/app4.log +46163 -0
  23. package/logs/app5.log +33173 -0
  24. package/models/faq.js +2 -5
  25. package/package.json +5 -2
  26. package/{TiledeskServices → services}/AIService.js +4 -3
  27. package/{models/faqKbService.js → services/FaqKbService.js} +1 -2
  28. package/{models/faqService.js → services/FaqService.js} +4 -3
  29. package/services/IntegrationService.js +43 -0
  30. package/services/TilebotService.js +47 -0
  31. package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +68 -142
  32. package/tiledeskChatbotPlugs/FillParamsChatbotPlug.js +8 -14
  33. package/tiledeskChatbotPlugs/Filler.js +0 -1
  34. package/tiledeskChatbotPlugs/MarkbotChatbotPlug.js +12 -20
  35. package/tiledeskChatbotPlugs/MessagePipeline.js +5 -15
  36. package/tiledeskChatbotPlugs/SplitsChatbotPlug.js +4 -13
  37. package/tiledeskChatbotPlugs/TiledeskRequestVariables.js +1 -1
  38. package/tiledeskChatbotPlugs/WebhookChatbotPlug.js +21 -89
  39. package/tiledeskChatbotPlugs/directives/DEPRECATED_DirSetAttribute.js +1 -1
  40. package/tiledeskChatbotPlugs/directives/DirAddTags.js +23 -140
  41. package/tiledeskChatbotPlugs/directives/DirAiPrompt.js +42 -128
  42. package/tiledeskChatbotPlugs/directives/DirAskGPT.js +37 -127
  43. package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +52 -145
  44. package/tiledeskChatbotPlugs/directives/DirAssign.js +12 -23
  45. package/tiledeskChatbotPlugs/directives/DirAssignFromFunction.js +16 -12
  46. package/tiledeskChatbotPlugs/directives/DirAssistant.js +56 -198
  47. package/tiledeskChatbotPlugs/directives/DirBrevo.js +41 -101
  48. package/tiledeskChatbotPlugs/directives/DirCaptureUserReply.js +9 -19
  49. package/tiledeskChatbotPlugs/directives/DirClearTranscript.js +1 -1
  50. package/tiledeskChatbotPlugs/directives/DirClose.js +4 -2
  51. package/tiledeskChatbotPlugs/directives/DirCode.js +16 -23
  52. package/tiledeskChatbotPlugs/directives/DirCondition.js +19 -25
  53. package/tiledeskChatbotPlugs/directives/DirConnectBlock.js +9 -82
  54. package/tiledeskChatbotPlugs/directives/DirContactUpdate.js +10 -22
  55. package/tiledeskChatbotPlugs/directives/DirCustomerio.js +34 -89
  56. package/tiledeskChatbotPlugs/directives/DirDeflectToHelpCenter.js +17 -22
  57. package/tiledeskChatbotPlugs/directives/DirDeleteVariable.js +8 -8
  58. package/tiledeskChatbotPlugs/directives/DirDepartment.js +13 -12
  59. package/tiledeskChatbotPlugs/directives/DirDisableInputText.js +2 -6
  60. package/tiledeskChatbotPlugs/directives/DirFireTiledeskEvent.js +4 -4
  61. package/tiledeskChatbotPlugs/directives/DirForm.js +17 -24
  62. package/tiledeskChatbotPlugs/directives/DirGptTask.js +49 -139
  63. package/tiledeskChatbotPlugs/directives/DirGptTask_OLD.js +8 -53
  64. package/tiledeskChatbotPlugs/directives/DirHubspot.js +31 -91
  65. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgents.js +19 -26
  66. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgentsV2.js +24 -78
  67. package/tiledeskChatbotPlugs/directives/DirIfOpenHours.js +17 -68
  68. package/tiledeskChatbotPlugs/directives/DirIntent.js +12 -104
  69. package/tiledeskChatbotPlugs/directives/DirJSONCondition.js +15 -14
  70. package/tiledeskChatbotPlugs/directives/DirLockIntent.js +7 -12
  71. package/tiledeskChatbotPlugs/directives/DirMake.js +21 -53
  72. package/tiledeskChatbotPlugs/directives/DirMessage.js +35 -28
  73. package/tiledeskChatbotPlugs/directives/DirMessageToBot.js +8 -87
  74. package/tiledeskChatbotPlugs/directives/DirMoveToAgent.js +6 -4
  75. package/tiledeskChatbotPlugs/directives/DirMoveToUnassigned.js +6 -4
  76. package/tiledeskChatbotPlugs/directives/DirQapla.js +26 -120
  77. package/tiledeskChatbotPlugs/directives/DirRandomReply.js +11 -19
  78. package/tiledeskChatbotPlugs/directives/DirRemoveCurrentBot.js +4 -0
  79. package/tiledeskChatbotPlugs/directives/DirReplaceBot.js +5 -2
  80. package/tiledeskChatbotPlugs/directives/DirReplaceBotV2.js +12 -58
  81. package/tiledeskChatbotPlugs/directives/DirReplaceBotV3.js +11 -57
  82. package/tiledeskChatbotPlugs/directives/DirReply.js +34 -37
  83. package/tiledeskChatbotPlugs/directives/DirReplyV2.js +30 -72
  84. package/tiledeskChatbotPlugs/directives/DirSendEmail.js +9 -10
  85. package/tiledeskChatbotPlugs/directives/DirSendWhatsapp.js +15 -40
  86. package/tiledeskChatbotPlugs/directives/DirSetAttribute.js +8 -5
  87. package/tiledeskChatbotPlugs/directives/DirSetAttributeV2.js +25 -113
  88. package/tiledeskChatbotPlugs/directives/DirSetConversationTags.js +14 -13
  89. package/tiledeskChatbotPlugs/directives/DirUnlockIntent.js +3 -3
  90. package/tiledeskChatbotPlugs/directives/DirWait.js +5 -6
  91. package/tiledeskChatbotPlugs/directives/DirWebRequest.js +14 -49
  92. package/tiledeskChatbotPlugs/directives/DirWebRequestV2.js +22 -77
  93. package/tiledeskChatbotPlugs/directives/DirWebRequestV2_old.js +1 -1
  94. package/tiledeskChatbotPlugs/directives/DirWebResponse.js +23 -64
  95. package/tiledeskChatbotPlugs/directives/DirWhatsappByAttribute.js +13 -42
  96. package/tiledeskChatbotPlugs/directives/Directives.js +1 -3
  97. package/utils/HttpUtils.js +128 -0
  98. package/{models → utils}/TiledeskChatbotUtil.js +140 -234
  99. package/utils/winston.js +42 -0
  100. package/TdCache copy.js +0 -242
  101. package/TiledeskServices/utils.js +0 -99
  102. /package/{models → engine/mock}/MockIntentsMachine.js +0 -0
@@ -1,14 +1,16 @@
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
- const { TiledeskChatbotConst } = require("../../models/TiledeskChatbotConst");
7
- const { TiledeskChatbotUtil } = require("../../models/TiledeskChatbotUtil");
6
+ const { TiledeskChatbotConst } = require("../../engine/TiledeskChatbotConst");
7
+ const { TiledeskChatbotUtil } = require("../../utils/TiledeskChatbotUtil");
8
8
  const req = require("express/lib/request");
9
9
  const { update } = require("../../models/faq");
10
10
  const { TiledeskClient } = require("@tiledesk/tiledesk-client");
11
11
  require('dotenv').config();
12
+ const winston = require('../../utils/winston');
13
+ const httpUtils = require("../../utils/HttpUtils");
12
14
 
13
15
  class DirAddTags {
14
16
 
@@ -33,13 +35,13 @@ class DirAddTags {
33
35
  }
34
36
 
35
37
  execute(directive, callback) {
36
- if (this.log) { console.log("AddTags directive: ", directive); }
38
+ winston.verbose("Execute AddTags directive");
37
39
  let action;
38
40
  if (directive.action) {
39
41
  action = directive.action;
40
42
  }
41
43
  else {
42
- console.error("Incorrect directive: ", JSON.stringify(directive));
44
+ winston.warn("Incorrect directive: ", directive);
43
45
  callback();
44
46
  return;
45
47
  }
@@ -49,9 +51,9 @@ class DirAddTags {
49
51
  }
50
52
 
51
53
  async go(action, callback) {
52
- if (this.log) { console.log("(DirAddTags) action:", JSON.stringify(action)); }
54
+ winston.debug("(DirAddTags) Action: ", action);
53
55
  if (!this.tdcache) {
54
- console.error("Error: (DirAddTags) tdcache is mandatory");
56
+ winston.error("(DirAddTags) Error: tdcache is mandatory");
55
57
  callback();
56
58
  return;
57
59
  }
@@ -64,7 +66,7 @@ class DirAddTags {
64
66
  pushToList = action.pushToList
65
67
 
66
68
  if (!action.tags || action.tags === '') {
67
- console.error("Error: (DirAddTags) tags attribute is mandatory")
69
+ winston.error("(DirAddTags) Error: tags attribute is mandatory")
68
70
  await this.chatbot.addParameter("flowError", "Add tags Error: tags attribute is mandatory");
69
71
  callback();
70
72
  return;
@@ -78,18 +80,7 @@ class DirAddTags {
78
80
 
79
81
  const filler = new Filler();
80
82
  const filled_tags = filler.fill(action.tags, requestVariables);
81
-
82
- if (this.log) {
83
- console.log("(DirAddTags) tags: ", filled_tags);
84
- }
85
-
86
- // let request = await this.tdClient.getRequestById(this.requestId);
87
- // if (this.log) { console.log('(DirAddTags) request detail: ', request) }
88
- // if(!request){
89
- // if (this.log) { console.log("(DirAddTags) - request not found for request_id:", this.requestId); }
90
- // callback();
91
- // return;
92
- // }
83
+ winston.debug("(DirAddTags) filled_tags: ", filled_tags);
93
84
 
94
85
  /** use case: CONVERSATION */
95
86
  if(target === 'request'){
@@ -106,7 +97,7 @@ class DirAddTags {
106
97
  })
107
98
  }
108
99
 
109
- if (this.log) { console.log('(DirAddTags) UPDATE request with newTags', newTags) }
100
+ winston.debug('(DirAddTags) UPDATE request with newTags', newTags)
110
101
  let updatedRequest = await this.updateRequestWithTags(newTags)
111
102
  if(!updatedRequest){
112
103
  callback();
@@ -120,9 +111,9 @@ class DirAddTags {
120
111
  let newTags = filled_tags.split(',').filter(tag => tag !== '').map(el => el.trim())
121
112
 
122
113
  let request = await this.tdClient.getRequestById(this.requestId);
123
- if (this.log) { console.log('(DirAddTags) request detail: ', request) }
114
+ winston.debug('(DirAddTags) request detail: ', request)
124
115
  if(!request){
125
- if (this.log) { console.log("(DirAddTags) - request not found for request_id:", this.requestId); }
116
+ winston.debug("(DirAddTags) - request not found for request_id: " + this.requestId);
126
117
  callback();
127
118
  return;
128
119
  }
@@ -137,7 +128,7 @@ class DirAddTags {
137
128
  })
138
129
  }
139
130
 
140
- if (this.log) { console.log('(DirAddTags) UPDATE lead with newTags', newTags) }
131
+ winston.debug('(DirAddTags) UPDATE lead with newTags ', newTags)
141
132
  let updatedLead = await this.updateLeadWithTags(request.lead._id, newTags)
142
133
  if(!updatedLead){
143
134
  callback();
@@ -165,113 +156,6 @@ class DirAddTags {
165
156
 
166
157
  }
167
158
 
168
- async #executeCondition(result, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes, callback) {
169
- let trueIntentDirective = null;
170
- if (trueIntent) {
171
- trueIntentDirective = DirIntent.intentDirectiveFor(trueIntent, trueIntentAttributes);
172
- }
173
- let falseIntentDirective = null;
174
- if (falseIntent) {
175
- falseIntentDirective = DirIntent.intentDirectiveFor(falseIntent, falseIntentAttributes);
176
- }
177
- if (result === true) {
178
- if (trueIntentDirective) {
179
- this.intentDir.execute(trueIntentDirective, () => {
180
- if (callback) {
181
- callback();
182
- }
183
- })
184
- }
185
- else {
186
- if (this.log) { console.log("No trueIntentDirective specified"); }
187
- if (callback) {
188
- callback();
189
- }
190
- }
191
- }
192
- else {
193
- if (falseIntentDirective) {
194
- this.intentDir.execute(falseIntentDirective, () => {
195
- if (callback) {
196
- callback();
197
- }
198
- });
199
- }
200
- else {
201
- if (this.log) { console.log("No falseIntentDirective specified"); }
202
- if (callback) {
203
- callback();
204
- }
205
- }
206
- }
207
- }
208
-
209
- async #assignAttributes(action, answer) {
210
- if (this.log) {
211
- console.log("assignAttributes action:", action)
212
- console.log("assignAttributes answer:", answer)
213
- }
214
- if (this.context.tdcache) {
215
- if (action.assignReplyTo && answer) {
216
- await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignReplyTo, answer);
217
- }
218
- // Debug log
219
- if (this.log) {
220
- const all_parameters = await TiledeskChatbot.allParametersStatic(this.context.tdcache, this.context.requestId);
221
- for (const [key, value] of Object.entries(all_parameters)) {
222
- if (this.log) { console.log("(DirAddTags) request parameter:", key, "value:", value, "type:", typeof value) }
223
- }
224
- }
225
- }
226
- }
227
-
228
- #myrequest(options, callback) {
229
- if (this.log) {
230
- console.log("API URL:", options.url);
231
- console.log("** Options:", JSON.stringify(options));
232
- }
233
- let axios_options = {
234
- url: options.url,
235
- method: options.method,
236
- params: options.params,
237
- headers: options.headers
238
- }
239
- if (options.json !== null) {
240
- axios_options.data = options.json
241
- }
242
- if (this.log) {
243
- console.log("axios_options:", JSON.stringify(axios_options));
244
- }
245
- if (options.url.startsWith("https:")) {
246
- const httpsAgent = new https.Agent({
247
- rejectUnauthorized: false,
248
- });
249
- axios_options.httpsAgent = httpsAgent;
250
- }
251
- axios(axios_options)
252
- .then((res) => {
253
- if (this.log) {
254
- console.log("Response for url:", options.url);
255
- console.log("Response headers:\n", JSON.stringify(res.headers));
256
- }
257
- if (res && res.status == 200 && res.data) {
258
- if (callback) {
259
- callback(null, res.data);
260
- }
261
- }
262
- else {
263
- if (callback) {
264
- callback(new Error("Response status is not 200"), null);
265
- }
266
- }
267
- })
268
- .catch((error) => {
269
- if (callback) {
270
- callback(error, null);
271
- }
272
- });
273
- }
274
-
275
159
  async addNewTag(tag){
276
160
  return new Promise((resolve, reject)=> {
277
161
  const HTTPREQUEST = {
@@ -286,10 +170,11 @@ class DirAddTags {
286
170
  color: '#f0806f'
287
171
  }
288
172
  }
289
- this.#myrequest(
173
+
174
+ httpUtils.request(
290
175
  HTTPREQUEST, async (err, resbody) => {
291
176
  if (err) {
292
- console.error("(httprequest) DirAddTags add tags to list err: ", err);
177
+ winston.error("(httprequest) DirAddTags add tags to list err: ", err);
293
178
  resolve(true)
294
179
  } else {
295
180
  if (resbody) {
@@ -309,9 +194,7 @@ class DirAddTags {
309
194
  let json = []
310
195
  let filteredTags = tags.map((tag) => ({tag: tag, color: '#f0806f'}))
311
196
  json.push(...filteredTags)
312
- if (this.log) {
313
- console.log('(httprequest) DirAddTags updateRequestWithTags tags--> ', json)
314
- }
197
+ winston.debug('(httprequest) DirAddTags updateRequestWithTags tags: ', json)
315
198
  const HTTPREQUEST = {
316
199
  url: this.API_ENDPOINT + "/" + this.context.projectId + "/requests/" + this.requestId + '/tag',
317
200
  headers: {
@@ -322,10 +205,10 @@ class DirAddTags {
322
205
  json: json
323
206
  }
324
207
 
325
- this.#myrequest(
208
+ httpUtils.request(
326
209
  HTTPREQUEST, async (err, resbody) => {
327
210
  if (err) {
328
- console.error("(httprequest) DirAddTags patch request with new tags err: ", err);
211
+ winston.error("(httprequest) DirAddTags patch request with new tags err: ", err);
329
212
  resolve(true)
330
213
  } else {
331
214
  if (resbody) {
@@ -351,10 +234,10 @@ class DirAddTags {
351
234
  json: tags
352
235
  }
353
236
 
354
- this.#myrequest(
237
+ httpUtils.request(
355
238
  HTTPREQUEST, async (err, resbody) => {
356
239
  if (err) {
357
- console.error("(httprequest) DirAddTags put lead with new tags err: ", err);
240
+ winston.error("(httprequest) DirAddTags put lead with new tags err: ", err);
358
241
  resolve(true)
359
242
  } else {
360
243
  if (resbody) {
@@ -1,11 +1,17 @@
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
- const { TiledeskChatbotConst } = require("../../models/TiledeskChatbotConst");
7
- const { TiledeskChatbotUtil } = require("../../models/TiledeskChatbotUtil");
6
+ const { TiledeskChatbotConst } = require("../../engine/TiledeskChatbotConst");
7
+ const { TiledeskChatbotUtil } = require("../../utils/TiledeskChatbotUtil");
8
8
  require('dotenv').config();
9
+ const winston = require('../../utils/winston');
10
+ const Utils = require("../../utils/HttpUtils");
11
+ const utils = require("../../utils/HttpUtils");
12
+ const httpUtils = require("../../utils/HttpUtils");
13
+ const integrationService = require("../../services/IntegrationService");
14
+
9
15
 
10
16
  class DirAiPrompt {
11
17
 
@@ -17,19 +23,21 @@ class DirAiPrompt {
17
23
  this.chatbot = this.context.chatbot;
18
24
  this.tdcache = this.context.tdcache;
19
25
  this.requestId = this.context.requestId;
26
+ this.projectId = this.context.projectId;
27
+ this.token = this.context.token;
20
28
  this.intentDir = new DirIntent(context);
21
29
  this.API_ENDPOINT = this.context.API_ENDPOINT;
22
30
  this.log = context.log;
23
31
  }
24
32
 
25
33
  execute(directive, callback) {
26
- if (this.log) { console.log("AiPrompt directive: ", directive); }
34
+ winston.verbose("Execute AiPrompt directive");
27
35
  let action;
28
36
  if (directive.action) {
29
37
  action = directive.action;
30
38
  }
31
39
  else {
32
- console.error("Incorrect directive: ", JSON.stringify(directive));
40
+ winston.debug("DirAiPrompt Incorrect directive: ", directive);
33
41
  callback();
34
42
  return;
35
43
  }
@@ -39,9 +47,9 @@ class DirAiPrompt {
39
47
  }
40
48
 
41
49
  async go(action, callback) {
42
- if (this.log) { console.log("DirAiPrompt action:", JSON.stringify(action)); }
50
+ winston.debug("DirAiPrompt action:", action);
43
51
  if (!this.tdcache) {
44
- console.error("Error: DirAiPrompt tdcache is mandatory");
52
+ winston.error("Error: DirAiPrompt tdcache is mandatory");
45
53
  callback();
46
54
  return;
47
55
  }
@@ -53,10 +61,8 @@ class DirAiPrompt {
53
61
  let transcript;
54
62
  let answer = "No answer"
55
63
 
56
- if (this.log) {
57
- console.log("DirAiPrompt trueIntent", trueIntent)
58
- console.log("DirAiPrompt falseIntent", falseIntent)
59
- }
64
+ winston.debug("DirAskGPTV2 trueIntent", trueIntent)
65
+ winston.debug("DirAskGPTV2 falseIntent", falseIntent)
60
66
 
61
67
  await this.checkMandatoryParameters(action).catch( async (missing_param) => {
62
68
  await this.chatbot.addParameter("flowError", "AiPrompt Error: '" + missing_param + "' attribute is undefined");
@@ -84,23 +90,23 @@ class DirAiPrompt {
84
90
  this.context.tdcache,
85
91
  this.context.requestId,
86
92
  TiledeskChatbotConst.REQ_TRANSCRIPT_KEY);
87
- if (this.log) { console.log("DirAiPrompt transcript string: ", transcript_string) }
93
+ winston.debug("DirAiPrompt transcript string: " + transcript_string)
88
94
 
89
95
  if (transcript_string) {
90
96
  transcript = await TiledeskChatbotUtil.transcriptJSON(transcript_string);
91
- if (this.log) { console.log("DirAiPrompt transcript: ", transcript) }
97
+ winston.debug("DirAiPrompt transcript: ", transcript)
92
98
  } else {
93
- if (this.log) { console.log("DirAiPrompt transcript_string is undefined. Skip JSON translation for chat history") }
99
+ winston.verbose("DirAiPrompt transcript_string is undefined. Skip JSON translation for chat history")
94
100
  }
95
101
  }
96
102
 
97
103
  const AI_endpoint = process.env.AI_ENDPOINT
98
- if (this.log) { console.log("DirAiPrompt AI_endpoint ", AI_endpoint); }
104
+ winston.verbose("DirAiPrompt AI_endpoint " + AI_endpoint);
99
105
 
100
- let key = await this.getKeyFromIntegrations(action.llm);
106
+ let key = await integrationService.getKeyFromIntegrations(this.projectId, action.llm, this.token);
101
107
 
102
108
  if (!key) {
103
- console.error("Error: DirAiPrompt llm key not found in integrations");
109
+ winston.error("Error: DirAiPrompt llm key not found in integrations");
104
110
  await this.chatbot.addParameter("flowError", "AiPrompt Error: missing key for llm " + action.llm);
105
111
  if (falseIntent) {
106
112
  await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
@@ -127,7 +133,7 @@ class DirAiPrompt {
127
133
  json.chat_history_dict = await this.transcriptToLLM(transcript);
128
134
  }
129
135
 
130
- if (this.log) { console.log("DirAiPrompt json: ", json) }
136
+ winston.debug("DirAiPrompt json: ", json);
131
137
 
132
138
  const HTTPREQUEST = {
133
139
  url: AI_endpoint + '/ask',
@@ -137,14 +143,12 @@ class DirAiPrompt {
137
143
  json: json,
138
144
  method: 'POST'
139
145
  }
140
- if (this.log) { console.log("DirAiPrompt HTTPREQUEST: ", HTTPREQUEST); }
146
+ winston.debug("DirAiPrompt HttpRequest: ", HTTPREQUEST);
141
147
 
142
- this.#myrequest(
148
+ httpUtils.request(
143
149
  HTTPREQUEST, async (err, resbody) => {
144
150
  if (err) {
145
- if (this.log) {
146
- console.error("(httprequest) DirAiPrompt openai err:", err.response.data);
147
- }
151
+ winston.error("DirAiPrompt openai err:", err.response.data);
148
152
  await this.#assignAttributes(action, answer);
149
153
  let error;
150
154
  if (err.response?.data?.detail[0]) {
@@ -164,7 +168,7 @@ class DirAiPrompt {
164
168
  return;
165
169
  } else {
166
170
 
167
- if (this.log) { console.log("DirAiPrompt resbody: ", JSON.stringify(resbody)); }
171
+ winston.debug("DirAiPrompt resbody: ", resbody);
168
172
  answer = resbody.answer;
169
173
 
170
174
  await this.#assignAttributes(action, answer);
@@ -260,7 +264,7 @@ class DirAiPrompt {
260
264
  })
261
265
  }
262
266
  else {
263
- if (this.log) { console.log("No trueIntentDirective specified"); }
267
+ winston.debug("DirAiPrompt No trueIntentDirective specified");
264
268
  if (callback) {
265
269
  callback();
266
270
  }
@@ -275,7 +279,7 @@ class DirAiPrompt {
275
279
  });
276
280
  }
277
281
  else {
278
- if (this.log) { console.log("No falseIntentDirective specified"); }
282
+ winston.debug("DirAiPrompt No falseIntentDirective specified");
279
283
  if (callback) {
280
284
  callback();
281
285
  }
@@ -284,103 +288,16 @@ class DirAiPrompt {
284
288
  }
285
289
 
286
290
  async #assignAttributes(action, answer) {
287
- if (this.log) {
288
- console.log("assignAttributes action:", action)
289
- console.log("assignAttributes answer:", answer)
290
- }
291
+ winston.debug("DirAiPrompt assignAttributes action: ", action)
292
+ winston.debug("DirAiPrompt assignAttributes answer: " + answer)
293
+
291
294
  if (this.context.tdcache) {
292
295
  if (action.assignReplyTo && answer) {
293
296
  await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignReplyTo, answer);
294
297
  }
295
- // Debug log
296
- if (this.log) {
297
- const all_parameters = await TiledeskChatbot.allParametersStatic(this.context.tdcache, this.context.requestId);
298
- for (const [key, value] of Object.entries(all_parameters)) {
299
- if (this.log) { console.log("(gpttask) request parameter:", key, "value:", value, "type:", typeof value) }
300
- }
301
- }
302
298
  }
303
299
  }
304
300
 
305
- #myrequest(options, callback) {
306
- if (this.log) {
307
- console.log("API URL:", options.url);
308
- console.log("** Options:", JSON.stringify(options));
309
- }
310
- let axios_options = {
311
- url: options.url,
312
- method: options.method,
313
- params: options.params,
314
- headers: options.headers
315
- }
316
- if (options.json !== null) {
317
- axios_options.data = options.json
318
- }
319
- if (this.log) {
320
- console.log("axios_options:", JSON.stringify(axios_options));
321
- }
322
- if (options.url.startsWith("https:")) {
323
- const httpsAgent = new https.Agent({
324
- rejectUnauthorized: false,
325
- });
326
- axios_options.httpsAgent = httpsAgent;
327
- }
328
- axios(axios_options)
329
- .then((res) => {
330
- if (this.log) {
331
- console.log("Response for url:", options.url);
332
- console.log("Response headers:\n", JSON.stringify(res.headers));
333
- }
334
- if (res && res.status == 200 && res.data) {
335
- if (callback) {
336
- callback(null, res.data);
337
- }
338
- }
339
- else {
340
- if (callback) {
341
- callback(new Error("Response status is not 200"), null);
342
- }
343
- }
344
- })
345
- .catch((error) => {
346
- console.error("(DirAiPrompt) Axios error: ", JSON.stringify(error));
347
- if (callback) {
348
- callback(error, null);
349
- }
350
- });
351
- }
352
-
353
- async getKeyFromIntegrations(model) {
354
- return new Promise((resolve) => {
355
-
356
- const INTEGRATIONS_HTTPREQUEST = {
357
- url: this.API_ENDPOINT + "/" + this.context.projectId + "/integration/name/" + model,
358
- headers: {
359
- 'Content-Type': 'application/json',
360
- 'Authorization': 'JWT ' + this.context.token
361
- },
362
- method: "GET"
363
- }
364
- if (this.log) { console.log("DirAiPrompt INTEGRATIONS_HTTPREQUEST ", INTEGRATIONS_HTTPREQUEST) }
365
-
366
- this.#myrequest(
367
- INTEGRATIONS_HTTPREQUEST, async (err, integration) => {
368
- if (err) {
369
- resolve(null);
370
- } else {
371
-
372
- if (integration &&
373
- integration.value) {
374
- resolve(integration.value.apikey)
375
- }
376
- else {
377
- resolve(null)
378
- }
379
- }
380
- })
381
- })
382
- }
383
-
384
301
  async getKeyFromKbSettings() {
385
302
  return new Promise((resolve) => {
386
303
 
@@ -392,15 +309,12 @@ class DirAiPrompt {
392
309
  },
393
310
  method: "GET"
394
311
  }
395
- if (this.log) { console.log("DirAiPrompt KB_HTTPREQUEST", KB_HTTPREQUEST); }
312
+ winston.debug("DirAiPrompt KB HttpRequest", KB_HTTPREQUEST);
396
313
 
397
- this.#myrequest(
314
+ httpUtils.request(
398
315
  KB_HTTPREQUEST, async (err, resbody) => {
399
316
  if (err) {
400
- console.error("(httprequest) DirAiPrompt Get KnowledgeBase err:", err.message);
401
- if (this.log) {
402
- console.error("(httprequest) DirAiPrompt Get KnowledgeBase full err", err);
403
- }
317
+ winston.error("(httprequest) DirAiPrompt Get KnowledgeBase err: " + err.message);
404
318
  resolve(null);
405
319
  } else {
406
320
  if (!resbody.gptkey) {
@@ -425,9 +339,9 @@ class DirAiPrompt {
425
339
  },
426
340
  method: "GET"
427
341
  }
428
- if (this.log) { console.log("DirAiPrompt check quote availability HTTPREQUEST", HTTPREQUEST); }
342
+ winston.debug("DirAiPrompt check quote availability HttpRequest", HTTPREQUEST);
429
343
 
430
- this.#myrequest(
344
+ httpUtils.request(
431
345
  HTTPREQUEST, async (err, resbody) => {
432
346
  if (err) {
433
347
  resolve(true)
@@ -455,15 +369,15 @@ class DirAiPrompt {
455
369
  json: tokens_usage,
456
370
  method: "POST"
457
371
  }
458
- if (this.log) { console.log("DirAiPrompt check quote availability HTTPREQUEST", HTTPREQUEST); }
372
+ winston.debug("DirAiPrompt update quote HttpRequest", HTTPREQUEST);
459
373
 
460
- this.#myrequest(
374
+ httpUtils.request(
461
375
  HTTPREQUEST, async (err, resbody) => {
462
376
  if (err) {
463
- console.error("(httprequest) DirAiPrompt Increment tokens quote err: ", err);
377
+ winston.error("(httprequest) DirAiPrompt Increment tokens quote err: ", err);
464
378
  reject(false)
465
379
  } else {
466
- if (this.log) { console.log("(httprequest) DirAiPrompt Increment token quote resbody: ", resbody); }
380
+ winston.debug("(httprequest) DirAiPrompt Increment token quote resbody: ", resbody);
467
381
  resolve(true);
468
382
  }
469
383
  }