@tiledesk/tiledesk-tybot-connector 0.2.601-rc1 → 0.3.1

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.
Files changed (73) hide show
  1. package/CHANGELOG.md +387 -1
  2. package/ExtApi.js +6 -7
  3. package/Logger.js +74 -0
  4. package/TdCache.js +81 -176
  5. package/TdCache_v3.js +261 -0
  6. package/TiledeskExpression.js +7 -3
  7. package/TiledeskServices/AIService.js +43 -0
  8. package/TiledeskServices/utils.js +99 -0
  9. package/index.js +305 -50
  10. package/logs/app.log +279 -0
  11. package/models/IntentsMachineFactory.js +5 -2
  12. package/models/MockBotsDataSource.js +19 -11
  13. package/models/TiledeskChatbot.js +97 -79
  14. package/models/TiledeskChatbotConst.js +12 -17
  15. package/models/TiledeskChatbotUtil.js +359 -109
  16. package/models/TiledeskIntentsMachine.js +1 -1
  17. package/models/faqKbService.js +1 -1
  18. package/package.json +8 -6
  19. package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +172 -106
  20. package/tiledeskChatbotPlugs/Filler.js +13 -2
  21. package/tiledeskChatbotPlugs/TildeskContextForCodeOrchestrator.js +8 -0
  22. package/tiledeskChatbotPlugs/WebhookChatbotPlug.js +13 -7
  23. package/tiledeskChatbotPlugs/directives/DirAddTags.js +374 -0
  24. package/tiledeskChatbotPlugs/directives/DirAiPrompt.js +476 -0
  25. package/tiledeskChatbotPlugs/directives/DirAskGPT.js +16 -19
  26. package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +221 -34
  27. package/tiledeskChatbotPlugs/directives/DirAssign.js +0 -11
  28. package/tiledeskChatbotPlugs/directives/DirAssignFromFunction.js +11 -21
  29. package/tiledeskChatbotPlugs/directives/DirAssistant.js +728 -0
  30. package/tiledeskChatbotPlugs/directives/DirBrevo.js +353 -0
  31. package/tiledeskChatbotPlugs/directives/DirCaptureUserReply.js +3 -30
  32. package/tiledeskChatbotPlugs/directives/DirClearTranscript.js +22 -0
  33. package/tiledeskChatbotPlugs/directives/DirClose.js +16 -3
  34. package/tiledeskChatbotPlugs/directives/DirCode.js +1 -1
  35. package/tiledeskChatbotPlugs/directives/DirCondition.js +0 -26
  36. package/tiledeskChatbotPlugs/directives/DirConnectBlock.js +182 -0
  37. package/tiledeskChatbotPlugs/directives/DirContactUpdate.js +121 -0
  38. package/tiledeskChatbotPlugs/directives/DirCustomerio.js +5 -8
  39. package/tiledeskChatbotPlugs/directives/DirDeflectToHelpCenter.js +11 -1
  40. package/tiledeskChatbotPlugs/directives/DirDepartment.js +15 -6
  41. package/tiledeskChatbotPlugs/directives/DirFireTiledeskEvent.js +17 -6
  42. package/tiledeskChatbotPlugs/directives/DirForm.js +12 -2
  43. package/tiledeskChatbotPlugs/directives/DirGptTask.js +83 -38
  44. package/tiledeskChatbotPlugs/directives/DirGptTask_OLD.js +4 -7
  45. package/tiledeskChatbotPlugs/directives/DirHubspot.js +5 -8
  46. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgents.js +14 -27
  47. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgentsV2.js +278 -0
  48. package/tiledeskChatbotPlugs/directives/DirIfOpenHours.js +147 -51
  49. package/tiledeskChatbotPlugs/directives/DirIfOpenHours_OLD.js +125 -0
  50. package/tiledeskChatbotPlugs/directives/DirIntent.js +8 -37
  51. package/tiledeskChatbotPlugs/directives/DirJSONCondition.js +5 -26
  52. package/tiledeskChatbotPlugs/directives/DirMessage.js +19 -17
  53. package/tiledeskChatbotPlugs/directives/DirMessageToBot.js +135 -0
  54. package/tiledeskChatbotPlugs/directives/DirMoveToAgent.js +20 -87
  55. package/tiledeskChatbotPlugs/directives/DirMoveToUnassigned.js +59 -0
  56. package/tiledeskChatbotPlugs/directives/DirQapla.js +6 -9
  57. package/tiledeskChatbotPlugs/directives/DirRandomReply.js +17 -7
  58. package/tiledeskChatbotPlugs/directives/DirRemoveCurrentBot.js +17 -7
  59. package/tiledeskChatbotPlugs/directives/DirReplaceBot.js +11 -2
  60. package/tiledeskChatbotPlugs/directives/DirReplaceBotV2.js +135 -21
  61. package/tiledeskChatbotPlugs/directives/DirReplaceBotV3.js +163 -0
  62. package/tiledeskChatbotPlugs/directives/DirReply.js +53 -9
  63. package/tiledeskChatbotPlugs/directives/DirReplyV2.js +347 -0
  64. package/tiledeskChatbotPlugs/directives/DirSendEmail.js +13 -23
  65. package/tiledeskChatbotPlugs/directives/DirSendWhatsapp.js +247 -0
  66. package/tiledeskChatbotPlugs/directives/DirSetAttributeV2.js +202 -15
  67. package/tiledeskChatbotPlugs/directives/DirSetConversationTags.js +13 -4
  68. package/tiledeskChatbotPlugs/directives/DirWait.js +21 -4
  69. package/tiledeskChatbotPlugs/directives/DirWebRequest.js +1 -2
  70. package/tiledeskChatbotPlugs/directives/DirWebRequestV2.js +166 -103
  71. package/tiledeskChatbotPlugs/directives/DirWhatsappByAttribute.js +2 -60
  72. package/tiledeskChatbotPlugs/directives/Directives.js +16 -1
  73. /package/tiledeskChatbotPlugs/directives/{DirOfflineHours.js → DEPRECATED_DirOfflineHours.js} +0 -0
