@tiledesk/tiledesk-tybot-connector 0.2.37 → 0.2.39

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,9 +5,15 @@
5
5
  available on:
6
6
  ▶️ https://www.npmjs.com/package/@tiledesk/tiledesk-tybot-connector
7
7
 
8
+ # v0.2.39
9
+ - fixed bug: wrong update of userFullname based on lead.fullname
10
+
11
+ # v0.2.38
12
+ - improved Qapla' action with condition
13
+
8
14
  # v0.2.37
9
- - improved GptTask action
10
- - improved AskGPT action
15
+ - improved GptTask action with condition
16
+ - improved AskGPT action with condition
11
17
 
12
18
  # v0.2.36
13
19
  - Fixes json conversion in GptTask issue
@@ -373,207 +373,226 @@ class TiledeskChatbotUtil {
373
373
  static async updateRequestAttributes(chatbot, message, projectId, requestId) {
374
374
  // update request context
375
375
  try {
376
- if (chatbot.log) {console.log("Updating request variables. Message:", JSON.stringify(message));}
377
- const messageId = message._id;
378
- const chat_url = `https://panel.tiledesk.com/v3/dashboard/#/project/${projectId}/wsrequest/${requestId}/messages`
379
- // await chatbot.addParameter("chatbot", chatbot);
380
- await chatbot.addParameter(TiledeskChatbotConst.REQ_CHAT_URL, chat_url);
381
- // console.log("Adding proj_", projectId);
382
- await chatbot.addParameter(TiledeskChatbotConst.REQ_PROJECT_ID_KEY, projectId);
383
- // TODO add projectName too
384
- await chatbot.addParameter(TiledeskChatbotConst.REQ_REQUEST_ID_KEY, requestId);
385
- if (chatbot.bot) {
386
- await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_NAME_KEY, chatbot.bot.name);
387
- }
388
-
389
- if (message.text && message.sender !== "_tdinternal") {
390
- await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_TEXT_KEY, message.text); // DEPRECATED
391
- await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_TEXT_v2_KEY, message.text);
392
- if (message.channel) {
393
- if (message.channel.name === "chat21") {
394
- await chatbot.addParameter(TiledeskChatbotConst.REQ_CHAT_CHANNEL, "web"); // renames the channel in chat21
376
+ if (chatbot.log) {console.log("Updating request variables. Message:", JSON.stringify(message));}
377
+ const messageId = message._id;
378
+ const chat_url = `https://panel.tiledesk.com/v3/dashboard/#/project/${projectId}/wsrequest/${requestId}/messages`
379
+ // await chatbot.addParameter("chatbot", chatbot);
380
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHAT_URL, chat_url);
381
+ // console.log("Adding proj_", projectId);
382
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_PROJECT_ID_KEY, projectId);
383
+ // TODO add projectName too
384
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_REQUEST_ID_KEY, requestId);
385
+ if (chatbot.bot) {
386
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_NAME_KEY, chatbot.bot.name);
387
+ }
388
+
389
+ if (message.text && message.sender !== "_tdinternal") {
390
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_TEXT_KEY, message.text); // DEPRECATED
391
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_TEXT_v2_KEY, message.text);
392
+ if (message.channel) {
393
+ if (message.channel.name === "chat21") {
394
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHAT_CHANNEL, "web"); // renames the channel in chat21
395
+ }
396
+ else {
397
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHAT_CHANNEL, message.channel.name);
398
+ }
399
+ }
400
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_MESSAGE_TYPE_KEY, message.type);
401
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_MESSAGE_KEY, TiledeskChatbotUtil.lastUserMessageFrom(message)); // JSON TYPE *NEW
402
+ // get image
403
+ if (message.type && message.type === "image" && message.metadata) {
404
+ // "text": "\nimage text",
405
+ // "id_project": "65203e12f8c0cf002cf4110b",
406
+ // "createdBy": "8ac52a30-133f-4ee1-8b4b-96055bb81757",
407
+ // "metadata": {
408
+ // "height": 905,
409
+ // "name": "tiledesk_Open graph_general.png",
410
+ // "src": "https://firebasestorage.googleapis.com/v0/b/chat21-pre-01.appspot.com/o/public%2Fimages%2F8ac52a30-133f-4ee1-8b4b-96055bb81757%2Fda5bbc8d-5174-49a8-a041-3d9355242da5%2Ftiledesk_Open%20graph_general.png?alt=media&token=be82fecb-3cd1-45b9-a135-c2c57a932862",
411
+ // "type": "image/png",
412
+ // "uid": "lo68iyq5",
413
+ // "width": 1724
414
+ // }
415
+ if (message.metadata.src) {
416
+ await chatbot.addParameter("lastUserImageURL", message.metadata.src);
417
+ await chatbot.addParameter("lastUserImageName", message.metadata.name);
418
+ await chatbot.addParameter("lastUserImageWidth", message.metadata.width);
419
+ await chatbot.addParameter("lastUserImageHeight", message.metadata.height);
420
+ await chatbot.addParameter("lastUserImageType", message.metadata.type);
421
+ }
395
422
  }
