@tiledesk/tiledesk-tybot-connector 0.5.0-rc1 → 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 (83) hide show
  1. package/ExtApi.js +3 -13
  2. package/ExtUtil.js +0 -1
  3. package/TdCache.js +5 -3
  4. package/TiledeskClientTest.js +5 -28
  5. package/TiledeskExpression.js +6 -47
  6. package/index.js +87 -273
  7. package/logs/app.log +12983 -275
  8. package/logs/app1.log +62258 -0
  9. package/logs/app2.log +46280 -0
  10. package/logs/app3.log +28864 -0
  11. package/models/IntentForm.js +30 -44
  12. package/models/IntentsMachineFactory.js +4 -3
  13. package/models/MockBotsDataSource.js +1 -17
  14. package/models/MockTdCache.js +0 -9
  15. package/models/MongodbBotsDataSource.js +29 -53
  16. package/models/MongodbIntentsMachine.js +5 -4
  17. package/models/TiledeskChatbot.js +79 -258
  18. package/models/TiledeskChatbotConst.js +9 -0
  19. package/models/TiledeskChatbotUtil.js +79 -230
  20. package/models/TiledeskIntentsMachine.js +5 -14
  21. package/models/faqKbService.js +0 -1
  22. package/package.json +4 -2
  23. package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +54 -140
  24. package/tiledeskChatbotPlugs/FillParamsChatbotPlug.js +7 -13
  25. package/tiledeskChatbotPlugs/Filler.js +0 -1
  26. package/tiledeskChatbotPlugs/MarkbotChatbotPlug.js +12 -20
  27. package/tiledeskChatbotPlugs/MessagePipeline.js +5 -15
  28. package/tiledeskChatbotPlugs/SplitsChatbotPlug.js +4 -13
  29. package/tiledeskChatbotPlugs/WebhookChatbotPlug.js +21 -89
  30. package/tiledeskChatbotPlugs/directives/DirAddTags.js +15 -98
  31. package/tiledeskChatbotPlugs/directives/DirAiPrompt.js +3 -3
  32. package/tiledeskChatbotPlugs/directives/DirAskGPT.js +32 -57
  33. package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +46 -70
  34. package/tiledeskChatbotPlugs/directives/DirAssign.js +11 -22
  35. package/tiledeskChatbotPlugs/directives/DirAssignFromFunction.js +15 -11
  36. package/tiledeskChatbotPlugs/directives/DirAssistant.js +48 -97
  37. package/tiledeskChatbotPlugs/directives/DirBrevo.js +37 -68
  38. package/tiledeskChatbotPlugs/directives/DirCaptureUserReply.js +8 -18
  39. package/tiledeskChatbotPlugs/directives/DirClose.js +3 -1
  40. package/tiledeskChatbotPlugs/directives/DirCode.js +15 -22
  41. package/tiledeskChatbotPlugs/directives/DirCondition.js +18 -24
  42. package/tiledeskChatbotPlugs/directives/DirConnectBlock.js +9 -13
  43. package/tiledeskChatbotPlugs/directives/DirContactUpdate.js +7 -19
  44. package/tiledeskChatbotPlugs/directives/DirCustomerio.js +30 -57
  45. package/tiledeskChatbotPlugs/directives/DirDeflectToHelpCenter.js +15 -20
  46. package/tiledeskChatbotPlugs/directives/DirDeleteVariable.js +7 -7
  47. package/tiledeskChatbotPlugs/directives/DirDepartment.js +13 -12
  48. package/tiledeskChatbotPlugs/directives/DirDisableInputText.js +2 -6
  49. package/tiledeskChatbotPlugs/directives/DirFireTiledeskEvent.js +4 -4
  50. package/tiledeskChatbotPlugs/directives/DirForm.js +15 -22
  51. package/tiledeskChatbotPlugs/directives/DirGptTask.js +38 -65
  52. package/tiledeskChatbotPlugs/directives/DirHubspot.js +27 -58
  53. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgents.js +19 -26
  54. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgentsV2.js +22 -40
  55. package/tiledeskChatbotPlugs/directives/DirIfOpenHours.js +17 -32
  56. package/tiledeskChatbotPlugs/directives/DirIntent.js +10 -28
  57. package/tiledeskChatbotPlugs/directives/DirJSONCondition.js +14 -13
  58. package/tiledeskChatbotPlugs/directives/DirLockIntent.js +7 -12
  59. package/tiledeskChatbotPlugs/directives/DirMake.js +20 -52
  60. package/tiledeskChatbotPlugs/directives/DirMessage.js +35 -28
  61. package/tiledeskChatbotPlugs/directives/DirMessageToBot.js +10 -20
  62. package/tiledeskChatbotPlugs/directives/DirMoveToAgent.js +4 -2
  63. package/tiledeskChatbotPlugs/directives/DirMoveToUnassigned.js +4 -2
  64. package/tiledeskChatbotPlugs/directives/DirQapla.js +21 -50
  65. package/tiledeskChatbotPlugs/directives/DirRandomReply.js +9 -17
  66. package/tiledeskChatbotPlugs/directives/DirRemoveCurrentBot.js +4 -0
  67. package/tiledeskChatbotPlugs/directives/DirReplaceBot.js +4 -1
  68. package/tiledeskChatbotPlugs/directives/DirReplaceBotV2.js +10 -21
  69. package/tiledeskChatbotPlugs/directives/DirReplaceBotV3.js +9 -19
  70. package/tiledeskChatbotPlugs/directives/DirReply.js +18 -34
  71. package/tiledeskChatbotPlugs/directives/DirReplyV2.js +25 -69
  72. package/tiledeskChatbotPlugs/directives/DirSendEmail.js +8 -9
  73. package/tiledeskChatbotPlugs/directives/DirSendWhatsapp.js +13 -12
  74. package/tiledeskChatbotPlugs/directives/DirSetAttribute.js +7 -4
  75. package/tiledeskChatbotPlugs/directives/DirSetAttributeV2.js +22 -75
  76. package/tiledeskChatbotPlugs/directives/DirSetConversationTags.js +14 -13
  77. package/tiledeskChatbotPlugs/directives/DirUnlockIntent.js +3 -3
  78. package/tiledeskChatbotPlugs/directives/DirWait.js +4 -5
  79. package/tiledeskChatbotPlugs/directives/DirWebRequest.js +13 -48
  80. package/tiledeskChatbotPlugs/directives/DirWebRequestV2.js +11 -76
  81. package/tiledeskChatbotPlugs/directives/DirWhatsappByAttribute.js +11 -14
  82. package/tiledeskChatbotPlugs/directives/Directives.js +0 -3
  83. package/utils/winston.js +42 -0
