@tiledesk/tiledesk-tybot-connector 0.2.601-rc1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/CHANGELOG.md +378 -1
  2. package/ExtApi.js +6 -6
  3. package/TdCache copy.js +242 -0
  4. package/TdCache.js +81 -176
  5. package/TdCache_v3.js +261 -0
  6. package/TiledeskExpression.js +7 -3
  7. package/index.js +290 -43
  8. package/logs/app.log +279 -0
  9. package/models/IntentsMachineFactory.js +5 -2
  10. package/models/MockBotsDataSource.js +19 -11
  11. package/models/TiledeskChatbot.js +97 -79
  12. package/models/TiledeskChatbotConst.js +12 -17
  13. package/models/TiledeskChatbotUtil.js +353 -109
  14. package/models/TiledeskIntentsMachine.js +1 -1
  15. package/models/faqKbService.js +1 -1
  16. package/package.json +7 -6
  17. package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +172 -106
  18. package/tiledeskChatbotPlugs/Filler.js +13 -2
  19. package/tiledeskChatbotPlugs/TildeskContextForCodeOrchestrator.js +8 -0
  20. package/tiledeskChatbotPlugs/WebhookChatbotPlug.js +13 -7
  21. package/tiledeskChatbotPlugs/directives/DirAddTags.js +374 -0
  22. package/tiledeskChatbotPlugs/directives/DirAiPrompt.js +476 -0
  23. package/tiledeskChatbotPlugs/directives/DirAskGPT.js +16 -19
  24. package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +221 -34
  25. package/tiledeskChatbotPlugs/directives/DirAssign.js +0 -11
  26. package/tiledeskChatbotPlugs/directives/DirAssignFromFunction.js +11 -21
  27. package/tiledeskChatbotPlugs/directives/DirAssistant.js +728 -0
  28. package/tiledeskChatbotPlugs/directives/DirBrevo.js +353 -0
  29. package/tiledeskChatbotPlugs/directives/DirCaptureUserReply.js +3 -30
  30. package/tiledeskChatbotPlugs/directives/DirClearTranscript.js +22 -0
  31. package/tiledeskChatbotPlugs/directives/DirClose.js +16 -3
  32. package/tiledeskChatbotPlugs/directives/DirCode.js +1 -1
  33. package/tiledeskChatbotPlugs/directives/DirCondition.js +0 -26
  34. package/tiledeskChatbotPlugs/directives/DirConnectBlock.js +183 -0
  35. package/tiledeskChatbotPlugs/directives/DirContactUpdate.js +121 -0
  36. package/tiledeskChatbotPlugs/directives/DirCustomerio.js +5 -8
  37. package/tiledeskChatbotPlugs/directives/DirDeflectToHelpCenter.js +11 -1
  38. package/tiledeskChatbotPlugs/directives/DirDepartment.js +15 -6
  39. package/tiledeskChatbotPlugs/directives/DirFireTiledeskEvent.js +17 -6
  40. package/tiledeskChatbotPlugs/directives/DirForm.js +12 -2
  41. package/tiledeskChatbotPlugs/directives/DirGptTask.js +83 -38
  42. package/tiledeskChatbotPlugs/directives/DirGptTask_OLD.js +4 -7
  43. package/tiledeskChatbotPlugs/directives/DirHubspot.js +5 -8
  44. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgents.js +14 -27
  45. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgentsV2.js +278 -0
  46. package/tiledeskChatbotPlugs/directives/DirIfOpenHours.js +147 -51
  47. package/tiledeskChatbotPlugs/directives/DirIfOpenHours_OLD.js +125 -0
  48. package/tiledeskChatbotPlugs/directives/DirIntent.js +8 -36
  49. package/tiledeskChatbotPlugs/directives/DirJSONCondition.js +5 -26
  50. package/tiledeskChatbotPlugs/directives/DirMessage.js +19 -17
  51. package/tiledeskChatbotPlugs/directives/DirMessageToBot.js +136 -0
  52. package/tiledeskChatbotPlugs/directives/DirMoveToAgent.js +20 -87
  53. package/tiledeskChatbotPlugs/directives/DirMoveToUnassigned.js +59 -0
  54. package/tiledeskChatbotPlugs/directives/DirQapla.js +6 -9
  55. package/tiledeskChatbotPlugs/directives/DirRandomReply.js +17 -7
  56. package/tiledeskChatbotPlugs/directives/DirRemoveCurrentBot.js +17 -7
  57. package/tiledeskChatbotPlugs/directives/DirReplaceBot.js +11 -2
  58. package/tiledeskChatbotPlugs/directives/DirReplaceBotV2.js +135 -21
  59. package/tiledeskChatbotPlugs/directives/DirReplaceBotV3.js +163 -0
  60. package/tiledeskChatbotPlugs/directives/DirReply.js +42 -9
  61. package/tiledeskChatbotPlugs/directives/DirReplyV2.js +347 -0
  62. package/tiledeskChatbotPlugs/directives/DirSendEmail.js +13 -23
  63. package/tiledeskChatbotPlugs/directives/DirSendWhatsapp.js +247 -0
  64. package/tiledeskChatbotPlugs/directives/DirSetAttributeV2.js +202 -15
  65. package/tiledeskChatbotPlugs/directives/DirSetConversationTags.js +13 -4
  66. package/tiledeskChatbotPlugs/directives/DirWait.js +21 -4
  67. package/tiledeskChatbotPlugs/directives/DirWebRequest.js +1 -2
  68. package/tiledeskChatbotPlugs/directives/DirWebRequestV2.js +166 -103
  69. package/tiledeskChatbotPlugs/directives/DirWhatsappByAttribute.js +2 -60
  70. package/tiledeskChatbotPlugs/directives/Directives.js +16 -1
  71. /package/tiledeskChatbotPlugs/directives/{DirOfflineHours.js → DEPRECATED_DirOfflineHours.js} +0 -0