396
423
  else {
397
- await chatbot.addParameter(TiledeskChatbotConst.REQ_CHAT_CHANNEL, message.channel.name);
424
+ await chatbot.addParameter("lastUserImageURL", null);
425
+ await chatbot.addParameter("lastUserImageName", null);
426
+ await chatbot.addParameter("lastUserImageWidth", null);
427
+ await chatbot.addParameter("lastUserImageHeight", null);
428
+ await chatbot.addParameter("lastUserImageType", null);
398
429
  }
399
- }
400
- await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_MESSAGE_TYPE_KEY, message.type);
401
- await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_MESSAGE_KEY, TiledeskChatbotUtil.lastUserMessageFrom(message)); // JSON TYPE *NEW
402
- // get image
403
- if (message.type && message.type === "image" && message.metadata) {
404
- // "text": "\nimage text",
405
- // "id_project": "65203e12f8c0cf002cf4110b",
406
- // "createdBy": "8ac52a30-133f-4ee1-8b4b-96055bb81757",
407
- // "metadata": {
408
- // "height": 905,
409
- // "name": "tiledesk_Open graph_general.png",
410
- // "src": "https://firebasestorage.googleapis.com/v0/b/chat21-pre-01.appspot.com/o/public%2Fimages%2F8ac52a30-133f-4ee1-8b4b-96055bb81757%2Fda5bbc8d-5174-49a8-a041-3d9355242da5%2Ftiledesk_Open%20graph_general.png?alt=media&token=be82fecb-3cd1-45b9-a135-c2c57a932862",
411
- // "type": "image/png",
412
- // "uid": "lo68iyq5",
413
- // "width": 1724
414
- // }
415
- if (message.metadata.src) {
416
- await chatbot.addParameter("lastUserImageURL", message.metadata.src);
417
- await chatbot.addParameter("lastUserImageName", message.metadata.name);
418
- await chatbot.addParameter("lastUserImageWidth", message.metadata.width);
419
- await chatbot.addParameter("lastUserImageHeight", message.metadata.height);
420
- await chatbot.addParameter("lastUserImageType", message.metadata.type);
430
+ // get document
431
+ if (message.type && message.type === "file" && message.metadata) {
432
+ // "type": "file",
433
+ // "text": "[LIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf](https://firebasestorage.googleapis.com/v0/b/chat21-pre-01.appspot.com/o/public%2Fimages%2F8ac52a30-133f-4ee1-8b4b-96055bb81757%2F502265ee-4f4a-47a4-9375-172bb0e6bf39%2FLIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf?alt=media&token=a09d065a-9b56-4507-8960-344cc294e4d1)\nistruzioni",
434
+ // "metadata": {
435
+ // "name": "LIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf",
436
+ // "src": "https://firebasestorage.googleapis.com/v0/b/chat21-pre-01.appspot.com/o/public%2Fimages%2F8ac52a30-133f-4ee1-8b4b-96055bb81757%2F502265ee-4f4a-47a4-9375-172bb0e6bf39%2FLIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf?alt=media&token=a09d065a-9b56-4507-8960-344cc294e4d1",
437
+ // "type": "application/pdf",
438
+ // "uid": "lo68oz8i"
439
+ // }
440
+ if (message.metadata.src) {
441
+ await chatbot.addParameter("lastUserDocumentURL", message.metadata.src);
442
+ await chatbot.addParameter("lastUserDocumentName", message.metadata.name);
443
+ await chatbot.addParameter("lastUserDocumentType", message.metadata.type);
444
+ }
445
+ }
446
+ else {
447
+ await chatbot.addParameter("lastUserDocumentURL", null);
448
+ await chatbot.addParameter("lastUserDocumentName", null);
449
+ await chatbot.addParameter("lastUserDocumentType", null);
450
+ }
451
+ if (message && message.request && message.request.lead) {
452
+ if (message.request.lead.email) {
453
+ await chatbot.addParameter("userEmail", message.request.lead.email);
454
+ }
455
+ if (message.request.lead.fullname) {
456
+ console.log("got lead.fullname:", message.request.lead.fullname);
457
+ try {
458
+ const current_userFullname = await chatbot.getParameter("userFullname");
459
+ console.log("current_userFullname:", current_userFullname);
460
+ if (current_userFullname && current_userFullname.startsWith("guest#")) {
461
+ console.log("current_userFullname && current_userFullname.startsWith('guest#')");
462
+ await chatbot.addParameter("userFullname", message.request.lead.fullname);
463
+ }
464
+ else if (!current_userFullname) {
465
+ console.log("no current_userFullname");
466
+ await chatbot.addParameter("userFullname", message.request.lead.fullname);
467
+ console.log("it was null, set to the incoming one");
468
+ }
469
+ }
470
+ catch(error) {
471
+ console.error("Error on setting userFullname:", error);
472
+ }
473
+ }
474
+ else {
475
+ console.log("!lead.fullname");
476
+ }
477
+ if (message.request.lead.phone) {
478
+ await chatbot.addParameter("userPhone", message.request.lead.phone);
479
+ }
480
+ if (message.request.lead.lead_id) {
481
+ await chatbot.addParameter("userLeadId", message.request.lead.lead_id);
482
+ }
483
+ if (message.request.lead.company) {
484
+ await chatbot.addParameter("userCompany", message.request.lead.company);
485
+ }
421
486
  }
422
487
  }
423
- else {
424
- await chatbot.addParameter("lastUserImageURL", null);
425
- await chatbot.addParameter("lastUserImageName", null);
426
- await chatbot.addParameter("lastUserImageWidth", null);
427
- await chatbot.addParameter("lastUserImageHeight", null);
428
- await chatbot.addParameter("lastUserImageType", null);
488
+
489
+
490
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_MESSAGE_ID_KEY, messageId);
491
+ if (message.request && message.request.location && message.request.location.country) {
492
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_COUNTRY_KEY, message.request.location.country);
493
+ }
494
+ if (message.request && message.request.location && message.request.location.city) {
495
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CITY_KEY, message.request.location.city);
429
496
  }
