@tiledesk/tiledesk-tybot-connector 0.2.138 → 0.2.139-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.
package/CHANGELOG.md CHANGED
@@ -17,10 +17,20 @@ available on:
17
17
  - Added flowError on JSONCondition when result = null
18
18
  - Added fix on Filler -->
19
19
 
20
+ # v0.2.139-rc1
21
+ - Improved ReplaceBotV2
22
+
20
23
  # v0.2.138
21
24
  - Bug-fixed: hiddenMessage not blocked
22
25
  - Bug-fixed: DirAskGPTV2 has hardcoded engine
23
26
 
27
+ # v0.2.138-rc8
28
+ - Updated redis to 4.7.0
29
+ - Updated tdCache
30
+
31
+ # v0.2.138-rc6
32
+ - First deploy external
33
+
24
34
  # v0.2.138-rc1
25
35
  - Updated redis to 4.7.0
26
36
  - Updated tdCache
package/TdCache.js CHANGED
@@ -3,38 +3,63 @@ const redis = require('redis');
3
3
  class TdCache {
4
4
 
5
5
  constructor(config) {
6
+ console.log("TdCache config: ", config);
6
7
  this.redis_host = config.host;
7
8
  this.redis_port = config.port;
8
9
  this.redis_password = config.password;
10
+ console.log("TdCache this.redis_host: ", this.redis_host);
11
+ console.log("TdCache this.redis_port: ", this.redis_port);
12
+ console.log("TdCache this.redis_password: ", this.redis_password);
9
13
  this.client = null;
14
+ this.redis_sub = null;
10
15
  }
11
16
 
12
17
  async connect(callback) {
13
- // client = redis.createClient();
18
+
14
19
  return new Promise( async (resolve, reject) => {
20
+ /**
21
+ * Connect redis client
22
+ */
15
23
  this.client = redis.createClient(
16
- {
17
- host: this.redis_host,
18
- port: this.redis_port,
19
- password: this.redis_password
20
- });
24
+ {
25
+ url: `redis://${this.redis_host}:${this.redis_port}`,
26
+ password: this.redis_password
27
+ });
21
28
  this.client.on('error', err => {
22
29
  reject(err);
23
30
  if (callback) {
24
31
  callback(err);
25
32
  }
26
33
  });
27
- // this.client.on('connect', function() {
28
- // console.log('Redis Connected!');
29
- // });
30
34
  this.client.on('ready',function() {
31
- // console.log("connected")
32
35
  resolve();
33
36
  if (callback) {
34
37
  callback();
35
38
  }
36
- //console.log("Redis is ready.");
37
39
  });
40
+ await this.client.connect();
41
+
42
+ /**
43
+ * Connect redis subscription client
44
+ */
45
+ this.redis_sub = redis.createClient(
46
+ {
47
+ url: `redis://${this.redis_host}:${this.redis_port}`,
48
+ password: this.redis_password
49
+ });
50
+ this.redis_sub.on('error', err => {
51
+ reject(err);
52
+ if (callback) {
53
+ callback(err);
54
+ }
55
+ });
56
+ this.redis_sub.on('ready',function() {
57
+ resolve();
58
+ if (callback) {
59
+ callback();
60
+ }
61
+ });
62
+ await this.redis_sub.connect();
38
63
  });
39
64
  }
40
65
 
@@ -43,202 +68,59 @@ class TdCache {
43
68
  if (!options) {
44
69
  options = {EX: 86400}
45
70
  }
46
- return new Promise( async (resolve, reject) => {
47
- if (options && options.EX) {
48
- //console.log("expires:", options.EX)
49
- try {
50
- this.client.set(
51
- key,
52
- value,
53
- 'EX', options.EX,
54
- (err) => {
55
- if (err) {
56
- reject(err);
57
- }
58
- else {
59
- return resolve();
60
- }
61
- }
62
- );
63
- }
64
- catch(error) {
65
- reject(error)
66
- }
67
- }
68
- else {
69
- try {
70
- //console.log("setting here...key", key, value)
71
- await this.client.set(
72
- key,
73
- value,
74
- (err) => {
75
- if (err) {
76
- reject(err);
77
- }
78
- else {
79
- return resolve();
80
- }
81
- }
82
- );
83
- }
84
- catch(error) {
85
- console.error("TdCache Error:", error);
86
- reject(error)
87
- }
88
- }
89
- // if (options && options.callback) {
90
- // options.callback();
91
- // }
92
- //console.log("resolving...", key);
93
- // return resolve();
94
- });
71
+ await this.client.set(
72
+ key,
73
+ value,
74
+ options);
95
75
  }
