@tiledesk/tiledesk-tybot-connector 0.2.135 → 0.2.137-rc1

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,6 +17,12 @@ available on:
17
17
  - Added flowError on JSONCondition when result = null
18
18
  - Added fix on Filler -->
19
19
 
20
+ # v0.2.137-rc1
21
+ - Updated: hidden message is enabled only in dev mode for dratf requests
22
+
23
+ # v0.2.136
24
+ - TdCache rollback
25
+
20
26
  # v0.2.135
21
27
  - Updated to redis 4.7.0 + Tdcache.js
22
28
  - Disabled close_directive_test.js - DirClose directive - too old, not passing
package/TdCache.js CHANGED
@@ -7,7 +7,6 @@ class TdCache {
7
7
  this.redis_port = config.port;
8
8
  this.redis_password = config.password;
9
9
  this.client = null;
10
- this.redis_sub = null;
11
10
  }
12
11
 
13
12
  async connect(callback) {
@@ -25,22 +24,17 @@ class TdCache {
25
24
  callback(err);
26
25
  }
27
26
  });
27
+ // this.client.on('connect', function() {
28
+ // console.log('Redis Connected!');
29
+ // });
28
30
  this.client.on('ready',function() {
31
+ // console.log("connected")
29
32
  resolve();
30
33
  if (callback) {
31
34
  callback();
32
35
  }
33
36
  //console.log("Redis is ready.");
34
37
  });
35
- await this.client.connect();
36
- this.redis_sub = redis.createClient(
37
- {
38
- host: this.redis_host,
39
- port: this.redis_port,
40
- password: this.redis_password
41
- });
42
- // console.log("redis is:", this.redis_sub)
43
- await this.redis_sub.connect();
44
38
  });
45
39
  }
46
40
 
@@ -49,59 +43,202 @@ class TdCache {
49
43
  if (!options) {
50
44
  options = {EX: 86400}
51
45
  }
52
- await this.client.set(
53
- key,
54
- value,
55
- options);
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
+ });
56
95
  }
57
96
 
58
97
  async incr(key) {
59
- await this.client.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
+ });
60
110
  }
61
111
 
62
112
  async hset(dict_key, key, value, options) {
63
- if (!value) {
64
- return;
65
- }
66
- if (!options) {
67
- options = {EX: 86400}
68
- }
69
- await this.client.HSET(
70
- dict_key,
71
- key,
72
- value,
73
- 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
+ });
74
146
  }
75
147
 
76
- async hdel(dict_key, key) {
77
- await this.client.HDEL(dict_key, key);
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
+ });
78
180
  }
79
181
 
80
182
  async setJSON(key, value, options) {
81
- if (!value) {
82
- return;
83
- }
84
- if (!options) {
85
- options = {EX: 86400}
86
- }
87
183
  const _string = JSON.stringify(value);
88
184
  return await this.set(key, _string, options);
89
185
  }
90
186
 
91
- async get(key) {
92
- const value = await this.client.GET(key);
93
- return value;
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
+ });
94
202
  }
95
203
 
96
- async hgetall(dict_key) {
97
- const all = await this.client.HGETALL(dict_key);
98
- return all;
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
+ });
99
222
  }
100
223
 
101
- async hget(dict_key, key) {
102
- // console.log("hgetting dics", dict_key);
103
- const value = await this.client.HGET(dict_key, key);
104
- return value;
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
+ });
105
242
  }
106
243
 
107
244
  async getJSON(key, callback) {
@@ -109,19 +246,16 @@ class TdCache {
109
246
  return JSON.parse(value);
110
247
  }
111
248
 
112
- async del(key) {
113
- await this.client.del(key);
114
- }
115
-
116
- async publish(key, value) {
117
- await this.redis_sub.publish(key, value);
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
+ })
118
258
  }