@@ -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 DirWebRequestV2 {
8
9
 
@@ -19,12 +20,13 @@ class DirWebRequestV2 {
19
20
  }
20
21
 
21
22
  execute(directive, callback) {
23
+ winston.verbose("Execute WebRequestV2 directive");
22
24
  let action;
23
25
  if (directive.action) {
24
26
  action = directive.action;
25
27
  }
26
28
  else {
27
- console.error("Incorrect directive:", JSON.stringify(directive));
29
+ winston.warn("DirWebRequestV2 Incorrect directive: ", directive);
28
30
  callback();
29
31
  return;
30
32
  }
@@ -36,7 +38,8 @@ class DirWebRequestV2 {
36
38
  }
37
39
 
38
40
  async go(action, callback) {
39
- if (this.log) { console.log("DirWebRequest action:", JSON.stringify(action)); }
41
+ winston.debug("(DirWebRequestV2) Action: ", action);
42
+
40
43
  if (!this.tdcache) {
41
44
  console.error("Error: DirWebRequest tdcache is mandatory");
42
45
  callback();
@@ -101,9 +104,6 @@ class DirWebRequestV2 {
101
104
 
102
105
  this.#myrequest(
103
106
  HTTPREQUEST, async (err, res) => {
104
- if (this.log && err) {
105
- console.log("webRequest error:", err);
106
- }
107
107
  if (this.log) { console.log("DirWebRequest res:", res); }
108
108
  let resbody = res.data;
109
109
  let status = res.status;
@@ -238,7 +238,7 @@ class DirWebRequestV2 {
238
238
  });
239
239
  }
240
240
  else {
241
- if (this.log) { console.log("No trueIntentDirective specified"); }
241
+ winston.debug("(DirWebRequestV2) No trueIntentDirective specified");
242
242
  if (callback) {
243
243
  callback();
244
244
  }
@@ -253,7 +253,7 @@ class DirWebRequestV2 {
253
253
  });
254
254
  }
255
255
  else {
256
- if (this.log) { console.log("No falseIntentDirective specified"); }
256
+ winston.debug("(DirWebRequestV2) No falseIntentDirective specified");
257
257
  if (callback) {
258
258
  callback();
259
259
  }
@@ -262,53 +262,22 @@ class DirWebRequestV2 {
262
262
  }
263
263
 
264
264
  async #assignAttributes(action, resbody, status, error) {
265
- if (this.log) {
266
- console.log("assignAttributes resbody:", resbody)
267
- console.log("assignAttributes error:", error)
268
- console.log("assignAttributes status:", status)
269
- console.log("assignAttributes action:", action)
270
- }
265
+
271
266
  if (this.context.tdcache) {
272
267
  if (action.assignResultTo && resbody) {
273
- if (this.log) { console.log("assign assignResultTo:", resbody); }
274
268
  await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignResultTo, resbody);
275
269
  }
276
270
  if (action.assignErrorTo && error) {
277
- if (this.log) { console.log("assign assignResultTo:", error); }
278
271
  await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignErrorTo, error);
279
272
  }
280
273
  if (action.assignStatusTo && status) {
281
- if (this.log) { console.log("assign assignStatusTo:", status); }
282
274
  await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignStatusTo, status);
283
275
  }
284
- // Debug log
285
- if (this.log) {
286
- const all_parameters = await TiledeskChatbot.allParametersStatic(this.context.tdcache, this.context.requestId);
287
- for (const [key, value] of Object.entries(all_parameters)) {
288
- if (this.log) { console.log("(webRequest) request parameter:", key, "value:", value, "type:", typeof value) }
289
- }
290
- }
291
276
  }
292
277
  }
