@tiledesk/tiledesk-tybot-connector 0.4.2 → 0.5.0-rc2

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 (87) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/ExtApi.js +3 -13
  3. package/ExtUtil.js +0 -1
  4. package/Logger.js +86 -0
  5. package/TdCache.js +5 -3
  6. package/TiledeskClientTest.js +5 -28
  7. package/TiledeskExpression.js +6 -47
  8. package/index.js +87 -273
  9. package/logs/app.log +12983 -275
  10. package/logs/app1.log +62258 -0
  11. package/logs/app2.log +46280 -0
  12. package/logs/app3.log +28864 -0
  13. package/models/IntentForm.js +30 -44
  14. package/models/IntentsMachineFactory.js +4 -3
  15. package/models/MockBotsDataSource.js +1 -17
  16. package/models/MockTdCache.js +0 -9
  17. package/models/MongodbBotsDataSource.js +29 -53
  18. package/models/MongodbIntentsMachine.js +5 -4
  19. package/models/TiledeskChatbot.js +79 -258
  20. package/models/TiledeskChatbotConst.js +9 -0
  21. package/models/TiledeskChatbotUtil.js +180 -232
  22. package/models/TiledeskIntentsMachine.js +5 -14
  23. package/models/faqKbService.js +0 -1
  24. package/package.json +5 -2
  25. package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +66 -139
  26. package/tiledeskChatbotPlugs/FillParamsChatbotPlug.js +7 -13
  27. package/tiledeskChatbotPlugs/Filler.js +0 -1
  28. package/tiledeskChatbotPlugs/MarkbotChatbotPlug.js +12 -20
  29. package/tiledeskChatbotPlugs/MessagePipeline.js +5 -15
  30. package/tiledeskChatbotPlugs/SplitsChatbotPlug.js +4 -13
  31. package/tiledeskChatbotPlugs/WebhookChatbotPlug.js +21 -89
  32. package/tiledeskChatbotPlugs/directives/DirAddTags.js +15 -98
  33. package/tiledeskChatbotPlugs/directives/DirAiPrompt.js +3 -3
  34. package/tiledeskChatbotPlugs/directives/DirAskGPT.js +32 -57
  35. package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +46 -70
  36. package/tiledeskChatbotPlugs/directives/DirAssign.js +11 -22
  37. package/tiledeskChatbotPlugs/directives/DirAssignFromFunction.js +15 -11
  38. package/tiledeskChatbotPlugs/directives/DirAssistant.js +48 -97
  39. package/tiledeskChatbotPlugs/directives/DirBrevo.js +37 -68
  40. package/tiledeskChatbotPlugs/directives/DirCaptureUserReply.js +8 -18
  41. package/tiledeskChatbotPlugs/directives/DirClose.js +3 -1
  42. package/tiledeskChatbotPlugs/directives/DirCode.js +15 -22
  43. package/tiledeskChatbotPlugs/directives/DirCondition.js +18 -24
  44. package/tiledeskChatbotPlugs/directives/DirConnectBlock.js +9 -13
  45. package/tiledeskChatbotPlugs/directives/DirContactUpdate.js +7 -19
  46. package/tiledeskChatbotPlugs/directives/DirCustomerio.js +30 -57
  47. package/tiledeskChatbotPlugs/directives/DirDeflectToHelpCenter.js +15 -20
  48. package/tiledeskChatbotPlugs/directives/DirDeleteVariable.js +7 -7
  49. package/tiledeskChatbotPlugs/directives/DirDepartment.js +13 -12
  50. package/tiledeskChatbotPlugs/directives/DirDisableInputText.js +2 -6
  51. package/tiledeskChatbotPlugs/directives/DirFireTiledeskEvent.js +4 -4
  52. package/tiledeskChatbotPlugs/directives/DirForm.js +15 -22
  53. package/tiledeskChatbotPlugs/directives/DirGptTask.js +38 -65
  54. package/tiledeskChatbotPlugs/directives/DirHubspot.js +27 -58
  55. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgents.js +19 -26
  56. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgentsV2.js +22 -40
  57. package/tiledeskChatbotPlugs/directives/DirIfOpenHours.js +17 -32
  58. package/tiledeskChatbotPlugs/directives/DirIntent.js +16 -33
  59. package/tiledeskChatbotPlugs/directives/DirJSONCondition.js +14 -13
  60. package/tiledeskChatbotPlugs/directives/DirLockIntent.js +7 -12
  61. package/tiledeskChatbotPlugs/directives/DirMake.js +20 -52
  62. package/tiledeskChatbotPlugs/directives/DirMessage.js +35 -28
  63. package/tiledeskChatbotPlugs/directives/DirMessageToBot.js +10 -20
  64. package/tiledeskChatbotPlugs/directives/DirMoveToAgent.js +4 -2
  65. package/tiledeskChatbotPlugs/directives/DirMoveToUnassigned.js +4 -2
  66. package/tiledeskChatbotPlugs/directives/DirQapla.js +21 -50
  67. package/tiledeskChatbotPlugs/directives/DirRandomReply.js +9 -17
  68. package/tiledeskChatbotPlugs/directives/DirRemoveCurrentBot.js +4 -0
  69. package/tiledeskChatbotPlugs/directives/DirReplaceBot.js +4 -1
  70. package/tiledeskChatbotPlugs/directives/DirReplaceBotV2.js +10 -21
  71. package/tiledeskChatbotPlugs/directives/DirReplaceBotV3.js +9 -19
  72. package/tiledeskChatbotPlugs/directives/DirReply.js +32 -35
  73. package/tiledeskChatbotPlugs/directives/DirReplyV2.js +27 -69
  74. package/tiledeskChatbotPlugs/directives/DirSendEmail.js +8 -9
  75. package/tiledeskChatbotPlugs/directives/DirSendWhatsapp.js +13 -12
  76. package/tiledeskChatbotPlugs/directives/DirSetAttribute.js +7 -4
  77. package/tiledeskChatbotPlugs/directives/DirSetAttributeV2.js +22 -75
  78. package/tiledeskChatbotPlugs/directives/DirSetConversationTags.js +14 -13
  79. package/tiledeskChatbotPlugs/directives/DirUnlockIntent.js +3 -3
  80. package/tiledeskChatbotPlugs/directives/DirWait.js +4 -5
  81. package/tiledeskChatbotPlugs/directives/DirWebRequest.js +13 -48
  82. package/tiledeskChatbotPlugs/directives/DirWebRequestV2.js +11 -76
  83. package/tiledeskChatbotPlugs/directives/DirWebResponse.js +34 -7
  84. package/tiledeskChatbotPlugs/directives/DirWhatsappByAttribute.js +11 -14
  85. package/tiledeskChatbotPlugs/directives/Directives.js +1 -3
  86. package/utils/winston.js +42 -0
  87. package/TdCache copy.js +0 -242
