@tiledesk/tiledesk-tybot-connector 0.5.0-rc1 → 0.5.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,7 +5,8 @@
5
5
  available on:
6
6
  ▶️ https://www.npmjs.com/package/@tiledesk/tiledesk-tybot-connector
7
7
 
8
- # v0.5.0-rc1
8
+ # v0.5.0
9
+ - added: AI_ENDPOINT env var
9
10
  - added: ability to get 'none' as bodytype in webresponse
10
11
 
11
12
  # v0.4.2
@@ -20,19 +21,6 @@ available on:
20
21
  - changed: refactoring of DIrWebRequestv2
21
22
  - bug-fixed: erro while parsing webrequestv2 body
22
23
 
23
- # v0.3.5-rc4
24
- - added: webhook action (same as intent one)
25
-
26
- # v0.3.5-rc3
27
- - bug-fixed: jsonBody parse error in web-request-v2
28
-
29
- # v0.3.5-rc2
30
- - bug-fixed: cannot set status of undefined reading res.status in DirAssistant
31
-
32
- # v0.3.5-rc1
33
- - changed: refactoring web-request-v2
34
- - bug-fixed: jsonBody parse error in web-request-v2
35
-
36
24
  # v0.3.4
37
25
  -bug-fixed: slit is undefined in TiledeskChatbotUtils
38
26
 
@@ -42,18 +30,6 @@ available on:
42
30
  # v0.3.2
43
31
  - bug-fixed: minor improvement
44
32
 
45
- # v0.2.153-rc9
46
- - changed: updated tiledesk-multi-worker to 0.2.1-rc2
47
-
48
- # v0.2.153-rc8
49
- - added: fixToken function in TiledeskService utils class
50
-
51
- # v0.2.153-rc4
52
- - log added
53
-
54
- # v0.2.153-rc3
55
- - added: specchToText function to transcript audio file
56
-
57
33
  # v0.2.153-rc1
58
34
  - changed: context for gpt-40 and gpt-40-mini
59
35
 
@@ -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 };
@@ -349,105 +349,6 @@ class TiledeskChatbotUtil {
349
349
  return all_buttons;
350
350
  }
351
351
 
