@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
@@ -7,6 +7,7 @@ const { TiledeskChatbotConst } = require("../../models/TiledeskChatbotConst");
7
7
  const { TiledeskChatbotUtil } = require("../../models/TiledeskChatbotUtil");
8
8
  const assert = require("assert");
9
9
  require('dotenv').config();
10
+ const winston = require('../../utils/winston');
10
11
 
11
12
  class DirAskGPTV2 {
12
13
 
@@ -24,13 +25,13 @@ class DirAskGPTV2 {
24
25
  }
25
26
 
26
27
  execute(directive, callback) {
27
- if (this.log) { console.log("AskGPT directive: ", directive); }
28
+ winston.debug("DirAskGPTV2 directive: ", directive);
28
29
  let action;
29
30
  if (directive.action) {
30
31
  action = directive.action;
31
32
  }
32
33
  else {
33
- console.error("Incorrect directive: ", JSON.stringify(directive));
34
+ winston.debug("DirAskGPTV2 Incorrect directive: ", directive);
34
35
  callback();
35
36
  return;
36
37
  }
@@ -40,9 +41,9 @@ class DirAskGPTV2 {
40
41
  }
41
42
 
42
43
  async go(action, callback) {
43
- if (this.log) { console.log("DirAskGPT action:", JSON.stringify(action)); }
44
+ winston.debug("DirAskGPTV2 action:", action);
44
45
  if (!this.tdcache) {
45
- console.error("Error: DirAskGPT tdcache is mandatory");
46
+ winston.error("DirAskGPTV2 Error: tdcache is mandatory");
46
47
  callback();
47
48
  return;
48
49
  }
@@ -53,12 +54,11 @@ class DirAskGPTV2 {
53
54
  let trueIntentAttributes = action.trueIntentAttributes;
54
55
  let falseIntentAttributes = action.falseIntentAttributes;
55
56
 
56
- if (this.log) {
57
- console.log("DirAskGPT trueIntent", trueIntent)
58
- console.log("DirAskGPT falseIntent", falseIntent)
59
- console.log("DirAskGPT trueIntentAttributes", trueIntentAttributes)
60
- console.log("DirAskGPT falseIntentAttributes", falseIntentAttributes)
61
- }
57
+ winston.debug("DirAskGPTV2 trueIntent", trueIntent)
58
+ winston.debug("DirAskGPTV2 falseIntent", falseIntent)
59
+ winston.debug("DirAskGPTV2 trueIntentAttributes", trueIntentAttributes)
60
+ winston.debug("DirAskGPTV2 falseIntentAttributes", falseIntentAttributes)
61
+
62
62
 
63
63
  // default values
64
64
  let answer = "No answers";
@@ -83,7 +83,7 @@ class DirAskGPTV2 {
83
83
  let source = null;
84
84
 
85
85
  if (!action.question || action.question === '') {
86
- console.error("Error: DirAskGPT question attribute is mandatory. Executing condition false...");
86
+ winston.error("DirAskGPTV2 Error: question attribute is mandatory. Executing condition false...");
87
87
  await this.#assignAttributes(action, answer, source);
88
88
  if (falseIntent) {
89
89
  await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
@@ -131,33 +131,33 @@ class DirAskGPTV2 {
131
131
  this.context.requestId,
132
132
  TiledeskChatbotConst.REQ_TRANSCRIPT_KEY
133
133
  )
134
- if (this.log) { console.log("DirAskGPT transcript string: ", transcript_string) }
134
+ winston.debug("DirAskGPTV2 transcript string: " + transcript_string)
135
135
 
136
136
  if (transcript_string) {
137
137
  transcript = await TiledeskChatbotUtil.transcriptJSON(transcript_string);
138
- if (this.log) { console.log("DirAskGPT transcript ", transcript) }
138
+ winston.debug("DirAskGPTV2 transcript ", transcript)
139
139
  } else {
140
- if (this.log) { console.log("DirAskGPT transcript_string is undefined. Skip JSON translation for chat history") }
140
+ winston.debug("DirAskGPT transcript_string is undefined. Skip JSON translation for chat history")
141
141
  }
142
142
  }
143
143
 
144
144
  const kb_endpoint = process.env.KB_ENDPOINT_QA
145
- if (this.log) { console.log("DirAskGPT KbEndpoint URL: ", kb_endpoint); }
145
+ winston.verbose("DirAskGPTV2 KbEndpoint URL: " + kb_endpoint);
146
146
 
147
147
  let key = await this.getKeyFromIntegrations();
148
148
  if (!key) {
149
- if (this.log) { console.log("DirAskGPT - Key not found in Integrations. Searching in kb settings..."); }
149
+ winston.verbose("DirAskGPTV2 - Key not found in Integrations. Searching in kb settings...");
150
150
  key = await this.getKeyFromKbSettings();
151
151
  }
152
152
 
153
153
  if (!key) {
154
- if (this.log) { console.log("DirAskGPT - Retrieve public gptkey")}
154
+ winston.verbose("DirAskGPTV2 - Retrieve public gptkey")
155
155
  key = process.env.GPTKEY;
156
156
  publicKey = true;
157
157
  }
158
158
 
159
159
  if (!key) {
160
- console.error("Error: DirAskGPT gptkey is mandatory");
160
+ winston.info("DirAskGPTV2 Error: gptkey is mandatory");
161
161
  await this.#assignAttributes(action, answer);
162
162
  if (falseIntent) {
163
163
  await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
@@ -171,7 +171,7 @@ class DirAskGPTV2 {
171
171
  if (publicKey === true) {
172
172
  let keep_going = await this.checkQuoteAvailability();
173
173
  if (keep_going === false) {
174
- if (this.log) { console.log("DirAskGPT - Quota exceeded for tokens. Skip the action")}
174
+ winston.verbose("DirAskGPTV2 - Quota exceeded for tokens. Skip the action")
175
175
  await this.chatbot.addParameter("flowError", "AskGPT Error: tokens quota exceeded");
176
176
  await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
177
177
  callback(true);
@@ -186,7 +186,7 @@ class DirAskGPTV2 {
186
186
  const filled_namespace = filler.fill(action.namespace, requestVariables)
187
187
  ns = await this.getNamespace(filled_namespace, null);
188
188
  namespace = ns?.id;
189
- if (this.log) { console.log("DirAskGPT - Retrieved namespace id from name ", namespace); }
189
+ winston.verbose("DirAskGPTV2 - Retrieved namespace id from name " + namespace);
190
190
  } else {
191
191
  ns = await this.getNamespace(null, namespace);
192
192
  }
@@ -210,7 +210,7 @@ class DirAskGPTV2 {
210
210
  }
211
211
 
212
212
  if (!namespace) {
213
- console.log("DirAskGPT - Error: namespace is undefined")
213
+ winston.verbose("DirAskGPTV2 - Error: namespace is undefined")
214
214
  if (falseIntent) {
215
215
  await this.chatbot.addParameter("flowError", "AskGPT Error: namespace is undefined");
216
216
  await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
@@ -251,7 +251,7 @@ class DirAskGPTV2 {
251
251
  json.chat_history_dict = await this.transcriptToLLM(transcript);
252
252
  }
253
253
 
254
- if (this.log) { console.log("DirAskGPT json:", json); }
254
+ winston.debug("DirAskGPTV2 json:", json);
255
255
 
256
256
  const HTTPREQUEST = {
257
257
  url: kb_endpoint + "/qa",
@@ -262,20 +262,13 @@ class DirAskGPTV2 {
262
262
  json: json,
263
263
  method: "POST"
264
264
  }
265
- if (this.log) { console.log("DirAskGPT HTTPREQUEST", HTTPREQUEST); }
265
+ winston.debug("DirAskGPTV2 HttpRequest: ", HTTPREQUEST);
266
266
 
267
267
  this.#myrequest(
268
268
  HTTPREQUEST, async (err, resbody) => {
269
- if (this.log && err) {
270
- console.log("DirAskGPT error: ", err);
271
- }
272
- if (this.log) { console.log("DirAskGPT resbody:", resbody); }
273
269
 
274
- // let answer = resbody.answer;
275
- // let source = resbody.source;
276
- // await this.#assignAttributes(action, answer, source);
277
-
278
270
  if (err) {
271
+ winston.error("DirAskGPTV2 error: ", err?.respose);
279
272
  await this.#assignAttributes(action, answer, source);
280
273
  if (callback) {
281
274
  if (falseIntent) {
@@ -288,7 +281,7 @@ class DirAskGPTV2 {
288
281
  }
289
282
  }
290
283
  else if (resbody.success === true) {
291
-
284
+ winston.debug("DirAskGPTV2 resbody: ", resbody);
292
285
  await this.#assignAttributes(action, resbody.answer, resbody.source);
293
286
  if (publicKey === true) {
294
287
  let tokens_usage = {
@@ -337,7 +330,7 @@ class DirAskGPTV2 {
337
330
  })
338
331
  }
339
332
  else {
340
- if (this.log) { console.log("No trueIntentDirective specified"); }
333
+ winston.debug("DirAskGPTV2 No trueIntentDirective specified");
341
334
  if (callback) {
342
335
  callback();
343
336
  }
@@ -352,7 +345,7 @@ class DirAskGPTV2 {
352
345
  });
353
346
  }
354
347
  else {
355
- if (this.log) { console.log("No falseIntentDirective specified"); }
348
+ winston.debug("DirAskGPTV2 No falseIntentDirective specified");
356
349
  if (callback) {
357
350
  callback();
358
351
  }
@@ -361,11 +354,9 @@ class DirAskGPTV2 {
361
354
  }
362
355
 
363
356
  async #assignAttributes(action, answer, source) {
364
- if (this.log) {
365
- console.log("assignAttributes action:", action)
366
- console.log("assignAttributes answer:", answer)
367
- console.log("assignAttributes source:", source)
368
- }
357
+ winston.debug("DirAskGPTV2assignAttributes action: ", action)
358
+ winston.debug("DirAskGPTV2assignAttributes answer: ", answer)
359
+ winston.debug("DirAskGPTV2assignAttributes source: ", source)
369
360
  if (this.context.tdcache) {
370
361
  if (action.assignReplyTo && answer) {
371
362
  await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignReplyTo, answer);
@@ -373,21 +364,12 @@ class DirAskGPTV2 {
373
364
  if (action.assignSourceTo && source) {
374
365
  await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignSourceTo, source);
375
366
  }
376
- // Debug log
377
- if (this.log) {
378
- const all_parameters = await TiledeskChatbot.allParametersStatic(this.context.tdcache, this.context.requestId);
379
- for (const [key, value] of Object.entries(all_parameters)) {
380
- if (this.log) { console.log("(askgpt) request parameter:", key, "value:", value, "type:", typeof value) }
381
- }
382
- }
383
367
  }
384
368
  }
385
369
 
386
370
  #myrequest(options, callback) {
387
- if (this.log) {
388
- console.log("API URL:", options.url);
389
- console.log("** Options:", JSON.stringify(options));
390
- }
371
+ winston.debug("DirAskGPTV2 API URL:", options.url);
372
+ winston.debug("DirAskGPTV2 Options:", options);
391
373
  let axios_options = {
392
374
  url: options.url,
393
375
  method: options.method,
@@ -397,9 +379,6 @@ class DirAskGPTV2 {
397
379
  if (options.json !== null) {
398
380
  axios_options.data = options.json
399
381
  }
400
- if (this.log) {
401
- console.log("axios_options:", JSON.stringify(axios_options));
402
- }
403
382
  if (options.url.startsWith("https:")) {
404
383
  const httpsAgent = new https.Agent({
405
384
  rejectUnauthorized: false,
@@ -408,10 +387,8 @@ class DirAskGPTV2 {
408
387
  }
409
388
  axios(axios_options)
410
389
  .then((res) => {
411
- if (this.log) {
412
- console.log("Response for url:", options.url);
413
- console.log("Response headers:\n", JSON.stringify(res.headers));
414
- }
390
+ winston.debug("DirAskGPTV2 Response for url: " + options.url);
391
+ winston.debug("DirAskGPTV2 Response headers: \n", res.headers);
415
392
  if (res && res.status == 200 && res.data) {
416
393
  if (callback) {
417
394
  callback(null, res.data);
@@ -424,7 +401,7 @@ class DirAskGPTV2 {
424
401
  }
425
402
  })
426
403
  .catch((error) => {
427
- console.error("(DirAskGPT) Axios error: ", JSON.stringify(error));
404
+ winston.error("DirAskGPTV2 Axios error: ", error.response.data);
428
405
  if (callback) {
429
406
  callback(error, null);
430
407
  }
@@ -442,12 +419,12 @@ class DirAskGPTV2 {
442
419
  },
443
420
  method: "GET"
444
421
  }
445
- if (this.log) { console.log("DirAskGPT INTEGRATIONS_HTTPREQUEST ", INTEGRATIONS_HTTPREQUEST) }
422
+ winston.debug("DirAskGPTV2 Integration HttpRequest ", INTEGRATIONS_HTTPREQUEST)
446
423
 
447
424
  this.#myrequest(
448
425
  INTEGRATIONS_HTTPREQUEST, async (err, integration) => {
449
426
  if (err) {
450
- if (this.log) { console.error("DirAskGPT Get integrations error ", err); }
427
+ winston.error("DirAskGPTV2 Get integrations error ", err);
451
428
  resolve(null);
452
429
  } else {
453
430
 
@@ -474,12 +451,12 @@ class DirAskGPTV2 {
474
451
  },
475
452
  method: "GET"
476
453
  }
477
- if (this.log) { console.log("DirAskGPT KB_HTTPREQUEST", KB_HTTPREQUEST); }
454
+ winston.debug("DirAskGPTV2 KB HttpRequest", KB_HTTPREQUEST);
478
455
 
479
456
  this.#myrequest(
480
457
  KB_HTTPREQUEST, async (err, resbody) => {
481
458
  if (err) {
482
- if (this.log) { console.error("DirAskGPT Get kb settings error ", err); }
459
+ winston.error("DirAskGPTV2 Get kb settings error ", err?.response?.data);
483
460
  resolve(null);
484
461
  } else {
485
462
  if (!resbody.gptkey) {
@@ -504,12 +481,12 @@ class DirAskGPTV2 {
504
481
  },
505
482
  method: "GET"
506
483
  }
507
- if (this.log) { console.log("DirAskGPT check quote availability HTTPREQUEST", HTTPREQUEST); }
484
+ winston.debug("DirAskGPTV2 check quote availability HttpRequest", HTTPREQUEST);
508
485
 
509
486
  this.#myrequest(
510
487
  HTTPREQUEST, async (err, resbody) => {
511
488
  if (err) {
512
- console.error("(httprequest) DirAskGPT Check quote availability err: ", err);
489
+ winston.error("DirAskGPTV2 Check quote availability err: ", err);
513
490
  resolve(true)
514
491
  } else {
515
492
  if (resbody.isAvailable === true) {
@@ -535,15 +512,14 @@ class DirAskGPTV2 {
535
512
  json: tokens_usage,
536
513
  method: "POST"
537
514
  }
538
- if (this.log) { console.log("DirAskGPT check quote availability HTTPREQUEST", HTTPREQUEST); }
515
+ winston.debug("DirAskGPTV2 update quote HttpRequest ", HTTPREQUEST);
539
516
 
540
517
  this.#myrequest(
541
518
  HTTPREQUEST, async (err, resbody) => {
542
519
  if (err) {
543
- console.error("(httprequest) DirAskGPT Increment tokens quote err: ", err);
520
+ winston.error("DirAskGPTV2 Increment tokens quote err: ", err);
544
521
  reject(false)
545
522
  } else {
546
- // console.log("(httprequest) DirAskGPT Increment token quote resbody: ", resbody);
547
523
  resolve(true);
548
524
  }
549
525
  }
@@ -609,14 +585,14 @@ class DirAskGPTV2 {
609
585
  },
610
586
  method: "GET"
611
587
  }
612
- if (this.log) { console.log("DirAskGPT get all namespaces HTTPREQUEST", HTTPREQUEST); }
588
+ winston.debug("DirAskGPTV2 get all namespaces HttpRequest", HTTPREQUEST);
613
589
  this.#myrequest(
614
590
  HTTPREQUEST, async (err, namespaces) => {
615
591
  if (err) {
616
- console.error("(httprequest) DirAskGPT get all namespaces err: ", err);
592
+ winston.error("DirAskGPTV2 get all namespaces err: ", err);
617
593
  resolve(null)
618
594
  } else {
619
- if (this.log) { console.log("(httprequest) DirAskGPT get all namespaces resbody: ", namespaces); }
595
+ winston.debug("DirAskGPTV2 get all namespaces resbody: ", namespaces);
620
596
  if (name) {
621
597
  let namespace = namespaces.find(n => n.name === name);
622
598
  resolve(namespace);
@@ -2,6 +2,7 @@ const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
2
2
  const { TiledeskExpression } = require('../../TiledeskExpression');
3
3
  const ms = require('minimist-string');
4
4
  const { Filler } = require('../Filler');
5
+ const winston = require('../../utils/winston')
5
6
 
6
7
  class DirAssign {
7
8
 
@@ -14,6 +15,7 @@ class DirAssign {
14
15
  }
15
16
 
16
17
  execute(directive, callback) {
18
+ winston.verbose("Execute Assign directive");
17
19
  let action;
18
20
  if (directive.action) {
19
21
  action = directive.action
@@ -21,88 +23,75 @@ class DirAssign {
21
23
  else if (directive.parameter) {
22
24
  let params;
23
25
  params = this.parseParams(directive.parameter);
24
- // console.log("DirAssign params:", params);
25
26
  action = {
26
27
  expression: params.expression,
27
28
  assignTo: params.assignTo
28
29
  }
29
- // console.log("DirAssign action:", action);
30
30
  }
31
31
  else {
32
+ winston.warn("Incorrect directive: ", directive);
32
33
  callback();
33
34
  return;
34
35
  }
35
- // console.log("go DirAssign with action:", action);
36
36
  this.go(action, () => {
37
37
  callback();
38
38
  });
39
39
  }
40
40
 
41
41
  async go(action, callback) {
42
- // console.log("DirAssign action processing:", action)
43
- // const expression = action.body.expression;
44
- // const assignTo = action.body.assignTo;
42
+ winston.debug("(DirAssign) Action: ", action);
45
43
  const expression = action.expression;
46
44
  const assignTo = action.assignTo;
47
45
  if (assignTo === null || expression === null) {
48
- if (this.log) {console.log("Invalid expression or assignTo parameters");}
46
+ winston.warn("Invalid expression or assignTo parameters");
49
47
  callback();
50
48
  return;
51
49
  }
52
50
 
53
51
  if (this.context.tdcache) {
54
- if (this.log) {console.log("(DirAssign) this.requestId:", this.context.requestId);}
52
+ winston.debug("(DirAssign) this.requestId: " + this.context.requestId);
55
53
  let variables =
56
54
  await TiledeskChatbot.allParametersStatic(
57
55
  this.context.tdcache, this.context.requestId);
58
56
  // filling
59
57
  let variableName;
60
58
  const filler = new Filler();
61
- // console.log("assign variable name:", variableName);
62
59
  variableName = filler.fill(assignTo, variables);
63
- // console.log("assign variable name (after filling):", variableName);
64
60
 
65
- if (this.log) {console.log("(DirAssign) Variables:", JSON.stringify(variables));}
61
+ winston.debug("(DirAssign) Variables: ", variables);
66
62
  // const value = await new TiledeskExpression().evaluateExpression(expression, variables);
67
63
  const value = new TiledeskExpression().evaluateExpression(expression, variables);
68
- if (this.log) {console.log("(DirAssign) executed expression:", expression, "value:", value);}
64
+ winston.debug("(DirAssign) executed expression: " + expression + " value: " + value);
69
65
  await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, variableName, value);
70
66
  if (this.log) {
71
- console.log("(DirAssign) Assigned:", variableName, "=", value);
67
+ winston.debug("(DirAssign) Assigned: " + variableName + " = " + value);
72
68
  const all_parameters = await TiledeskChatbot.allParametersStatic(this.context.tdcache, this.context.requestId);
73
69
  for (const [key, value] of Object.entries(all_parameters)) {
74
70
  const value_type = typeof value;
75
- if (this.log) {console.log("(DirAssign) request parameter:", key, "value:", value, "type:", value_type)}
71
+ winston.debug("(DirAssign) request parameter: " + key + " value: " + value + " type: " + value_type)
76
72
  }
77
73
  }
78
74
 
79
75
  callback();
80
76
  }
81
77
  else {
82
- console.error("(DirAssign) No this.context.tdcache");
78
+ winston.error("(DirAssign) No this.context.tdcache");
83
79
  callback();
84
80
  }
85
81
  }
86
82
 
87
83
  // async evaluateExpression(_expression, variables) {
88
84
  // let expression = _expression.replace("$", "$data.");
89
- // console.log("Evaluating expression:", expression);
90
- // console.log("With variables:", variables);
91
85
  // const result = new TiledeskExpression().evaluate(expression, variables);
92
- // console.log("Expression result:", result);
93
86
  // return result;
94
87
  // }
95
88
 
96
89
  parseParams(directive_parameter) {
97
- // console.log("Parsing directive_parameter:", directive_parameter);
98
90
  let expression = null;
99
91
  let assignTo = null;
100
92
  const params = ms(directive_parameter);
101
- // console.log("params dirassign:", params)
102
93
  if (params.expression !== null) {
103
- // console.log("go expression:", params.expression)
104
94
  expression = params.expression
105
- // console.log("got it expression:", expression)
106
95
  }
107
96
  if (params.assignTo !== null) {
108
97
  assignTo = params.assignTo;
@@ -1,6 +1,7 @@
1
1
  const { TiledeskClient } = require('@tiledesk/tiledesk-client');
2
2
  const ms = require('minimist-string');
3
3
  const { TiledeskChatbot } = require('../../models/TiledeskChatbot.js');
4
+ const winston = require('../../utils/winston')
4
5
 
5
6
  class DirAssignFromFunction {
6
7
 
@@ -23,6 +24,7 @@ class DirAssignFromFunction {
23
24
  }
24
25
 
25
26
  async execute(directive, callback) {
27
+ winston.verbose("Execute AssignFromFunction directive");
26
28
  let action;
27
29
  if (directive.action) {
28
30
  action = directive.action
@@ -36,29 +38,32 @@ class DirAssignFromFunction {
36
38
  functionName: params.functionName,
37
39
  assignTo: params.assignTo
38
40
  }
41
+ } else {
42
+ winston.warn("Incorrect directive: ", directive);
43
+ callback();
44
+ return;
39
45
  }
40
- console.log("execute assign");
41
46
  this.go(action, () => {
42
- console.log("assign end.");
43
47
  callback();
44
48
  });
45
49
  }
46
50
 
47
51
  async go(action, callback) {
48
- console.log("invoke function => assign action:", action);
52
+ winston.debug("(DirAssignFromFunction) Action: ", action);
49
53
  // const functionName = action.body.functionName;
50
54
  // const variableName = action.body.assignTo;
51
55
  const functionName = action.functionName;
52
56
  const variableName = action.assignTo;
53
- console.log("functionName:", functionName)
54
- console.log("variableName:", variableName)
57
+
58
+ winston.debug("(DirAssignFromFunction) functionName: " + functionName)
59
+ winston.debug("(DirAssignFromFunction) variableName: " + variableName)
55
60
  this.invoke(functionName, async (err, value) => {
56
61
  if (!err) {
57
62
  await TiledeskChatbot.addParameterStatic(this.tdcache, this.context.requestId, variableName, value);
58
- console.log("Assigned:", value, "to", variableName);
63
+ winston.debug("(DirAssignFromFunction) Assigned: " + value + "to" + variableName);
59
64
  }
60
65
  else {
61
- console.error("invoke() error:", err);
66
+ winston.error("(DirAssignFromFunction) invoke() error: ", err);
62
67
  }
63
68
  callback();
64
69
  });
@@ -68,7 +73,6 @@ class DirAssignFromFunction {
68
73
  switch (functionName) {
69
74
  case "openNow":
70
75
  this.tdClient.openNow((err, result) => {
71
- if (this.log) {console.log("openNow():", result);}
72
76
  if (err) {
73
77
  callback(err);
74
78
  }
@@ -82,13 +86,13 @@ class DirAssignFromFunction {
82
86
  break;
83
87
  case "availableAgents":
84
88
  this.tdClient.getProjectAvailableAgents((err, agents) => {
85
- if (this.log) {console.log("Agents on 'open'", agents);}
89
+ winston.debug("(DirAssignFromFunction) Agents on 'open' ", agents);
86
90
  if (err || !agents) {
87
- console.error("Error getting available agents in DirWhenAvailableAgents", err);
91
+ winston.error("(DirAssignFromFunction) Error getting available agents in DirWhenAvailableAgents", err);
88
92
  callback(err, 0);
89
93
  }
90
94
  else {
91
- if (this.log) {console.log("Agents count:", agents.length);}
95
+ winston.debug("(DirAssignFromFunction) Agents count: " + agents.length);
92
96
  // if (agents.length === 0) {
93
97
  // else if (agents.length > 0 && this.checkAgents) {
94
98
  callback(null, agents.length);