293
278
 
294
279
  #myrequest(options, callback) {
295
280
  try {
296
- if (this.log) {
297
- console.log("API URL:", options.url);
298
- //console.log("** Options:", JSON.stringify(options));
299
- // Stringify "options". FIX THE STRINGIFY OF CIRCULAR STRUCTURE BUG - START
300
- let cache = [];
301
- let str_Options = JSON.stringify(options, function (key, value) { // try to use a separate function
302
- if (typeof value === 'object' && value != null) {
303
- if (cache.indexOf(value) !== -1) {
304
- return;
305
- }
306
- cache.push(value);
307
- }
308
- return value;
309
- });
310
- console.log("** Options:", str_Options);
311
- }
312
281
  let axios_options = {
313
282
  url: options.url,
314
283
  method: options.method,
@@ -322,9 +291,6 @@ class DirWebRequestV2 {
322
291
  if (options.json !== null) {
323
292
  axios_options.data = options.json
324
293
  }
325
- // if (this.log) {
326
- // console.log("axios_options:", JSON.stringify(axios_options));
327
- // }
328
294
  if (options.url.startsWith("https:")) {
329
295
  const httpsAgent = new https.Agent({
330
296
  rejectUnauthorized: false,
@@ -334,35 +300,11 @@ class DirWebRequestV2 {
334
300
 
335
301
  axios(axios_options)
336
302
  .then((res) => {
337
- if (this.log) {
338
- console.log("Success Response:", res);
339
- console.log("Response for url:", options.url);
340
- console.log("Response headers:\n", JSON.stringify(res.headers));
341
- }
342
303
  if (callback) {
343
304
  callback(null, res);
344
305
  }
345
306
  })
346
307
  .catch((err) => {
347
- if (this.log) {
348
- if (err.response) {
349
- console.log("Error Response data:", err.response.data);
350
- }
351
- // FIX THE STRINGIFY OF CIRCULAR STRUCTURE BUG - START
352
- let cache = [];
353
- let error_log = JSON.stringify(err, function (key, value) { // try to use a separate function
354
- if (typeof value === 'object' && value != null) {
355
- if (cache.indexOf(value) !== -1) {
356
- return;
357
- }
358
- cache.push(value);
359
- }
360
- return value;
361
- });
362
- console.error("(DirWebRequestv2) An error occurred: ", error_log);
363
- // FIX THE STRINGIFY OF CIRCULAR STRUCTURE BUG - END
364
- // console.error("An error occurred:", JSON.stringify(err));
365
- }
366
308
  if (callback) {
367
309
  let status = 1000;
368
310
  let cache = [];
@@ -398,7 +340,7 @@ class DirWebRequestV2 {
398
340
  });
399
341
  }
400
342
  catch (error) {
401
- console.error("Error:", error);
343
+ winston.error("Error: ", error);
402
344
  }
403
345
  }
404
346
 
@@ -407,20 +349,13 @@ class DirWebRequestV2 {
407
349
  if (!action.settings) {
408
350
  return timeout;
409
351
  }
410
- // console.log("default timeout:", timeout);
411
- // console.log("action.settings:", action.settings);
412
- // console.log("action.settings.timeout:", action.settings.timeout);
413
- // console.log("typeof action.settings.timeout:", typeof action.settings.timeout);
414
- // console.log("action.settings.timeout > min", action.settings.timeout > min)
415
- // console.log("action.settings.timeout < max", action.settings.timeout < max)
416
-
352
+
353
+
417
354
  if (action.settings.timeout) {
418
355
  if ((typeof action.settings.timeout === "number") && action.settings.timeout > min && action.settings.timeout < max) {
419
356
  timeout = Math.round(action.settings.timeout)
420
- // console.log("new timeout:", timeout);
421
357
  }
422
358
  }
423
- // console.log("returning timeout:", timeout);
424
359
  return timeout
425
360
  }
426
361
 
@@ -1,5 +1,6 @@
1
1
  const axios = require("axios").default;
2
2
  const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
3
+ const winston = require('../../utils/winston');
3
4
 
4
5
  let whatsapp_api_url;
5
6
 
@@ -15,13 +16,13 @@ class DirWhatsappByAttribute {
15
16
  }
16
17
 
17
18
  execute(directive, callback) {
18
- console.log("\n\nwhatsapp by attribtues action (execute) - directive: ", directive);
19
+ winston.verbose("Execute WhatsappByAttribute directive");
19
20
  let action;
20
21
  if (directive.action) {
21
22
  action = directive.action;
22
23
  }
23
24
  else {
24
- console.error("Incorrect directive: ", JSON.stringify(directive));
25
+ winston.warn("DirWhatsappByAttribute Incorrect directive: ", directive);
25
26
  callback();
26
27
  return;
27
28
  }
@@ -31,10 +32,7 @@ class DirWhatsappByAttribute {
31
32
  }
32
33
 
33
34
  async go(action, callback) {
34
-
35
- if (this.log) {
36
- console.log("whatsapp by attributes action: ", JSON.stringify(action))
37
- }
35
+ winston.debug("(DirWhatsappByAttribute) Action: ", action);
38
36
 
39
37
  const whatsapp_api_url_pre = process.env.WHATSAPP_ENDPOINT;
40
38
 
@@ -43,20 +41,20 @@ class DirWhatsappByAttribute {
43
41
  } else {
44
42
  whatsapp_api_url = this.API_ENDPOINT + "/modules/whatsapp/api"
45
43
  }
46
- console.log("DirWhatsappByAttribute whatsapp_api_url: ", whatsapp_api_url);
44
+ winston.debug("(DirWhatsappByAttribute) whatsapp_api_url: " + whatsapp_api_url);
47
45
 
48
46
  if (!action.attributeName) {
49
- console.error("DirWhatsappByAttribute attributeName is mandatory")
47
+ winston.error("(DirWhatsappByAttribute) attributeName is mandatory")
50
48
  callback();
51
49
  return;
52
50
  }
53
- if (this.log) { console.log("DirWhatsappByAttribute attributeName: ", action.attributeName )};
51
+ winston.debug("(DirWhatsappByAttribute) attributeName: " + action.attributeName )
54
52
 
55
53
  const attribute_value = await TiledeskChatbot.getParameterStatic(this.context.tdcache, this.context.requestId, action.attributeName)
56
- if (this.log) { console.log("attribute_value:", JSON.stringify(attribute_value)); }
54
+ winston.debug("(DirWhatsappByAttribute) attribute_value:", attribute_value);
57
55
 
58
56
  if (attribute_value == null) {
59
- console.error("DirWhatsappByAttribute attribute_value is undefined");
57
+ winston.error("(DirWhatsappByAttribute) attribute_value is undefined");
60
58
  callback();
61
59
  return;
62
60
  }
@@ -86,7 +84,7 @@ class DirWhatsappByAttribute {
86
84
  if (callback) {
87
85
  callback(null, resbody);
88
86
  }
89
- console.log("(tybot) broadcast sent: ", resbody);
87
+ winston.debug("(DirWhatsappByAttribute) broadcast sent: ", resbody);
90
88
  resolve(resbody);
91
89
  }
92
90
  }, true);
@@ -96,7 +94,6 @@ class DirWhatsappByAttribute {
96
94
 
97
95
  // HTTP REQUEST
98
96
  static async myrequest(options, callback, log) {
99
- console.log("my request execution")
100
97
  return await axios({
101
98
  url: options.url,
102
99
  method: options.method,
@@ -115,7 +112,7 @@ class DirWhatsappByAttribute {
115
112
  }
116
113
  }
117
114
  }).catch((err) => {
118
- console.error("(tybot request) An error occured: ", err);
115
+ winston.error("(DirWhatsappByAttribute) An error occured: ", err);
119
116
  if (callback) {
120
117
  callback(err, null, null);
121
118
  }
@@ -75,13 +75,10 @@ class Directives {
75
75
  // static CONDITION = "condition"; // DEPRECATED
76
76
 
77
77
  static actionToDirective(action) {
78
- // console.log("actionToDirective:", action);
79
78
  let directive = {
80
79
  name: action["_tdActionType"],
81
80
  action: action
82
81
  }
83
- // delete directive.action["_tdActionType"];
84
- // console.log("Directive out:", directive);
85
82
  return directive;
86
83
  }
87
84
  }
@@ -0,0 +1,42 @@
1
+ require('dotenv').config();
2
+ var appRoot = require('app-root-path');
3
+ var winston = require('winston');
4
+ var level = process.env.LOG_LEVEL || "info";
5
+
6
+ var options = {
7
+ file: {
8
+ level:level ,
9
+ filename: `${appRoot}/logs/app.log`,
10
+ handleExceptions: true,
11
+ json: false,
12
+ maxsize: 5242880, // 5MB
13
+ maxFiles: 5,
14
+ colorize: false,
15
+ format: winston.format.simple()
16
+ },
17
+ console: {
18
+ level: level,
19
+ handleExceptions: true,
20
+ json: true,
21
+ colorize: true,
22
+ // timestamp: true,
23
+ format: winston.format.simple()
24
+ },
25
+ };
26
+
27
+ let logger = winston.createLogger({
28
+ transports: [
29
+ new (winston.transports.Console)(options.console),
30
+ new (winston.transports.File)(options.file),
31
+ ],
32
+ exitOnError: false, // do not exit on handled exceptions
33
+ });
34
+
35
+ logger.stream = {
36
+ write: function(message, encoding) {
37
+ logger.info(message);
38
+ },
39
+ };
40
+
41
+
42
+ module.exports = logger;