430
- // get document
431
- if (message.type && message.type === "file" && message.metadata) {
432
- // "type": "file",
433
- // "text": "[LIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf](https://firebasestorage.googleapis.com/v0/b/chat21-pre-01.appspot.com/o/public%2Fimages%2F8ac52a30-133f-4ee1-8b4b-96055bb81757%2F502265ee-4f4a-47a4-9375-172bb0e6bf39%2FLIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf?alt=media&token=a09d065a-9b56-4507-8960-344cc294e4d1)\nistruzioni",
434
- // "metadata": {
435
- // "name": "LIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf",
436
- // "src": "https://firebasestorage.googleapis.com/v0/b/chat21-pre-01.appspot.com/o/public%2Fimages%2F8ac52a30-133f-4ee1-8b4b-96055bb81757%2F502265ee-4f4a-47a4-9375-172bb0e6bf39%2FLIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf?alt=media&token=a09d065a-9b56-4507-8960-344cc294e4d1",
437
- // "type": "application/pdf",
438
- // "uid": "lo68oz8i"
439
- // }
440
- if (message.metadata.src) {
441
- await chatbot.addParameter("lastUserDocumentURL", message.metadata.src);
442
- await chatbot.addParameter("lastUserDocumentName", message.metadata.name);
443
- await chatbot.addParameter("lastUserDocumentType", message.metadata.type);
497
+ // console.log("message.request.language", message.request["language"]);
498
+ if (message.request) {
499
+ let user_language = message.request["language"];
500
+ if (message.request["language"]) {
501
+ // console.log("HTTP language:", message.request["language"]);
502
+ var languages = parser.parse(message.request["language"]);
503
+ // console.log("languages:", languages);
504
+ if (languages && languages.length > 0 && languages[0].code) {
505
+ user_language = languages[0].code;
444
506
  }
445
507
  }
446
- else {
447
- await chatbot.addParameter("lastUserDocumentURL", null);
448
- await chatbot.addParameter("lastUserDocumentName", null);
449
- await chatbot.addParameter("lastUserDocumentType", null);
508
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_SOURCE_PAGE_KEY, message.request.sourcePage);
509
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_LANGUAGE_KEY, user_language);
510
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_AGENT_KEY, message.request.userAgent);
450
511
  }
