@tiledesk/tiledesk-tybot-connector 0.5.0-rc2 → 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 (80) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/ExtApi.js +2 -71
  3. package/Logger.js +32 -5
  4. package/{models → engine}/IntentsMachineFactory.js +1 -1
  5. package/{models → engine}/MongodbBotsDataSource.js +2 -2
  6. package/{models → engine}/MongodbIntentsMachine.js +1 -1
  7. package/{models → engine}/TiledeskChatbot.js +3 -1
  8. package/{models → engine}/TiledeskIntentsMachine.js +1 -2
  9. package/{models → engine/mock}/MockBotsDataSource.js +1 -2
  10. package/index.js +23 -111
  11. package/logs/app.log +3041 -4469
  12. package/logs/app3.log +2822 -0
  13. package/logs/app4.log +46163 -0
  14. package/logs/app5.log +33173 -0
  15. package/models/faq.js +2 -5
  16. package/package.json +1 -1
  17. package/{TiledeskServices → services}/AIService.js +4 -3
  18. package/{models/faqKbService.js → services/FaqKbService.js} +1 -1
  19. package/{models/faqService.js → services/FaqService.js} +4 -3
  20. package/services/IntegrationService.js +43 -0
  21. package/services/TilebotService.js +47 -0
  22. package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +2 -3
  23. package/tiledeskChatbotPlugs/FillParamsChatbotPlug.js +1 -1
  24. package/tiledeskChatbotPlugs/TiledeskRequestVariables.js +1 -1
  25. package/tiledeskChatbotPlugs/directives/DEPRECATED_DirSetAttribute.js +1 -1
  26. package/tiledeskChatbotPlugs/directives/DirAddTags.js +8 -42
  27. package/tiledeskChatbotPlugs/directives/DirAiPrompt.js +42 -128
  28. package/tiledeskChatbotPlugs/directives/DirAskGPT.js +9 -74
  29. package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +14 -83
  30. package/tiledeskChatbotPlugs/directives/DirAssign.js +1 -1
  31. package/tiledeskChatbotPlugs/directives/DirAssignFromFunction.js +1 -1
  32. package/tiledeskChatbotPlugs/directives/DirAssistant.js +13 -106
  33. package/tiledeskChatbotPlugs/directives/DirBrevo.js +5 -34
  34. package/tiledeskChatbotPlugs/directives/DirCaptureUserReply.js +1 -1
  35. package/tiledeskChatbotPlugs/directives/DirClearTranscript.js +1 -1
  36. package/tiledeskChatbotPlugs/directives/DirClose.js +1 -1
  37. package/tiledeskChatbotPlugs/directives/DirCode.js +1 -1
  38. package/tiledeskChatbotPlugs/directives/DirCondition.js +1 -1
  39. package/tiledeskChatbotPlugs/directives/DirConnectBlock.js +3 -72
  40. package/tiledeskChatbotPlugs/directives/DirContactUpdate.js +3 -3
  41. package/tiledeskChatbotPlugs/directives/DirCustomerio.js +5 -33
  42. package/tiledeskChatbotPlugs/directives/DirDeflectToHelpCenter.js +2 -2
  43. package/tiledeskChatbotPlugs/directives/DirDeleteVariable.js +1 -1
  44. package/tiledeskChatbotPlugs/directives/DirForm.js +2 -2
  45. package/tiledeskChatbotPlugs/directives/DirGptTask.js +13 -76
  46. package/tiledeskChatbotPlugs/directives/DirGptTask_OLD.js +8 -53
  47. package/tiledeskChatbotPlugs/directives/DirHubspot.js +5 -34
  48. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgentsV2.js +3 -39
  49. package/tiledeskChatbotPlugs/directives/DirIfOpenHours.js +2 -38
  50. package/tiledeskChatbotPlugs/directives/DirIntent.js +2 -77
  51. package/tiledeskChatbotPlugs/directives/DirJSONCondition.js +1 -1
  52. package/tiledeskChatbotPlugs/directives/DirMake.js +1 -1
  53. package/tiledeskChatbotPlugs/directives/DirMessage.js +1 -1
  54. package/tiledeskChatbotPlugs/directives/DirMessageToBot.js +3 -72
  55. package/tiledeskChatbotPlugs/directives/DirMoveToAgent.js +2 -2
  56. package/tiledeskChatbotPlugs/directives/DirMoveToUnassigned.js +2 -2
  57. package/tiledeskChatbotPlugs/directives/DirQapla.js +7 -72
  58. package/tiledeskChatbotPlugs/directives/DirRandomReply.js +2 -2
  59. package/tiledeskChatbotPlugs/directives/DirReplaceBot.js +1 -1
  60. package/tiledeskChatbotPlugs/directives/DirReplaceBotV2.js +3 -38
  61. package/tiledeskChatbotPlugs/directives/DirReplaceBotV3.js +3 -39
  62. package/tiledeskChatbotPlugs/directives/DirReply.js +2 -2
  63. package/tiledeskChatbotPlugs/directives/DirReplyV2.js +3 -3
  64. package/tiledeskChatbotPlugs/directives/DirSendEmail.js +1 -1
  65. package/tiledeskChatbotPlugs/directives/DirSendWhatsapp.js +3 -29
  66. package/tiledeskChatbotPlugs/directives/DirSetAttribute.js +1 -1
  67. package/tiledeskChatbotPlugs/directives/DirSetAttributeV2.js +3 -38
  68. package/tiledeskChatbotPlugs/directives/DirWait.js +1 -1
  69. package/tiledeskChatbotPlugs/directives/DirWebRequest.js +1 -1
  70. package/tiledeskChatbotPlugs/directives/DirWebRequestV2.js +32 -22
  71. package/tiledeskChatbotPlugs/directives/DirWebRequestV2_old.js +1 -1
  72. package/tiledeskChatbotPlugs/directives/DirWebResponse.js +23 -64
  73. package/tiledeskChatbotPlugs/directives/DirWhatsappByAttribute.js +3 -29
  74. package/utils/HttpUtils.js +128 -0
  75. package/{models → utils}/TiledeskChatbotUtil.js +8 -50
  76. package/TiledeskServices/utils.js +0 -99
  77. /package/{models → engine}/IntentForm.js +0 -0
  78. /package/{models → engine}/TiledeskChatbotConst.js +0 -0
  79. /package/{models → engine/mock}/MockIntentsMachine.js +0 -0
  80. /package/{models → engine/mock}/MockTdCache.js +0 -0