352
- static replaceJSONButtons(message, flow_attributes) {
353
- let all_buttons = [];
354
- if (message.attributes && message.attributes.commands) {
355
- let commands = message.attributes.commands;
356
- if (commands.length > 0) {
357
- for (let i = 0; i < commands.length; i++) {
358
- let command = commands[i];
359
- if (command.type === 'message' && command.message) {
360
- if (command.message.attributes && command.message.attributes.attachment && command.message.attributes.attachment.json_buttons){
361
- // console.log("command with buttons ok:")
362
- let json_buttons_string = command.message.attributes.attachment.json_buttons;
363
- let json_buttons = null;
364
- let final_buttons = [];
365
- try {
366
- // fill buttons
367
- const filler = new Filler();
368
- json_buttons_string = filler.fill(json_buttons_string, flow_attributes);
369
- console.log("json_buttons_string:", json_buttons_string);
370
- console.log("type json_buttons_string:", typeof json_buttons_string);
371
- try {
372
- json_buttons = JSON.parse(json_buttons_string);
373
- console.log("json_buttons (parsed): ", json_buttons);
374
- } catch(err) {
375
- console.error("Error parsing json_buttons_string: ", err)
376
- }
377
-
378
- console.log("after try type json_buttons: ", typeof json_buttons, json_buttons);
379
- console.log("Array.isArray(json_buttons): ", Array.isArray(json_buttons));
380
- if (Array.isArray(json_buttons)) {
381
- console.log("json_buttons is array");
382
- json_buttons.forEach(button => {
383
- console.log("analyze button:", button);
384
- console.log("button.value:", typeof button.value, button.value);
385
- console.log("button.type:", typeof button.type, button.type);
386
- console.log("button.action:", typeof button.action, button.action);
387
- console.log("final_buttons:", final_buttons);
388
- if (button.value && button.type === "action" && button.action) {
389
- console.log("Case 1");
390
- button.show_echo = true;
391
- // console.log("pushing:", button)
392
- final_buttons.push(button);
393
- }
394
- else if (button.value && button.type === "text") {
395
- console.log("Case 2");
396
- button.show_echo = true;
397
- // console.log("pushing:", button)
398
- final_buttons.push(button);
399
- }
400
- else if (button.value && button.type === "url" && button.link) {
401
- console.log("Case 3");
402
- button.show_echo = true;
403
- // console.log("pushing:", button)
404
- final_buttons.push(button);
405
- }
406
- else {
407
- console.log("Invalid button. Skipping:", JSON.stringify(button) );
408
- }
409
- });
410
- }
411
-
412
- // "buttons": [
413
- // {
414
- // "type": "action",
415
- // "value": "Button1", // obbligatorio sempre
416
- // "action": "#bb347206-d639-4926-94c9-e94930623dce", // mandatory
417
- // "show_echo": true, // lo inserisco sempre
418
- // "alias": "button1 alias"
419
- // },
420
- // {
421
- // "type": "text",
422
- // "value": "Button2 text", // obbligatorio sempre
423
- // "show_echo": true // lo inserisco sempre
424
- // },
425
- // {
426
- // "type": "url",
427
- // "value": "Button3 link", // obbligatorio sempre
428
- // "link": "http://", // obbligatorio
429
- // "show_echo": true // lo inserisco sempre
430
- // }
431
- // ]
432
- }
433
- catch(error) {
434
- console.error("Invalid json_buttons:", error)
435
- }
436
- if (final_buttons && final_buttons.length > 0) {
437
- command.message.attributes.attachment.buttons = final_buttons;
438
- delete command.message.attributes.attachment.json_buttons;
439
- }
440
- else {
441
- console.log("Invalid json buttons. Skipped:", JSON.stringify(final_buttons));
442
- }
443
- }
444
- }
445
- }
446
- }
447
- }
448
- return all_buttons;
449
- }
450
-
451
352
  static buttonByText(text, buttons) {
452
353
  if (buttons === null || text === null) {
453
354
  return null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.5.0-rc1",
3
+ "version": "0.5.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",
@@ -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);
@@ -94,8 +94,8 @@ class DirAiPrompt {
94
94
  }
95
95
  }
96
96
 
97
- const llm_endpoint = process.env.KB_ENDPOINT_QA;
98
- if (this.log) { console.log("DirAiPrompt llm_endpoint ", llm_endpoint); }
97
+ const AI_endpoint = process.env.AI_ENDPOINT
98
+ if (this.log) { console.log("DirAiPrompt AI_endpoint ", AI_endpoint); }
99
99
 
100
100
  let key = await this.getKeyFromIntegrations(action.llm);
101
101
 