119
-
120
- // subscribe(key, callback) {
121
- // this.redis_sub.subscribe(key, (message) => {
122
- // callback(message);
123
- // });
124
- // }
125
259
  }
126
260
 
127
261
  module.exports = { TdCache };
@@ -1,6 +1,7 @@
1
1
  const { TiledeskExpression } = require('../TiledeskExpression');
2
2
  const { Filler } = require('../tiledeskChatbotPlugs/Filler');
3
3
  const { TiledeskChatbotConst } = require('./TiledeskChatbotConst');
4
+ const { TiledeskChatbot } = require('./TiledeskChatbot.js');
4
5
  let parser = require('accept-language-parser');
5
6
  const { Directives } = require('../tiledeskChatbotPlugs/directives/Directives.js');
6
7
  require('dotenv').config();
@@ -516,13 +517,10 @@ class TiledeskChatbotUtil {
516
517
  await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN, chatbotToken); // DEPRECATED
517
518
  await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN_v2, "JWT " + chatbotToken);
518
519
  }
519
- /**
520
- * RefactoringCheck: can this be deleted?
521
- */
522
- // if (process.env.TILEDESK_API) {
523
- // await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN, chatbotToken); // DEPRECATED
524
- // await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN_v2, "JWT " + chatbotToken);
525
- // }
520
+ if (process.env.TILEDESK_API) {
521
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN, chatbotToken); // DEPRECATED
522
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN_v2, "JWT " + chatbotToken);
523
+ }
526
524
 
527
525
  if (process.env.API_URL) {
528
526
  await chatbot.addParameter(TiledeskChatbotConst.API_BASE_URL, process.env.API_URL);
@@ -701,14 +699,7 @@ class TiledeskChatbotUtil {
701
699
  await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY, message.attributes.departmentId);
702
700
  await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY, message.attributes.departmentName);
703
701
  }
704
- if (message && message.request && message.request.attributes && message.request.attributes.payload) {
705
- if (!message.attributes) {
706
- message.attributes = {}
707
- }
708
- message.attributes.payload = message.request.attributes.payload
709
- if (chatbot.log) {console.log("FORCED SET message.attributes.payload:", JSON.stringify(message.attributes.payload))}
710
- // if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > FORCED SET message.attributes.payload:", JSON.stringify(message.attributes.payload))}
711
- }
702
+
712
703
  if (message.attributes) {
713
704
  if (chatbot.log) {console.log("Ok message.attributes", JSON.stringify(message.attributes));}
714
705
  // if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > Ok message.attributes", JSON.stringify(message.attributes));}
@@ -743,6 +734,17 @@ class TiledeskChatbotUtil {
743
734
  await chatbot.addParameter("ani", message.attributes.ani);
744
735
  }
745
736
  }
737
+
738
+ /** DEPRECATED */
739
+ if (message && message.request && message.request.attributes && message.request.attributes.payload) {
740
+ if (!message.attributes) {
741
+ message.attributes = {}
742
+ }
743
+ message.attributes.payload = message.request.attributes.payload
744
+ if (chatbot.log) {console.log("FORCED SET message.attributes.payload:", JSON.stringify(message.attributes.payload))}
745
+ // if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > FORCED SET message.attributes.payload:", JSON.stringify(message.attributes.payload))}
746
+ }
747
+
746
748
 
747
749
  const _bot = chatbot.bot; // aka FaqKB
748
750
  if (chatbot.log) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.2.135",
3
+ "version": "0.2.137-rc1",
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": "^4.7.0",
32
+ "redis": "^3.1.2",
33
33
  "uuid": "^3.3.3",
34
34
  "vm2": "^3.9.13"
35
35
  },
@@ -6,7 +6,6 @@ const { DirIntent } = require("./DirIntent");
6
6
  const { TiledeskChatbotConst } = require("../../models/TiledeskChatbotConst");
7
7
  const { TiledeskChatbotUtil } = require("../../models/TiledeskChatbotUtil");