@@ -3,6 +3,7 @@ let https = require("https");
3
3
  const { v4: uuidv4 } = require('uuid');
4
4
  const ms = require('minimist-string');
5
5
  const winston = require('../../utils/winston');
6
+ const tilebotService = require('../../services/TilebotService');
6
7
 
7
8
  class DirConnectBlock {
8
9
  constructor(context) {
@@ -66,7 +67,7 @@ class DirConnectBlock {
66
67
  }
67
68
  winston.debug("(DirConnectBlock) move to intent message: ", intent_command_request);
68
69
 
69
- this.sendMessageToBot(this.TILEBOT_ENDPOINT, intent_command_request, botId, () => {
70
+ tilebotService.sendMessageToBot(intent_command_request, botId, () => {
70
71
  callback();
71
72
  });
72
73
  }
@@ -102,77 +103,7 @@ class DirConnectBlock {
102
103
  return intentDirective;
103
104
  }
104
105
 
105
- /**
106
- * A stub to send message to the "ext/botId" endpoint, hosted by tilebot on:
107
- * /${TILEBOT_ROUTE}/ext/${botId}
108
- *
109
- * @param {Object} message. The message to send
110
- * @param {string} botId. Tiledesk botId
111
- * @param {string} token. User token
112
- */
113
- sendMessageToBot(TILEBOT_ENDPOINT, message, botId, callback) {
114
-
115
- const url = `${TILEBOT_ENDPOINT}/ext/${botId}`;
116
- winston.verbose("sendMessageToBot URL" + url);
117
- const HTTPREQUEST = {
118
- url: url,
119
- headers: {
120
- 'Content-Type' : 'application/json'
121
- },
122
- json: message,
123
- method: 'POST'
124
- };
125
- this.myrequest(
126
- HTTPREQUEST,
127
- function(err, resbody) {
128
- if (err) {
129
- if (callback) {
130
- callback(err);
131
- }
132
- }
133
- else {
134
- if (callback) {
135
- callback(null, resbody);
136
- }
137
- }
138
- }, false
139
- );
140
- }
141
-
142
- myrequest(options, callback, log) {
143
- let axios_options = {
144
- url: options.url,
145
- method: options.method,
146
- data: options.json,
147
- params: options.params,
148
- headers: options.headers
149
- }
150
- if (options.url.startsWith("https:")) {
151
- const httpsAgent = new https.Agent({
152
- rejectUnauthorized: false,
153
- });
154
- axios_options.httpsAgent = httpsAgent;
155
- }
156
- axios(axios_options)
157
- .then((res) => {
158
- if (res && res.status == 200 && res.data) {
159
- if (callback) {
160
- callback(null, res.data);
161
- }
162
- }
163
- else {
164
- if (callback) {
165
- callback(TiledeskClient.getErr({message: "Response status not 200"}, options, res), null, null);
166
- }
167
- }
168
- })
169
- .catch( (error) => {
170
- winston.error("(DirConnectBlock) Axios error: ", error.response.data);
171
- if (callback) {
172
- callback(error, null, null);
173
- }
174
- });
175
- }
106
+
176
107
  }
177
108
 
178
109
  module.exports = { DirConnectBlock };
@@ -1,8 +1,8 @@
1
1
  const { Filler } = require('../Filler');
2
- const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
3
- const { TiledeskChatbotUtil } = require('../../models/TiledeskChatbotUtil');
2
+ const { TiledeskChatbot } = require('../../engine/TiledeskChatbot');
3
+ const { TiledeskChatbotUtil } = require('../../utils/TiledeskChatbotUtil');
4
4
  let axios = require('axios');
5
- const { TiledeskChatbotConst } = require('../../models/TiledeskChatbotConst');
5
+ const { TiledeskChatbotConst } = require('../../engine/TiledeskChatbotConst');
6
6
  const { TiledeskClient } = require('@tiledesk/tiledesk-client');
7
7
  const winston = require('../../utils/winston');
8
8
 
@@ -1,10 +1,11 @@
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
  const { DirIntent } = require("./DirIntent");
5
5
  let https = require("https");
6
6
  require('dotenv').config();
7
7
  const winston = require('../../utils/winston');
8
+ const integrationService = require("../../services/IntegrationService");
8
9
 
9
10
  class DirCustomerio {
10
11
 
@@ -15,6 +16,8 @@ class DirCustomerio {
15
16
  this.context = context;
16
17
  this.tdcache = this.context.tdcache;
17
18
  this.requestId = this.context.requestId;
19
+ this.projectId = this.context.projectId;
20
+ this.token = this.context.token;
18
21
  this.intentDir = new DirIntent(context);
19
22
  this.API_ENDPOINT = this.context.API_ENDPOINT;
20
23
  this.log = context.log;
@@ -71,7 +74,7 @@ class DirCustomerio {
71
74
  const customerio_base_url = process.env.CUSTOMERIO_ENDPOINT || "https://track.customer.io/api/v1";
72
75
  winston.debug("(DirCustomerio) customerio_base_url: " + customerio_base_url);
73
76
 
74
- let key = await this.getKeyFromIntegrations();
77
+ let key = await integrationService.getKeyFromIntegrations(this.projectId, 'customerio', this.token);
75
78
  if (!key) {
76
79
  winston.debug("(DirCustomerio) - Key not found in Integrations.");
77
80
  let status = 422;
@@ -257,37 +260,6 @@ class DirCustomerio {
257
260
  }
258
261
  }
259
262
  }
260
-
261
- async getKeyFromIntegrations() {
262
- return new Promise((resolve) => {
263
-
264
- const INTEGRATIONS_HTTPREQUEST = {
265
- url: this.API_ENDPOINT + "/" + this.context.projectId + "/integration/name/customerio",
266
- headers: {
267
- 'Content-Type': 'application/json',
268
- 'Authorization': 'JWT ' + this.context.token
269
- },
270
- method: "GET"
271
- }
272
- winston.debug("(DirCustomerio) Integration HttpRequest ", INTEGRATIONS_HTTPREQUEST)
273
-
274
- this.#myrequest(
275
- INTEGRATIONS_HTTPREQUEST, async (err, integration) => {
276
- if (err) {
277
- resolve(null);
278
- } else {
279
- if (integration &&
280
- integration.value) {
281
- resolve(integration.value.apikey)
282
- }
283
- else {
284
- resolve(null)
285
- }
286
- }
287
- })
288
- })
289
- }
290
-
291
263
  }
292
264
 
293
265
  module.exports = { DirCustomerio }
@@ -1,6 +1,6 @@
1
1
  const { HelpCenterQuery } = require('@tiledesk/helpcenter-query-client');
2
- const { TiledeskChatbot } = require('../../models/TiledeskChatbot.js');
3
- const { TiledeskChatbotConst } = require('../../models/TiledeskChatbotConst');
2
+ const { TiledeskChatbot } = require('../../engine/TiledeskChatbot.js');
3
+ const { TiledeskChatbotConst } = require('../../engine/TiledeskChatbotConst.js');
4
4
  const ms = require('minimist-string');
5
5
  const { TiledeskClient } = require('@tiledesk/tiledesk-client');
6
6
  const winston = require('../../utils/winston');
@@ -1,6 +1,6 @@
1
1
  const { param } = require('express/lib/request');
2
2
  const ms = require('minimist-string');
3
- const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
3
+ const { TiledeskChatbot } = require('../../engine/TiledeskChatbot');
4
4
  const { Filler } = require('../Filler');
5
5
  const winston = require('../../utils/winston');
6
6
 
@@ -1,7 +1,7 @@
1
1
  const { Filler } = require('../Filler');
2
- const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
2
+ const { TiledeskChatbot } = require('../../engine/TiledeskChatbot');
3
3
  const { DirIntent } = require('./DirIntent');
4
- const { IntentForm } = require('../../models/IntentForm.js');
4
+ const { IntentForm } = require('../../engine/IntentForm.js');
5
5
  const { TiledeskClient } = require('@tiledesk/tiledesk-client');
6
6
  const winston = require('../../utils/winston');
7
7
 
@@ -1,12 +1,14 @@
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
9
  const winston = require('../../utils/winston');
10
+ const httpUtils = require("../../utils/HttpUtils");
11
+ const integrationService = require("../../services/IntegrationService");
10
12
 
11
13
  class DirGptTask {
12
14
 
@@ -18,6 +20,8 @@ class DirGptTask {
18
20
  this.chatbot = this.context.chatbot;
19
21
  this.tdcache = this.context.tdcache;
20
22
  this.requestId = this.context.requestId;
23
+ this.projectId = this.context.projectId;
24
+ this.token = this.context.token;
21
25
  this.intentDir = new DirIntent(context);
22
26
  this.API_ENDPOINT = this.context.API_ENDPOINT;
23
27
  this.log = context.log;
@@ -113,7 +117,7 @@ class DirGptTask {
113
117
  const openai_url = process.env.OPENAI_ENDPOINT + "/chat/completions";
114
118
  winston.debug("(DirGptTask) openai_url " + openai_url);
115
119
 
116
- let key = await this.getKeyFromIntegrations();
120
+ let key = await integrationService.getKeyFromIntegrations(this.projectId, 'openai', this.token);
117
121
  if (!key) {
118
122
  winston.debug("(DirGptTask) - Key not found in Integrations. Searching in kb settings...");
119
123
  key = await this.getKeyFromKbSettings();
@@ -192,7 +196,8 @@ class DirGptTask {
192
196
  method: 'POST'
193
197
  }
194
198
  winston.debug("(DirGptTask) HttpRequest: ", HTTPREQUEST);
195
- this.#myrequest(
199
+
200
+ httpUtils.request(
196
201
  HTTPREQUEST, async (err, resbody) => {
197
202
  if (err) {
198
203
  winston.debug("(DirGptTask) openai err: ", err);
@@ -303,74 +308,6 @@ class DirGptTask {
303
308
  }
304
309
  }
305
310
 
306
- #myrequest(options, callback) {
307
- let axios_options = {
308
- url: options.url,
309
- method: options.method,
310
- params: options.params,
311
- headers: options.headers
312
- }
313
- if (options.json !== null) {
314
- axios_options.data = options.json
315
- }
316
- if (options.url.startsWith("https:")) {
317
- const httpsAgent = new https.Agent({
318
- rejectUnauthorized: false,
319
- });
320
- axios_options.httpsAgent = httpsAgent;
321
- }
322
- axios(axios_options)
323
- .then((res) => {
324
- if (res && res.status == 200 && res.data) {
325
- if (callback) {
326
- callback(null, res.data);
327
- }
328
- }
329
- else {
330
- if (callback) {
331
- callback(new Error("Response status is not 200"), null);
332
- }
333
- }
334
- })
335
- .catch((error) => {
336
- winston.error("(DirGptTask) Axios error: ", JSON.stringify(error));
337
- if (callback) {
338
- callback(error, null);
339
- }
340
- });
341
- }
342
-
343
- async getKeyFromIntegrations() {
344
- return new Promise((resolve) => {
345
-
346
- const INTEGRATIONS_HTTPREQUEST = {
347
- url: this.API_ENDPOINT + "/" + this.context.projectId + "/integration/name/openai",
348
- headers: {
349
- 'Content-Type': 'application/json',
350
- 'Authorization': 'JWT ' + this.context.token
351
- },
352
- method: "GET"
353
- }
354
- winston.debug("(DirGptTask) Integrations HttpRequest ", INTEGRATIONS_HTTPREQUEST)
355
-
356
- this.#myrequest(
357
- INTEGRATIONS_HTTPREQUEST, async (err, integration) => {
358
- if (err) {
359
- resolve(null);
360
- } else {
361
-
362
- if (integration &&
363
- integration.value) {
364
- resolve(integration.value.apikey)
365
- }
366
- else {
367
- resolve(null)
368
- }
369
- }
370
- })
371
- })
372
- }
373
-
374
311
  async getKeyFromKbSettings() {
375
312
  return new Promise((resolve) => {
376
313
 
@@ -384,7 +321,7 @@ class DirGptTask {
384
321
  }
385
322
  winston.debug("(DirGptTask) KB HttpRequest ", KB_HTTPREQUEST);
386
323
 
387
- this.#myrequest(
324
+ httpUtils.request(
388
325
  KB_HTTPREQUEST, async (err, resbody) => {
389
326
  if (err) {
390
327
  winston.error("(DirGptTask) Get KnowledgeBase err:", err.message);
@@ -414,7 +351,7 @@ class DirGptTask {
414
351
  }
415
352
  winston.debug("(DirGptTask) check quote availability HttpRequest ", HTTPREQUEST);
416
353
 
417
- this.#myrequest(
354
+ httpUtils.request(
418
355
  HTTPREQUEST, async (err, resbody) => {
419
356
  if (err) {
420
357
  resolve(true)
@@ -444,7 +381,7 @@ class DirGptTask {
444
381
  }
445
382
  winston.debug("(DirGptTask) update quote HttpRequest ", HTTPREQUEST);
446
383
 
447
- this.#myrequest(
384
+ httpUtils.request(
448
385
  HTTPREQUEST, async (err, resbody) => {
449
386
  if (err) {
450
387
  winston.debug("(DirGptTask) Increment tokens quote err: ", err);
@@ -1,8 +1,9 @@
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 httpUtils = require("../../utils/HttpUtils");
6
7
  require('dotenv').config();
7
8
 
8
9
  class DirGptTask {
@@ -98,8 +99,8 @@ class DirGptTask {
98
99
  method: "GET"
99
100
  }
100
101
  if (this.log) { console.log("DirGptTask INTEGRATIONS_HTTPREQUEST ", INTEGRATIONS_HTTPREQUEST) }
101
-
102
- this.#myrequest(
102
+
103
+ httpUtils.request(
103
104
  INTEGRATIONS_HTTPREQUEST, async (err, integration) => {
104
105
  if (err) {
105
106
  if (callback) {
@@ -133,7 +134,7 @@ class DirGptTask {
133
134
  }
134
135
  if (this.log) { console.log("DirGptTask KB_HTTPREQUEST", KB_HTTPREQUEST); }
135
136
 
136
- this.#myrequest(
137
+ httpUtils.request(
137
138
  KB_HTTPREQUEST, async (err, resbody) => {
138
139
  if (err) {
139
140
  if (callback) {
@@ -199,7 +200,7 @@ class DirGptTask {
199
200
  method: 'POST'
200
201
  }
201
202
  if (this.log) { console.log("DirGptTask HTTPREQUEST: ", HTTPREQUEST); }
202
- this.#myrequest(
203
+ httpUtils.request(
203
204
  HTTPREQUEST, async (err, resbody) => {
204
205
  if (err) {
205
206
  if (this.log) {
@@ -267,7 +268,7 @@ class DirGptTask {
267
268
  method: 'POST'
268
269
  }
269
270
  if (this.log) { console.log("DirGptTask HTTPREQUEST: ", HTTPREQUEST); }
270
- this.#myrequest(
271
+ httpUtils.request(
271
272
  HTTPREQUEST, async (err, resbody) => {
272
273
  if (err) {
273
274
  if (this.log) {
@@ -381,53 +382,7 @@ class DirGptTask {
381
382
  }
382
383
  }
383
384
  }
384
-
385
- #myrequest(options, callback) {
386
- if (this.log) {
387
- console.log("API URL:", options.url);
388
- console.log("** Options:", JSON.stringify(options));
389
- }
390
- let axios_options = {
391
- url: options.url,
392
- method: options.method,
393
- params: options.params,
394
- headers: options.headers
395
- }
396
- if (options.json !== null) {
397
- axios_options.data = options.json
398
- }
399
- if (this.log) {
400
- console.log("axios_options:", JSON.stringify(axios_options));
401
- }
402
- if (options.url.startsWith("https:")) {
403
- const httpsAgent = new https.Agent({
404
- rejectUnauthorized: false,
405
- });
406
- axios_options.httpsAgent = httpsAgent;
407
- }
408
- axios(axios_options)
409
- .then((res) => {
410
- if (this.log) {
411
- console.log("Response for url:", options.url);
412
- console.log("Response headers:\n", JSON.stringify(res.headers));
413
- }
414
- if (res && res.status == 200 && res.data) {
415
- if (callback) {
416
- callback(null, res.data);
417
- }
418
- }
419
- else {
420
- if (callback) {
421
- callback(new Error("Response status is not 200"), null);
422
- }
423
- }
424
- })
425
- .catch((error) => {
426
- if (callback) {
427
- callback(error, null);
428
- }
429
- });
430
- }
385
+
431
386
  }
432
387
 
433
388
  module.exports = { DirGptTask }
@@ -1,10 +1,11 @@
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
  const { DirIntent } = require("./DirIntent");
5
5
  let https = require("https");
6
6
  require('dotenv').config();
7
7
  const winston = require('../../utils/winston');
8
+ const integrationService = require("../../services/IntegrationService");
8
9
 
9
10
  class DirHubspot {
10
11
 
@@ -15,6 +16,8 @@ class DirHubspot {
15
16
  this.context = context;
16
17
  this.tdcache = this.context.tdcache;
17
18
  this.requestId = this.context.requestId;
19
+ this.projectId = this.context.projectId;
20
+ this.token = this.context.token;
18
21
  this.intentDir = new DirIntent(context);
19
22
  this.API_ENDPOINT = this.context.API_ENDPOINT;
20
23
  this.log = context.log;
@@ -73,7 +76,7 @@ class DirHubspot {
73
76
  const hubspot_base_url = process.env.HUBSPOT_ENDPOINT || "https://api.hubapi.com/crm/v3/";
74
77
  winston.debug("(DirHubspot) hubspot_base_url " + hubspot_base_url);
75
78
 
76
- let key = await this.getKeyFromIntegrations();
79
+ let key = await integrationService.getKeyFromIntegrations(this.projectId, 'hubspot', this.token);
77
80
  if (!key) {
78
81
  winston.debug("(DirHubspot) - Key not found in Integrations.");
79
82
  if (falseIntent) {
@@ -254,38 +257,6 @@ class DirHubspot {
254
257
  }
255
258
  }
256
259
  }
257
-
258
- async getKeyFromIntegrations() {
259
- return new Promise((resolve) => {
260
-
261
- const INTEGRATIONS_HTTPREQUEST = {
262
- url: this.API_ENDPOINT + "/" + this.context.projectId + "/integration/name/hubspot",
263
- headers: {
264
- 'Content-Type': 'application/json',
265
- 'Authorization': 'JWT ' + this.context.token
266
- },
267
- method: "GET"
268
- }
269
- winston.debug("(DirHubspot) Integration HttpRequest ", INTEGRATIONS_HTTPREQUEST)
270
-
271
- this.#myrequest(
272
- INTEGRATIONS_HTTPREQUEST, async (err, integration) => {
273
- if (err) {
274
- resolve(null);
275
- } else {
276
-
277
- if (integration &&
278
- integration.value) {
279
- resolve(integration.value.apikey)
280
- }
281
- else {
282
- resolve(null)
283
- }
284
- }
285
- })
286
- })
287
- }
288
-
289
260
  }
290
261
 
291
262
  module.exports = { DirHubspot }
@@ -2,9 +2,10 @@
2
2
  const { DirIntent } = require('./DirIntent');
3
3
  const axios = require("axios").default;
4
4
  let https = require("https");
5
- const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
5
+ const { TiledeskChatbot } = require('../../engine/TiledeskChatbot');
6
6
  const { TiledeskClient } = require('@tiledesk/tiledesk-client');
7
7
  const winston = require('../../utils/winston');
8
+ const httpUtils = require('../../utils/HttpUtils');
8
9
 
9
10
  class DirIfOnlineAgentsV2 {
10
11
 
@@ -190,7 +191,7 @@ class DirIfOnlineAgentsV2 {
190
191
  // json: true,
191
192
  method: 'GET',
192
193
  };
193
- this.#myrequest(
194
+ httpUtils.request(
194
195
  HTTPREQUEST,
195
196
  function(err, resbody) {
196
197
  if (err) {
@@ -210,43 +211,6 @@ class DirIfOnlineAgentsV2 {
210
211
 
211
212
  }
212
213
 
213
- #myrequest(options, callback) {
214
- let axios_options = {
215
- url: options.url,
216
- method: options.method,
217
- params: options.params,
218
- headers: options.headers
219
- }
220
- if (options.json !== null) {
221
- axios_options.data = options.json
222
- }
223
- if (options.url.startsWith("https:")) {
224
- const httpsAgent = new https.Agent({
225
- rejectUnauthorized: false,
226
- });
227
- axios_options.httpsAgent = httpsAgent;
228
- }
229
- axios(axios_options)
230
- .then((res) => {
231
- if (res && res.status == 200 && res.data) {
232
- if (callback) {
233
- callback(null, res.data);
234
- }
235
- }
236
- else {
237
- if (callback) {
238
- callback(new Error("Response status is not 200"), null);
239
- }
240
- }
241
- })
242
- .catch((error) => {
243
- winston.error("(DirIfOnlineAgents) Axios error: ", error.response.data);
244
- if (callback) {
245
- callback(error, null);
246
- }
247
- });
248
- }
249
-
250
214
  fixToken(token) {
251
215
  if (token.startsWith('JWT ')) {
252
216
  return token
@@ -4,6 +4,7 @@ const { DirIntent } = require('./DirIntent');
4
4
  let https = require("https");
5
5
  const ms = require('minimist-string');
6
6
  const winston = require('../../utils/winston');
7
+ const httpUtils = require('../../utils/HttpUtils');
7
8
 
8
9
  class DirIfOpenHours {
9
10
 
@@ -89,7 +90,7 @@ class DirIfOpenHours {
89
90
  }
90
91
  winston.debug("(DirIfOpenHours) HttpRequest ", HTTPREQUEST);
91
92
 
92
- this.#myrequest(
93
+ httpUtils.request(
93
94
  HTTPREQUEST, async (err, resbody) => {
94
95
 
95
96
  winston.debug("(DirIfOpenHours) resbody:", resbody);
@@ -160,43 +161,6 @@ class DirIfOpenHours {
160
161
  // });
161
162
  }
162
163
 
163
- #myrequest(options, callback) {
164
- let axios_options = {
165
- url: options.url,
166
- method: options.method,
167
- params: options.params,
168
- headers: options.headers
169
- }
170
- if (options.json !== null) {
171
- axios_options.data = options.json
172
- }
173
- if (options.url.startsWith("https:")) {
174
- const httpsAgent = new https.Agent({
175
- rejectUnauthorized: false,
176
- });
177
- axios_options.httpsAgent = httpsAgent;
178
- }
179
- axios(axios_options)
180
- .then((res) => {
181
- if (res && res.status == 200 && res.data) {
182
- if (callback) {
183
- callback(null, res.data);
184
- }
185
- }
186
- else {
187
- if (callback) {
188
- callback(new Error("Response status is not 200"), null);
189
- }
190
- }
191
- })
192
- .catch((error) => {
193
- winston.error("(DirIfOpenHours) Axios error: ", error);
194
- if (callback) {
195
- callback(error, null);
196
- }
197
- });
198
- }
199
-
200
164
  parseParams(directive_parameter) {
201
165
  let trueIntent = null;
202
166
  let falseIntent = null;