451
- if (message && message.request && message.request.lead) {
452
- if (message.request.lead.email) {
453
- await chatbot.addParameter("userEmail", message.request.lead.email);
454
- }
455
- if (message.request.lead.fullname) {
456
- await chatbot.addParameter("userFullname", message.request.lead.fullname);
457
- }
458
- if (message.request.lead.phone) {
459
- await chatbot.addParameter("userPhone", message.request.lead.phone);
512
+ // console.log("message.request.language", message.request["language"])
513
+ if (message.request && message.request.department) {
514
+ // It was an error when getting this from widget message's attributes
515
+ // await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY, message.attributes.departmentId);
516
+ // await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY, message.attributes.departmentName);
517
+ // get from request.department instead
518
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY, message.request.department._id);
519
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY, message.request.department.name);
520
+ }
521
+
522
+ if (projectId === "641864da99c1fb00131ba495") {
523
+ console.log("641864da99c1fb00131ba495 > for projectId:", JSON.stringify(message))
524
+ }
525
+ // for BUG
526
+ // if (chatbot.log) {console.log("message.request.attributes.payload", JSON.stringify(message.request.attributes.payload))}
527
+ if (message && message.request && message.request.attributes && message.request.attributes.payload) {
528
+ if (!message.attributes) {
529
+ message.attributes = {}
530
+ }
531
+ message.attributes.payload = message.request.attributes.payload
532
+ if (chatbot.log) {console.log("FORCED SET message.attributes.payload:", JSON.stringify(message.attributes.payload))}
533
+ // if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > FORCED SET message.attributes.payload:", JSON.stringify(message.attributes.payload))}
534
+ }
535
+
536
+ if (message.attributes) {
537
+ if (chatbot.log) {console.log("Ok message.attributes", JSON.stringify(message.attributes));}
538
+ if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > Ok message.attributes", JSON.stringify(message.attributes));}
539
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_END_USER_ID_KEY, message.attributes.requester_id);
540
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_END_USER_IP_ADDRESS_KEY, message.attributes.ipAddress);
541
+ if (message.attributes.payload) {
542
+ try {
543
+ for (const [key, value] of Object.entries(message.attributes.payload)) {
544
+ // const value = all_parameters[key];
545
+ const value_type = typeof value;
546
+ //if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > importing payload parameter:", key, "value:", value, "type:", value_type);}
547
+ //await chatbot.addParameter(key, String(value));
548
+ await chatbot.addParameter(key, value);
460
549
  }
461
- if (message.request.lead.lead_id) {
462
- await chatbot.addParameter("userLeadId", message.request.lead.lead_id);
463
550
  }
464
- if (message.request.lead.company) {
465
- await chatbot.addParameter("userCompany", message.request.lead.company);
551
+ catch(err) {
552
+ console.error("Error importing message payload in request variables:", err);
466
553
  }
467
554
  }
468
- }
469
-
470
-
471
- await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_MESSAGE_ID_KEY, messageId);
472
- if (message.request && message.request.location && message.request.location.country) {
473
- await chatbot.addParameter(TiledeskChatbotConst.REQ_COUNTRY_KEY, message.request.location.country);
474
- }
475
- if (message.request && message.request.location && message.request.location.city) {
476
- await chatbot.addParameter(TiledeskChatbotConst.REQ_CITY_KEY, message.request.location.city);
477
- }
478
- // console.log("message.request.language", message.request["language"]);
479
- if (message.request) {
480
- let user_language = message.request["language"];
481
- if (message.request["language"]) {
482
- // console.log("HTTP language:", message.request["language"]);
483
- var languages = parser.parse(message.request["language"]);
484
- // console.log("languages:", languages);
485
- if (languages && languages.length > 0 && languages[0].code) {
486
- user_language = languages[0].code;
487
555
  }
488
- }
489
- await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_SOURCE_PAGE_KEY, message.request.sourcePage);
490
- await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_LANGUAGE_KEY, user_language);
491
- await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_AGENT_KEY, message.request.userAgent);
492
- }
493
- // console.log("message.request.language", message.request["language"])
494
- if (message.request && message.request.department) {
495
- // It was an error when getting this from widget message's attributes
496
- // await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY, message.attributes.departmentId);
497
- // await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY, message.attributes.departmentName);
498
- // get from request.department instead
499
- await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY, message.request.department._id);
500
- await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY, message.request.department.name);
501
- }
502
-
503
- if (projectId === "641864da99c1fb00131ba495") {
504
- console.log("641864da99c1fb00131ba495 > for projectId:", JSON.stringify(message))
505
- }
506
- // for BUG
507
- // if (chatbot.log) {console.log("message.request.attributes.payload", JSON.stringify(message.request.attributes.payload))}
508
- if (message && message.request && message.request.attributes && message.request.attributes.payload) {
509
- if (!message.attributes) {
510
- message.attributes = {}
511
- }
512
- message.attributes.payload = message.request.attributes.payload
513
- if (chatbot.log) {console.log("FORCED SET message.attributes.payload:", JSON.stringify(message.attributes.payload))}
514
- // if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > FORCED SET message.attributes.payload:", JSON.stringify(message.attributes.payload))}
515
- }
516
-
517
- if (message.attributes) {
518
- if (chatbot.log) {console.log("Ok message.attributes", JSON.stringify(message.attributes));}
519
- if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > Ok message.attributes", JSON.stringify(message.attributes));}
520
- await chatbot.addParameter(TiledeskChatbotConst.REQ_END_USER_ID_KEY, message.attributes.requester_id);
521
- await chatbot.addParameter(TiledeskChatbotConst.REQ_END_USER_IP_ADDRESS_KEY, message.attributes.ipAddress);
522
- if (message.attributes.payload) {
523
- try {
524
- for (const [key, value] of Object.entries(message.attributes.payload)) {
525
- // const value = all_parameters[key];
526
- const value_type = typeof value;
527
- //if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > importing payload parameter:", key, "value:", value, "type:", value_type);}
528
- //await chatbot.addParameter(key, String(value));
529
- await chatbot.addParameter(key, value);
530
- }
556
+
557
+ const _bot = chatbot.bot; // aka FaqKB
558
+ if (chatbot.log) {
559
+ console.log("Adding Globals to context..., chatbot.attributes?", JSON.stringify(_bot));
531
560
  }
532
- catch(err) {
533
- console.error("Error importing message payload in request variables:", err);
561
+
562
+ if (_bot.attributes && _bot.attributes.globals) {
563
+ if (chatbot.log) {console.log("Got Globals:", JSON.stringify(_bot.attributes.globals));}
564
+ _bot.attributes.globals.forEach(async (global_var) => {
565
+ if (chatbot.log) {console.log("Adding global:", global_var.key, "value:", global_var.value);}
566
+ await chatbot.addParameter(global_var.key, global_var.value);
567
+ });
534
568
  }
535
- }
536
- }
537
-
538
- const _bot = chatbot.bot; // aka FaqKB
539
- if (chatbot.log) {
540
- console.log("Adding Globals to context..., chatbot.attributes?", JSON.stringify(_bot));
541
- }
542
-
543
- if (_bot.attributes && _bot.attributes.globals) {
544
- if (chatbot.log) {console.log("Got Globals:", JSON.stringify(_bot.attributes.globals));}
545
- _bot.attributes.globals.forEach(async (global_var) => {
546
- if (chatbot.log) {console.log("Adding global:", global_var.key, "value:", global_var.value);}
547
- await chatbot.addParameter(global_var.key, global_var.value);
548
- });
549
- }
550
- // await chatbot.addParameter("testVar",
551
- // {
552
- // name: "Andrea",
553
- // coords: {
554
- // x: 2, y: 1
555
- // }
556
- // }
557
- // );
558
-
559
- if (chatbot.log) {
560
- // console.log("tdcache:", chatbot.tdcache);
561
- console.log("requestId:", requestId);
562
- console.log("KEY:", TiledeskChatbotConst.REQ_PROJECT_ID_KEY);
563
- // console.log("TiledeskChatbot:", TiledeskChatbot);
564
- let proj_ = await chatbot.getParameter(TiledeskChatbotConst.REQ_PROJECT_ID_KEY);
565
- console.log("request parameter proj_:", proj_);
566
- const all_parameters = await chatbot.allParameters();
567
- for (const [key, value] of Object.entries(all_parameters)) {
568
- // const value = all_parameters[key];
569
- const value_type = typeof value;
570
- if (chatbot.log) {console.log("REQUEST ATTRIBUTE:", key, "VALUE:", value, "TYPE:", value_type)}
571
- }
569
+ // await chatbot.addParameter("testVar",
570
+ // {
571
+ // name: "Andrea",
572
+ // coords: {
573
+ // x: 2, y: 1
574
+ // }
575
+ // }
576
+ // );
577
+
578
+ if (chatbot.log) {
579
+ // console.log("tdcache:", chatbot.tdcache);
580
+ console.log("requestId:", requestId);
581
+ console.log("KEY:", TiledeskChatbotConst.REQ_PROJECT_ID_KEY);
582
+ // console.log("TiledeskChatbot:", TiledeskChatbot);
583
+ let proj_ = await chatbot.getParameter(TiledeskChatbotConst.REQ_PROJECT_ID_KEY);
584
+ console.log("request parameter proj_:", proj_);
585
+ const all_parameters = await chatbot.allParameters();
586
+ for (const [key, value] of Object.entries(all_parameters)) {
587
+ // const value = all_parameters[key];
588
+ const value_type = typeof value;
589
+ if (chatbot.log) {console.log("REQUEST ATTRIBUTE:", key, "VALUE:", value, "TYPE:", value_type)}
590
+ }
591
+ }
592
+ } catch(error) {
593
+ console.error("Error", error)
594
+ process.exit(1)
572
595
  }
573
- } catch(error) {
574
- console.error("Error", error)
575
- process.exit(1)
576
- }
577
596
  // message["attributes"]: {
578
597
  // "departmentId": "63c980054f857c00350535bc",
579
598
  // "departmentName": "Default Department",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.2.37",
3
+ "version": "0.2.39",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -14,7 +14,7 @@
14
14
  "@tiledesk/helpcenter-query-client": "^0.1.8",
15
15
  "@tiledesk/tiledesk-chatbot-client": "^0.5.30",
16
16
  "@tiledesk/tiledesk-chatbot-util": "^0.8.39",
17
- "@tiledesk/tiledesk-client": "^0.10.4",
17
+ "@tiledesk/tiledesk-client": "^0.10.5",
18
18
  "accept-language-parser": "^1.5.0",
19
19
  "axios": "^0.27.2",
20
20
  "body-parser": "^1.19.0",
@@ -619,9 +619,16 @@ class DirectivesChatbotPlug {
619
619
  });
