@tiledesk/tiledesk-tybot-connector 0.3.5-rc4 → 0.4.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.
package/CHANGELOG.md CHANGED
@@ -5,18 +5,11 @@
5
5
  available on:
6
6
  ▶️ https://www.npmjs.com/package/@tiledesk/tiledesk-tybot-connector
7
7
 
8
- # v0.3.5-rc4
9
- - added: webhook action (same as intent one)
10
-
11
- # v0.3.5-rc3
12
- - bug-fixed: jsonBody parse error in web-request-v2
13
-
14
- # v0.3.5-rc2
15
- - bug-fixed: cannot set status of undefined reading res.status in DirAssistant
16
-
17
- # v0.3.5-rc1
18
- - changed: refactoring web-request-v2
19
- - bug-fixed: jsonBody parse error in web-request-v2
8
+ # v0.4.0
9
+ - added: DirWebResposne
10
+ - added: management of webhook
11
+ - changed: refactoring of DIrWebRequestv2
12
+ - bug-fixed: erro while parsing webrequestv2 body
20
13
 
21
14
  # v0.3.4
22
15
  -bug-fixed: slit is undefined in TiledeskChatbotUtils
@@ -27,18 +20,6 @@ available on:
27
20
  # v0.3.2
28
21
  - bug-fixed: minor improvement
29
22
 
30
- # v0.2.153-rc9
31
- - changed: updated tiledesk-multi-worker to 0.2.1-rc2
32
-
33
- # v0.2.153-rc8
34
- - added: fixToken function in TiledeskService utils class
35
-
36
- # v0.2.153-rc4
37
- - log added
38
-
39
- # v0.2.153-rc3
40
- - added: specchToText function to transcript audio file
41
-
42
23
  # v0.2.153-rc1
43
24
  - changed: context for gpt-40 and gpt-40-mini
44
25
 
@@ -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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.3.5-rc4",
3
+ "version": "0.4.0",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -15,7 +15,6 @@
15
15
  "@tiledesk/tiledesk-chatbot-client": "^0.5.30",
16
16
  "@tiledesk/tiledesk-chatbot-util": "^0.8.39",
17
17
  "@tiledesk/tiledesk-client": "^0.10.13",
18
- "@tiledesk/tiledesk-multi-worker": "^0.2.1-rc2",
19
18
  "accept-language-parser": "^1.5.0",
20
19
  "axios": "^1.7.7",
21
20
  "body-parser": "^1.19.0",
@@ -55,8 +55,8 @@ const { DirAddTags } = require('./directives/DirAddTags');
55
55
  const { DirSendWhatsapp } = require('./directives/DirSendWhatsapp');
56
56
  const { DirReplaceBotV3 } = require('./directives/DirReplaceBotV3');
57
57
  const { DirAiTask, DirAiPrompt } = require('./directives/DirAiPrompt');
58
- const { DirConnectBlock } = require('./directives/DirConnectBlock');
59
58
  const { DirWebResponse } = require('./directives/DirWebResponse');
59
+ const { DirConnectBlock } = require('./directives/DirConnectBlock');
60
60
 
61
61
  class DirectivesChatbotPlug {
62
62
 
@@ -774,19 +774,6 @@ class DirectivesChatbotPlug {
774
774
  }
775
775
  });
776
776
  }