package/CHANGELOG.md CHANGED
@@ -5,8 +5,385 @@
5
5
  available on:
6
6
  ▶️ https://www.npmjs.com/package/@tiledesk/tiledesk-tybot-connector
7
7
 
8
+ # v0.3.0
9
+ - bug-fixed: callback is not defined
8
10
 
9
- # v0.2.61 (next-release)
11
+ # v0.2.153-rc1
12
+ - changed: context for gpt-40 and gpt-40-mini
13
+
14
+ # v0.2.152
15
+ - Restored old default context for AskKB Action
16
+ - Deleted message "Intent not found".
17
+
18
+ # v0.2.151
19
+ - bug-fixed: context for gpt-40 and gpt-40-mini
20
+
21
+ # v0.2.150
22
+ - changed: AiPrompt action to improves errors management
23
+
24
+ # v0.2.149
25
+
26
+ # v0.2.149-rc3
27
+ - changed: context for gpt-40 and gpt-40-mini
28
+
29
+ # v0.2.149-rc2
30
+ - bug-fixed: botid null or undefined while calling /ext/:boid
31
+
32
+ # v0.2.148
33
+ - bug-fixed: namespace is undefined and "engine" property if noIntent is not specified
34
+
35
+ # v0.2.147
36
+ - bug-fixed: namespace is undefined and "engine" property cannot be evaluated in DirAskGPTV2
37
+
38
+ # v0.2.146
39
+ - bug-fixed: message.attributes["flowAttributes"]
40
+
41
+ # v0.2.146-rc1
42
+ - bug-fixed: message.attributes["flowAttributes"] added only if value size is <= 1kb
43
+
44
+ # v0.2.145
45
+ - added: TILEBOT_ENDPOINT env variable to startApp method
46
+
47
+ # v0.2.144
48
+ - Removed userFlowAttributes from message.attributes (fix message too long issue)
49
+
50
+ # v0.2.143
51
+ - Changed: env var TILEBOT_ENDPOINT replaced with TILEBOT_ENDPOINT
52
+
53
+ # v0.2.142
54
+ - Bug-fix: action.isInfo added in DirMessage fo fix test
55
+
56
+ # v0.2.141
57
+ - Bug-fix: message.attributes.payload not visible on first message
58
+
59
+ # v0.2.140
60
+ - Bug-fix: block hidden message for non-dev conversation
61
+
62
+ # v0.2.139
63
+ - Added ReplaceBotV3 action
64
+ - Updated Redis to 4.7.0
65
+
66
+ # v0.2.138
67
+ - Bug-fixed: hiddenMessage not blocked
68
+ - Bug-fixed: DirAskGPTV2 has hardcoded engine
69
+
70
+ # v0.2.138-rc8
71
+ - Updated redis to 4.7.0
72
+ - Updated tdCache
73
+
74
+ # v0.2.138-rc6
75
+ - First deploy external
76
+
77
+ # v0.2.138-rc1
78
+ - Updated redis to 4.7.0
79
+ - Updated tdCache
80
+
81
+ # v0.2.137
82
+ - Updated: hidden message is enabled only in dev mode for dratf requests
83
+ - Bug-fixed: flows variables is not updated with attributes.payload
84
+
85
+ # v0.2.136
86
+ - TdCache rollback
87
+
88
+ # v0.2.135
89
+ - Updated to redis 4.7.0 + Tdcache.js
90
+ - Disabled close_directive_test.js - DirClose directive - too old, not passing
91
+ - Disabled conversation-gpt_assistant_test.js
92
+
93
+ # v0.2.134
94
+ - Added filter for mweb on hidden messages
95
+
96
+ # v0.2.133
97
+ - Removed MAX_STEPS and MAX_EXECUTION_TIME as static variable of TiledeskChatbot
98
+
99
+ # v0.2.132
100
+ - Added support for external API_ENDPOINT
101
+ - Removed logs
102
+
103
+ # v0.2.131-rc2
104
+ - changed API_URL with API_ENDPOINT
105
+
106
+ # v0.2.131
107
+ - added: AUDIO_RECORD vxml action as reply_v2 actionType
108
+ - added: DirAddTags action
109
+ - externalized: process.env.CHATBOT_MAX_STEPS and process.env.CHATBOT_MAX_EXECUTION_TIME
110
+
111
+ # v0.2.130
112
+ - (TiledeskChatbotUtil) fix process.env.API_ENDPOINT => process.env.API_URL
113
+ - index.js /block => added "token": "NO-TOKEN"
114
+ - (TiledeskChatbotUtil) added: chatbot.addParameter(TiledeskChatbotConst.API_BASE_URL, process.env.API_ENDPOINT);
115
+
116
+ # v0.2.130-rc5
117
+ - (TiledeskChatbotUtil) fix process.env.API_ENDPOINT => process.env.API_URL
118
+
119
+ # v0.2.130-rc4
120
+ - (TiledeskChatbotUtil) process.env.API_ENDPOINT => process.env.API_URL
121
+
122
+ # v0.2.130-rc3
123
+ - index.js /block => added "token": "NO-TOKEN"
124
+ - (TiledeskChatbotUtil) added: chatbot.addParameter(TiledeskChatbotConst.API_BASE_URL, process.env.API_ENDPOINT);
125
+
126
+ # v0.2.129 -> test
127
+ - (TiledeskChatbot) sending as "info": "An error occurred while getting locked intent:'" + locked_intent + "'"
128
+ - (DirectivesChatbotPlug) added try catch on new TiledeskClient() => try {tdclient = new TiledeskClient({...})}
129
+
130
+ # v0.2.128 -> test
131
+ - Updated axios from 0.27.2 to 1.7.7
132
+ - fixes tdcache .set() with callback
133
+
134
+ # v0.2.127 -> test
135
+ - TiledeskChatbot =>
136
+ - static MAX_STEPS = 1000;
137
+ - static MAX_EXECUTION_TIME = 1000 * 3600 * 8;
138
+
139
+ # v0.2.126 -> test
140
+ - TdCache async del(key, callback) FIXED (await unsupported problem)
141
+
142
+ # v0.2.125 -> test
143
+ - class TiledeskChatbot static MAX_STEPS = 1000;
144
+ - class TiledeskChatbot static MAX_EXECUTION_TIME = 1000 * 3600 * 6; Increased from 4 => 6 hours
145
+
146
+ # v0.2.124 -> test
147
+ - TiledeskChatbot.js: fixed problem with locked-intent error.
148
+ - DirectivesChatbotPlug -> errorMessage is now hidden
149
+ - TESTING WITH VALUES: static MAX_STEPS = 10 && MAX_EXECUTION_TIME = 1000 * 60 * 2; (2 minutes)
150
+
151
+ # v0.2.123 -> test
152
+ - DirSendEmail removed log
153
+
154
+ # v0.2.122 -> test
155
+ - fixed check if (!this.validWebhookURL(this.webhookurl)) {... on WebhookChatbotPlug.js with return
156
+ - removed logs, err => JSON.stringify(err)
157
+
158
+ # v0.2.121 -> test
159
+ - added check if (!this.validWebhookURL(this.webhookurl)) {... on WebhookChatbotPlug.js
160
+
161
+ # v0.2.120 -> prod
162
+ - removed check in go() - it is already in execute() - on SetAttributeV2: if (!action) {...}
163
+ - merged Whatsapp Action
164
+ - updated "@tiledesk/tiledesk-client": "^0.10.13"
165
+
166
+ # v0.2.119 -> test
167
+ - fixed: addParameter("payload", message) => addParameter("payload", message.attributes.payload)
168
+ - JSONCondition: added in constructor => this.chatbot = context.chatbot;
169
+
170
+ # v0.2.118 -> test
171
+ - added payload to attributes: await chatbot.addParameter("payload", message);
172
+
173
+ # v0.2.117 -> test
174
+ - block endpoint fixed with APIURL
175
+
176
+ # v0.2.116 -> test
177
+ - updates @tiledesk/tiledesk-client => v0.10.12
178
+
179
+ # v0.2.115 -> test
180
+ - removed log console.error("TiledeskExpression.evaluate() error:...")
181
+ - Log setting fix in DirectivesChatbotPlug: const tdclient = new TiledeskClient({...
182
+ - SetAttributeV2: added check for action null: if (!action) {...
183
+ - SetAttributeV2:added check with ? operator: if (action.operation?.operators === undefined && ...
184
+
185
+ # v0.2.114 -> test
186
+ - Creating draft webhook endpoint on index.js: router.post('/block/:project_id/:bot_id/:block_id' ...
187
+ - Added check on attribute size < 20Mb
188
+ - SetAttributeV2: added persistency service
189
+
190
+ # v0.2.113 -> prod
191
+ - Fixed DirClose not importing TiledeskChatbotConst
192
+
193
+ # v0.2.112 -> prod
194
+ - Fix index name in DirAskGPTV2
195
+
196
+ # v0.2.111 -> test
197
+ - Updated DirAskGPTV2 with engine support
198
+
199
+ # v0.2.110 -> test
200
+ - Added DirMoveToUnassigned
201
+ - Added DirConnectBlock
202
+
203
+ # v0.2.109 -> test
204
+ - Added DirMoveToUnassigned
205
+ - Fixed bug: GptTask doesn't work properly with trascript
206
+
207
+ # v0.2.108 -> test
208
+ - Added chatbot_id
209
+ - Added support for citations in AskGPTV2
210
+ - Added support Json type in GPT Task
211
+
212
+ # v0.2.107 -> production
213
+ - clean logs (more)
214
+
215
+ # v0.2.106 -> production
216
+ - clean logs
217
+
218
+ # v0.2.105 -> production
219
+ - clean logs
220
+
221
+ # v0.2.104 -> production
222
+ - isValid fixed
223
+
224
+ # v0.2.103 -> test
225
+ - isValid removed
226
+
227
+ # v0.2.102
228
+ - isValid modified: if (parts.length >= 4) instead of "if (parts.length === 4)" that blocked whatsapp requests
229
+
230
+ # v0.2.101
231
+ - Dynamic attribute "now" is a ISOString date
232
+ - MAX_EXECUTION_TIME = 1000 * 3600 * 4 // 4 hours
233
+
234
+ # v0.2.100 -> test
235
+ - Added "chatbotToken" to RESERVED attributes
236
+ - Added UUID, UUIDv4 dynamic attributes
237
+ - Remove console.error from Filler liquidJS catch clause
238
+
239
+ # v0.2.98
240
+ - Support for MAX_EXECUTION_TIME (total execution time of a flow without user interaction)
241
+ - MAX_STEPS = 1000
242
+ - MAX_EXECUTION_TIME = 12 hrs
243
+
244
+ # v0.2.97
245
+ - Added support for "chatbotToken” attribute
246
+ - Added new Action: clear_transcript
247
+ - Exclude transcript generation when request type differs from "support-group-*"
248
+ - "step" key reset on DirWait => this.chatbot.addParameter( step_key, 0 )
249
+
250
+ # v0.2.96
251
+ - Added timestamp (number) and now (Date Object) attributes
252
+
253
+ - Added a limit in upload and download for WebRequestV2: maxContentLength: 10000000, // max 10mb response size, maxBodyLength: 10000000 // max 10mb request body size
254
+ - Added jsonCondition test on json objects properties
255
+ - Added flowError on JSONCondition when result = null
256
+ - Added fix on Filler
257
+
258
+ - Added possibility to select namespace by name
259
+ - Added filler to namespace in DirAskGPTv2
260
+ - Added filler to command.settings in DirReply
261
+
262
+ # v0.2.95
263
+ - If Online Agents V2 - bug fix (If Project Available Agents V2 -> MWeb)
264
+
265
+ # v0.2.94
266
+ - Added support for chat history for AskGPTv2 action
267
+
268
+ # v0.2.93
269
+ - Added model contexts
270
+
271
+ # v0.2.92
272
+ - Improved GPTTask Action with support for history
273
+
274
+ # v0.2.91
275
+ - Added voice flow attributes: dnis, callId, ani
276
+
277
+ # v0.2.89
278
+ - Added convertToNumber to operations in setAttribute
279
+ - Added setAttributeV2 test
280
+
281
+ # v0.2.88
282
+ - Added DirContactUpdate (aka LeadUpdate). No test case added for LeadUpdate. Only testable with a validation test.
283
+ - Fixed: Attribute parameters userFullname & userEmail now are able to "bypass" the request.lead not correctly updating on the same conversation. If updated in the flow (through LeadUpdate action), they will maintain their own value through the current conversation flow.
284
+
285
+ # v0.2.87
286
+ - Fixed DirAssistant empty error {}
287
+ - Added to DirAssistant "lastMessageData" attribute (The message content original JSON structure useful to get annotations in messages, message type etc.)
288
+
289
+ # v0.2.86
290
+ - Added to ifOnlineAgents Action: ignoreProjectWideOperatingHours = action.ignoreOperatingHours;
291
+ - Updated ifOnlineAgents with get available agents with 'raw' option
292
+
293
+ # v0.2.85
294
+ - Improved "If operating hours" action with time slots
295
+ - Other fix (?)
296
+
297
+ # v0.2.84
298
+ - Fixed "guest#" bug
299
+ - Fixed default_context in AskGPTv2
300
+
301
+ # v0.2.83
302
+ - Added support for advanced context in AskGPTv2
303
+
304
+ # v0.2.82
305
+ - Added {context} in 'system_context' in AskGPTv2
306
+
307
+ # v0.2.81
308
+ - Changed from 'context' to 'system_context' in AskGPTv2
309
+
310
+ # v0.2.80
311
+ - Changed AskGPT Action to support namespaces
312
+
313
+ # v0.2.79 - dev
314
+ - Fixed. Get DepartmentId for default department in test-mode in IfOnelineAgentsV2 with correct attribute get with chatbot.getParameter()
315
+
316
+ # v0.2.78 - dev
317
+ - Fixed? Get DepartmentId for default department in test-mode in IfOnelineAgentsV2
318
+
319
+ # v0.2.77 - dev
320
+ - Fix. get images and urls. image URL was Wrongly removed after the upload message
321
+
322
+ # v0.2.76 - dev
323
+ - Added support to button alias
324
+
325
+ # v0.2.75 - dev
326
+ - Fix. get images and urls also without text
327
+ - "userLeadId" = message.request.lead._id (instead of the unuseful lead.lead_id)
328
+
329
+ # v0.2.74 - online
330
+ - Fix. Get DepartmentId for default department in test-mode in IfOnelineAgentsV2
331
+
332
+ # v0.2.73 - debug
333
+ - Fix. static getMachine with check on bot not null
334
+
335
+ # v0.2.72
336
+ - Fix. userParams not in /reserved...
337
+
338
+ # v0.2.71
339
+ - Fix. DirReplyV2 "NoInput" bug. Refactored with the introduction of "timeout_id", an ID specific for each noInput timeout: this.chatbot.addParameter(TiledeskChatbotConst.USER_INPUT, timeout_id)
340
+
341
+ # v0.2.70
342
+ - added DirIfOnlineAgentsV2
343
+ - Fix. TiledeskChatbot: WRONG: await chatbot.addParameter("userInput", true); ====> FIXED: this.addParameter("userInput", true)
344
+ - Fix. Fixed WRONG addParameter(key, value) invocation in TiledeskChatbot with 3 (instead of key, value) parameters
345
+
346
+ # v0.2.69
347
+ - added flow attributes in reply action (original)
348
+
349
+ # v0.2.68
350
+ - added flow attributes in replyv2 action
351
+ - ReplyV2 now "re-forwards" the original message when the "no-match" connector is undefined (behaving exactly like the classic Reply)
352
+
353
+ # v0.2.67
354
+ - console.log crash fixed on noinput
355
+
356
+ # v0.2.66
357
+ - changed updateQuotes method
358
+ - fix missing model in updateQuote
359
+ - added ReplyV2
360
+
361
+ # v0.2.66-rc1
362
+ - fix missing model in updateQuote
363
+
364
+ # v0.2.65 - online
365
+ - fix OPENAI_APIKEY: removed Bearer prefix
366
+
367
+ # v0.2.64
368
+ - removed system parameters from /reserved/parameters
369
+ - Web Request Action: added multipart form-data support with files binary from URL source
370
+ - Added DirAssistant Action
371
+
372
+ # v0.2.63
373
+ - BUG fix on /ext/parameters/requests/:requestid
374
+ - Moved getChatbotParameters function in TiledeskChatbotUtil
375
+
376
+ # v0.2.62
377
+ - Whatsapp fix (transcript error whitout "senderFullname")
378
+
379
+ # v0.2.62-rc4
380
+ - Whatsapp fix (transcript error whitout "senderFullname")
381
+
382
+ # v0.2.62-rc1
383
+ - Added VXML actions (speech_form, play_prompt)
384
+
385
+ # v0.2.61
386
+ - fixed WebRequestv2 timeout
10
387
 
11
388
  # v0.2.61-rc1
12
389
  - Added VXML actions (dmtf_menu, dtmf_form, blind_transfer)
package/ExtApi.js CHANGED
@@ -4,9 +4,9 @@ let https = require("https");
4
4
  class ExtApi {
5
5
 
6
6
  constructor(options) {
7
- if (!options.ENDPOINT) {
8
- throw new Error("options.ENDPOINT is mandatory");
9
- //this.extEndpoint = `${options.TYBOT_ENDPOINT}/;
7
+ if (!options.TILEBOT_ENDPOINT) {
8
+ throw new Error("options.TILEBOT_ENDPOINT is mandatory");
9
+ //this.extEndpoint = `${options.TILEBOT_ENDPOINT}/;
10
10
  }
11
11
  if (options.log) {
12
12
  this.log = options.log;
@@ -14,7 +14,7 @@ class ExtApi {
14
14
  else {
15
15
  this.log = false;
16
16
  }
17
- this.ENDPOINT = options.ENDPOINT;
17
+ this.TILEBOT_ENDPOINT = options.TILEBOT_ENDPOINT;
18
18
  }
19
19
 
20
20
  fixToken(token) {
@@ -37,7 +37,7 @@ class ExtApi {
37
37
  */
38
38
  sendSupportMessageExt(message, projectId, requestId, token, callback) {
39
39
  const jwt_token = this.fixToken(token);
40
- const url = `${this.ENDPOINT}/ext/${projectId}/requests/${requestId}/messages`;
40
+ const url = `${this.TILEBOT_ENDPOINT}/ext/${projectId}/requests/${requestId}/messages`;
41
41
  if (this.log) {console.log("sendSupportMessageExt URL", url);}
42
42
  const HTTPREQUEST = {
43
43
  url: url,
@@ -141,7 +141,7 @@ class ExtApi {
141
141
  }
142
142
  })
143
143
  .catch( (error) => {
144
- console.error("An error occurred:", error);
144
+ console.error("(ExtApi) An error occurred:", JSON.stringify(error));
145
145
  if (callback) {
146
146
  callback(error, null, null);
147
147
  }
@@ -0,0 +1,242 @@
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 };