@@ -4,6 +4,8 @@ const { TiledeskChatbotConst } = require('./TiledeskChatbotConst');
4
4
  const { TiledeskChatbot } = require('./TiledeskChatbot.js');
5
5
  let parser = require('accept-language-parser');
6
6
  const { Directives } = require('../tiledeskChatbotPlugs/directives/Directives.js');
7
+ require('dotenv').config();
8
+ let axios = require('axios');
7
9
 
8
10
  class TiledeskChatbotUtil {
9
11
 
@@ -156,22 +158,6 @@ class TiledeskChatbotUtil {
156
158
 
157
159
  }
158
160
 
159
- // static errorMessage(message) {
160
- // return {
161
- // name: "message",
162
- // action: {
163
- // "_tdThenStop": true,
164
- // text: message,
165
- // attributes: {
166
- // runtimeError: {
167
- // message: message
168
- // }
169
- // }
170
- // }
171
- // }
172
- // }
173
-
174
- //static filterOnVariables(commands, variables) {
175
161
  static filterOnVariables(message, variables) {
176
162
  if (!variables) {
177
163
  return;
@@ -250,13 +236,13 @@ class TiledeskChatbotUtil {
250
236
  if (commands[i].message) {
251
237
  if (commands[i].message.type === "text") { // check text commands
252
238
  if (( commands[i].message.text && commands[i].message.text.trim() === "") || !commands[i].message.text) {
253
- console.log("deleting command:", commands[i]);
239
+ // console.log("deleting command:", commands[i]);
254
240
  commands.splice(i, 1);
255
241
  if (commands[i-1]) {
256
242
  if (commands[i-1].type === "wait") {
257
243
  commands.splice(i-1, 1);
258
244
  i--;
259
- console.log("deleted wait");
245
+ // console.log("deleted wait");
260
246
  }
261
247
  }
262
248
  }
@@ -334,9 +320,82 @@ class TiledeskChatbotUtil {
334
320
  }
335
321
  }
336
322
 
323
+ static allReplyButtons(message) {
324
+ let all_buttons = [];
325
+ if (message.attributes && message.attributes.commands) {
326
+ // console.log("message.attributes ok")
327
+ let commands = message.attributes.commands;
328
+ if (commands.length > 0) {
329
+ // console.log("commands ok", commands.length)
330
+ for (let i = 0; i < commands.length; i++) {
331
+ let command = commands[i];
332
+ // console.log("got command:", command)s
333
+ if (command.type === 'message' && command.message) {
334
+ if (command.message.attributes && command.message.attributes.attachment && command.message.attributes.attachment.buttons && command.message.attributes.attachment.buttons.length > 0){
335
+ // console.log("command with buttons ok:")
336
+ let buttons = command.message.attributes.attachment.buttons;
337
+
338
+ buttons.forEach(button => {
339
+ if (button.type === "action") {
340
+ // console.log("pushing button:", button);
341
+ all_buttons.push(button);
342
+ }
343
+ });
344
+ }
345
+ }
346
+ }
347
+ }
348
+ }
349
+ return all_buttons;
350
+ }
351
+
352
+ static buttonByText(text, buttons) {
353
+ if (buttons === null || text === null) {
354
+ return null;
355
+ }
356
+ let search_text = text.toLowerCase().trim();
357
+ let selected_button = null;
358
+ for (let i = 0; i < buttons.length; i++) {
359
+ const button = buttons[i];
360
+ if (button.value !== null && button.value.toLowerCase() === search_text) {
361
+ selected_button = button;
362
+ break;
363
+ }
364
+ else if (button.alias && button.alias.trim() !== "") { // search in button alias
365
+ let alias = button.alias.split(",");
366
+ // console.log("alias splitted:", alias);
367
+ if (alias.length > 0) {
368
+ for (let ii = 0; ii < alias.length; ii++) {
369
+ alias[ii] = alias[ii].toLowerCase().trim();
370
+ }
371
+ // console.log("alias proc:", alias);
372
+ if (alias.indexOf(search_text) > -1) {
373
+ selected_button = button;
374
+ break;
375
+ }
376
+ }
377
+ }
378
+ }
379
+
380
+ return selected_button;
381
+ }
382
+
383
+ static stripEmoji(str) {
384
+ // console.log("checking:", str);
385
+ if (str === null) {
386
+ return str;
387
+ }
388
+ return str.replace(/\p{Emoji}/gu, '')
389
+ .replace(/\s+/g, ' ')
390
+ .trim();
391
+ }
392
+
337
393
  static async updateConversationTranscript(chatbot, message) {
394
+ if (!message || !message.senderFullname) { // not a conversation, can it be an Automation invocation?
395
+ return null;
396
+ }
397
+
338
398
  const chatbot_name = chatbot.bot.name.trim();
339
-
340
399
  if (message && message.text && message.text.trim() !== "" && message.sender !== "_tdinternal" && !this.isHiddenMessage(message)) {
341
400
  let transcript = await chatbot.getParameter("transcript");
342
401
  // console.log("transcript got:", transcript);
@@ -354,14 +413,26 @@ class TiledeskChatbotUtil {
354
413
  }
355
414
 
356
415
  function name_of(message, chatbot_name) {
416
+ if (!message.senderFullname) {
417
+ return null;
418
+ }
357
419
  let fullName = message.senderFullname;
358
420
  if (fullName.trim() === chatbot_name) {
359
421
  fullName = "bot:" + fullName;
422
+ } else {
423
+ fullName = "user:" + fullName;
360
424
  }
361
425
  return "<" + fullName + ">";
362
426
  }
363
427
  }
364
428
 
429
+ static async clearConversationTranscript(chatbot, callback) {
430
+ await chatbot.addParameter("transcript", "");
431
+ if (callback) {
432
+ callback();
433
+ }
434
+ }
435
+
365
436
  static transcriptJSON(transcript) {
366
437
  const regexp = /(<.*>)/gm;
367
438
  const parts = transcript.split(regexp);
@@ -409,6 +480,13 @@ class TiledeskChatbotUtil {
409
480
  return false;
410
481
  }
411
482
 
483
+ static isAudioMessage(message){
484
+ if (message && message.type && message.type === 'file' && message.metadata && message.metadata.src && message.metadata.type.includes('audio') ) {
485
+ return true;
486
+ }
487
+ return false;
488
+ }
489
+
412
490
  static lastUserMessageFrom(msg) {
413
491
  let message = {};
414
492
  message["senderFullname"] = msg["senderFullname"]; // ex. "Bot"
@@ -426,7 +504,7 @@ class TiledeskChatbotUtil {
426
504
  return message;
427
505
  }
428
506
 
429
- static async updateRequestAttributes(chatbot, message, projectId, requestId) {
507
+ static async updateRequestAttributes(chatbot, chatbotToken, message, projectId, requestId) {
430
508
  // update request context
431
509
  try {
432
510
  if (chatbot.log) {console.log("Updating request variables. Message:", JSON.stringify(message));}
@@ -439,10 +517,25 @@ class TiledeskChatbotUtil {
439
517
  // TODO add projectName too
440
518
  await chatbot.addParameter(TiledeskChatbotConst.REQ_REQUEST_ID_KEY, requestId);
441
519
  if (chatbot.bot) {
442
- await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_NAME_KEY, chatbot.bot.name);
520
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_NAME_KEY, chatbot.bot.name);
521
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_ID_KEY, chatbot.bot._id);
522
+ }
523
+ if (chatbotToken) {
524
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN, chatbotToken); // DEPRECATED
525
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN_v2, "JWT " + chatbotToken);
526
+ }
527
+ if (process.env.TILEDESK_API) {
528
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN, chatbotToken); // DEPRECATED
529
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN_v2, "JWT " + chatbotToken);
443
530
  }
444
531
 
532
+ if (process.env.API_URL) {
533
+ await chatbot.addParameter(TiledeskChatbotConst.API_BASE_URL, process.env.API_URL);
534
+ }
535
+
445
536
  if (message.text && message.sender !== "_tdinternal") {
537
+ // await chatbot.addParameter(TiledeskChatbotConst.USER_INPUT, true); // set userInput
538
+ await chatbot.deleteParameter(TiledeskChatbotConst.USER_INPUT); // user wrote, delete userInput, replyv2 will not trigger timeout action
446
539
  await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_TEXT_KEY, message.text); // DEPRECATED
447
540
  await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_TEXT_v2_KEY, message.text);
448
541
  if (message.channel) {
@@ -455,104 +548,116 @@ class TiledeskChatbotUtil {
455
548
  }
456
549
  await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_MESSAGE_TYPE_KEY, message.type);
457
550
  await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_MESSAGE_KEY, TiledeskChatbotUtil.lastUserMessageFrom(message)); // JSON TYPE *NEW
458
- // get image
459
- if (message.type && message.type === "image" && message.metadata) {
460
- // "text": "\nimage text",
461
- // "id_project": "65203e12f8c0cf002cf4110b",
462
- // "createdBy": "8ac52a30-133f-4ee1-8b4b-96055bb81757",
463
- // "metadata": {
464
- // "height": 905,
465
- // "name": "tiledesk_Open graph_general.png",
466
- // "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",
467
- // "type": "image/png",
468
- // "uid": "lo68iyq5",
469
- // "width": 1724
470
- // }
471
- if (message.metadata.src) {
551
+ }
552
+
553
+ // get image
554
+ if (message.type && message.type === "image" && message.metadata) {
555
+ // "text": "\nimage text",
556
+ // "id_project": "65203e12f8c0cf002cf4110b",
557
+ // "createdBy": "8ac52a30-133f-4ee1-8b4b-96055bb81757",
558
+ // "metadata": {
559
+ // "height": 905,
560
+ // "name": "tiledesk_Open graph_general.png",
561
+ // "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",
562
+ // "type": "image/png",
563
+ // "uid": "lo68iyq5",
564
+ // "width": 1724
565
+ // }
566
+ if (message.metadata.src) {
472
567
  await chatbot.addParameter("lastUserImageURL", message.metadata.src);
473
568
  await chatbot.addParameter("lastUserImageName", message.metadata.name);
474
569
  await chatbot.addParameter("lastUserImageWidth", message.metadata.width);
475
570
  await chatbot.addParameter("lastUserImageHeight", message.metadata.height);
476
571
  await chatbot.addParameter("lastUserImageType", message.metadata.type);
477
- }
478
- }
479
- else {
480
- await chatbot.addParameter("lastUserImageURL", null);
481
- await chatbot.addParameter("lastUserImageName", null);
482
- await chatbot.addParameter("lastUserImageWidth", null);
483
- await chatbot.addParameter("lastUserImageHeight", null);
484
- await chatbot.addParameter("lastUserImageType", null);
485
572
  }
486
- // get document
487
- if (message.type && message.type === "file" && message.metadata) {
488
- // "type": "file",
489
- // "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",
490
- // "metadata": {
491
- // "name": "LIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf",
492
- // "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",
493
- // "type": "application/pdf",
494
- // "uid": "lo68oz8i"
495
- // }
496
- if (message.metadata.src) {
497
- await chatbot.addParameter("lastUserDocumentURL", message.metadata.src); // legacy. will be deprecated
498
- const url_as_attachment = message.metadata.src;
499
- await chatbot.addParameter("lastUserDocumentAsAttachmentURL", url_as_attachment);
500
- let url_inline = url_as_attachment;
501
- if (url_as_attachment.match(/.*\/download.*/)) { // removing "/download" removes the "Content-disposion: attachment" HTTP header
502
- url_inline = url_as_attachment.replace('/download', '/');
503
- }
504
- await chatbot.addParameter("lastUserDocumentAsInlineURL", url_inline);
505
- await chatbot.addParameter("lastUserDocumentName", message.metadata.name);
506
- await chatbot.addParameter("lastUserDocumentType", message.metadata.type);
573
+ }
574
+ // else {
575
+ // await chatbot.addParameter("lastUserImageURL", null);
576
+ // await chatbot.addParameter("lastUserImageName", null);
577
+ // await chatbot.addParameter("lastUserImageWidth", null);
578
+ // await chatbot.addParameter("lastUserImageHeight", null);
579
+ // await chatbot.addParameter("lastUserImageType", null);
580
+ // }
581
+ // get document
582
+ if (message.type && message.type === "file" && message.metadata) {
583
+ // "type": "file",
584
+ // "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",
585
+ // "metadata": {
586
+ // "name": "LIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf",
587
+ // "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",
588
+ // "type": "application/pdf",
589
+ // "uid": "lo68oz8i"
590
+ // }
591
+ if (message.metadata.src) {
592
+
593
+ await chatbot.addParameter("lastUserDocumentURL", message.metadata.src); // legacy. will be deprecated
594
+ const url_as_attachment = message.metadata.src;
595
+ await chatbot.addParameter("lastUserDocumentAsAttachmentURL", url_as_attachment);
596
+ let url_inline = url_as_attachment;
597
+ if (url_as_attachment.match(/.*\/download.*/)) { // removing "/download" removes the "Content-disposion: attachment" HTTP header
598
+ url_inline = url_as_attachment.replace('/download', '/');
507
599
  }
600
+ await chatbot.addParameter("lastUserDocumentAsInlineURL", url_inline);
601
+ await chatbot.addParameter("lastUserDocumentName", message.metadata.name);
602
+ await chatbot.addParameter("lastUserDocumentType", message.metadata.type);
508
603
  }
509
- else {
510
- await chatbot.addParameter("lastUserDocumentURL", null);
511
- await chatbot.addParameter("lastUserDocumentName", null);
512
- await chatbot.addParameter("lastUserDocumentType", null);
513
- }
514
- if (message && message.request && message.request.lead) {
515
- if (message.request.lead.email) {
516
- await chatbot.addParameter("userEmail", message.request.lead.email);
517
- }
518
- if (message.request.lead.fullname && !message.request.lead.fullname.startsWith("guest#")) {
519
- // worth saving
520
- try {
521
- // const current_userFullname = await chatbot.getParameter("userFullname");
522
- // if (current_userFullname && current_userFullname.startsWith("guest#")) { // replace if exists as guest#
523
- // await chatbot.addParameter("userFullname", message.request.lead.fullname);
524
- // }
525
- // else if (!current_userFullname) {
526
- await chatbot.addParameter("userFullname", message.request.lead.fullname);
527
- // }
528
- }
529
- catch(error) {
530
- console.error("Error on setting userFullname:", error);
531
- }
532
- }
533
- else {
534
- // console.log("!lead.fullname");
535
- }
536
- // console.log("Getting userPhone:", JSON.stringify(message.request));
537
- if (message.request.lead.phone) {
538
- await chatbot.addParameter("userPhone", message.request.lead.phone);
604
+ }
605
+ // else {
606
+ // await chatbot.addParameter("lastUserDocumentURL", null);
607
+ // await chatbot.addParameter("lastUserDocumentName", null);
608
+ // await chatbot.addParameter("lastUserDocumentType", null);
609
+ // }
610
+ if (message && message.request && message.request.lead) {
611
+ if (chatbot.log) {console.log("lead found. lead.email:", message.request.lead.email, "lead.fullname:", message.request.lead.fullname)}
612
+ let currentLeadEmail = await chatbot.getParameter(TiledeskChatbotConst.REQ_LEAD_EMAIL_KEY);
613
+ if (chatbot.log) {console.log("You lead email from attributes:", currentLeadEmail, "message.request.lead.email:", message.request.lead.email);}
614
+ if (message.request.lead.email && !currentLeadEmail) {
615
+ // worth saving
616
+ if (chatbot.log) {console.log("worth saving email. lead found. lead.email:", message.request.lead.email, "lead.fullname:", message.request.lead.fullname)}
617
+ try {
618
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_LEAD_EMAIL_KEY, message.request.lead.email);
539
619
  }
540
- if (message.request.lead.lead_id && message.request.lead.lead_id.startsWith("wab-")) {
541
- const splits = message.request.lead.lead_id.split("-");
542
- if (splits && splits.length > 1) {
543
- await chatbot.addParameter("currentPhoneNumber",splits[1]);
544
- }
620
+ catch(error) {
621
+ console.error("Error on setting userEmail:", error);
545
622
  }
546
- if (message.request.lead.lead_id) {
547
- await chatbot.addParameter("userLeadId", message.request.lead.lead_id);
623
+ }
624
+ let currentLeadName = await chatbot.getParameter(TiledeskChatbotConst.REQ_LEAD_USERFULLNAME_KEY);
625
+ if (chatbot.log) {console.log("You lead name from attributes:", currentLeadName, "message.request.lead.fullname:", message.request.lead.fullname)}
626
+ // if (message.request.lead.fullname && message.request.lead.fullname.startsWith("guest#") && !currentLeadName) {
627
+ // if (message.request.lead.fullname) {
628
+ if (message.request.lead.fullname && !currentLeadName) {
629
+ // worth saving
630
+ if (chatbot.log) {console.log("worth saving fullname. lead found. lead.email:", message.request.lead.email, "lead.fullname:", message.request.lead.fullname)}
631
+ try {
632
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_LEAD_USERFULLNAME_KEY, message.request.lead.fullname);
548
633
  }
549
- if (message.request.lead.company) {
550
- await chatbot.addParameter("userCompany", message.request.lead.company);
634
+ catch(error) {
635
+ console.error("Error on setting userFullname:", error);
551
636
  }
552
- if (message.request.ticket_id) {
553
- await chatbot.addParameter("ticketId", message.request.ticket_id);
637
+ // }
638
+ // else {
639
+ // // console.log("!lead.fullname");
640
+ // }
641
+ }
642
+ // console.log("Getting userPhone:", JSON.stringify(message.request));
643
+ if (message.request.lead.phone) {
644
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_PHONE_KEY, message.request.lead.phone);
645
+ }
646
+ if (message.request.lead.lead_id && message.request.lead.lead_id.startsWith("wab-")) {
647
+ const splits = message.request.lead.lead_id.split("-");
648
+ if (splits && splits.length > 1) {
649
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CURRENT_PHONE_NUMBER_KEY,splits[1]);
554
650
  }
555
651
  }
652
+ if (message.request.lead._id) {
653
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_LEAD_ID_KEY, message.request.lead._id);
654
+ }
655
+ if (message.request.lead.company) {
656
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_COMPANY_KEY, message.request.lead.company);
657
+ }
658
+ if (message.request.ticket_id) {
659
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_TICKET_ID_KEY, message.request.ticket_id);
660
+ }
556
661
  }
557
662
 
558
663
  await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_MESSAGE_ID_KEY, messageId);
@@ -597,15 +702,19 @@ class TiledeskChatbotUtil {
597
702
  await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY, message.request.department._id);
598
703
  await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY, message.request.department.name);
599
704
  }
705
+ else if (message.attributes && message.attributes.departmentId) {
706
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY, message.attributes.departmentId);
707
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY, message.attributes.departmentName);
708
+ }
709
+
600
710
  if (message && message.request && message.request.attributes && message.request.attributes.payload) {
601
711
  if (!message.attributes) {
602
712
  message.attributes = {}
603
713
  }
604
- message.attributes.payload = message.request.attributes.payload
714
+ message.attributes.payload = { ...message.attributes.payload, ...message.request.attributes.payload}
605
715
  if (chatbot.log) {console.log("FORCED SET message.attributes.payload:", JSON.stringify(message.attributes.payload))}
606
716
  // if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > FORCED SET message.attributes.payload:", JSON.stringify(message.attributes.payload))}
607
- }
608
-
717
+ }
609
718
  if (message.attributes) {
610
719
  if (chatbot.log) {console.log("Ok message.attributes", JSON.stringify(message.attributes));}
611
720
  // if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > Ok message.attributes", JSON.stringify(message.attributes));}
@@ -618,14 +727,31 @@ class TiledeskChatbotUtil {
618
727
  const value_type = typeof value;
619
728
  //if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > importing payload parameter:", key, "value:", value, "type:", value_type);}
620
729
  //await chatbot.addParameter(key, String(value));
730
+ // console.log("Adding from message.attributes:", key, "->", value);
621
731
  await chatbot.addParameter(key, value);
622
732
  }
733
+ await chatbot.addParameter("payload", message.attributes.payload);
623
734
  }