777
- else if (directive_name === Directives.WEBHOOK) {
778
- // console.log(".....DirIntent")
779
- new DirIntent(context).execute(directive, async (stop) => {
780
- if (stop) {
781
- if (context.log) { console.log("Stopping Actions on:", JSON.stringify(directive));}
782
- this.theend();
783
- }
784
- else {
785
- let next_dir = await this.nextDirective(this.directives);
786
- this.process(next_dir);
787
- }
788
- });
789
- }
790
777
  else if (directive_name === Directives.WEB_RESPONSE) {
791
778
  new DirWebResponse(context).execute(directive, async () => {
792
779
  let next_dir = await this.nextDirective(this.directives);
@@ -71,12 +71,11 @@ class DirIntent {
71
71
  "recipient": requestId,
72
72
  "text": intent_command,
73
73
  "id_project": projectId,
74
- "request": this.supportRequest,
75
- // "request": {
76
- // "request_id": requestId,
77
- // "id_project": projectId
78
- // // "bot_id": botId
79
- // }
74
+ "request": {
75
+ "request_id": requestId,
76
+ "id_project": projectId
77
+ // "bot_id": botId
78
+ }
80
79
  },
81
80
  "token": this.token
82
81
  }
@@ -3,7 +3,6 @@ const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
3
3
  const { TiledeskChatbotUtil } = require('../../models/TiledeskChatbotUtil');
4
4
  let axios = require('axios');
5
5
  const { TiledeskClient } = require('@tiledesk/tiledesk-client');
6
- const { Logger } = require('../../Logger');
7
6
 
8
7
  class DirReply {
9
8
 
@@ -17,8 +16,6 @@ class DirReply {
17
16
  this.token = context.token;
18
17
  this.tdcache = context.tdcache;
19
18
  this.log = context.log;
20
- this.supportRequest = this.context.supportRequest;
21
- this.logger = new Logger({ request_id: this.requestId, dev: this.context.supportRequest.draft });
22
19
 
23
20
  this.API_ENDPOINT = context.API_ENDPOINT;
24
21
  this.tdClient = new TiledeskClient({
@@ -41,14 +38,10 @@ class DirReply {
41
38
  }
42
39
  else {
43
40
  console.error("Incorrect directive (no action provided):", directive);
44
- this.logger.error("Incorrect directive (no action provided):", directive);
45
41
  callback();
46
42
  return;
47
43
  }
48
- this.logger.info("Executing Action Reply ", directive.action)
49
-
50
44
  this.go(action, () => {
51
- this.logger.info("Action Reply terminated")
52
45
  callback();
53
46
  });
54
47
  }
@@ -71,16 +64,13 @@ class DirReply {
71
64
  const filler = new Filler();
72
65
  // fill text attribute
73
66
  message.text = filler.fill(message.text, requestAttributes);
74
-
75
67
  if (message.metadata) {
76
68
  if (this.log) {console.log("filling message 'metadata':", JSON.stringify(message.metadata));}
77
69
  if (message.metadata.src) {
78
70
  message.metadata.src = filler.fill(message.metadata.src, requestAttributes);
79
- this.logger.debug("Filled metadata.src with ", message.metadata.src);
80
71
  }
81
72
  if (message.metadata.name) {
82
73
  message.metadata.name = filler.fill(message.metadata.name, requestAttributes);
83
- this.logger.debug("Filled metadata.name with ", message.metadata.name);
84
74
  }
85
75
  }
86
76
  if (this.log) {console.log("filling commands'. Message:", JSON.stringify(message));}
@@ -94,7 +84,6 @@ class DirReply {
94
84
  let command = commands[i];
95
85
  if (command.type === 'message' && command.message && command.message.text) {
96
86
  command.message.text = filler.fill(command.message.text, requestAttributes);
97
- this.logger.debug("Filled message.text with ", command.message.text)
98
87
  TiledeskChatbotUtil.fillCommandAttachments(command, requestAttributes, this.log);
99
88
  if (this.log) {console.log("command filled:", command.message.text);}
100
89
  }
@@ -144,14 +133,12 @@ class DirReply {
144
133
  }
145
134
  catch(err) {
146
135
  console.error("An error occurred while JSON.parse(). Parsed value:" + value + " in allParametersStatic(). Error:", err);
147
- this.logger.error("An error occurred while JSON.parse(). Parsed value:" + value + " in allParametersStatic(). Error:", err);
148
136
  }
149
137
  }
150
138
  }
151
139
  }
152
140
  // send!
153
141
  let cleanMessage = message;
154
- this.logger.info("Sending reply with text ", cleanMessage.text);
155
142
  // cleanMessage = TiledeskChatbotUtil.removeEmptyReplyCommands(message);
156
143
  // if (!TiledeskChatbotUtil.isValidReply(cleanMessage)) {
157
144
  // console.log("invalid message", cleanMessage);
@@ -169,10 +156,8 @@ class DirReply {
169
156
  (err) => {
170
157
  if (err) {
171
158
  console.error("Error sending reply:", err);
172
- this.logger.error("Error sending reply ", err.response.data);
173
159
  }
174
160
  if (this.log) {console.log("Reply message sent:", JSON.stringify(cleanMessage));}
175
- this.logger.info("Reply message sent!", cleanMessage.text);
176
161
  const delay = TiledeskChatbotUtil.totalMessageWait(cleanMessage);
177
162
  // console.log("got total delay:", delay)
178
163
  if (delay > 0 && delay <= 30000) { // prevent long delays
@@ -58,8 +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';
62
- static WEB_RESPONSE = "web_response";
61
+ static WEB_RESPONSE = "webresponse";
63
62
 
64
63
  // static WHEN_ONLINE_MOVE_TO_AGENT = "whenonlinemovetoagent"; // DEPRECATED?
65
64
  // static WHEN_OFFLINE_HOURS = "whenofflinehours"; // DEPRECATED // adds a message on top of the original message when offline hours opts: --replace
package/Logger.js DELETED
@@ -1,86 +0,0 @@
1
- let { Publisher } = require("@tiledesk/tiledesk-multi-worker");
2
-
3
- const AMQP_MANAGER_URL = process.env.AMQP_MANAGER_URL;
4
- let publisher = new Publisher(AMQP_MANAGER_URL, {
5
- debug: false,
6
- queueName: "logs_queue",
7
- exchange: "tiledesk-multi",
8
- topic: "logs",
9
- })
10
-
11
- class Logger {
12
-
13
- constructor(config) {
14
-
15
- if (!config) {
16
- throw new Error('config is mandatory');
17
- }
18
-
19
- if (!config.request_id) {
20
- console.error('config.request_id is mandatory');
21
- //throw new Error('config.request_id is mandatory');
22
- }
23
-
24
- this.request_id = config.request_id;
25
- this.dev = false;
26
- if (config.dev && config.dev === true) {
27
- this.dev = true;
28
- }
29
-
30
- if (!AMQP_MANAGER_URL) {
31
- console.error('AMQP_MANAGER_URL is undefined. Logger not available...');
32
- return;
33
- //throw new Error("Error starting logger: AMQP_MANAGER_URL is undefined.")
34
- }
35
-
36
- }
37
-
38
- error(...args) {
39
- let log = this.formatLog(args);
40
- return this.base('error', log);
41
- }
42
-
43
- warn(...args) {
44
- let log = this.formatLog(args);
45
- return this.base('warn', log);
46
- }
47
-
48
- info(...args) {
49
- let log = this.formatLog(args);
50
- return this.base('info', log);
51
- }
52
-
53
- debug(...args) {
54
- let log = this.formatLog(args);
55
- return this.base('debug', log);
56
- }
57
-
58
- base(level, text) {
59
- if (!this.request_id || !publisher) {
60
- console.log("Return because request or publisher is undefined", this.request_id, publisher);
61
- return;
62
- }
63
-
64
- let data = {
65
- request_id: this.request_id,
66
- text: text,
67
- level: level,
68
- timestamp: new Date(),
69
- dev: this.dev
70
- }
71
-
72
- publisher.publish(data, (err, ok) => {
73
- if (err) console.warn("publish log fail: ", err);
74
- return;
75
- })
76
- }
77
-
78
- formatLog(args) {
79
- return args
80
- .map(arg => (typeof arg === "object" ? JSON.stringify(arg, null, 2) : arg ))
81
- .join(" ")
82
- }
83
-
84
- }
85
-
86
- module.exports = { Logger }