@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 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
 
@@ -34,10 +34,10 @@ class DirWebResponse {
34
34
  }
35
35
 
36
36
  async go(action, callback) {
37
- console.log("Web response...");
38
- let payload = action.payload;
39
- let status = action.status;
40
-
37
+ if (this.log) {
38
+ console.log("(DirWebResponse) action:", action);
39
+ }
40
+
41
41
  let requestAttributes = null;
42
42
  if (this.tdcache) {
43
43
  requestAttributes =
@@ -45,7 +45,7 @@ class DirWebResponse {
45
45
  const filler = new Filler();
46
46
 
47
47
  try {
48
- payload = filler.fill(payload, requestAttributes);
48
+ let status = action.status;
49
49
  status = filler.fill(status, requestAttributes);
50
50
  }
51
51
  catch(e) {
@@ -54,16 +54,19 @@ class DirWebResponse {
54
54
 
55
55
  }
56
56
 
57
+ const json = await this.getJsonFromAction(action, filler, requestAttributes)
57
58
  let webResponse = {
58
59
  status: status,
59
- payload: payload
60
+ payload: json
60
61
  }
61
62
 
62
63
  const topic = `/webhooks/${this.requestId}`;
63
64
 
64
65
  try {
65
66
  this.tdcache.publish(topic, JSON.stringify(webResponse));
66
- console.log("Published webresponse to topic:", topic);
67
+ if (this.log) {
68
+ console.log("(DirWebResponse) Published webresponse to topic:", topic);
69
+ }
67
70
  }
68
71
  catch(e) {
69
72
  console.error(e)
@@ -72,8 +75,32 @@ class DirWebResponse {
72
75
  callback();
73
76
 
74
77
  }
78
+
79
+ async getJsonFromAction(action, filler, requestAttributes) {
80
+
81
+ return new Promise( async (resolve, reject) => {
82
+
83
+ if (action.payload && action.bodyType == "json") {
84
+ let jsonBody = filler.fill(action.payload, requestAttributes);
85
+ try {
86
+ let json = JSON.parse(jsonBody);
87
+ resolve(json);
88
+ }
89
+ catch (err) {
90
+ if (this.log) { console.error("Error parsing webRequest jsonBody:", jsonBody, err) };
91
+ reject("Error parsing jsonBody");
92
+ }
93
+ }
94
+ else {
95
+ resolve(null);
96
+ }
97
+ })
98
+ }
99
+
75
100
  }
76
101
 
102
+
103
+
77
104
  /**
78
105
  * A stub to send message to the "ext/botId" endpoint, hosted by tilebot on:
79
106
  * /${TILEBOT_ROUTE}/ext/${botId}
@@ -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
  }
@@ -58,6 +58,7 @@ class Directives {
58
58
  static MOVE_TO_UNASSIGNED = "move_to_unassigned";
59
59
  static CONNECT_BLOCK = "connect_block";
60
60
  static ADD_TAGS = 'add_tags'
61
+ static WEBHOOK = 'webhook';
61
62
  static WEB_RESPONSE = "web_response";
62
63
 
63
64
  // static WHEN_ONLINE_MOVE_TO_AGENT = "whenonlinemovetoagent"; // DEPRECATED?
@@ -74,13 +75,10 @@ class Directives {
74
75
  // static CONDITION = "condition"; // DEPRECATED
75
76
 
76
77
  static actionToDirective(action) {
77
- // console.log("actionToDirective:", action);
78
78
  let directive = {
79
79
  name: action["_tdActionType"],
80
80
  action: action
81
81
  }
82
- // delete directive.action["_tdActionType"];
83
- // console.log("Directive out:", directive);
84
82
  return directive;
85
83
  }
86
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;
package/TdCache copy.js DELETED
@@ -1,242 +0,0 @@
1
- const redis = require('redis');
2
-
3
- class TdCache {
4
-
5
- constructor(config) {
6
- this.redis_host = config.host;
7
- this.redis_port = config.port;
8
- this.redis_password = config.password;
9
- this.client = null;
10
- }
11
-
12
- async connect(callback) {
13
- // client = redis.createClient();
14
- return new Promise( async (resolve, reject) => {
15
- this.client = redis.createClient(
16
- {
17
- host: this.redis_host,
18
- port: this.redis_port,
19
- password: this.redis_password
20
- });
21
- this.client.on('error', err => {
22
- reject(err);
23
- if (callback) {
24
- callback(err);
25
- }
26
- });
27
- // this.client.on('connect', function() {
28
- // console.log('Redis Connected!');
29
- // });
30
- this.client.on('ready',function() {
31
- console.log("connected")
32
- resolve();
33
- if (callback) {
34
- callback();
35
- }
36
- //console.log("Redis is ready.");
37
- });
38
- });
39
- }
40
-
41
- async set(key, value, options) {
42
- //console.log("setting key value", key, value)
43
- if (!options) {
44
- options = {EX: 86400}
45
- }
46
- return new Promise( async (resolve, reject) => {
47
- if (options && options.EX) {
48
- //console.log("expires:", options.EX)
49
- try {
50
- await this.client.set(
51
- key,
52
- value,
53
- 'EX', options.EX);
54
- }
55
- catch(error) {
56
- reject(error)
57
- }
58
- }
59
- else {
60
- try {
61
- //console.log("setting here...key", key, value)
62
- await this.client.set(
63
- key,
64
- value);
65
- }
66
- catch(error) {
67
- console.error("Error", error);
68
- reject(error)
69
- }
70
- }
71
- if (options && options.callback) {
72
- options.callback();
73
- }
74
- //console.log("resolving...", key);
75
- return resolve();
76
- });
77
- }
78
-
79
- async incr(key) {
80
- // console.log("incr key:", key)
81
- return new Promise( async (resolve, reject) => {
82
- try {
83
- // console.log("incr here...key", key)
84
- await this.client.incr(key);
85
- }
86
- catch(error) {
87
- console.error("Error on incr:", error);
88
- reject(error)
89
- }
90
- return resolve();
91
- });
92
- }
93
-
94
- async hset(dict_key, key, value, options) {
95
- //console.log("hsetting dict_key key value", dict_key, key, value)
96
- return new Promise( async (resolve, reject) => {
97
- if (options && options.EX) {
98
- //console.log("expires:", options.EX)
99
- try {
100
- await this.client.hset(
101
- dict_key,
102
- key,
103
- value,
104
- 'EX', options.EX);
105
- }
106
- catch(error) {
107
- reject(error)
108
- }
109
- }
110
- else {
111
- try {
112
- //console.log("setting here...key", key, value)
113
- await this.client.hset(
114
- dict_key,
115
- key,
116
- value);
117
- }
118
- catch(error) {
119
- console.error("Error", error);
120
- reject(error)
121
- }
122
- }
123
- if (options && options.callback) {
124
- options.callback();
125
- }
126
- return resolve();
127
- });
128
- }
129
-
130
- async hdel(dict_key, key, options) {
131
- //console.log("hsetting dict_key key value", dict_key, key, value)
132
- return new Promise( async (resolve, reject) => {
133
- if (options && options.EX) {
134
- //console.log("expires:", options.EX)
135
- try {
136
- await this.client.hdel(
137
- dict_key,
138
- key,
139
- 'EX', options.EX);
140
- }
141
- catch(error) {
142
- reject(error)
143
- }
144
- }
145
- else {
146
- try {
147
- //console.log("setting here...key", key, value)
148
- await this.client.hdel(
149
- dict_key,
150
- key);
151
- }
152
- catch(error) {
153
- console.error("Error", error);
154
- reject(error);
155
- }
156
- }
157
- if (options && options.callback) {
158
- options.callback();
159
- }
160
- return resolve();
161
- });
162
- }
163
-
164
- async setJSON(key, value, options) {
165
- const _string = JSON.stringify(value);
166
- return await this.set(key, _string, options);
167
- }
168
-
169
- async get(key, callback) {
170
- //console.log("getting key", key)
171
- return new Promise( async (resolve, reject) => {
172
- this.client.get(key, (err, value) => {
173
- if (err) {
174
- reject(err);
175
- }
176
- else {
177
- if (callback) {
178
- callback(value);
179
- }
180
- return resolve(value);
181
- }
182
- });
183
- });
184
- }
185
-
186
- async hgetall(dict_key, callback) {
187
- //console.log("hgetting dics", dict_key);
188
- return new Promise( async (resolve, reject) => {
189
- this.client.hgetall(dict_key, (err, value) => {
190
- if (err) {
191
- reject(err);
192
- if (callback) {
193
- callback(err, null);
194
- }
195
- }
196
- else {
197
- if (callback) {
198
- callback(null, value);
199
- }
200
- resolve(value);
201
- }
202
- });
203
- });
204
- }
205
-
206
- async hget(dict_key, key, callback) {
207
- //console.log("hgetting dics", dict_key);
208
- return new Promise( async (resolve, reject) => {
209
- this.client.hget(dict_key, key, (err, value) => {
210
- if (err) {
211
- reject(err);
212
- if (callback) {
213
- callback(err, null);
214
- }
215
- }
216
- else {
217
- if (callback) {
218
- callback(null, value);
219
- }
220
- resolve(value);
221
- }
222
- });
223
- });
224
- }
225
-
226
- async getJSON(key, callback) {
227
- const value = await this.get(key);
228
- return JSON.parse(value);
229
- }
230
-
231
- async del(key, callback) {
232
- return new Promise( async (resolve, reject) => {
233
- await this.client.del(key);
234
- if (callback) {
235
- callback();
236
- }
237
- return resolve();
238
- })
239
- }
240
- }
241
-
242
- module.exports = { TdCache };