96
76
 
97
77
  async incr(key) {
98
- // console.log("incr key:", key)
99
- return new Promise( async (resolve, reject) => {
100
- try {
101
- // console.log("incr here...key", key)
102
- await this.client.incr(key);
103
- }
104
- catch(error) {
105
- console.error("Error on incr:", error);
106
- reject(error)
107
- }
108
- return resolve();
109
- });
78
+ await this.client.incr(key);
110
79
  }
111
80
 
112
81
  async hset(dict_key, key, value, options) {
113
- //console.log("hsetting dict_key key value", dict_key, key, value)
114
- return new Promise( async (resolve, reject) => {
115
- if (options && options.EX) {
116
- //console.log("expires:", options.EX)
117
- try {
118
- await this.client.hset(
119
- dict_key,
120
- key,
121
- value,
122
- 'EX', options.EX);
123
- }
124
- catch(error) {
125
- reject(error)
126
- }
127
- }
128
- else {
129
- try {
130
- //console.log("setting here...key", key, value)
131
- await this.client.hset(
132
- dict_key,
133
- key,
134
- value);
135
- }
136
- catch(error) {
137
- console.error("Error", error);
138
- reject(error)
139
- }
140
- }
141
- if (options && options.callback) {
142
- options.callback();
143
- }
144
- return resolve();
145
- });
82
+ if (!value) {
83
+ return;
84
+ }
85
+ if (!options) {
86
+ options = {EX: 86400}
87
+ }
88
+ await this.client.HSET(
89
+ dict_key,
90
+ key,
91
+ value,
92
+ options);
146
93
  }
147
94
 
