@tiledesk/tiledesk-tybot-connector 2.0.21-rc9 → 2.0.21

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.
@@ -8,6 +8,8 @@ require('dotenv').config();
8
8
  let axios = require('axios');
9
9
  const winston = require('./winston');
10
10
 
11
+ const { CHANNEL_NAME } = require('./constants.js')
12
+
11
13
  class TiledeskChatbotUtil {
12
14
 
13
15
  static parseIntent(explicit_intent_name) {
@@ -307,204 +309,32 @@ class TiledeskChatbotUtil {
307
309
  if (command.message.attributes && command.message.attributes.attachment && command.message.attributes.attachment.json_buttons){
308
310
  // console.log("command with buttons ok:")
309
311
  let json_buttons_string = command.message.attributes.attachment.json_buttons;
310
- console.log("json_buttons_string:", json_buttons_string)
311
- let final_buttons = this.renderJSONButtons(json_buttons_string, flow_attributes);
312
- // let final_buttons = [];
313
- // try {
314
- // // fill buttons
315
- // const filler = new Filler();
316
- // json_buttons_string = filler.fill(json_buttons_string, flow_attributes);
317
- // let json_buttons = JSON.parse(json_buttons_string);
318
- // if (Array.isArray(json_buttons)) {
319
- // json_buttons.forEach(button => {
320
- // if (button.value && button.type === "action" && button.action) {
321
- // button.show_echo = true;
322
- // final_buttons.push(button);
323
- // }
324
- // else if (button.value && button.type === "text") {
325
- // button.show_echo = true;
326
- // final_buttons.push(button);
327
- // }
328
- // else if (button.value && button.type === "url" && button.link) {
329
- // button.show_echo = true;
330
- // final_buttons.push(button);
331
- // }
332
- // else {
333
- // winston.verbose("Invalid button. Skipping:", button);
334
- // }
335
- // });
336
- // }
337
-
338
- // // "buttons": [
339
- // // {
340
- // // "type": "action",
341
- // // "value": "Button1", // obbligatorio sempre
342
- // // "action": "#bb347206-d639-4926-94c9-e94930623dce", // mandatory
343
- // // "show_echo": true, // lo inserisco sempre
344
- // // "alias": "button1 alias"
345
- // // },
346
- // // {
347
- // // "type": "text",
348
- // // "value": "Button2 text", // obbligatorio sempre
349
- // // "show_echo": true // lo inserisco sempre
350
- // // },
351
- // // {
352
- // // "type": "url",
353
- // // "value": "Button3 link", // obbligatorio sempre
354
- // // "link": "http://", // obbligatorio
355
- // // "show_echo": true // lo inserisco sempre
356
- // // }
357
- // // ]
358
- // }
359
- // catch(error) {
360
- // winston.warn("Invalid json_buttons:", error)
361
- // }
362
- if (final_buttons && final_buttons.length > 0) {
363
- command.message.attributes.attachment.buttons = final_buttons;
364
- delete command.message.attributes.attachment.json_buttons;
365
- }
366
- else {
367
- winston.verbose("Invalid json_buttons. Skipping...")
368
- }
369
- }
370
- }
371
- }
372
- }
373
- }
374
- return all_buttons;
375
- }
376
-
377
- static renderJSONButtons(json_buttons_string, flow_attributes) {
378
- let final_buttons = [];
379
- try {
380
- // fill buttons
381
- const filler = new Filler();
382
- json_buttons_string = filler.fill(json_buttons_string, flow_attributes);
383
- let json_buttons = JSON.parse(json_buttons_string);
384
- if (Array.isArray(json_buttons)) {
385
- json_buttons.forEach(button => {
386
- if (button.value && button.type === "action" && button.action) {
387
- button.show_echo = true;
388
- final_buttons.push(button);
389
- }
390
- else if (button.value && button.type === "text") {
391
- button.show_echo = true;
392
- final_buttons.push(button);
393
- }
394
- else if (button.value && button.type === "url" && button.link) {
395
- button.show_echo = true;
396
- final_buttons.push(button);
397
- }
398
- else {
399
- winston.verbose("Invalid button. Skipping:", button);
400
- }
401
- });
402
- }
403
-
404
- // "buttons": [
405
- // {
406
- // "type": "action",
407
- // "value": "Button1", // obbligatorio sempre
408
- // "action": "#bb347206-d639-4926-94c9-e94930623dce", // mandatory
409
- // "show_echo": true, // lo inserisco sempre
410
- // "alias": "button1 alias"
411
- // },
412
- // {
413
- // "type": "text",
414
- // "value": "Button2 text", // obbligatorio sempre
415
- // "show_echo": true // lo inserisco sempre
416
- // },
417
- // {
418
- // "type": "url",
419
- // "value": "Button3 link", // obbligatorio sempre
420
- // "link": "http://", // obbligatorio
421
- // "show_echo": true // lo inserisco sempre
422
- // }
423
- // ]
424
- }
425
- catch(error) {
426
- winston.warn("Invalid json_buttons:", error)
427
- return null;
428
- }
429
- return final_buttons;
430
- }
431
-
432
- static replaceJSONGalleries(message, flow_attributes) {
433
- console.log("replaceJSONGalleries...");
434
- if (message.attributes && message.attributes.commands) {
435
- console.log("message.attributes && message.attributes.commands...");
436
- let commands = message.attributes.commands;
437
- if (commands.length > 0) {
438
- console.log("commands.length > 0");
439
- for (let i = 0; i < commands.length; i++) {
440
- let command = commands[i];
441
- console.log("command:", JSON.stringify(command));
442
- if (command.type === 'message' && command.message) {
443
- console.log("command.type === 'message' && command.message");
444
- if (command.message.attributes && command.message.attributes.attachment && command.message.attributes.attachment.json_gallery){
445
- console.log("command with json_galley")
446
- let final_gallery = [];
312
+ let json_buttons = null;
313
+ let final_buttons = [];
447
314
  try {
448
- // fill previews
315
+ // fill buttons
449
316
  const filler = new Filler();
450
- let json_gallery_string = command.message.attributes.attachment.json_gallery;
451
- console.log("gallerystring is:", json_gallery_string)
452
- json_gallery_string = filler.fill(json_gallery_string, flow_attributes);
453
- let json_gallery = JSON.parse(json_gallery_string);
454
- if (Array.isArray(json_gallery)) {
455
- console.log("is gallery")
456
- json_gallery.forEach(el => {
457
- if (el.buttons) {
458
- el.buttons = TiledeskChatbotUtil.renderJSONButtons(JSON.stringify(el.buttons));
317
+ json_buttons_string = filler.fill(json_buttons_string, flow_attributes);
318
+ json_buttons = JSON.parse(json_buttons_string);
319
+ if (Array.isArray(json_buttons)) {
320
+ json_buttons.forEach(button => {
321
+ if (button.value && button.type === "action" && button.action) {
322
+ button.show_echo = true;
323
+ final_buttons.push(button);
324
+ }
325
+ else if (button.value && button.type === "text") {
326
+ button.show_echo = true;
327
+ final_buttons.push(button);
328
+ }
329
+ else if (button.value && button.type === "url" && button.link) {
330
+ button.show_echo = true;
331
+ final_buttons.push(button);
332
+ }
333
+ else {
334
+ winston.verbose("Invalid button. Skipping:", button);
459
335
  }
460
- final_gallery.push(el);
461
336
  });
462
- console.log("final: ", final_gallery)
463
- }
464
- else {
465
- winston.verbose("Invalid json_gallery.");
466
337
  }
467
- // "gallery": [
468
- // {
469
- // "preview": {
470
- // "src": "https://eu.rtmv3.tiledesk.com/api/images?path=uploads%2Fusers%2F63a05d755f117f0013541383%2Fimages%2F8913ff2c-d788-45e1-ac71-ee5bae8479e2%2Fhybrid-settings.png",
471
- // "uid": "mcamfa6s"
472
- // },
473
- // "title": "Title 1",
474
- // "description": "Description 1",
475
- // "buttons": [
476
- // {
477
- // "uid": "0a956f4637584ee4862360c19a161f8f",
478
- // "type": "url",
479
- // "value": "Prod1",
480
- // "link": "https://URL1",
481
- // "target": "blank",
482
- // "action": "",
483
- // "attributes": "",
484
- // "show_echo": true
485
- // },
486
- // {
487
- // "uid": "4a87abe3d03a4b6fbdbc3fc33c4a8430",
488
- // "type": "action",
489
- // "value": "Prod1.1 (connector)",
490
- // "link": "",
491
- // "target": "blank",
492
- // "action": "#0f7aaefd-3147-466b-82a4-06756f36eea5",
493
- // "attributes": "",
494
- // "show_echo": true
495
- // },
496
- // {
497
- // "uid": "31fac2c82ce24da0a2e9850a32165fe8",
498
- // "type": "text",
499
- // "value": "Prod1.2 (text)",
500
- // "link": "https://url2",
501
- // "target": "blank",
502
- // "action": "",
503
- // "attributes": "",
504
- // "show_echo": true
505
- // }
506
- // ]
507
- // },
508
338
 
509
339
  // "buttons": [
510
340
  // {
@@ -528,23 +358,21 @@ class TiledeskChatbotUtil {
528
358
  // ]
529
359
  }
530
360
  catch(error) {
531
- winston.warn("Error on JSON gallery parsing:", error);
361
+ winston.warn("Invalid json_buttons:", error)
532
362
  }
533
- console.log("final gallery...", final_gallery)
534
- if (final_gallery && final_gallery.length > 0) {
535
- console.log("updating with final gallery...", final_gallery)
536
- command.message.attributes.attachment.gallery = final_gallery;
537
- delete command.message.attributes.attachment.json_gallery;
363
+ if (final_buttons && final_buttons.length > 0) {
364
+ command.message.attributes.attachment.buttons = final_buttons;
365
+ delete command.message.attributes.attachment.json_buttons;
538
366
  }
539
367
  else {
540
- winston.verbose("Invalid JSON Gallery.")
368
+ winston.verbose("Invalid json_buttons. Skipping...")
541
369
  }
542
370
  }
543
371
  }
544
372
  }
545
373
  }
546
374
  }
547
- return message;
375
+ return all_buttons;
548
376
  }
549
377
 
550
378
  static buttonByText(text, buttons) {
@@ -778,10 +606,8 @@ class TiledeskChatbotUtil {
778
606
  }
779
607
  let currentLeadName = await chatbot.getParameter(TiledeskChatbotConst.REQ_LEAD_USERFULLNAME_KEY);
780
608
  winston.debug("(TiledeskChatbotUtil) You lead email from attributes: " + currentLeadEmail);
781
- console.log("currentLeadName: ", currentLeadName)
782
609
  if (message.request.lead.fullname && !currentLeadName) {
783
610
  // worth saving
784
- console.log("inside if")
785
611
  winston.debug("(TiledeskChatbotUtil) worth saving email");
786
612
  try {
787
613
  await chatbot.addParameter(TiledeskChatbotConst.REQ_LEAD_USERFULLNAME_KEY, message.request.lead.fullname);
@@ -794,7 +620,10 @@ class TiledeskChatbotUtil {
794
620
  if (message.request.lead.phone) {
795
621
  await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_PHONE_KEY, message.request.lead.phone);
796
622
  }
797
- if (message.request.lead.lead_id && message.request.lead.lead_id.startsWith("wab-")) {
623
+ if (message.request.lead.lead_id && (message.request.lead.lead_id.startsWith("wab-") ||
624
+ message.request.lead.lead_id.startsWith("vxml-") ||
625
+ message.request.lead.lead_id.startsWith(CHANNEL_NAME.VOICE_TWILIO) ||
626
+ message.request.lead.lead_id.startsWith(CHANNEL_NAME.SMS))) {
798
627
  const splits = message.request.lead.lead_id.split("-");
799
628
  if (splits && splits.length > 1) {
800
629
  await chatbot.addParameter(TiledeskChatbotConst.REQ_CURRENT_PHONE_NUMBER_KEY,splits[1]);
@@ -0,0 +1,17 @@
1
+
2
+ module.exports = {
3
+ CHANNEL_NAME: {
4
+ SUPPORT_GROUP: 'support-group',
5
+ GROUP: 'group',
6
+ DIRECT: 'direct',
7
+ CHAT21: 'chat21',
8
+ EMAIL: 'email',
9
+ FORM:'form',
10
+ MESSENGER: 'messenger',
11
+ WHATSAPP: 'whatsapp',
12
+ TELEGRAM: 'telegram',
13
+ VOICE: 'voice-vxml',
14
+ VOICE_TWILIO: 'voice-twilio',
15
+ SMS_TWILIO: 'sms-twilio',
16
+ }
17
+ }
@@ -1,105 +0,0 @@
1
- const httpUtils = require('../utils/HttpUtils');
2
- const winston = require('../utils/winston');
3
- const API_ENDPOINT = process.env.API_ENDPOINT;
4
-
5
- class LLMService {
6
-
7
- constructor() { }
8
-
9
- async getNamespace(id_project, token, name, id) {
10
- return new Promise((resolve) => {
11
- const http_request = {
12
- url: API_ENDPOINT + "/" + id_project + "/kb/namespace/all",
13
- headers: {
14
- 'Content-Type': 'application/json',
15
- 'Authorization': 'JWT ' + token
16
- },
17
- method: "GET"
18
- }
19
- winston.debug("Kb HttpRequest", http_request);
20
-
21
- httpUtils.request(
22
- http_request, async (err, namespaces) => {
23
- if (err) {
24
- winston.error("Error getting namespaces:", err);
25
- reject(err);
26
- } else {
27
- winston.debug("Get namespaces response:", namespaces);
28
- if (!Array.isArray(namespaces)) {
29
- reject(new Error('Invalid response format'));
30
- return;
31
- }
32
-
33
- let namespace;
34
- if (name) {
35
- namespace = namespaces.find(n => n.name === name);
36
- } else {
37
- namespace = namespaces.find(n => n.id === id);
38
- }
39
- resolve(namespace || null);
40
- }
41
- }
42
- )
43
- })
44
- }
45
-
46
- async getKeyFromKbSettings(id_project, token) {
47
-
48
- return new Promise((resolve) => {
49
- const http_request = {
50
- url: API_ENDPOINT + "/" + id_project + "/kbsettings",
51
- headers: {
52
- 'Content-Type': 'application/json',
53
- 'Authorization': 'JWT ' + token
54
- },
55
- method: "GET"
56
- }
57
- winston.debug("Kb HttpRequest", http_request);
58
-
59
- httpUtils.request(
60
- http_request, async (err, resbody) => {
61
- if (err) {
62
- winston.error("Error getting kb settings:", err?.response?.data);
63
- resolve(null);
64
- } else {
65
- if (!resbody || !resbody.gptkey) {
66
- resolve(null);
67
- } else {
68
- resolve(resbody.gptkey);
69
- }
70
- }
71
- }
72
- )
73
- })
74
- }
75
-
76
- async addUnansweredQuestion(id_project, namespace, question, token) {
77
-
78
- const json = { namespace, question };
79
-
80
- return new Promise((resolve, reject) => {
81
- const http_request = {
82
- url: API_ENDPOINT + "/" + id_project + "/kb/unanswered/",
83
- headers: {
84
- 'Content-Type': 'application/json',
85
- 'Authorization': 'JWT ' + token
86
- },
87
- method: "POST",
88
- json: json
89
- }
90
- winston.debug("Kb HttpRequest", http_request);
91
-
92
- httpUtils.request(http_request, (err, response) => {
93
- if (err) {
94
- winston.error("Error adding unanswered question:", err);
95
- reject(err);
96
- } else {
97
- resolve(response);
98
- }
99
- });
100
- });
101
- }
102
- }
103
-
104
- const llmService = new LLMService();
105
- module.exports = llmService;
@@ -1,69 +0,0 @@
1
- const httpUtils = require('../utils/HttpUtils');
2
- const winston = require('../utils/winston');
3
- const API_ENDPOINT = process.env.API_ENDPOINT;
4
-
5
- class QuotasService {
6
-
7
- constructor() { }
8
-
9
- async checkQuoteAvailability(id_project, token) {
10
- return new Promise((resolve) => {
11
-
12
- const http_request = {
13
- url: API_ENDPOINT + "/" + id_project + "/quotes/tokens",
14
- headers: {
15
- 'Content-Type': 'application/json',
16
- 'Authorization': 'JWT ' + token
17
- },
18
- method: "GET"
19
- }
20
- winston.debug("QuotasService HttpRequest", http_request);
21
-
22
- httpUtils.request(
23
- http_request, async (err, resbody) => {
24
- if (err) {
25
- winston.error("Check quote availability err: ", err);
26
- resolve(true)
27
- } else {
28
- if (resbody.isAvailable === true) {
29
- resolve(true)
30
- } else {
31
- resolve(false)
32
- }
33
- }
34
- }
35
- )
36
- })
37
- }
38
-
39
- async updateQuote(id_project, token, tokens_usage) {
40
- return new Promise((resolve, reject) => {
41
-
42
- const http_request = {
43
- url: API_ENDPOINT + "/" + id_project + "/quotes/incr/tokens",
44
- headers: {
45
- 'Content-Type': 'application/json',
46
- 'Authorization': 'JWT ' + token
47
- },
48
- json: tokens_usage,
49
- method: "POST"
50
- }
51
- winston.debug("DirAskGPTV2 update quote HttpRequest ", http_request);
52
-
53
- httpUtils.request(
54
- http_request, async (err, resbody) => {
55
- if (err) {
56
- winston.error("Increment tokens quote err: ", err);
57
- reject(false)
58
- } else {
59
- resolve(true);
60
- }
61
- }
62
- )
63
- })
64
- }
65
-
66
- }
67
-
68
- const quotasService = new QuotasService();
69
- module.exports = quotasService;