624
735
  catch(err) {
625
736
  console.error("Error importing message payload in request variables:", err);
626
737
  }
627
738
  }
739
+
740
+ // TODO - REMOVE - THEY ARE IN ATTRIBUTES.PAYLOAD
741
+ // voice-vxml attributes
742
+ if (message.attributes.dnis) {
743
+ await chatbot.addParameter("dnis", message.attributes.dnis);
744
+ }
745
+ if (message.attributes.callId) {
746
+ await chatbot.addParameter("callId", message.attributes.callId);
747
+ }
748
+ if (message.attributes.ani) {
749
+ await chatbot.addParameter("ani", message.attributes.ani);
750
+ }
628
751
  }
752
+
753
+
754
+
629
755
 
630
756
  const _bot = chatbot.bot; // aka FaqKB
631
757
  if (chatbot.log) {
@@ -693,6 +819,8 @@ class TiledeskChatbotUtil {
693
819
  // "sub": "guest",
694
820
  // "jti": "f053af3d-14ca-411b-9903-78bd74e24218"
695
821
  // }
822
+ // let userFullname = await chatbot.getParameter(TiledeskChatbotConst.REQ_LEAD_USERFULLNAME_KEY);
823
+ // console.log("userFullname:", userFullname);
696
824
  }
697
825
 
698
826
  static actionsToDirectives(actions) {
@@ -724,7 +852,7 @@ class TiledeskChatbotUtil {
724
852
  if (requestId.startsWith("support-group-")) {
725
853
  const parts = requestId.split("-");
726
854
  // console.log("parts support request:", parts);
727
- if (parts.length === 4) {
855
+ if (parts.length >= 4) {
728
856
  isValid = (parts[0] === "support" && parts[1] === "group" && parts[2] === projectId && parts[3].length > 0);
729
857
  }
730
858
  else {
@@ -746,6 +874,128 @@ class TiledeskChatbotUtil {
746
874
  return isValid;
747
875
  }
748
876
 
877
+ static userFlowAttributes(flowAttributes) {
878
+ const RESERVED = [
879
+ TiledeskChatbotConst.REQ_CHATBOT_NAME_KEY,
880
+ TiledeskChatbotConst.REQ_CHAT_URL,
881
+ TiledeskChatbotConst.REQ_CITY_KEY,
882
+ TiledeskChatbotConst.REQ_COUNTRY_KEY,
883
+ TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY,
884
+ TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY,
885
+ TiledeskChatbotConst.REQ_END_USER_ID_KEY,
886
+ TiledeskChatbotConst.REQ_END_USER_IP_ADDRESS_KEY,
887
+ TiledeskChatbotConst.REQ_LAST_MESSAGE_ID_KEY,
888
+ TiledeskChatbotConst.REQ_LAST_USER_TEXT_KEY,
889
+ TiledeskChatbotConst.REQ_PROJECT_ID_KEY,
890
+ TiledeskChatbotConst.REQ_REQUEST_ID_KEY,
891
+ TiledeskChatbotConst.REQ_USER_AGENT_KEY,
892
+ TiledeskChatbotConst.REQ_USER_LANGUAGE_KEY,
893
+ TiledeskChatbotConst.REQ_USER_SOURCE_PAGE_KEY,
894
+ TiledeskChatbotConst.REQ_LAST_USER_MESSAGE_TYPE_KEY,
895
+ TiledeskChatbotConst.REQ_TRANSCRIPT_KEY,
896
+ TiledeskChatbotConst.REQ_LAST_USER_MESSAGE_KEY,
897
+ TiledeskChatbotConst.REQ_DECODED_JWT_KEY,
898
+ "lastUserImageURL", // image
899
+ "lastUserImageName", // image
900
+ "lastUserImageWidth", // image
901
+ "lastUserImageHeight", // image
902
+ "lastUserImageType", // image
903
+ "lastUserDocumentURL", // file
904
+ "lastUserDocumentName", // file
905
+ "lastUserDocumentType", // file
906
+ "ticketId",
907
+ TiledeskChatbotConst.REQ_CHAT_CHANNEL,
908
+ "user_lead_id",
909
+ "userLeadId",
910
+ "lastUserText",
911
+ TiledeskChatbotConst.REQ_REQUESTER_IS_AUTHENTICATED_KEY,
912
+ TiledeskChatbotConst.USER_INPUT,
913
+ TiledeskChatbotConst.REQ_CHATBOT_TOKEN,
914
+ TiledeskChatbotConst.REQ_CHATBOT_TOKEN_v2,
915
+ ]
916
+ let userParams = {};
917
+ if (flowAttributes) {
918
+ for (const [key, value] of Object.entries(flowAttributes)) {
919
+ // console.log(key, value);
920
+ // There is a bug that moves the requestId as a key in request attributes, so: && !key.startsWith("support-group-")
921
+ if (!key.startsWith("_") && !RESERVED.some(e => e === key) && !key.startsWith("support-group-")) {
922
+ userParams[key] = value;
923
+ }
924
+ }
925
+ }
926
+ return userParams;
927
+ }
928
+
929
+ /**
930
+ * A stub to get the request parameters, hosted by tilebot on:
931
+ * /${TILEBOT_ROUTE}/ext/parameters/requests/${requestId}?all
932
+ *
933
+ * @param {string} requestId. Tiledesk chatbot/requestId parameters
934
+ */
935
+ getChatbotParameters(requestId, callback) {
936
+ const url = `${process.env.TILEBOT_ENDPOINT}/ext/reserved/parameters/requests/${requestId}?all`;
937
+ const HTTPREQUEST = {
938
+ url: url,
939
+ headers: {
940
+ 'Content-Type': 'application/json'
941
+ },
942
+ method: 'get'
943
+ };
944
+ this.myrequest(
945
+ HTTPREQUEST,
946
+ function (err, resbody) {
947
+ if (err) {
948
+ if (callback) {
949
+ callback(err);
950
+ }
951
+ }
952
+ else {
953
+ if (callback) {
954
+ callback(null, resbody);
955
+ }
956
+ }
957
+ }, false
958
+ );
959
+ }
960
+
961
+ myrequest(options, callback, log) {
962
+ if (log) {
963
+ console.log("API URL:", options.url);
964
+ console.log("** Options:", JSON.stringify(options));
965
+ }
966
+ axios(
967
+ {
968
+ url: options.url,
969
+ method: options.method,
970
+ data: options.json,
971
+ params: options.params,
972
+ headers: options.headers
973
+ })
974
+ .then((res) => {
975
+ if (log) {
976
+ console.log("Response for url:", options.url);
977
+ console.log("Response headers:\n", JSON.stringify(res.headers));
978
+ }
979
+ if (res && res.status == 200 && res.data) {
980
+ if (callback) {
981
+ callback(null, res.data);
982
+ }
983
+ }
984
+ else {
985
+ if (callback) {
986
+ callback(TiledeskClient.getErr({ message: "Response status not 200" }, options, res), null, null);
987
+ }
988
+ }
989
+ })
990
+ .catch((error) => {
991
+ console.error("(TiledeskChatbotUtil) Axios error: ", JSON.stringify(error));
992
+ if (callback) {
993
+ callback(error, null, null);
994
+ }
995
+ });
996
+ }
997
+
998
+
749
999
  }
750
1000
 
751
1001
  module.exports = { TiledeskChatbotUtil };
@@ -124,7 +124,7 @@ class TiledeskIntentsMachine {
124
124
  }
125
125
  })
126
126
  .catch( (error) => {
127
- // console.error("An error occurred:", error);
127
+ console.error("(TiledeskIntentsMachine) Axios error: ", JSON.stringify(error));
128
128
  if (callback) {
129
129
  callback(error, null, null);
130
130
  }
@@ -13,7 +13,7 @@ class FaqKbService {
13
13
  }
14
14
  var sortQuery = {};
15
15
  sortQuery["score"] = -1;
16
- console.log("(Service) GET ALL FAQ_KBs");
16
+ // console.log("(Service) GET ALL FAQ_KBs");
17
17
  return new Promise((resolve, reject) => {
18
18
  // let query = {public: options.public, certified: options.certified};
19
19
  Faq_kb.find(query).sort(sortQuery).lean().exec( (err, bots) => {