8
8
  const req = require("express/lib/request");
9
- const { rejects } = require("assert");
10
9
  const { update } = require("../../models/faq");
11
10
  const { TiledeskClient } = require("@tiledesk/tiledesk-client");
12
11
  require('dotenv').config();
@@ -274,7 +273,7 @@ class DirAddTags {
274
273
  }
275
274
 
276
275
  async addNewTag(tag){
277
- return new Promise((resolve, rejects)=> {
276
+ return new Promise((resolve, reject)=> {
278
277
  const HTTPREQUEST = {
279
278
  url: this.API_ENDPOINT + "/" + this.context.projectId + "/tags",
280
279
  headers: {
@@ -507,7 +507,7 @@ class DirAskGPTV2 {
507
507
  }
508
508
 
509
509
  async updateQuote(tokens_usage) {
510
- return new Promise((resolve) => {
510
+ return new Promise((resolve, reject) => {
511
511
 
512
512
  const HTTPREQUEST = {
513
513
  url: this.API_ENDPOINT + "/" + this.context.projectId + "/quotes/incr/tokens",
@@ -524,7 +524,7 @@ class DirAskGPTV2 {
524
524
  HTTPREQUEST, async (err, resbody) => {
525
525
  if (err) {
526
526
  console.error("(httprequest) DirAskGPT Increment tokens quote err: ", err);
527
- rejects(false)
527
+ reject(false)
528
528
  } else {
529
529
  // console.log("(httprequest) DirAskGPT Increment token quote resbody: ", resbody);
530
530
  resolve(true);
@@ -458,7 +458,7 @@ class DirGptTask {
458
458
  }
459
459
 
460
460
  async updateQuote(tokens_usage) {
461
- return new Promise((resolve) => {
461
+ return new Promise((resolve, reject) => {
462
462
 
463
463
  const HTTPREQUEST = {
464
464
  url: this.API_ENDPOINT + "/" + this.context.projectId + "/quotes/incr/tokens",
@@ -475,7 +475,7 @@ class DirGptTask {
475
475
  HTTPREQUEST, async (err, resbody) => {
476
476
  if (err) {
477
477
  console.error("(httprequest) DirGptTask Increment tokens quote err: ", err);
478
- rejects(false)
478
+ reject(false)
479
479
  } else {
480
480
  if (this.log) { console.log("(httprequest) DirGptTask Increment token quote resbody: ", resbody); }
481
481
  resolve(true);
@@ -1,5 +1,4 @@
1
1
  // const { TiledeskClient } = require('@tiledesk/tiledesk-client');
2
- const { rejects } = require('assert');
3
2
  const { DirIntent } = require('./DirIntent');
4
3
  const axios = require("axios").default;
5
4
  let https = require("https");
@@ -25,6 +25,7 @@ class DirMessage {
25
25
  this.requestId = context.requestId;
26
26
  this.token = context.token;
27
27
  this.log = this.context.log;
28
+ this.supportRequest = this.context.supportRequest
28
29
  }
29
30
 
30
31
  execute(directive, callback) {
@@ -94,12 +95,18 @@ class DirMessage {
94
95
  const message = action;
95
96
  if (this.log) {console.log("Message to extEndpoint:", JSON.stringify(message))};
96
97
 
97
- if (this.projectId === "656054000410fa00132e5dcc") {
98
+ if(this.supportRequest && this.supportRequest.draft){
98
99
  if (!message.text.startsWith('/')) {
99
100
  callback();
100
101
  return;
101
102
  }
102
103
  }
104
+ // if (this.projectId === "656054000410fa00132e5dcc") {
105
+ // if (!message.text.startsWith('/')) {
106
+ // callback();
107
+ // return;
108
+ // }
109
+ // }
103
110
 
104
111
  let extEndpoint = `${this.API_ENDPOINT}/modules/tilebot`;
105
112
  if (this.TILEBOT_ENDPOINT) {