@@ -130,7 +130,7 @@ class DirAiPrompt {
130
130
  if (this.log) { console.log("DirAiPrompt json: ", json) }
131
131
 
132
132
  const HTTPREQUEST = {
133
- url: llm_endpoint + '/ask',
133
+ url: AI_endpoint + '/ask',
134
134
  headers: {
135
135
  'Content-Type': 'application/json'
136
136
  },
@@ -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
  }
@@ -76,7 +76,7 @@ class DirReplaceBotV2 {
76
76
  this.#myrequest(
77
77
  HTTPREQUEST, async (err, resbody) => {
78
78
  if (err) {
79
- console.log("DirReplaceBot error: ", err);
79
+ console.error("DirReplaceBot error: ", err);
80
80
  if (callback) {
81
81
  callback();
82
82
  return;
@@ -76,7 +76,7 @@ class DirReplaceBotV3 {
76
76
  this.#myrequest(
77
77
  HTTPREQUEST, async (err, resbody) => {
78
78
  if (err) {
79
- console.log("DirReplaceBot error: ", err);
79
+ console.error("DirReplaceBotv3 error: ", err);
80
80
  if (callback) {
81
81
  callback();
82
82
  return;
@@ -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,21 +38,16 @@ 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
  }
55
48
 
56
49
  async go(action, callback) {
57
50
  const message = action;
58
-
59
51
  // fill
60
52
  let requestAttributes = null;
61
53
  if (this.tdcache) {
@@ -63,22 +55,22 @@ class DirReply {
63
55
  await TiledeskChatbot.allParametersStatic(
64
56
  this.tdcache, this.requestId
65
57
  );
66
-
67
- TiledeskChatbotUtil.replaceJSONButtons(message, requestAttributes);
68
-
58
+ if (this.log) {
59
+ for (const [key, value] of Object.entries(requestAttributes)) {
60
+ const value_type = typeof value;
61
+ // if (this.log) {console.log("(DirReply) request parameter:", key, "value:", value, "type:", value_type)}
62
+ }
63
+ }
69
64
  const filler = new Filler();
70
65
  // fill text attribute
71
66
  message.text = filler.fill(message.text, requestAttributes);
72
-
73
67
  if (message.metadata) {
74
68
  if (this.log) {console.log("filling message 'metadata':", JSON.stringify(message.metadata));}
75
69
  if (message.metadata.src) {
76
70
  message.metadata.src = filler.fill(message.metadata.src, requestAttributes);
77
- this.logger.debug("Filled metadata.src with ", message.metadata.src);
78
71
  }
79
72
  if (message.metadata.name) {
80
73
  message.metadata.name = filler.fill(message.metadata.name, requestAttributes);
81
- this.logger.debug("Filled metadata.name with ", message.metadata.name);
82
74
  }
83
75
  }
84
76
  if (this.log) {console.log("filling commands'. Message:", JSON.stringify(message));}
@@ -92,7 +84,6 @@ class DirReply {
92
84
  let command = commands[i];
93
85
  if (command.type === 'message' && command.message && command.message.text) {
94
86
  command.message.text = filler.fill(command.message.text, requestAttributes);
95
- this.logger.debug("Filled message.text with ", command.message.text)
96
87
  TiledeskChatbotUtil.fillCommandAttachments(command, requestAttributes, this.log);
97
88
  if (this.log) {console.log("command filled:", command.message.text);}
98
89
  }
@@ -142,14 +133,12 @@ class DirReply {
142
133
  }
143
134
  catch(err) {
144
135
  console.error("An error occurred while JSON.parse(). Parsed value:" + value + " in allParametersStatic(). Error:", err);
145
- this.logger.error("An error occurred while JSON.parse(). Parsed value:" + value + " in allParametersStatic(). Error:", err);
146
136
  }
147
137
  }
148
138
  }
149
139
  }
150
140
  // send!
151
141
  let cleanMessage = message;
152
- this.logger.info("Sending reply with text ", cleanMessage.text);
153
142
  // cleanMessage = TiledeskChatbotUtil.removeEmptyReplyCommands(message);
154
143
  // if (!TiledeskChatbotUtil.isValidReply(cleanMessage)) {
155
144
  // console.log("invalid message", cleanMessage);
@@ -167,10 +156,8 @@ class DirReply {
167
156
  (err) => {
168
157
  if (err) {
169
158
  console.error("Error sending reply:", err);
170
- this.logger.error("Error sending reply ", err.response.data);
171
159
  }
172
160
  if (this.log) {console.log("Reply message sent:", JSON.stringify(cleanMessage));}
173
- this.logger.info("Reply message sent!", cleanMessage.text);
174
161
  const delay = TiledeskChatbotUtil.totalMessageWait(cleanMessage);
175
162
  // console.log("got total delay:", delay)
176
163
  if (delay > 0 && delay <= 30000) { // prevent long delays
@@ -79,8 +79,6 @@ class DirReplyV2 {
79
79
  }
80
80
  }
81
81
 
82
- TiledeskChatbotUtil.replaceJSONButtons(message, requestAttributes);
83
-
84
82
  try {
85
83
  // lock/unlock + no-match
86
84
  // get buttons if available
@@ -58,7 +58,6 @@ 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
61
  static WEB_RESPONSE = "web_response";
63
62
 
64
63
  // static WHEN_ONLINE_MOVE_TO_AGENT = "whenonlinemovetoagent"; // DEPRECATED?
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 }