@@ -3,6 +3,7 @@ let https = require("https");
3
3
  const { Filler } = require('../Filler');
4
4
  const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
5
5
  const { DirIntent } = require('./DirIntent');
6
+ const winston = require('../../utils/winston')
6
7
 
7
8
  class DirAssistant {
8
9
  constructor(context) {
@@ -18,23 +19,23 @@ class DirAssistant {
18
19
  }
19
20
 
20
21
  execute(directive, callback) {
22
+ winston.verbose("Execute Assistant directive");
21
23
  let action;
22
24
  if (directive.action) {
23
25
  action = directive.action;
24
26
  }
25
27
  else {
26
- console.error("Incorrect directive:", JSON.stringify(directive));
28
+ winston.warn("Incorrect directive: ", directive);
27
29
  callback();
28
30
  return;
29
31
  }
30
32
  this.go(action, (stop) => {
31
- if (this.log) {console.log("(DirAssistant, stop?", stop); }
32
33
  callback(stop);
33
34
  });
34
35
  }
35
36
 
36
37
  async go(action, callback) {
37
- if (this.log) {console.log("DirAssistant action:", JSON.stringify(action));}
38
+ winston.debug("(DirAssistant) Action: ", action);
38
39
  let requestAttributes = null;
39
40
  if (this.tdcache) {
40
41
  requestAttributes =
@@ -73,7 +74,7 @@ class DirAssistant {
73
74
  }
74
75
  else {
75
76
  // TODO: LOG SETTINGS ERROR
76
- console.error("DirAssistant error: no assistantId.");
77
+ winston.error("(DirAssistant) Error: no assistantId.");
77
78
  callback();
78
79
  return;
79
80
  }
@@ -84,7 +85,7 @@ class DirAssistant {
84
85
  }
85
86
  else {
86
87
  // TODO: LOG SETTINGS ERROR
87
- console.error("DirAssistant error: no prompt.");
88
+ winston.error("(DirAssistant) Error: no prompt.");
88
89
  callback();
89
90
  return;
90
91
  }
@@ -94,7 +95,7 @@ class DirAssistant {
94
95
  assistantId = filler.fill(_assistantId, requestAttributes);
95
96
  }
96
97
  catch(error) {
97
- console.error("Error while filling assistantId:", error);
98
+ winston.debug("(DirAssistant) Error while filling assistantId:", error);
98
99
  }
99
100
 
100
101
  let prompt = _prompt;
@@ -102,14 +103,13 @@ class DirAssistant {
102
103
  prompt = filler.fill(_prompt, requestAttributes);
103
104
  }
104
105
  catch(error) {
105
- console.error("Error while filling prompt:", error);
106
+ winston.debug("(DirAssistant) Error while filling prompt:", error);
106
107
  }
107
108
 
108
- if (this.log) {
109
- console.log("settings ok");
110
- console.log("prompt:", prompt);
111
- console.log("assistantId:", assistantId);
112
- }
109
+ winston.debug("(DirAssistant) settings ok");
110
+ winston.debug("(DirAssistant) prompt: " + prompt);
111
+ winston.debug("(DirAssistant) assistantId: " + assistantId);
112
+
113
113
  // Condition branches
114
114
  let trueIntent = action.trueIntent;
115
115
  let falseIntent = action.falseIntent;
@@ -123,10 +123,9 @@ class DirAssistant {
123
123
  this.timeout = this.#webrequest_timeout(action, 20000, 1, 300000);
124
124
 
125
125
  let apikey = await this.getGPT_APIKEY();
126
- if (this.log) {console.log("apikey:", apikey);}
127
126
  if (!apikey) {
128
127
  const reply = "OpenAI APIKEY is mandatory for ChatGPT Assistants. Add your personal OpenAI APIKEY in Settings > Integrations";
129
- if (this.log) { console.error(reply); };
128
+ winston.debug("(DirAssistant) Error: " + reply)
130
129
  await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, assignErrorTo, reply);
131
130
  if (falseIntent) {
132
131
  await this.#executeCondition(false, trueIntent, null, falseIntent, null);
@@ -142,21 +141,19 @@ class DirAssistant {
142
141
  threadId = requestAttributes[threadIdAttribute];
143
142
  if (!threadId || (threadId && threadId.trim() === '') ) {
144
143
  // create thread if it doesn't exist
145
- if (this.log) {console.log("Creating thread");}
144
+ winston.debug("(DirAssistant) Creating thread");
146
145
  const thread = await this.createThread(apikey);
147
- if (this.log) {console.log("Thread crated.");}
146
+ winston.debug("(DirAssistant) Thread crated.");
148
147
  threadId = thread.id;
149
148
  await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, threadIdAttribute, threadId);
150
- if (this.log) {
151
- console.log("thread:", thread);
152
- console.log("threadId:", threadId);
153
- }
149
+ winston.debug("(DirAssistant) thread: ", thread);
150
+ winston.debug("(DirAssistant) threadId: " + threadId);
154
151
  }
155
152
  else {
156
- if (this.log) { console.log("Reusing threadId (used flow attribute:" + threadIdAttribute + "):", threadId); }
153
+ winston.debug("(DirAssistant) Reusing threadId (used flow attribute: " + threadIdAttribute + "):" + threadId);
157
154
  }
158
155
  await this.addMessage(prompt, threadId, apikey);
159
- if (this.log) {console.log("Message added.");}
156
+ winston.debug("(DirAssistant) Message added.");
160
157
  await this.runThreadOnAssistant(assistantId, threadId, apikey);
161
158
  let messages = await this.threadMessages(threadId, apikey);
162
159
  let lastMessage = null;
@@ -192,7 +189,7 @@ class DirAssistant {
192
189
  }
193
190
  }
194
191
  catch (error) {
195
- if (this.log) { console.error("error:", error); }
192
+ winston.debug("(DirAssistant) error:", error);
196
193
  await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, assignErrorTo, error);
197
194
  if (falseIntent) {
198
195
  await this.#executeCondition(false, trueIntent, null, falseIntent, null);
@@ -221,7 +218,7 @@ class DirAssistant {
221
218
  });
222
219
  }
223
220
  else {
224
- if (this.log) {console.log("No trueIntentDirective specified");}
221
+ winston.debug("(DirAssistant) No trueIntentDirective specified");
225
222
  if (callback) {
226
223
  callback();
227
224
  }
@@ -236,7 +233,7 @@ class DirAssistant {
236
233
  });
237
234
  }
238
235
  else {
239
- if (this.log) {console.log("No falseIntentDirective specified");}
236
+ winston.debug("(DirAssistant) No falseIntentDirective specified");
240
237
  if (callback) {
241
238
  callback();
242
239
  }
@@ -253,7 +250,6 @@ class DirAssistant {
253
250
  if (action.settings.timeout) {
254
251
  if ((typeof action.settings.timeout === "number") && action.settings.timeout > min && action.settings.timeout < max) {
255
252
  timeout = Math.round(action.settings.timeout)
256
- // console.log("new timeout:", timeout);
257
253
  }
258
254
  }
259
255
  return timeout
@@ -269,7 +265,7 @@ class DirAssistant {
269
265
  }
270
266
 
271
267
  async createThread(apikey) {
272
- if (this.log) { console.log("creating thread..."); }
268
+ winston.debug("(DirAssistant) creating thread...");
273
269
  return new Promise( async (resolve, reject) => {
274
270
  const url = "https://api.openai.com/v1/threads";
275
271
  const headers = {
@@ -283,16 +279,16 @@ class DirAssistant {
283
279
  method: "POST",
284
280
  timeout: this.timeout
285
281
  };
286
- if (this.log) {console.log("DirAssistant HTTPREQUEST", HTTPREQUEST);}
282
+ winston.debug("(DirAssistant) DirAssistant HttpRequest", HTTPREQUEST);
287
283
  this.#myrequest(
288
284
  HTTPREQUEST, async (err, res) => {
289
285
  let status = res.status;
290
286
  if (err) {
291
- if (this.log) {console.error("DirAssistant error:", err);}
287
+ winston.error("(DirAssistant) error: ", err);
292
288
  reject(err);
293
289
  }
294
290
  else if(res.status >= 200 && res.status <= 299) {
295
- if (this.log) {console.log("got threadid res:", res);}
291
+ winston.debug("(DirAssistant) got threadid res: ", res);
296
292
  let thread = res.data;
297
293
  resolve(thread)
298
294
  }
@@ -343,16 +339,16 @@ class DirAssistant {
343
339
  method: "POST",
344
340
  timeout: this.timeout
345
341
  };
346
- if (this.log) {console.log("DirAssistant HTTPREQUEST", HTTPREQUEST);}
342
+ winston.debug("(DirAssistant) HttpRequest: ", HTTPREQUEST);
347
343
  this.#myrequest(
348
344
  HTTPREQUEST, async (err, res) => {
349
345
  let status = res.status;
350
346
  if (err) {
351
- if (this.log) {console.error("DirAssistant error:", err);}
347
+ winston.error("(DirAssistant) error: ", err);
352
348
  reject(err);
353
349
  }
354
350
  else if(res.status >= 200 && res.status <= 299) {
355
- if (this.log) {console.log("got response data:", res.data);}
351
+ winston.debug("(DirAddTags) got response data: ", res.data);
356
352
  // let return_body = res.data;
357
353
  resolve();
358
354
  }
@@ -366,23 +362,23 @@ class DirAssistant {
366
362
 
367
363
  async runThreadOnAssistant(assistantId, threadId, apikey) {
368
364
  let _run = await this.createRun(threadId, assistantId, apikey);
369
- if (this.log) {console.log("Got run:", _run);}
365
+ winston.debug("(DirAssistant) Got run: ", _run);
370
366
  let runId = _run.id;
371
- if (this.log) {console.log("runId:", runId);}
367
+ winston.debug("(DirAssistant) runId: " + runId);
372
368
  let status = null;
373
369
  do {
374
- if (this.log) {console.log("Getting run...");}
370
+ winston.debug("(DirAssistant) Getting run...");
375
371
  const wait_for = 2000;
376
- if (this.log) {console.log("Waiting:", wait_for);}
372
+ winston.debug("(DirAssistant) Waiting: " + wait_for);
377
373
  await new Promise(resolve => setTimeout(resolve, wait_for));
378
374
  let run = await this.getRun(threadId, runId, apikey);
379
375
  status = run.status;
380
- if (this.log) {console.log("Run status:", status);}
376
+ winston.debug("(DirAssistant) Run status: " + status);
381
377
  }
382
378
  while (status === "queued" || status === "in_progress" || status === "requires_action" && status === "cancelling");
383
379
  // while (status != "completed" && status != "cancelled" && status != "failed" && status != "expired");
384
380
  // queued, in_progress, requires_action, cancelling
385
- if (this.log) {console.log("Run end.");}
381
+ winston.debug("(DirAssistant) Run end.");
386
382
  }
387
383
 
388
384
  async createRun(threadId, assistantId, apikey) {
@@ -391,7 +387,7 @@ class DirAssistant {
391
387
  }
392
388
 
393
389
  return new Promise( async (resolve, reject) => {
394
- if (this.log) {console.log("adding message to thread...");}
390
+ winston.debug("(DirAssistant) adding message to thread...");
395
391
  const url = `https://api.openai.com/v1/threads/${threadId}/runs`;
396
392
  const headers = {
397
393
  "Authorization": apikey,
@@ -404,16 +400,15 @@ class DirAssistant {
404
400
  method: "POST",
405
401
  timeout: this.timeout
406
402
  };
407
- if (this.log) {console.log("DirAssistant HTTPREQUEST", HTTPREQUEST);}
403
+ winston.debug("(DirAssistant) HttpRequest: ", HTTPREQUEST);
408
404
  this.#myrequest(
409
405
  HTTPREQUEST, async (err, res) => {
410
406
  if (err) {
411
- if (this.log) {console.error("DirAssistant error:", err);}
407
+ winston.error("(DirAssistant) error: ", err);
412
408
  reject(err);
413
409
  }
414
410
  else if(res?.status >= 200 && res?.status <= 299) {
415
- if (this.log) {console.log("got response data:", res.data);}
416
- // let return_body = res.data;
411
+ winston.debug("(DirAddTags) got response data: ", res.data);
417
412
  resolve(res.data);
418
413
  }
419
414
  else {
@@ -439,16 +434,15 @@ class DirAssistant {
439
434
  method: "GET",
440
435
  timeout: this.timeout
441
436
  };
442
- if (this.log) {console.log("DirAssistant HTTPREQUEST", HTTPREQUEST);}
437
+ winston.debug("(DirAssistant) HttpRequest: ", HTTPREQUEST);
443
438
  this.#myrequest(
444
439
  HTTPREQUEST, async (err, res) => {
445
440
  if (err) {
446
- if (this.log) {console.error("DirAssistant error:", err);}
441
+ winston.error("(DirAssistant) error: ", err);
447
442
  reject(err);
448
443
  }
449
444
  else if(res?.status >= 200 && res?.status <= 299) {
450
- if (this.log) {console.log("got response data:", res.data);}
451
- // let return_body = res.data;
445
+ winston.debug("(DirAddTags) got response data: ", res.data);
452
446
  resolve(res.data);
453
447
  }
454
448
  else {
@@ -474,15 +468,15 @@ class DirAssistant {
474
468
  method: "GET",
475
469
  timeout: this.timeout
476
470
  };
477
- if (this.log) {console.log("DirAssistant HTTPREQUEST", HTTPREQUEST);}
471
+ winston.debug("(DirAssistant) HttpRequest: ", HTTPREQUEST);
478
472
  this.#myrequest(
479
473
  HTTPREQUEST, async (err, res) => {
480
474
  if (err) {
481
- if (this.log) {console.error("DirAssistant error:", err);}
475
+ winston.error("(DirAssistant) error: ", err);
482
476
  reject(err);
483
477
  }
484
478
  else if(res?.status >= 200 && res?.status <= 299) {
485
- if (this.log) {console.log("got response data:", res.data);}
479
+ winston.debug("(DirAddTags) got response data: ", res.data);
486
480
  // let return_body = res.data;
487
481
  resolve(res.data);
488
482
  }
@@ -506,7 +500,7 @@ class DirAssistant {
506
500
  },
507
501
  method: "GET"
508
502
  }
509
- if (this.log) { console.log("DirAssistant INTEGRATIONS_HTTPREQUEST ", INTEGRATIONS_HTTPREQUEST) }
503
+ winston.debug("(DirAssistant) Integrations HttpRequest ", INTEGRATIONS_HTTPREQUEST)
510
504
 
511
505
  this.#myrequest(
512
506
  INTEGRATIONS_HTTPREQUEST, async (err, res) => {
@@ -528,24 +522,6 @@ class DirAssistant {
528
522
 
529
523
  #myrequest(options, callback) {
530
524
  try {
531
- if (this.log) {
532
- console.log("API URL:", options.url);
533
- //console.log("** Options:", JSON.stringify(options));
534
- // Stringify "options". FIX THE STRINGIFY OF CIRCULAR STRUCTURE BUG - START
535
- let cache = [];
536
- let str_Options = JSON.stringify(options, function(key, value) { // try to use a separate function
537
- if (typeof value === 'object' && value != null) {
538
- if (cache.indexOf(value) !== -1) {
539
- return;
540
- }
541
- cache.push(value);
542
- }
543
- return value;
544
- });
545
- console.log("** Options:", str_Options);
546
-
547
-
548
- }
549
525
  let axios_options = {
550
526
  url: options.url,
551
527
  method: options.method,
@@ -557,9 +533,7 @@ class DirAssistant {
557
533
  if (options.json !== null) {
558
534
  axios_options.data = options.json
559
535
  }
560
- // if (this.log) {
561
- // console.log("axios_options:", JSON.stringify(axios_options));
562
- // }
536
+
563
537
  if (options.url.startsWith("https:")) {
564
538
  const httpsAgent = new https.Agent({
565
539
  rejectUnauthorized: false,
@@ -569,34 +543,11 @@ class DirAssistant {
569
543
 
570
544
  axios(axios_options)
571
545
  .then((res) => {
572
- if (this.log) {
573
- // console.log("Success Response:", res);
574
- console.log("Response for url:", options.url);
575
- console.log("Response headers:\n", JSON.stringify(res.headers));
576
- }
577
546
  if (callback) {
578
547
  callback(null, res);
579
548
  }
580
549
  })
581
550
  .catch( (err) => {
582
- if (this.log) {
583
- if (err.response) {
584
- console.log("Error Response data:", err.response.data);
585
- }
586
- // FIX THE STRINGIFY OF CIRCULAR STRUCTURE BUG - START
587
- let cache = [];
588
- let error_log = JSON.stringify(err, function(key, value) { // try to use a separate function
589
- if (typeof value === 'object' && value != null) {
590
- if (cache.indexOf(value) !== -1) {
591
- return;
592
- }
593
- cache.push(value);
594
- }
595
- return value;
596
- });
597
- console.error("(DirAssistant) An error occurred: ", error_log);
598
- // FIX THE STRINGIFY OF CIRCULAR STRUCTURE BUG - END
599
- }
600
551
  if (callback) {
601
552
  let status = 1000;
602
553
  let cache = [];
@@ -632,7 +583,7 @@ class DirAssistant {
632
583
  });
633
584
  }
634
585
  catch(error) {
635
- console.error("Error:", error);
586
+ winston.error("(DirAssistant) Axios error: ", error);
636
587
  }
637
588
  }
638
589
 
@@ -4,7 +4,7 @@ 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
8
 
9
9
  class DirBrevo {
10
10
 
@@ -21,13 +21,13 @@ class DirBrevo {
21
21
  }
22
22
 
23
23
  execute(directive, callback) {
24
- if (this.log) { console.log("DirBrevo directive: ", directive); }
24
+ winston.verbose("Execute DirBrevo directive");
25
25
  let action;
26
26
  if (directive.action) {
27
27
  action = directive.action;
28
28
  }
29
29
  else {
30
- console.error("DirBrevo Incorrect directive: ", JSON.stringify(directive));
30
+ winston.warn("(DirBrevo) Incorrect directive: ", directive);
31
31
  callback();
32
32
  return;
33
33
  }
@@ -37,9 +37,9 @@ class DirBrevo {
37
37
  }
38
38
 
39
39
  async go(action, callback) {
40
- if (this.log) { console.log("DirBrevo action:", JSON.stringify(action)); }
40
+ winston.debug("(DirBrevo) Action: ", action);
41
41
  if (!this.tdcache) {
42
- console.error("Error: DirBrevo tdcache is mandatory");
42
+ winston.error("(DirBrevo) Error: tdcache is mandatory");
43
43
  callback();
44
44
  return;
45
45
  }
@@ -49,12 +49,11 @@ class DirBrevo {
49
49
  let trueIntentAttributes = action.trueIntentAttributes;
50
50
  let falseIntentAttributes = action.falseIntentAttributes;
51
51
 
52
- if (this.log) {
53
- console.log("DirBrevo trueIntent", trueIntent)
54
- console.log("DirBrevo falseIntent", falseIntent)
55
- console.log("DirBrevo trueIntentAttributes", trueIntentAttributes)
56
- console.log("DirBrevo falseIntentAttributes", falseIntentAttributes)
57
- }
52
+ winston.debug("(DirBrevo) trueIntent " + trueIntent)
53
+ winston.debug("(DirBrevo) falseIntent " + falseIntent)
54
+ winston.debug("(DirBrevo) trueIntentAttributes " + trueIntentAttributes)
55
+ winston.debug("(DirBrevo) falseIntentAttributes " + falseIntentAttributes)
56
+
58
57
 
59
58
  let requestVariables = null;
60
59
  requestVariables =
@@ -64,25 +63,21 @@ class DirBrevo {
64
63
 
65
64
  //let token = action.token;
66
65
  let bodyParameters = action.bodyParameters;
67
- if (this.log) { console.log("DirBrevo bodyParameters: ", bodyParameters); }
66
+ winston.debug("(DirBrevo) bodyParameters: ", bodyParameters);
68
67
 
69
68
  if (!bodyParameters || bodyParameters === '') {
70
- if (this.log) { console.error("DirBrevo ERROR - bodyParameters is undefined or null or empty string") };
69
+ winston.error("(DirBrevo) Error: bodyParameters is undefined or null or empty string");
71
70
  callback();
72
71
  return;
73
72
  }
74
73
 
75
74
  const brevo_base_url = process.env.BREVO_ENDPOINT || "https://api.brevo.com/v3"
76
- if (this.log) { console.log("DirBrevo brevo_base_url ", brevo_base_url); }
75
+ winston.debug("(DirBrevo) brevo_base_url: " + brevo_base_url);
77
76
 
78
77
  let key = await this.getKeyFromIntegrations();
79
- if (this.log) { console.log('DirBrevo key Debug1: ', key) }
80
- // ONLY FOR DEBUG CANCELLARE!!!!!
81
- // if (process.env.BREVO_DEBUG == '1') {
82
- // key = process.env.BREVO_TOKEN;
83
- // }
78
+ winston.debug("(DirBrevo) key: ", key)
84
79
  if (!key) {
85
- if (this.log) { console.log("DirBrevo - Key not found in Integrations."); }
80
+ winston.debug("(DirBrevo) - Key not found in Integrations.");
86
81
  if (falseIntent) {
87
82
  await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
88
83
  callback(true);
@@ -92,22 +87,22 @@ class DirBrevo {
92
87
 
93
88
  const filler = new Filler();
94
89
  for (const [key, value] of Object.entries(bodyParameters)) {
95
- if (this.log) { console.log("bodyParam:", key, "value:", value) }
90
+ winston.debug("(DirBrevo) bodyParam: " + key + " value: " + value)
96
91
  let filled_value = filler.fill(value, requestVariables);
97
92
  bodyParameters[key] = filled_value;
98
93
  }
99
- if (this.log) { console.log('DirBrevo bodyParameters filler: ', bodyParameters) }
94
+ winston.debug("(DirBrevo) bodyParameters filler: ", bodyParameters)
100
95
 
101
96
  // CREATE THE JSON FOR BREVO
102
97
  let brevo_email = '';
103
98
  let brevo_bodyParameters = {};
104
99
  for (const [key, value] of Object.entries(bodyParameters)) {
105
- if (this.log) { console.log("bodyParam:", key, "value:", value) }
100
+ winston.debug("(DirBrevo) bodyParam: " + key + " value: " + value)
106
101
  if (key === 'email') {brevo_email = value}
107
102
  else { brevo_bodyParameters[key] = value;}
108
103
  }
109
- if (this.log) { console.log('DirBrevo brevo_email: ', brevo_email) }
110
- if (this.log) { console.log('DirBrevo brevo_bodyParameters: ', brevo_bodyParameters) }
104
+ winston.debug("(DirBrevo) brevo_email: " + brevo_email)
105
+ winston.debug("(DirBrevo) brevo_bodyParameters: ", brevo_bodyParameters)
111
106
 
112
107
 
113
108
  let json = {
@@ -123,10 +118,9 @@ class DirBrevo {
123
118
  "info@mytest.com"
124
119
  ]
125
120
  }
126
- if (this.log) { console.log('DirBrevo key Debug2: ', key) }
127
- //----------------
128
- if (this.log) {console.log("DirBrevo brevo_base_url ",brevo_base_url);}
129
- if (this.log) { console.log('DirBrevo json: ', json) }
121
+
122
+ winston.debug("(DirBrevo) brevo_base_url: " + brevo_base_url);
123
+ winston.debug("(DirBrevo) json: ", json);
130
124
  const BREVO_HTTPREQUEST = {
131
125
  url: brevo_base_url + '/contacts',
132
126
  headers: {
@@ -137,16 +131,14 @@ class DirBrevo {
137
131
  json: json,
138
132
  method: "POST"
139
133
  }
140
- if (this.log) { console.log("DirBrevo BREVO_HTTPREQUEST", JSON.stringify(BREVO_HTTPREQUEST)); }
134
+ winston.debug("(DirBrevo) HttpRequest ", BREVO_HTTPREQUEST);
141
135
 
142
136
  this.#myrequest(
143
137
  BREVO_HTTPREQUEST, async (err, resbody) => {
144
138
  if (err) {
145
139
  if (callback) {
146
- if (this.log) {
147
- console.error("(httprequest) DirBrevo err response:", err.response)
148
- console.error("(httprequest) DirBrevo err data:", err.response.data)
149
- };
140
+ winston.debug("(DirBrevo) err response: ", err.response)
141
+ winston.debug("(DirBrevo) err data:", err.response.data)
150
142
 
151
143
  let result = null;
152
144
  let status = null;
@@ -164,9 +156,9 @@ class DirBrevo {
164
156
  }
165
157
 
166
158
  if (this.log) {
167
- console.error("(httprequest) DirBrevo err data result:", result); // CONTROLLA IL VALORE
168
- console.error("(httprequest) DirBrevo err data status:", status);
169
- console.error("(httprequest) DirBrevo err data error:", error);
159
+ winston.error("(DirBrevo) DirBrevo err data result:", result); // CONTROLLA IL VALORE
160
+ winston.error("(DirBrevo) DirBrevo err data status:", status);
161
+ winston.error("(DirBrevo) DirBrevo err data error:", error);
170
162
  }
171
163
 
172
164
  await this.#assignAttributes(action, status, result, error);
@@ -179,7 +171,7 @@ class DirBrevo {
179
171
  return;
180
172
  }
181
173
  } else if (callback) {
182
- if (this.log) { console.log("DirBrevo resbody: ", JSON.stringify(resbody, null, 2).slice(2, -1)); }
174
+ winston.debug("(DirBrevo) resbody: ", resbody);
183
175
 
184
176
  let status = 201;
185
177
  let error = null;
@@ -199,12 +191,10 @@ class DirBrevo {
199
191
  }
200
192
 
201
193
  async #assignAttributes(action, status, result, error) {
202
- if (this.log) {
203
- console.log("DirBrevo assignAttributes action:", action)
204
- console.log("DirBrevo assignAttributes status:", status)
205
- console.log("DirBrevo assignAttributes result:", result)
206
- console.log("DirBrevo assignAttributes error:", error)
207
- }
194
+ winston.debug("(DirBrevo) assignAttributes action: ", action)
195
+ winston.debug("(DirBrevo) assignAttributes status: " + status)
196
+ winston.debug("(DirBrevo) assignAttributes result: ", result)
197
+ winston.debug("(DirBrevo) assignAttributes error: ", error)
208
198
  if (this.context.tdcache) {
209
199
  if (action.assignStatusTo) {
210
200
  await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignStatusTo, status);
@@ -215,22 +205,10 @@ class DirBrevo {
215
205
  if (action.assignErrorTo) {
216
206
  await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignErrorTo, error);
217
207
  }
218
-
219
- // Debug log
220
- if (this.log) {
221
- const all_parameters = await TiledeskChatbot.allParametersStatic(this.context.tdcache, this.context.requestId);
222
- for (const [key, value] of Object.entries(all_parameters)) {
223
- if (this.log) { console.log("DirBrevo request parameter:", key, "value:", value, "type:", typeof value) }
224
- }
225
- }
226
208
  }
227
209
  }
228
210
 
229
211
  #myrequest(options, callback) {
230
- if (this.log) {
231
- console.log("** API URL:", options.url);
232
- console.log("** Options:", JSON.stringify(options));
233
- }
234
212
  let axios_options = {
235
213
  url: options.url,
236
214
  method: options.method,
@@ -240,9 +218,6 @@ class DirBrevo {
240
218
  if (options.json !== null) {
241
219
  axios_options.data = options.json
242
220
  }
243
- if (this.log) {
244
- console.log("axios_options:", JSON.stringify(axios_options));
245
- }
246
221
  if (options.url.startsWith("https:")) {
247
222
  const httpsAgent = new https.Agent({
248
223
  rejectUnauthorized: false,
@@ -251,11 +226,6 @@ class DirBrevo {
251
226
  }
252
227
  axios(axios_options)
253
228
  .then((res) => {
254
- if (this.log) {
255
- console.log("Response for url:", options.url);
256
- console.log("Response headers:\n", JSON.stringify(res.headers));
257
- console.log("Response status:", JSON.stringify(res.status));
258
- }
259
229
  if (res && (res.status == 200 || res.status == 201) && res.data) {
260
230
  if (callback) {
261
231
  callback(null, res.data);
@@ -284,7 +254,6 @@ class DirBrevo {
284
254
  if (falseIntent) {
285
255
  falseIntentDirective = DirIntent.intentDirectiveFor(falseIntent, falseIntentAttributes);
286
256
  }
287
- if (this.log) { console.log('DirBrevo executeCondition/result', result) }
288
257
  if (result === true) {
289
258
  if (trueIntentDirective) {
290
259
  this.intentDir.execute(trueIntentDirective, () => {
@@ -294,7 +263,7 @@ class DirBrevo {
294
263
  });
295
264
  }
296
265
  else {
297
- if (this.log) { console.log("No trueIntentDirective specified"); }
266
+ winston.debug("(DirBrevo) No trueIntentDirective specified");
298
267
  if (callback) {
299
268
  callback();
300
269
  }
@@ -309,7 +278,7 @@ class DirBrevo {
309
278
  });
310
279
  }
311
280
  else {
312
- if (this.log) { console.log("No falseIntentDirective specified"); }
281
+ winston.debug("(DirBrevo) No falseIntentDirective specified");
313
282
  if (callback) {
314
283
  callback();
315
284
  }
@@ -328,7 +297,7 @@ class DirBrevo {
328
297
  },
329
298
  method: "GET"
330
299
  }
331
- if (this.log) { console.log("Brevo INTEGRATIONS_HTTPREQUEST ", INTEGRATIONS_HTTPREQUEST) }
300
+ winston.debug("(DirBrevo) Inteegration HttpRequest ", INTEGRATIONS_HTTPREQUEST)
332
301
 
333
302
  this.#myrequest(
334
303
  INTEGRATIONS_HTTPREQUEST, async (err, integration) => {