148
- async hdel(dict_key, key, options) {
149
- //console.log("hsetting dict_key key value", dict_key, key, value)
150
- return new Promise( async (resolve, reject) => {
151
- if (options && options.EX) {
152
- //console.log("expires:", options.EX)
153
- try {
154
- await this.client.hdel(
155
- dict_key,
156
- key,
157
- 'EX', options.EX);
158
- }
159
- catch(error) {
160
- reject(error)
161
- }
162
- }
163
- else {
164
- try {
165
- //console.log("setting here...key", key, value)
166
- await this.client.hdel(
167
- dict_key,
168
- key);
169
- }
170
- catch(error) {
171
- console.error("Error", error);
172
- reject(error);
173
- }
174
- }
175
- if (options && options.callback) {
176
- options.callback();
177
- }
178
- return resolve();
179
- });
95
+ async hdel(dict_key, key) {
96
+ await this.client.HDEL(dict_key, key);
180
97
  }
181
98
 
182
99
  async setJSON(key, value, options) {
100
+ if (!value) {
101
+ return;
102
+ }
103
+ if (!options) {
104
+ options = {EX: 86400}
105
+ }
183
106
  const _string = JSON.stringify(value);
184
107
  return await this.set(key, _string, options);
185
108
  }
186
109
 
187
- async get(key, callback) {
188
- //console.log("getting key", key)
189
- return new Promise( async (resolve, reject) => {
190
- this.client.get(key, (err, value) => {
191
- if (err) {
192
- reject(err);
193
- }
194
- else {
195
- if (callback) {
196
- callback(value);
197
- }
198
- return resolve(value);
199
- }
200
- });
201
- });
110
+ async get(key) {
111
+ const value = await this.client.GET(key);
112
+ return value;
202
113
  }
203
114
 
204
- async hgetall(dict_key, callback) {
205
- //console.log("hgetting dics", dict_key);
206
- return new Promise( async (resolve, reject) => {
207
- this.client.hgetall(dict_key, (err, value) => {
208
- if (err) {
209
- reject(err);
210
- if (callback) {
211
- callback(err, null);
212
- }
213
- }
214
- else {
215
- if (callback) {
216
- callback(null, value);
217
- }
218
- resolve(value);
219
- }
220
- });
221
- });
115
+ async hgetall(dict_key) {
116
+ const all = await this.client.HGETALL(dict_key);
117
+ return all;
222
118
  }
223
119
 
224
- async hget(dict_key, key, callback) {
225
- //console.log("hgetting dics", dict_key);
226
- return new Promise( async (resolve, reject) => {
227
- this.client.hget(dict_key, key, (err, value) => {
228
- if (err) {
229
- reject(err);
230
- if (callback) {
231
- callback(err, null);
232
- }
233
- }
234
- else {
235
- if (callback) {
236
- callback(null, value);
237
- }
238
- resolve(value);
239
- }
240
- });
241
- });
120
+ async hget(dict_key, key) {
121
+ // console.log("hgetting dics", dict_key);
122
+ const value = await this.client.HGET(dict_key, key);
123
+ return value;
242
124
  }
243
125
 
244
126
  async getJSON(key, callback) {
@@ -246,16 +128,19 @@ class TdCache {
246
128
  return JSON.parse(value);
247
129
  }
248
130
 
249
- async del(key, callback) {
250
- return new Promise( async (resolve, reject) => {
251
- this.client.del(key, () => {
252
- if (callback) {
253
- callback();
254
- }
255
- return resolve();
256
- });
257
- })
131
+ async del(key) {
132
+ await this.client.del(key);
258
133
  }
134
+
135
+ async publish(key, value) {
136
+ await this.redis_sub.publish(key, value);
137
+ }
138
+
139
+ // subscribe(key, callback) {
140
+ // this.redis_sub.subscribe(key, (message) => {
141
+ // callback(message);
142
+ // });
143
+ // }
259
144
  }
260
145
 
261
146
  module.exports = { TdCache };
package/TdCache_v3.js ADDED
@@ -0,0 +1,261 @@
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
+ this.client.set(
51
+ key,
52
+ value,
53
+ 'EX', options.EX,
54
+ (err) => {
55
+ if (err) {
56
+ reject(err);
57
+ }
58
+ else {
59
+ return resolve();
60
+ }
61
+ }
62
+ );
63
+ }
64
+ catch(error) {
65
+ reject(error)
66
+ }
67
+ }
68
+ else {
69
+ try {
70
+ //console.log("setting here...key", key, value)
71
+ await this.client.set(
72
+ key,
73
+ value,
74
+ (err) => {
75
+ if (err) {
76
+ reject(err);
77
+ }
78
+ else {
79
+ return resolve();
80
+ }
81
+ }
82
+ );
83
+ }
84
+ catch(error) {
85
+ console.error("TdCache Error:", error);
86
+ reject(error)
87
+ }
88
+ }
89
+ // if (options && options.callback) {
90
+ // options.callback();
91
+ // }
92
+ //console.log("resolving...", key);
93
+ // return resolve();
94
+ });
95
+ }
96
+
97
+ async incr(key) {
98
+ // console.log("incr key:", key)
99
+ return new Promise( async (resolve, reject) => {
100
+ try {
101
+ // console.log("incr here...key", key)
102
+ await this.client.incr(key);
103
+ }
104
+ catch(error) {
105
+ console.error("Error on incr:", error);
106
+ reject(error)
107
+ }
108
+ return resolve();
109
+ });
110
+ }
111
+
112
+ async hset(dict_key, key, value, options) {
113
+ //console.log("hsetting dict_key key value", dict_key, key, value)
114
+ return new Promise( async (resolve, reject) => {
115
+ if (options && options.EX) {
116
+ //console.log("expires:", options.EX)
117
+ try {
118
+ await this.client.hset(
119
+ dict_key,
120
+ key,
121
+ value,
122
+ 'EX', options.EX);
123
+ }
124
+ catch(error) {
125
+ reject(error)
126
+ }
127
+ }
128
+ else {
129
+ try {
130
+ //console.log("setting here...key", key, value)
131
+ await this.client.hset(
132
+ dict_key,
133
+ key,
134
+ value);
135
+ }
136
+ catch(error) {
137
+ console.error("Error", error);
138
+ reject(error)
139
+ }
140
+ }
141
+ if (options && options.callback) {
142
+ options.callback();
143
+ }
144
+ return resolve();
145
+ });
146
+ }
147
+
148
+ async hdel(dict_key, key, options) {
149
+ //console.log("hsetting dict_key key value", dict_key, key, value)
150
+ return new Promise( async (resolve, reject) => {
151
+ if (options && options.EX) {
152
+ //console.log("expires:", options.EX)
153
+ try {
154
+ await this.client.hdel(
155
+ dict_key,
156
+ key,
157
+ 'EX', options.EX);
158
+ }
159
+ catch(error) {
160
+ reject(error)
161
+ }
162
+ }
163
+ else {
164
+ try {
165
+ //console.log("setting here...key", key, value)
166
+ await this.client.hdel(
167
+ dict_key,
168
+ key);
169
+ }
170
+ catch(error) {
171
+ console.error("Error", error);
172
+ reject(error);
173
+ }
174
+ }
175
+ if (options && options.callback) {
176
+ options.callback();
177
+ }
178
+ return resolve();
179
+ });
180
+ }
181
+
182
+ async setJSON(key, value, options) {
183
+ const _string = JSON.stringify(value);
184
+ return await this.set(key, _string, options);
185
+ }
186
+
187
+ async get(key, callback) {
188
+ //console.log("getting key", key)
189
+ return new Promise( async (resolve, reject) => {
190
+ this.client.get(key, (err, value) => {
191
+ if (err) {
192
+ reject(err);
193
+ }
194
+ else {
195
+ if (callback) {
196
+ callback(value);
197
+ }
198
+ return resolve(value);
199
+ }
200
+ });
201
+ });
202
+ }
203
+
204
+ async hgetall(dict_key, callback) {
205
+ //console.log("hgetting dics", dict_key);
206
+ return new Promise( async (resolve, reject) => {
207
+ this.client.hgetall(dict_key, (err, value) => {
208
+ if (err) {
209
+ reject(err);
210
+ if (callback) {
211
+ callback(err, null);
212
+ }
213
+ }
214
+ else {
215
+ if (callback) {
216
+ callback(null, value);
217
+ }
218
+ resolve(value);
219
+ }
220
+ });
221
+ });
222
+ }
223
+
224
+ async hget(dict_key, key, callback) {
225
+ //console.log("hgetting dics", dict_key);
226
+ return new Promise( async (resolve, reject) => {
227
+ this.client.hget(dict_key, key, (err, value) => {
228
+ if (err) {
229
+ reject(err);
230
+ if (callback) {
231
+ callback(err, null);
232
+ }
233
+ }
234
+ else {
235
+ if (callback) {
236
+ callback(null, value);
237
+ }
238
+ resolve(value);
239
+ }
240
+ });
241
+ });
242
+ }
243
+
244
+ async getJSON(key, callback) {
245
+ const value = await this.get(key);
246
+ return JSON.parse(value);
247
+ }
248
+
249
+ async del(key, callback) {
250
+ return new Promise( async (resolve, reject) => {
251
+ this.client.del(key, () => {
252
+ if (callback) {
253
+ callback();
254
+ }
255
+ return resolve();
256
+ });
257
+ })
258
+ }
259
+ }
260
+
261
+ module.exports = { TdCache };
package/index.js CHANGED
@@ -609,6 +609,7 @@ router.post('/block/:project_id/:bot_id/:block_id', async (req, res) => {
609
609
 
610
610
  async function startApp(settings, completionCallback) {
611
611
  console.log("Starting Tilebot...");
612
+ // console.log("Starting Tilebot with Settings:", settings);
612
613
  if (settings.bots) { // static bots data source
613
614
  staticBots = settings.bots;
614
615
  }
@@ -699,7 +699,6 @@ class TiledeskChatbotUtil {
699
699
  await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY, message.attributes.departmentId);
700
700
  await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY, message.attributes.departmentName);
701
701
  }
702
-
703
702
  if (message.attributes) {
704
703
  if (chatbot.log) {console.log("Ok message.attributes", JSON.stringify(message.attributes));}
705
704
  // if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > Ok message.attributes", JSON.stringify(message.attributes));}
@@ -920,6 +919,7 @@ class TiledeskChatbotUtil {
920
919
  }
921
920
 
922
921
  /**
922
+ * DEPRECATED
923
923
  * A stub to get the request parameters, hosted by tilebot on:
924
924
  * /${TILEBOT_ROUTE}/ext/parameters/requests/${requestId}?all
925
925
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.2.138",
3
+ "version": "0.2.139-rc2",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -29,7 +29,7 @@
29
29
  "mongoose": "^6.3.5",
30
30
  "multer": "^1.4.5-lts.1",
31
31
  "nanoid": "^3.1.25",
32
- "redis": "^3.1.2",
32
+ "redis": "^4.7.0",
33
33
  "uuid": "^3.3.3",
34
34
  "vm2": "^3.9.13"
35
35
  },
@@ -2,6 +2,9 @@ const { TiledeskClient } = require('@tiledesk/tiledesk-client');
2
2
  const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
3
3
  const { Filler } = require('../Filler');
4
4
 
5
+ const axios = require("axios").default;
6
+ let https = require("https");
7
+
5
8
  class DirReplaceBotV2 {
6
9
 
7
10
  constructor(context) {
@@ -52,30 +55,125 @@ class DirReplaceBotV2 {
52
55
  );
53
56
  const filler = new Filler();
54
57
  botName = filler.fill(botName, variables);
55
- this.tdClient.replaceBotByName(this.requestId, botName, () => {
56
- if (blockName) {
57
- if (this.log) {console.log("Sending hidden /start message to bot in dept");}
58
- const message = {
59
- type: "text",
60
- text: "/" + blockName,
61
- attributes : {
62
- subtype: "info"
58
+
59
+ const HTTPREQUEST = {
60
+ url: this.API_ENDPOINT + "/" + this.context.projectId + "/requests/" + this.requestId + "/replace",
61
+ headers: {
62
+ 'Content-Type': 'application/json',
63
+ 'Authorization': 'JWT ' + this.context.token
64
+ },
65
+ json: { name: botName},
66
+ method: 'PUT'
67
+ }
68
+
69
+ this.#myrequest(
70
+ HTTPREQUEST, async (err, resbody) => {
71
+ if (err) {
72
+ console.log("DirReplaceBot error: ", err);
73
+ if (callback) {
74
+ callback();
75
+ return;
63
76
  }
64
77
  }
65
- this.tdClient.sendSupportMessage(
66
- this.requestId,
67
- message, (err) => {
68
- if (err) {
69
- console.error("Error sending hidden message:", err.message);
78
+
79
+ if (this.log) { console.log("DirReplaceBot replace resbody: ", resbody) };
80
+ if (blockName) {
81
+ if (this.log) { console.log("Sending hidden /start message to bot in dept"); }
82
+ const message = {
83
+ type: "text",
84
+ text: "/" + blockName,
85
+ attributes: {
86
+ subtype: "info"
70
87
  }
71
- if (this.log) {console.log("Hidden message sent.");}
72
- callback();
73
- });
74
- }
75
- else {
76
- callback();
88
+ }
89
+ this.tdClient.sendSupportMessage(
90
+ this.requestId,
91
+ message, (err) => {
92
+ if (err) {
93
+ console.error("Error sending hidden message:", err.message);
94
+ }
95
+ if (this.log) { console.log("Hidden message sent."); }
96
+ callback();
97
+ });
98
+ }
99
+ else {
100
+ callback();
101
+ }
77
102
  }
78
- });
103
+ )
104
+
105
+ // this.tdClient.replaceBotByName(this.requestId, botName, () => {
106
+ // if (blockName) {
107
+ // if (this.log) {console.log("Sending hidden /start message to bot in dept");}
108
+ // const message = {
109
+ // type: "text",
110
+ // text: "/" + blockName,
111
+ // attributes : {
112
+ // subtype: "info"
113
+ // }
114
+ // }
115
+ // this.tdClient.sendSupportMessage(
116
+ // this.requestId,
117
+ // message, (err) => {
118
+ // if (err) {
119
+ // console.error("Error sending hidden message:", err.message);
120
+ // }
121
+ // if (this.log) {console.log("Hidden message sent.");}
122
+ // callback();
123
+ // });
124
+ // }
125
+ // else {
126
+ // callback();
127
+ // }
128
+ // });
129
+ }
130
+
131
+ #myrequest(options, callback) {
132
+ if (this.log) {
133
+ console.log("API URL:", options.url);
134
+ console.log("** Options:", JSON.stringify(options));
135
+ }
136
+ let axios_options = {
137
+ url: options.url,
138
+ method: options.method,
139
+ params: options.params,
140
+ headers: options.headers
141
+ }
142
+ if (options.json !== null) {
143
+ axios_options.data = options.json
144
+ }
145
+ if (this.log) {
146
+ console.log("axios_options:", JSON.stringify(axios_options));
147
+ }
148
+ if (options.url.startsWith("https:")) {
149
+ const httpsAgent = new https.Agent({
150
+ rejectUnauthorized: false,
151
+ });
152
+ axios_options.httpsAgent = httpsAgent;
153
+ }
154
+ axios(axios_options)
155
+ .then((res) => {
156
+ if (this.log) {
157
+ console.log("Response for url:", options.url);
158
+ console.log("Response headers:\n", JSON.stringify(res.headers));
159
+ }
160
+ if (res && res.status == 200 && res.data) {
161
+ if (callback) {
162
+ callback(null, res.data);
163
+ }
164
+ }
165
+ else {
166
+ if (callback) {
167
+ callback(new Error("Response status is not 200"), null);
168
+ }
169
+ }
170
+ })
171
+ .catch((error) => {
172
+ console.error("(DirAskGPT) Axios error: ", JSON.stringify(error));
173
+ if (callback) {
174
+ callback(error, null);
175
+ }
176
+ });
79
177
  }
80
178
  }
81
179
 
package/TdCache_v4.js DELETED
@@ -1,146 +0,0 @@
1
- const redis = require('redis');
2
-
3
- class TdCache {
4
-
5
- constructor(config) {
6
- console.log("TdCache config: ", config);
7
- this.redis_host = config.host;
8
- this.redis_port = config.port;
9
- this.redis_password = config.password;
10
- console.log("TdCache this.redis_host: ", this.redis_host);
11
- console.log("TdCache this.redis_port: ", this.redis_port);
12
- console.log("TdCache this.redis_password: ", this.redis_password);
13
- this.client = null;
14
- this.redis_sub = null;
15
- }
16
-
17
- async connect(callback) {
18
-
19
- return new Promise( async (resolve, reject) => {
20
- /**
21
- * Connect redis client
22
- */
23
- this.client = redis.createClient(
24
- {
25
- url: `redis://${this.redis_host}:${this.redis_port}`,
26
- password: this.redis_password
27
- });
28
- this.client.on('error', err => {
29
- reject(err);
30
- if (callback) {
31
- callback(err);
32
- }
33
- });
34
- this.client.on('ready',function() {
35
- resolve();
36
- if (callback) {
37
- callback();
38
- }
39
- });
40
- await this.client.connect();
41
-
42
- /**
43
- * Connect redis subscription client
44
- */
45
- this.redis_sub = redis.createClient(
46
- {
47
- url: `redis://${this.redis_host}:${this.redis_port}`,
48
- password: this.redis_password
49
- });
50
- this.redis_sub.on('error', err => {
51
- reject(err);
52
- if (callback) {
53
- callback(err);
54
- }
55
- });
56
- this.redis_sub.on('ready',function() {
57
- resolve();
58
- if (callback) {
59
- callback();
60
- }
61
- });
62
- await this.redis_sub.connect();
63
- });
64
- }
65
-
66
- async set(key, value, options) {
67
- //console.log("setting key value", key, value)
68
- if (!options) {
69
- options = {EX: 86400}
70
- }
71
- await this.client.set(
72
- key,
73
- value,
74
- options);
75
- }
76
-
77
- async incr(key) {
78
- await this.client.incr(key);
79
- }
80
-
81
- async hset(dict_key, key, value, options) {
82
- if (!value) {
83
- return;
84
- }
85
- if (!options) {
86
- options = {EX: 86400}
87
- }
88
- await this.client.HSET(
89
- dict_key,
90
- key,
91
- value,
92
- options);
93
- }
94
-
95
- async hdel(dict_key, key) {
96
- await this.client.HDEL(dict_key, key);
97
- }
98
-
99
- async setJSON(key, value, options) {
100
- if (!value) {
101
- return;
102
- }
103
- if (!options) {
104
- options = {EX: 86400}
105
- }
106
- const _string = JSON.stringify(value);
107
- return await this.set(key, _string, options);
108
- }
109
-
110
- async get(key) {
111
- const value = await this.client.GET(key);
112
- return value;
113
- }
114
-
115
- async hgetall(dict_key) {
116
- const all = await this.client.HGETALL(dict_key);
117
- return all;
118
- }
119
-
120
- async hget(dict_key, key) {
121
- // console.log("hgetting dics", dict_key);
122
- const value = await this.client.HGET(dict_key, key);
123
- return value;
124
- }
125
-
126
- async getJSON(key, callback) {
127
- const value = await this.get(key);
128
- return JSON.parse(value);
129
- }
130
-
131
- async del(key) {
132
- await this.client.del(key);
133
- }
134
-
135
- async publish(key, value) {
136
- await this.redis_sub.publish(key, value);
137
- }
138
-
139
- // subscribe(key, callback) {
140
- // this.redis_sub.subscribe(key, (message) => {
141
- // callback(message);
142
- // });
143
- // }
144
- }
145
-
146
- module.exports = { TdCache };