@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,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,19 +16,21 @@ 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;
21
24
  }
22
25
 
23
26
  execute(directive, callback) {
24
- if (this.log) { console.log("DirHubspot directive: ", directive); }
27
+ winston.verbose("Execute Hubspot directive");
25
28
  let action;
26
29
  if (directive.action) {
27
30
  action = directive.action;
28
31
  }
29
32
  else {
30
- console.error("DirHubspot Incorrect directive: ", JSON.stringify(directive));
33
+ winston.warn("DirHubspot Incorrect directive: ", directive);
31
34
  callback();
32
35
  return;
33
36
  }
@@ -37,9 +40,9 @@ class DirHubspot {
37
40
  }
38
41
 
39
42
  async go(action, callback) {
40
- if (this.log) { console.log("DirHubspot action:", JSON.stringify(action)); }
43
+ winston.debug("(DirHubspot) Action: ", action);
41
44
  if (!this.tdcache) {
42
- console.error("Error: DirHubspot tdcache is mandatory");
45
+ winston.error("(DirHubspot) Error: tdcache is mandatory");
43
46
  callback();
44
47
  return;
45
48
  }
@@ -49,12 +52,10 @@ class DirHubspot {
49
52
  let trueIntentAttributes = action.trueIntentAttributes;
50
53
  let falseIntentAttributes = action.falseIntentAttributes;
51
54
 
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
- }
55
+ winston.debug("(DirHubspot) trueIntent " + trueIntent)
56
+ winston.debug("(DirHubspot) falseIntent " + falseIntent)
57
+ winston.debug("(DirHubspot) trueIntentAttributes " + trueIntentAttributes)
58
+ winston.debug("(DirHubspot) falseIntentAttributes " + falseIntentAttributes)
58
59
 
59
60
  let requestVariables = null;
60
61
  requestVariables =
@@ -64,20 +65,20 @@ class DirHubspot {
64
65
 
65
66
  //let token = action.token;
66
67
  let bodyParameters = action.bodyParameters;
67
- if (this.log) { console.log("DirHubspot bodyParameters: ", bodyParameters); }
68
+ winston.debug("(DirHubspot) bodyParameters: ", bodyParameters);
68
69
 
69
70
  if (!bodyParameters || bodyParameters === '') {
70
- if (this.log) { console.error("DirHubspot ERROR - bodyParameters is undefined or null or empty string") };
71
+ winston.error("(DirHubspot) Error: bodyParameters is undefined or null or empty string");
71
72
  callback();
72
73
  return;
73
74
  }
74
75
 
75
76
  const hubspot_base_url = process.env.HUBSPOT_ENDPOINT || "https://api.hubapi.com/crm/v3/";
76
- if (this.log) { console.log("DirHubspot hubspot_base_url ", hubspot_base_url); }
77
+ winston.debug("(DirHubspot) hubspot_base_url " + hubspot_base_url);
77
78
 
78
- let key = await this.getKeyFromIntegrations();
79
+ let key = await integrationService.getKeyFromIntegrations(this.projectId, 'hubspot', this.token);
79
80
  if (!key) {
80
- if (this.log) { console.log("DirHubspot - Key not found in Integrations."); }
81
+ winston.debug("(DirHubspot) - Key not found in Integrations.");
81
82
  if (falseIntent) {
82
83
  await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
83
84
  callback(true);
@@ -87,11 +88,10 @@ class DirHubspot {
87
88
 
88
89
  const filler = new Filler();
89
90
  for (const [key, value] of Object.entries(bodyParameters)) {
90
- if (this.log) { console.log("bodyParam:", key, "value:", value) }
91
91
  let filled_value = filler.fill(value, requestVariables);
92
92
  bodyParameters[key] = filled_value;
93
93
  }
94
- if (this.log) { console.log('DirHubspot bodyParameters filler: ', bodyParameters) }
94
+ winston.debug("(DirHubspot) bodyParameters filled: ", bodyParameters);
95
95
 
96
96
  let json = {
97
97
  inputs: [
@@ -107,17 +107,13 @@ class DirHubspot {
107
107
  json: json,
108
108
  method: "POST"
109
109
  }
110
- if (this.log) { console.log("DirHubspot HUBSPOT_HTTPREQUEST", JSON.stringify(HUBSPOT_HTTPREQUEST)); }
110
+ winston.debug("(DirHubspot) HttpRequest ", HUBSPOT_HTTPREQUEST);
111
111
 
112
112
  this.#myrequest(
113
113
  HUBSPOT_HTTPREQUEST, async (err, resbody) => {
114
114
  if (err) {
115
115
  if (callback) {
116
- if (this.log) {
117
- console.error("(httprequest) DirHubspot err response:", err.response)
118
- console.error("(httprequest) DirHubspot err data:", err.response.data)
119
- };
120
-
116
+ winston.error("(DirHubspot) err response: ", err.response.data)
121
117
  let result = null;
122
118
  let status = null;
123
119
  let error;
@@ -133,11 +129,9 @@ class DirHubspot {
133
129
  error = err.response.data.message;
134
130
  }
135
131
 
136
- if (this.log) {
137
- console.error("(httprequest) DirHubspot err data result:", result);
138
- console.error("(httprequest) DirHubspot err data status:", status);
139
- console.error("(httprequest) DirHubspot err data error:", error);
140
- }
132
+ winston.debug("(DirHubspot) err data result: " + result);
133
+ winston.debug("(DirHubspot) err data status: " + status);
134
+ winston.debug("(DirHubspot) err data error: ", error);
141
135
 
142
136
  await this.#assignAttributes(action, status, result, error);
143
137
  if (falseIntent) {
@@ -149,7 +143,7 @@ class DirHubspot {
149
143
  return;
150
144
  }
151
145
  } else if (callback) {
152
- if (this.log) { console.log("DirHubspot resbody: ", JSON.stringify(resbody, null, 2)); }
146
+ winston.debug("(DirHubspot) resbody: ", resbody);
153
147
 
154
148
  let status = 201;
155
149
  let error = null;
@@ -169,12 +163,10 @@ class DirHubspot {
169
163
  }
170
164
 
171
165
  async #assignAttributes(action, status, result, error) {
172
- if (this.log) {
173
- console.log("DirHubspot assignAttributes action:", action)
174
- console.log("DirHubspot assignAttributes status:", status)
175
- console.log("DirHubspot assignAttributes result:", result)
176
- console.log("DirHubspot assignAttributes error:", error)
177
- }
166
+ winston.debug("(DirHubspot) assignAttributes action: ", action)
167
+ winston.debug("(DirHubspot) assignAttributes status: " + status)
168
+ winston.debug("(DirHubspot) assignAttributes result: ", result)
169
+ winston.debug("(DirHubspot) assignAttributes error: ", error)
178
170
  if (this.context.tdcache) {
179
171
  if (action.assignStatusTo) {
180
172
  await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignStatusTo, status);
@@ -185,22 +177,10 @@ class DirHubspot {
185
177
  if (action.assignErrorTo) {
186
178
  await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignErrorTo, error);
187
179
  }
188
-
189
- // Debug log
190
- if (this.log) {
191
- const all_parameters = await TiledeskChatbot.allParametersStatic(this.context.tdcache, this.context.requestId);
192
- for (const [key, value] of Object.entries(all_parameters)) {
193
- if (this.log) { console.log("DirHubspot request parameter:", key, "value:", value, "type:", typeof value) }
194
- }
195
- }
196
180
  }
197
181
  }
198
182
 
199
183
  #myrequest(options, callback) {
200
- if (this.log) {
201
- console.log("** API URL:", options.url);
202
- console.log("** Options:", JSON.stringify(options));
203
- }
204
184
  let axios_options = {
205
185
  url: options.url,
206
186
  method: options.method,
@@ -210,9 +190,6 @@ class DirHubspot {
210
190
  if (options.json !== null) {
211
191
  axios_options.data = options.json
212
192
  }
213
- if (this.log) {
214
- console.log("axios_options:", JSON.stringify(axios_options));
215
- }
216
193
  if (options.url.startsWith("https:")) {
217
194
  const httpsAgent = new https.Agent({
218
195
  rejectUnauthorized: false,
@@ -221,10 +198,6 @@ class DirHubspot {
221
198
  }
222
199
  axios(axios_options)
223
200
  .then((res) => {
224
- if (this.log) {
225
- console.log("Response for url:", options.url);
226
- console.log("Response headers:\n", JSON.stringify(res.headers));
227
- }
228
201
  if (res && (res.status == 200 || res.status == 201) && res.data) {
229
202
  if (callback) {
230
203
  callback(null, res.data);
@@ -253,7 +226,6 @@ class DirHubspot {
253
226
  if (falseIntent) {
254
227
  falseIntentDirective = DirIntent.intentDirectiveFor(falseIntent, falseIntentAttributes);
255
228
  }
256
- if (this.log) { console.log('DirHubspot executeCondition/result', result) }
257
229
  if (result === true) {
258
230
  if (trueIntentDirective) {
259
231
  this.intentDir.execute(trueIntentDirective, () => {
@@ -263,7 +235,7 @@ class DirHubspot {
263
235
  });
264
236
  }
265
237
  else {
266
- if (this.log) { console.log("No trueIntentDirective specified"); }
238
+ winston.debug("(DirHubspot) No trueIntentDirective specified");
267
239
  if (callback) {
268
240
  callback();
269
241
  }
@@ -278,45 +250,13 @@ class DirHubspot {
278
250
  });
279
251
  }
280
252
  else {
281
- if (this.log) { console.log("No falseIntentDirective specified"); }
253
+ winston.debug("(DirHubspot) No falseIntentDirective specified");
282
254
  if (callback) {
283
255
  callback();
284
256
  }
285
257
  }
286
258
  }
287
259
  }
288
-
289
- async getKeyFromIntegrations() {
290
- return new Promise((resolve) => {
291
-
292
- const INTEGRATIONS_HTTPREQUEST = {
293
- url: this.API_ENDPOINT + "/" + this.context.projectId + "/integration/name/hubspot",
294
- headers: {
295
- 'Content-Type': 'application/json',
296
- 'Authorization': 'JWT ' + this.context.token
297
- },
298
- method: "GET"
299
- }
300
- if (this.log) { console.log("DirGptTask INTEGRATIONS_HTTPREQUEST ", INTEGRATIONS_HTTPREQUEST) }
301
-
302
- this.#myrequest(
303
- INTEGRATIONS_HTTPREQUEST, async (err, integration) => {
304
- if (err) {
305
- resolve(null);
306
- } else {
307
-
308
- if (integration &&
309
- integration.value) {
310
- resolve(integration.value.apikey)
311
- }
312
- else {
313
- resolve(null)
314
- }
315
- }
316
- })
317
- })
318
- }
319
-
320
260
  }
321
261
 
322
262
  module.exports = { DirHubspot }
@@ -2,6 +2,7 @@
2
2
  const { TiledeskClient } = require('@tiledesk/tiledesk-client');
3
3
  const { DirIntent } = require('./DirIntent');
4
4
  const ms = require('minimist-string');
5
+ const winston = require('../../utils/winston');
5
6
 
6
7
  class DirIfOnlineAgents {
7
8
 
@@ -25,6 +26,7 @@ class DirIfOnlineAgents {
25
26
  }
26
27
 
27
28
  execute(directive, callback) {
29
+ winston.verbose("Execute IfOnlineAgents directive");
28
30
  let action;
29
31
  if (directive.action) {
30
32
  action = directive.action
@@ -32,19 +34,13 @@ class DirIfOnlineAgents {
32
34
  else if (directive.parameter) {
33
35
  let params;
34
36
  params = this.parseParams(directive.parameter);
35
- // if (!params.trueIntent && !params.falseIntent) {
36
- // if (this.log) {
37
- // console.log("missing both params.trueIntent & params.falseIntent");
38
- // }
39
- // callback();
40
- // return;
41
- // }
42
37
  action = {
43
38
  trueIntent: params.trueIntent,
44
39
  falseIntent: params.falseIntent
45
40
  }
46
41
  }
47
42
  else {
43
+ winston.warn("DirIfOnlineAgents Incorrect directive: ", directive);
48
44
  callback();
49
45
  return;
50
46
  }
@@ -54,56 +50,55 @@ class DirIfOnlineAgents {
54
50
  }
55
51
 
56
52
  go(action, callback) {
53
+ winston.debug("(DirIfOnlineAgents) Action: ", action);
54
+
57
55
  if (!action.trueIntent && !action.falseIntent) {
58
- if (this.log) {
59
- console.log("Error DirIfOnlineAgents: missing both action.trueIntent & action.falseIntent");
60
- }
56
+ winston.error("(DirIfOnlineAgents) Error: missing both action.trueIntent & action.falseIntent");
61
57
  callback();
62
58
  return;
63
59
  }
64
60
  const trueIntent = action.trueIntent;
65
61
  const falseIntent = action.falseIntent;
66
- if (this.log) {
67
- console.log("(DirIfOnlineAgents) IfOnlineAgents:trueIntent:", trueIntent);
68
- console.log("(DirIfOnlineAgents) IfOnlineAgents:falseIntent:", falseIntent);
69
- }
70
62
  const trueIntentAttributes = action.trueIntentAttributes;
71
63
  const falseIntentAttributes = action.falseIntentAttributes;
64
+
65
+ winston.debug("(DirIfOnlineAgents) IfOnlineAgents:trueIntent: " + trueIntent);
66
+ winston.debug("(DirIfOnlineAgents) IfOnlineAgents:falseIntent: " + falseIntent);
67
+
72
68
  let stopOnConditionMet = action.stopOnConditionMet;
73
69
  this.tdClient.openNow((err, result) => {
74
- if (this.log) {console.log("openNow():", result);}
70
+ winston.debug("(DirIfOnlineAgents) openNow(): ", result);
75
71
  if (err) {
76
- console.error("IfOnlineAgents:tdclient.openNow Error:", err);
72
+ winston.error("(DirIfOnlineAgents) openNow Error: ", err);
77
73
  callback();
78
74
  return;
79
75
  }
80
76
  else {
81
77
  if (result && result.isopen) {
82
78
  this.tdClient.getProjectAvailableAgents((err, agents) => {
83
- if (this.log) {console.log("Agents", agents);}
84
79
  if (err) {
85
- console.error("IfOnlineAgents:Error getting available agents:", err);
80
+ winston.error("(DirIfOnlineAgents) Error getting available agents: ", err);
86
81
  callback();
87
82
  }
88
83
  else {
89
- if (this.log) {console.log("Agents count:", agents.length);}
84
+ winston.debug("(DirIfOnlineAgents) Agents count: " + agents.length);
90
85
  if (agents.length > 0) {
91
86
  if (trueIntent) {
92
87
  let intentDirective = DirIntent.intentDirectiveFor(trueIntent, trueIntentAttributes);
93
- if (this.log) {console.log("agents (openHours) => trueIntent");}
88
+ winston.debug("(DirIfOnlineAgents) agents (openHours) => trueIntent");
94
89
  this.intentDir.execute(intentDirective, () => {
95
90
  callback(stopOnConditionMet);
96
91
  });
97
92
  }
98
93
  else {
99
- console.log("NO IfOnlineAgents trueIntent defined. callback()") // prod
94
+ winston.debug("(DirIfOnlineAgents) NO IfOnlineAgents trueIntent defined. callback()") // prod
100
95
  callback();
101
96
  return;
102
97
  }
103
98
  }
104
99
  else if (falseIntent) {
105
100
  let intentDirective = DirIntent.intentDirectiveFor(falseIntent, falseIntentAttributes);
106
- if (this.log) {console.log("!agents (openHours) => falseIntent", intentDirective);}
101
+ winston.debug("(DirIfOnlineAgents) !agents (openHours) => falseIntent: ", intentDirective);
107
102
  this.intentDir.execute(intentDirective, () => {
108
103
  callback(stopOnConditionMet);
109
104
  });
@@ -117,8 +112,7 @@ class DirIfOnlineAgents {
117
112
  else if (result && !result.isopen) {
118
113
  if (falseIntent) {
119
114
  let intentDirective = DirIntent.intentDirectiveFor(falseIntent, falseIntentAttributes);
120
- if (this.log) {console.log("!agents (!openHours) => falseIntent");}
121
- console.log("!agents (!openHours) => falseIntent BECAUSE CLOSED"); //PROD
115
+ winston.debug("(DirIfOnlineAgents) !agents (!openHours) => falseIntent BECAUSE CLOSED"); //PROD
122
116
  this.intentDir.execute(intentDirective, () => {
123
117
  callback();
124
118
  });
@@ -128,8 +122,7 @@ class DirIfOnlineAgents {
128
122
  }
129
123
  }
130
124
  else {
131
- if (this.log) {console.log("undeterminate result.");}
132
- console.log("undeterminate result.");
125
+ winston.verbose("(DirIfOnlineAgents) undeterminate result.");
133
126
  callback();
134
127
  }
135
128
  }
@@ -2,8 +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
+ const winston = require('../../utils/winston');
8
+ const httpUtils = require('../../utils/HttpUtils');
7
9
 
8
10
  class DirIfOnlineAgentsV2 {
9
11
 
@@ -28,11 +30,13 @@ class DirIfOnlineAgentsV2 {
28
30
  }
29
31
 
30
32
  execute(directive, callback) {
33
+ winston.verbose("Execute IfOnlineAgentsV2 directive");
31
34
  let action;
32
35
  if (directive.action) {
33
36
  action = directive.action
34
37
  }
35
38
  else {
39
+ winston.warn("DirIfOnlineAgentsV2 Incorrect directive: ", directive);
36
40
  callback();
37
41
  return;
38
42
  }
@@ -42,22 +46,21 @@ class DirIfOnlineAgentsV2 {
42
46
  }
43
47
 
44
48
  async go(action, callback) {
45
- // console.log("(DirIfOnlineAgents) action:", action);
49
+ winston.debug("(DirIfOnlineAgentsV2) Action: ", action);
50
+
46
51
  if (!action.trueIntent && !action.falseIntent) {
47
- if (this.log) {
48
- console.log("Error DirIfOnlineAgents: missing both action.trueIntent & action.falseIntent");
49
- }
52
+ winston.error("(DirIfOnlineAgentsV2) Error: missing both action.trueIntent & action.falseIntent");
50
53
  callback();
51
54
  return;
52
55
  }
53
56
  const trueIntent = action.trueIntent;
54
57
  const falseIntent = action.falseIntent;
55
- if (this.log) {
56
- console.log("(DirIfOnlineAgents) IfOnlineAgents:trueIntent:", trueIntent);
57
- console.log("(DirIfOnlineAgents) IfOnlineAgents:falseIntent:", falseIntent);
58
- }
59
58
  const trueIntentAttributes = action.trueIntentAttributes;
60
59
  const falseIntentAttributes = action.falseIntentAttributes;
60
+
61
+ winston.debug("(DirIfOnlineAgentsV2) IfOnlineAgents:trueIntent: " + trueIntent);
62
+ winston.debug("(DirIfOnlineAgentsV2) IfOnlineAgents:falseIntent: " + falseIntent);
63
+
61
64
  let stopOnConditionMet = true; //action.stopOnConditionMet;
62
65
 
63
66
  try {
@@ -83,18 +86,15 @@ class DirIfOnlineAgentsV2 {
83
86
 
84
87
  let agents;
85
88
  if (selectedOption === "currentDep") {
86
- if (this.log) {console.log("(DirIfOnlineAgents) currentDep"); }
89
+ winston.debug("(DirIfOnlineAgentsV2) currentDep");
87
90
  let departmentId = await this.chatbot.getParameter("department_id");
88
- if (this.log) {console.log("this.context.departmentId:", departmentId);}
91
+ winston.debug("(DirIfOnlineAgentsV2) this.context.departmentId: " + departmentId);
89
92
 
90
93
  if (departmentId) {
91
- if (this.log) {console.log("(DirIfOnlineAgents) agents = await this.getProjectAvailableAgents(", departmentId, ", true);"); }
92
94
  agents = await this.getProjectAvailableAgents(departmentId, true);
93
- if (this.log) {console.log("(DirIfOnlineAgents) agents:", agents); }
94
95
  } else {
95
- console.error("(DirIfOnlineAgents) no departmentId found in attributes");
96
+ winston.error("(DirIfOnlineAgentsV2) no departmentId found in attributes");
96
97
  await this.chatbot.addParameter("flowError", "(If online Agents) No departmentId found in attributes.");
97
- if (this.log) {console.log("(DirIfOnlineAgents) flowError added in attributes", await this.chatbot.getParameter("flowError") ); }
98
98
  if (falseIntent) { // no agents available
99
99
  let intentDirective = DirIntent.intentDirectiveFor(falseIntent, falseIntentAttributes);
100
100
  this.intentDir.execute(intentDirective, () => {
@@ -108,29 +108,23 @@ class DirIfOnlineAgentsV2 {
108
108
  }
109
109
  }
110
110
  }
111
- else if (selectedOption === "selectedDep") {
112
- if (this.log) {console.log("(DirIfOnlineAgents) selectedOption === selectedDep", action.selectedDepartmentId); }
113
- if (this.log) {console.log("(DirIfOnlineAgents) agents = await this.getProjectAvailableAgents(", action.selectedDepartmentId, ", true);"); }
114
-
111
+ else if (selectedOption === "selectedDep") {
115
112
  agents = await this.getProjectAvailableAgents(action.selectedDepartmentId, true);
116
- if (this.log) {console.log("(DirIfOnlineAgents) agents:", agents); }
117
113
  }
118
114
  else { // if (checkAll) => go project-wide
119
- if (this.log) {console.log("(DirIfOnlineAgents) selectedOption === all | getProjectAvailableAgents(null, true)"); }
120
115
  agents = await this.getProjectAvailableAgents(null, true);
121
- if (this.log) {console.log("(DirIfOnlineAgents) agents:", agents); }
122
116
  }
123
117
 
124
118
  if (agents && agents.length > 0) {
125
119
  if (trueIntent) {
126
120
  let intentDirective = DirIntent.intentDirectiveFor(trueIntent, trueIntentAttributes);
127
- if (this.log) {console.log("agents (openHours) => trueIntent");}
121
+ winston.debug("(DirIfOnlineAgentsV2) agents (openHours) => trueIntent");
128
122
  this.intentDir.execute(intentDirective, () => {
129
123
  callback(stopOnConditionMet);
130
124
  });
131
125
  }
132
126
  else {
133
- if (this.log) { console.log("(DirIfOnlineAgents) No IfOnlineAgents trueIntent defined. callback()"); } // prod
127
+ winston.debug("(DirIfOnlineAgentsV2) No IfOnlineAgents trueIntent defined. callback()"); // prod
134
128
  this.chatbot.addParameter("flowError", "(If online Agents) No IfOnlineAgents success path defined.");
135
129
  callback();
136
130
  return;
@@ -138,20 +132,20 @@ class DirIfOnlineAgentsV2 {
138
132
  }
139
133
  else if (falseIntent) { // no agents available
140
134
  let intentDirective = DirIntent.intentDirectiveFor(falseIntent, falseIntentAttributes);
141
- if (this.log) {console.log("(DirIfOnlineAgents) !agents (openHours) => falseIntent", intentDirective);}
135
+ winston.debug("(DirIfOnlineAgentsV2) !agents (openHours) => falseIntent", intentDirective);
142
136
  this.intentDir.execute(intentDirective, () => {
143
137
  callback(stopOnConditionMet);
144
138
  });
145
139
  }
146
140
  else {
147
- if (this.log) {console.log("(DirIfOnlineAgents) Error: No falseIntent defined", intentDirective);}
141
+ winston.error("(DirIfOnlineAgentsV2) Error: No falseIntent defined", intentDirective);
148
142
  this.chatbot.addParameter("flowError", "(If online Agents) No path for 'no available agents' defined.");
149
143
  callback();
150
144
  }
151
145
  } else {
152
146
  if (falseIntent) {
153
147
  let intentDirective = DirIntent.intentDirectiveFor(falseIntent, falseIntentAttributes);
154
- if (this.log) { console.log("!agents (!openHours) => falseIntent"); }
148
+ winston.debug("(DirIfOnlineAgentsV2) !agents (!openHours) => falseIntent");
155
149
  this.intentDir.execute(intentDirective, () => {
156
150
  callback();
157
151
  });
@@ -162,7 +156,7 @@ class DirIfOnlineAgentsV2 {
162
156
  }
163
157
  }
164
158
  catch(err) {
165
- console.error("(DirIfOnlineAgents) An error occurred:" + err);
159
+ winston.error("(DirIfOnlineAgentsV2) An error occurred: ", err);
166
160
  this.chatbot.addParameter("flowError", "(If online Agents) An error occurred: " + err);
167
161
  callback();
168
162
  }
@@ -171,7 +165,7 @@ class DirIfOnlineAgentsV2 {
171
165
  async openNow() {
172
166
  return new Promise( (resolve, reject) => {
173
167
  this.tdClient.openNow(async (err, result) => {
174
- if (this.log) {console.log("(DirIfOnlineAgents) openNow():", result);}
168
+ winston.debug("(DirIfOnlineAgentsV2) openNow(): ", result);
175
169
  if (err) {
176
170
  reject(err);
177
171
  }
@@ -197,7 +191,7 @@ class DirIfOnlineAgentsV2 {
197
191
  // json: true,
198
192
  method: 'GET',
199
193
  };
200
- this.#myrequest(
194
+ httpUtils.request(
201
195
  HTTPREQUEST,
202
196
  function(err, resbody) {
203
197
  if (err) {
@@ -217,54 +211,6 @@ class DirIfOnlineAgentsV2 {
217
211
 
218
212
  }
219
213
 
220
- #myrequest(options, callback) {
221
- if (this.log) {
222
- console.log("API URL:", options.url);
223
- console.log("** Options:", JSON.stringify(options));
224
- }
225
- let axios_options = {
226
- url: options.url,
227
- method: options.method,
228
- params: options.params,
229
- headers: options.headers
230
- }
231
- if (options.json !== null) {
232
- axios_options.data = options.json
233
- }
234
- if (this.log) {
235
- console.log("axios_options:", JSON.stringify(axios_options));
236
- }
237
- if (options.url.startsWith("https:")) {
238
- const httpsAgent = new https.Agent({
239
- rejectUnauthorized: false,
240
- });
241
- axios_options.httpsAgent = httpsAgent;
242
- }
243
- axios(axios_options)
244
- .then((res) => {
245
- if (this.log) {
246
- console.log("Response for url:", options.url);
247
- console.log("Response headers:\n", JSON.stringify(res.headers));
248
- }
249
- if (res && res.status == 200 && res.data) {
250
- if (callback) {
251
- callback(null, res.data);
252
- }
253
- }
254
- else {
255
- if (callback) {
256
- callback(new Error("Response status is not 200"), null);
257
- }
258
- }
259
- })
260
- .catch((error) => {
261
- console.error("(DirIfOnlineAgents) Axios error: ", JSON.stringify(error));
262
- if (callback) {
263
- callback(error, null);
264
- }
265
- });
266
- }
267
-
268
214
  fixToken(token) {
269
215
  if (token.startsWith('JWT ')) {
270
216
  return token