620
620
  }
621
621
  else if (directive_name === Directives.QAPLA) {
622
- new DirQapla(context).execute(directive, async () => {
623
- let next_dir = await this.nextDirective(this.directives);
624
- this.process(next_dir);
622
+ new DirQapla(context).execute(directive, async (stop) => {
623
+ if (context.log) { console.log("DirQapla stop?", stop);}
624
+ if (stop == true) {
625
+ if (context.log) { console.log("Stopping Actions on:", JSON.stringify(directive));}
626
+ this.theend();
627
+ }
628
+ else {
629
+ let next_dir = await this.nextDirective(this.directives);
630
+ this.process(next_dir);
631
+ }
625
632
  })
626
633
  }
627
634
  else if (directive_name === Directives.MAKE) {
@@ -2,6 +2,7 @@ const axios = require("axios").default;
2
2
  const { TiledeskChatbot } = require("../../models/TiledeskChatbot");
3
3
  const { Filler } = require("../Filler");
4
4
  let https = require("https");
5
+ const { DirIntent } = require("./DirIntent");
5
6
  require('dotenv').config();
6
7
 
7
8
  class DirQapla {
@@ -13,6 +14,7 @@ class DirQapla {
13
14
  this.context = context;
14
15
  this.tdcache = this.context.tdcache;
15
16
  this.requestId = this.context.requestId;
17
+ this.intentDir = new DirIntent(context);
16
18
  this.log = context.log;
17
19
  }
18
20
 
@@ -27,8 +29,8 @@ class DirQapla {
27
29
  callback();
28
30
  return;
29
31
  }
30
- this.go(action, () => {
31
- callback();
32
+ this.go(action, (stop) => {
33
+ callback(stop);
32
34
  })
33
35
  }
34
36
 
@@ -40,34 +42,44 @@ class DirQapla {
40
42
  return;
41
43
  }
42
44
 
45
+ let trueIntent = action.trueIntent;
46
+ let falseIntent = action.falseIntent;
47
+ let trueIntentAttributes = action.trueIntentAttributes;
48
+ let falseIntentAttributes = action.falseIntentAttributes;
49
+
50
+ if (this.log) {
51
+ console.log("DirAskGPT trueIntent", trueIntent)
52
+ console.log("DirAskGPT falseIntent", falseIntent)
53
+ console.log("DirAskGPT trueIntentAttributes", trueIntentAttributes)
54
+ console.log("DirAskGPT falseIntentAttributes", falseIntentAttributes)
55
+ }
56
+
57
+ // let default values??
58
+ let status = null;
59
+ let result = null;
60
+ let error;
61
+
43
62
  let requestVariables = null;
44
63
  requestVariables =
45
64
  await TiledeskChatbot.allParametersStatic(
46
65
  this.tdcache, this.requestId
47
66
  )
48
67
 
49
- if (this.log) {
50
- const all_parameters = await TiledeskChatbot.allParametersStatic(this.context.tdcache, this.context.requestId);
51
- for (const [key, value] of Object.entries(all_parameters)) {
52
- if (this.log) { console.log("DirQapla request parameter:", key, "value:", value, "type:", typeof value) }
53
- }
54
- }
55
-
56
68
  const filler = new Filler();
57
69
  const tracking_number = filler.fill(action.trackingNumber, requestVariables);
58
70
  if (this.log) { console.log("DirQapla tracking number: ", tracking_number); }
59
71
 
60
72
  if (!tracking_number || tracking_number === '') {
61
- console.error("DirQapla ERROR - tracking number is undefined or null or empty string");
62
- callback();
63
- }
64
-
65
- const key = action.apiKey;
66
- console.log("action.key: ", action.apiKey)
67
-
68
- if (!key) {
69
- console.error("DirQapla ERROR - key is undefined");
73
+ console.error("Error: DirQapla tracking number is undefined or null or empty string");
74
+ error = "Tracking number is not defined";
75
+ await this.#assignAttributes(action, status, result, error);
76
+ if (falseIntent) {
77
+ await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
78
+ callback(true);
79
+ return;
80
+ }
70
81
  callback();
82
+ return;
71
83
  }
72
84
 
73
85
  const server_base_url = process.env.API_ENDPOINT || process.env.API_URL;
@@ -76,6 +88,27 @@ class DirQapla {
76
88
  console.log("DirQapla server_base_url: ", qapla_base_url);
77
89
  console.log("DirQapla qapla_base_url: ", qapla_base_url);
78
90
  }
91
+
92
+ let key = action.apiKey;
93
+
94
+ if (!key) {
95
+ if (this.log) { console.log("DirQapla - Key not found into action. Searching in integrations..."); }
96
+ key = await this.getKeyFromIntegrations(server_base_url);
97
+ }
98
+
99
+ if (!key) {
100
+ console.error("Error: DirQapla api key is mandatory");
101
+ error = "Invalid or empty ApiKey";
102
+ await this.#assignAttributes(action, status, result, error);
103
+ if (falseIntent) {
104
+ await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
105
+ callback(true);
106
+ return;
107
+ }
108
+ callback();
109
+ return;
110
+ }
111
+
79
112
  const QAPLA_HTTPREQUEST = {
80
113
  url: qapla_base_url + "/getShipment/",
81
114
  headers: {
@@ -94,14 +127,18 @@ class DirQapla {
94
127
  if (err) {
95
128
  if (callback) {
96
129
  console.error("(httprequest) DirQapla getShipment err:", err);
130
+ error = "Unable to get shipment";
131
+ await this.#assignAttributes(action, status, result, error);
132
+ if (falseIntent) {
133
+ await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
134
+ callback(true);
135
+ return;
136
+ }
97
137
  callback();
138
+ return;
98
139
  }
99
140
  } else if (callback) {
100
- if (this.log) { console.log("DirQapla getShipment resbody: ", resbody); }
101
-
102
- let status = null;;
103
- let result;
104
- let error;
141
+ if (this.log) { console.log("DirQapla getShipment resbody: ", JSON.stringify(resbody, null, 2)); }
105
142
 
106
143
  if (resbody.getShipment &&
107
144
  resbody.getShipment.shipments &&
@@ -111,16 +148,66 @@ class DirQapla {
111
148
  resbody.getShipment.shipments[0].status.qaplaStatus.status) {
112
149
  status = resbody.getShipment.shipments[0].status.qaplaStatus.status;
113
150
  }
151
+ // status = resbody.getShipment?.shipments[0]?.status?.qaplaStatus?.status; // doesn't works
152
+
153
+ if (resbody.getShipment &&
154
+ resbody.getShipment.result)
155
+ result = resbody.getShipment?.result;
156
+ error = resbody.getShipment?.error;
114
157
 
115
- result = resbody.getShipment.result;
116
- error = resbody.getShipment.error;
117
158
  await this.#assignAttributes(action, status, result, error);
159
+ if (trueIntent) {
160
+ await this.#executeCondition(true, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
161
+ callback(true);
162
+ return;
163
+ }
118
164
  callback();
165
+ return;
119
166
  }
120
167
  }
121
168
  )
122
169
  }
123
170
 
171
+ async #executeCondition(result, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes, callback) {
172
+ let trueIntentDirective = null;
173
+ if (trueIntent) {
174
+ trueIntentDirective = DirIntent.intentDirectiveFor(trueIntent, trueIntentAttributes);
175
+ }
176
+ let falseIntentDirective = null;
177
+ if (falseIntent) {
178
+ falseIntentDirective = DirIntent.intentDirectiveFor(falseIntent, falseIntentAttributes);
179
+ }
180
+ if (result === true) {
181
+ if (trueIntentDirective) {
182
+ this.intentDir.execute(trueIntentDirective, () => {
183
+ if (callback) {
184
+ callback();
185
+ }
186
+ })
187
+ }
188
+ else {
189
+ if (this.log) { console.log("No trueIntentDirective specified"); }
190
+ if (callback) {
191
+ callback();
192
+ }
193
+ }
194
+ }
195
+ else {
196
+ if (falseIntentDirective) {
197
+ this.intentDir.execute(falseIntentDirective, () => {
198
+ if (callback) {
199
+ callback();
200
+ }
201
+ });
202
+ }
203
+ else {
204
+ if (this.log) { console.log("No falseIntentDirective specified"); }
205
+ if (callback) {
206
+ callback();
207
+ }
208
+ }
209
+ }
210
+ }
124
211
 
125
212
  async #assignAttributes(action, status, result, error) {
126
213
  if (this.log) {
@@ -197,6 +284,38 @@ class DirQapla {
197
284
  }
198
285
  });
199
286
  }
287
+
288
+ async getKeyFromIntegrations(server_base_url) {
289
+ return new Promise((resolve) => {
290
+
291
+ const INTEGRATIONS_HTTPREQUEST = {
292
+ url: server_base_url + "/" + this.context.projectId + "/integration/name/openai",
293
+ headers: {
294
+ 'Content-Type': 'application/json',
295
+ 'Authorization': 'JWT ' + this.context.token
296
+ },
297
+ method: "GET"
298
+ }
299
+ if (this.log) { console.log("DirGptTask INTEGRATIONS_HTTPREQUEST ", INTEGRATIONS_HTTPREQUEST) }
300
+
301
+ this.#myrequest(
302
+ INTEGRATIONS_HTTPREQUEST, async (err, integration) => {
303
+ if (err) {
304
+ resolve(null);
305
+ } else {
306
+
307
+ if (integration &&
308
+ integration.value) {
309
+ resolve(integration.value.apikey)
310
+ }
311
+ else {
312
+ resolve(null)
313
+ }
314
+ }
315
+ })
316
+ })
317
+ }
318
+
200
319
  }
201
320
 
202
321
  module.exports = { DirQapla }
@@ -0,0 +1,127 @@
1
+ const { Filler } = require('../Filler');
2
+
3
+ class DirSetConversationTags {
4
+
5
+ constructor(context) {
6
+ if (!context) {
7
+ throw new Error('context object is mandatory.');
8
+ }
9
+ this.context = context;
10
+ this.log = context.log;
11
+ this.tdclient = context.tdclient;
12
+ this.requestId = context.requestId;
13
+ }
14
+
15
+ execute(directive, callback) {
16
+ let action;
17
+ if (directive.action) {
18
+ action = directive.action
19
+ }
20
+ else {
21
+ callback();
22
+ return;
23
+ }
24
+ this.go(action, () => {
25
+ callback();
26
+ });
27
+ }
28
+
29
+ async go(action, callback) {
30
+ if (this.log) {console.log("(DirSetConversationTags) Adding conversation tags:", action.depName);}
31
+ let tagsString = action.tags;
32
+ tagsString = tagsString.replace(/ /g,"");
33
+ if (tagsString.length === 0) {
34
+ if (this.log) {console.error("(DirSetConversationTags) Invalid action: tags string is empty")};
35
+ callback();
36
+ return;
37
+ }
38
+ try {
39
+ if (this.tdcache) {
40
+ const requestAttributes =
41
+ await TiledeskChatbot.allParametersStatic(this.tdcache, this.context.requestId);
42
+ const filler = new Filler();
43
+ tags = filler.fill(tags, requestAttributes);
44
+ }
45
+ }
46
+ catch(error) {
47
+ console.error("Error while filling operands:", error);
48
+ }
49
+ this.moveToDepartment(this.requestId, depName, (deps) => {
50
+ if (!deps) {
51
+ if (this.log) {console.log("Dep not found");}
52
+ callback();
53
+ return
54
+ }
55
+ if (this.log) {console.log("Switched to dept:", depName, "action:", JSON.stringify(action));}
56
+ if (action.triggerBot) {
57
+ let dep = null;
58
+ let i;
59
+ for (i = 0; i < deps.length; i++) {
60
+ let d = deps[i];
61
+ if (d.name.toLowerCase() === depName.toLowerCase()) {
62
+ dep = d;
63
+ break;
64
+ }
65
+ }
66
+ if (dep && dep.hasBot === true && dep.id_bot) {
67
+ if (this.log) {console.log("Sending hidden /start message to bot in dept");}
68
+ const message = {
69
+ type: "text",
70
+ text: "/start",
71
+ attributes : {
72
+ subtype: "info"
73
+ }
74
+ }
75
+ this.tdclient.sendSupportMessage(
76
+ this.requestId,
77
+ message, (err) => {
78
+ if (err) {
79
+ console.error("Error sending hidden message:", err.message);
80
+ }
81
+ if (this.log) {console.log("Hidden message sent.");}
82
+ callback();
83
+ });
84
+ }
85
+ }
86
+ else {
87
+ if (this.log) {console.log("No action.triggerBot");}
88
+ callback();
89
+ }
90
+ });
91
+ }
92
+
93
+ moveToDepartment(requestId, depName, callback) {
94
+ this.tdclient.getAllDepartments((err, deps) => {
95
+ if (this.log) {console.log("deps:", JSON.stringify(deps));}
96
+ if (err) {
97
+ console.error("getAllDepartments() error:", err);
98
+ callback();
99
+ return;
100
+ }
101
+ let dep = null;
102
+ let i;
103
+ for (i = 0; i < deps.length; i++) {
104
+ let d = deps[i];
105
+ if (d.name.toLowerCase() === depName.toLowerCase()) {
106
+ dep = d;
107
+ break;
108
+ }
109
+ }
110
+ if (dep) {
111
+ this.tdclient.updateRequestDepartment(requestId, dep._id, null, (err, res) => {
112
+ if (err) {
113
+ console.error("DirDepartment error:", err);
114
+ callback();
115
+ }
116
+ else {
117
+ console.log("DirDepartment response:",JSON.stringify(res));
118
+ callback(deps);
119
+ }
120
+ });
121
+ }
122
+ });
123
+ }
124
+
125
+ }
126
+
127
+ module.exports = { DirSetConversationTags };