@tiledesk/tiledesk-tybot-connector 0.2.601-rc1 → 0.3.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.
Files changed (71) hide show
  1. package/CHANGELOG.md +378 -1
  2. package/ExtApi.js +6 -6
  3. package/TdCache copy.js +242 -0
  4. package/TdCache.js +81 -176
  5. package/TdCache_v3.js +261 -0
  6. package/TiledeskExpression.js +7 -3
  7. package/index.js +290 -43
  8. package/logs/app.log +279 -0
  9. package/models/IntentsMachineFactory.js +5 -2
  10. package/models/MockBotsDataSource.js +19 -11
  11. package/models/TiledeskChatbot.js +97 -79
  12. package/models/TiledeskChatbotConst.js +12 -17
  13. package/models/TiledeskChatbotUtil.js +353 -109
  14. package/models/TiledeskIntentsMachine.js +1 -1
  15. package/models/faqKbService.js +1 -1
  16. package/package.json +7 -6
  17. package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +172 -106
  18. package/tiledeskChatbotPlugs/Filler.js +13 -2
  19. package/tiledeskChatbotPlugs/TildeskContextForCodeOrchestrator.js +8 -0
  20. package/tiledeskChatbotPlugs/WebhookChatbotPlug.js +13 -7
  21. package/tiledeskChatbotPlugs/directives/DirAddTags.js +374 -0
  22. package/tiledeskChatbotPlugs/directives/DirAiPrompt.js +476 -0
  23. package/tiledeskChatbotPlugs/directives/DirAskGPT.js +16 -19
  24. package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +221 -34
  25. package/tiledeskChatbotPlugs/directives/DirAssign.js +0 -11
  26. package/tiledeskChatbotPlugs/directives/DirAssignFromFunction.js +11 -21
  27. package/tiledeskChatbotPlugs/directives/DirAssistant.js +728 -0
  28. package/tiledeskChatbotPlugs/directives/DirBrevo.js +353 -0
  29. package/tiledeskChatbotPlugs/directives/DirCaptureUserReply.js +3 -30
  30. package/tiledeskChatbotPlugs/directives/DirClearTranscript.js +22 -0
  31. package/tiledeskChatbotPlugs/directives/DirClose.js +16 -3
  32. package/tiledeskChatbotPlugs/directives/DirCode.js +1 -1
  33. package/tiledeskChatbotPlugs/directives/DirCondition.js +0 -26
  34. package/tiledeskChatbotPlugs/directives/DirConnectBlock.js +183 -0
  35. package/tiledeskChatbotPlugs/directives/DirContactUpdate.js +121 -0
  36. package/tiledeskChatbotPlugs/directives/DirCustomerio.js +5 -8
  37. package/tiledeskChatbotPlugs/directives/DirDeflectToHelpCenter.js +11 -1
  38. package/tiledeskChatbotPlugs/directives/DirDepartment.js +15 -6
  39. package/tiledeskChatbotPlugs/directives/DirFireTiledeskEvent.js +17 -6
  40. package/tiledeskChatbotPlugs/directives/DirForm.js +12 -2
  41. package/tiledeskChatbotPlugs/directives/DirGptTask.js +83 -38
  42. package/tiledeskChatbotPlugs/directives/DirGptTask_OLD.js +4 -7
  43. package/tiledeskChatbotPlugs/directives/DirHubspot.js +5 -8
  44. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgents.js +14 -27
  45. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgentsV2.js +278 -0
  46. package/tiledeskChatbotPlugs/directives/DirIfOpenHours.js +147 -51
  47. package/tiledeskChatbotPlugs/directives/DirIfOpenHours_OLD.js +125 -0
  48. package/tiledeskChatbotPlugs/directives/DirIntent.js +8 -36
  49. package/tiledeskChatbotPlugs/directives/DirJSONCondition.js +5 -26
  50. package/tiledeskChatbotPlugs/directives/DirMessage.js +19 -17
  51. package/tiledeskChatbotPlugs/directives/DirMessageToBot.js +136 -0
  52. package/tiledeskChatbotPlugs/directives/DirMoveToAgent.js +20 -87
  53. package/tiledeskChatbotPlugs/directives/DirMoveToUnassigned.js +59 -0
  54. package/tiledeskChatbotPlugs/directives/DirQapla.js +6 -9
  55. package/tiledeskChatbotPlugs/directives/DirRandomReply.js +17 -7
  56. package/tiledeskChatbotPlugs/directives/DirRemoveCurrentBot.js +17 -7
  57. package/tiledeskChatbotPlugs/directives/DirReplaceBot.js +11 -2
  58. package/tiledeskChatbotPlugs/directives/DirReplaceBotV2.js +135 -21
  59. package/tiledeskChatbotPlugs/directives/DirReplaceBotV3.js +163 -0
  60. package/tiledeskChatbotPlugs/directives/DirReply.js +42 -9
  61. package/tiledeskChatbotPlugs/directives/DirReplyV2.js +347 -0
  62. package/tiledeskChatbotPlugs/directives/DirSendEmail.js +13 -23
  63. package/tiledeskChatbotPlugs/directives/DirSendWhatsapp.js +247 -0
  64. package/tiledeskChatbotPlugs/directives/DirSetAttributeV2.js +202 -15
  65. package/tiledeskChatbotPlugs/directives/DirSetConversationTags.js +13 -4
  66. package/tiledeskChatbotPlugs/directives/DirWait.js +21 -4
  67. package/tiledeskChatbotPlugs/directives/DirWebRequest.js +1 -2
  68. package/tiledeskChatbotPlugs/directives/DirWebRequestV2.js +166 -103
  69. package/tiledeskChatbotPlugs/directives/DirWhatsappByAttribute.js +2 -60
  70. package/tiledeskChatbotPlugs/directives/Directives.js +16 -1
  71. /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);
@@ -426,7 +497,7 @@ class TiledeskChatbotUtil {
426
497
  return message;
427
498
  }
428
499
 
429
- static async updateRequestAttributes(chatbot, message, projectId, requestId) {
500
+ static async updateRequestAttributes(chatbot, chatbotToken, message, projectId, requestId) {
430
501
  // update request context
431
502
  try {
432
503
  if (chatbot.log) {console.log("Updating request variables. Message:", JSON.stringify(message));}
@@ -439,10 +510,25 @@ class TiledeskChatbotUtil {
439
510
  // TODO add projectName too
440
511
  await chatbot.addParameter(TiledeskChatbotConst.REQ_REQUEST_ID_KEY, requestId);
441
512
  if (chatbot.bot) {
442
- await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_NAME_KEY, chatbot.bot.name);
513
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_NAME_KEY, chatbot.bot.name);
514
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_ID_KEY, chatbot.bot._id);
515
+ }
516
+ if (chatbotToken) {
517
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN, chatbotToken); // DEPRECATED
518
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN_v2, "JWT " + chatbotToken);
519
+ }
520
+ if (process.env.TILEDESK_API) {
521
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN, chatbotToken); // DEPRECATED
522
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN_v2, "JWT " + chatbotToken);
443
523
  }
444
524
 
525
+ if (process.env.API_URL) {
526
+ await chatbot.addParameter(TiledeskChatbotConst.API_BASE_URL, process.env.API_URL);
527
+ }
528
+
445
529
  if (message.text && message.sender !== "_tdinternal") {
530
+ // await chatbot.addParameter(TiledeskChatbotConst.USER_INPUT, true); // set userInput
531
+ await chatbot.deleteParameter(TiledeskChatbotConst.USER_INPUT); // user wrote, delete userInput, replyv2 will not trigger timeout action
446
532
  await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_TEXT_KEY, message.text); // DEPRECATED
447
533
  await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_TEXT_v2_KEY, message.text);
448
534
  if (message.channel) {
@@ -455,104 +541,116 @@ class TiledeskChatbotUtil {
455
541
  }
456
542
  await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_MESSAGE_TYPE_KEY, message.type);
457
543
  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) {
544
+ }
545
+
546
+ // get image
547
+ if (message.type && message.type === "image" && message.metadata) {
548
+ // "text": "\nimage text",
549
+ // "id_project": "65203e12f8c0cf002cf4110b",
550
+ // "createdBy": "8ac52a30-133f-4ee1-8b4b-96055bb81757",
551
+ // "metadata": {
552
+ // "height": 905,
553
+ // "name": "tiledesk_Open graph_general.png",
554
+ // "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",
555
+ // "type": "image/png",
556
+ // "uid": "lo68iyq5",
557
+ // "width": 1724
558
+ // }
559
+ if (message.metadata.src) {
472
560
  await chatbot.addParameter("lastUserImageURL", message.metadata.src);
473
561
  await chatbot.addParameter("lastUserImageName", message.metadata.name);
474
562
  await chatbot.addParameter("lastUserImageWidth", message.metadata.width);
475
563
  await chatbot.addParameter("lastUserImageHeight", message.metadata.height);
476
564
  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
565
  }
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);
566
+ }
567
+ // else {
568
+ // await chatbot.addParameter("lastUserImageURL", null);
569
+ // await chatbot.addParameter("lastUserImageName", null);
570
+ // await chatbot.addParameter("lastUserImageWidth", null);
571
+ // await chatbot.addParameter("lastUserImageHeight", null);
572
+ // await chatbot.addParameter("lastUserImageType", null);
573
+ // }
574
+ // get document
575
+ if (message.type && message.type === "file" && message.metadata) {
576
+ // "type": "file",
577
+ // "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",
578
+ // "metadata": {
579
+ // "name": "LIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf",
580
+ // "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",
581
+ // "type": "application/pdf",
582
+ // "uid": "lo68oz8i"
583
+ // }
584
+ if (message.metadata.src) {
585
+
586
+ await chatbot.addParameter("lastUserDocumentURL", message.metadata.src); // legacy. will be deprecated
587
+ const url_as_attachment = message.metadata.src;
588
+ await chatbot.addParameter("lastUserDocumentAsAttachmentURL", url_as_attachment);
589
+ let url_inline = url_as_attachment;
590
+ if (url_as_attachment.match(/.*\/download.*/)) { // removing "/download" removes the "Content-disposion: attachment" HTTP header
591
+ url_inline = url_as_attachment.replace('/download', '/');
507
592
  }
593
+ await chatbot.addParameter("lastUserDocumentAsInlineURL", url_inline);
594
+ await chatbot.addParameter("lastUserDocumentName", message.metadata.name);
595
+ await chatbot.addParameter("lastUserDocumentType", message.metadata.type);
508
596
  }
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);
597
+ }
598
+ // else {
599
+ // await chatbot.addParameter("lastUserDocumentURL", null);
600
+ // await chatbot.addParameter("lastUserDocumentName", null);
601
+ // await chatbot.addParameter("lastUserDocumentType", null);
602
+ // }
603
+ if (message && message.request && message.request.lead) {
604
+ if (chatbot.log) {console.log("lead found. lead.email:", message.request.lead.email, "lead.fullname:", message.request.lead.fullname)}
605
+ let currentLeadEmail = await chatbot.getParameter(TiledeskChatbotConst.REQ_LEAD_EMAIL_KEY);
606
+ if (chatbot.log) {console.log("You lead email from attributes:", currentLeadEmail, "message.request.lead.email:", message.request.lead.email);}
607
+ if (message.request.lead.email && !currentLeadEmail) {
608
+ // worth saving
609
+ if (chatbot.log) {console.log("worth saving email. lead found. lead.email:", message.request.lead.email, "lead.fullname:", message.request.lead.fullname)}
610
+ try {
611
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_LEAD_EMAIL_KEY, message.request.lead.email);
539
612
  }
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
- }
613
+ catch(error) {
614
+ console.error("Error on setting userEmail:", error);
545
615
  }
546
- if (message.request.lead.lead_id) {
547
- await chatbot.addParameter("userLeadId", message.request.lead.lead_id);
616
+ }
617
+ let currentLeadName = await chatbot.getParameter(TiledeskChatbotConst.REQ_LEAD_USERFULLNAME_KEY);
618
+ if (chatbot.log) {console.log("You lead name from attributes:", currentLeadName, "message.request.lead.fullname:", message.request.lead.fullname)}
619
+ // if (message.request.lead.fullname && message.request.lead.fullname.startsWith("guest#") && !currentLeadName) {
620
+ // if (message.request.lead.fullname) {
621
+ if (message.request.lead.fullname && !currentLeadName) {
622
+ // worth saving
623
+ if (chatbot.log) {console.log("worth saving fullname. lead found. lead.email:", message.request.lead.email, "lead.fullname:", message.request.lead.fullname)}
624
+ try {
625
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_LEAD_USERFULLNAME_KEY, message.request.lead.fullname);
548
626
  }
549
- if (message.request.lead.company) {
550
- await chatbot.addParameter("userCompany", message.request.lead.company);
627
+ catch(error) {
628
+ console.error("Error on setting userFullname:", error);
551
629
  }
552
- if (message.request.ticket_id) {
553
- await chatbot.addParameter("ticketId", message.request.ticket_id);
630
+ // }
631
+ // else {
632
+ // // console.log("!lead.fullname");
633
+ // }
634
+ }
635
+ // console.log("Getting userPhone:", JSON.stringify(message.request));
636
+ if (message.request.lead.phone) {
637
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_PHONE_KEY, message.request.lead.phone);
638
+ }
639
+ if (message.request.lead.lead_id && message.request.lead.lead_id.startsWith("wab-")) {
640
+ const splits = message.request.lead.lead_id.split("-");
641
+ if (splits && splits.length > 1) {
642
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CURRENT_PHONE_NUMBER_KEY,splits[1]);
554
643
  }
555
644
  }
645
+ if (message.request.lead._id) {
646
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_LEAD_ID_KEY, message.request.lead._id);
647
+ }
648
+ if (message.request.lead.company) {
649
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_COMPANY_KEY, message.request.lead.company);
650
+ }
651
+ if (message.request.ticket_id) {
652
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_TICKET_ID_KEY, message.request.ticket_id);
653
+ }
556
654
  }
557
655
 
558
656
  await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_MESSAGE_ID_KEY, messageId);
@@ -597,15 +695,19 @@ class TiledeskChatbotUtil {
597
695
  await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY, message.request.department._id);
598
696
  await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY, message.request.department.name);
599
697
  }
698
+ else if (message.attributes && message.attributes.departmentId) {
699
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY, message.attributes.departmentId);
700
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY, message.attributes.departmentName);
701
+ }
702
+
600
703
  if (message && message.request && message.request.attributes && message.request.attributes.payload) {
601
704
  if (!message.attributes) {
602
705
  message.attributes = {}
603
706
  }
604
- message.attributes.payload = message.request.attributes.payload
707
+ message.attributes.payload = { ...message.attributes.payload, ...message.request.attributes.payload}
605
708
  if (chatbot.log) {console.log("FORCED SET message.attributes.payload:", JSON.stringify(message.attributes.payload))}
606
709
  // if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > FORCED SET message.attributes.payload:", JSON.stringify(message.attributes.payload))}
607
- }
608
-
710
+ }
609
711
  if (message.attributes) {
610
712
  if (chatbot.log) {console.log("Ok message.attributes", JSON.stringify(message.attributes));}
611
713
  // if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > Ok message.attributes", JSON.stringify(message.attributes));}
@@ -618,14 +720,31 @@ class TiledeskChatbotUtil {
618
720
  const value_type = typeof value;
619
721
  //if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > importing payload parameter:", key, "value:", value, "type:", value_type);}
620
722
  //await chatbot.addParameter(key, String(value));
723
+ // console.log("Adding from message.attributes:", key, "->", value);
621
724
  await chatbot.addParameter(key, value);
622
725
  }
726
+ await chatbot.addParameter("payload", message.attributes.payload);
623
727
  }
624
728
  catch(err) {
625
729
  console.error("Error importing message payload in request variables:", err);
626
730
  }
627
731
  }
732
+
733
+ // TODO - REMOVE - THEY ARE IN ATTRIBUTES.PAYLOAD
734
+ // voice-vxml attributes
735
+ if (message.attributes.dnis) {
736
+ await chatbot.addParameter("dnis", message.attributes.dnis);
737
+ }
738
+ if (message.attributes.callId) {
739
+ await chatbot.addParameter("callId", message.attributes.callId);
740
+ }
741
+ if (message.attributes.ani) {
742
+ await chatbot.addParameter("ani", message.attributes.ani);
743
+ }
628
744
  }
745
+
746
+
747
+
629
748
 
630
749
  const _bot = chatbot.bot; // aka FaqKB
631
750
  if (chatbot.log) {
@@ -693,6 +812,8 @@ class TiledeskChatbotUtil {
693
812
  // "sub": "guest",
694
813
  // "jti": "f053af3d-14ca-411b-9903-78bd74e24218"
695
814
  // }
815
+ // let userFullname = await chatbot.getParameter(TiledeskChatbotConst.REQ_LEAD_USERFULLNAME_KEY);
816
+ // console.log("userFullname:", userFullname);
696
817
  }
697
818
 
698
819
  static actionsToDirectives(actions) {
@@ -724,7 +845,7 @@ class TiledeskChatbotUtil {
724
845
  if (requestId.startsWith("support-group-")) {
725
846
  const parts = requestId.split("-");
726
847
  // console.log("parts support request:", parts);
727
- if (parts.length === 4) {
848
+ if (parts.length >= 4) {
728
849
  isValid = (parts[0] === "support" && parts[1] === "group" && parts[2] === projectId && parts[3].length > 0);
729
850
  }
730
851
  else {
@@ -746,6 +867,129 @@ class TiledeskChatbotUtil {
746
867
  return isValid;
747
868
  }
748
869
 
870
+ static userFlowAttributes(flowAttributes) {
871
+ const RESERVED = [
872
+ TiledeskChatbotConst.REQ_CHATBOT_NAME_KEY,
873
+ TiledeskChatbotConst.REQ_CHAT_URL,
874
+ TiledeskChatbotConst.REQ_CITY_KEY,
875
+ TiledeskChatbotConst.REQ_COUNTRY_KEY,
876
+ TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY,
877
+ TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY,
878
+ TiledeskChatbotConst.REQ_END_USER_ID_KEY,
879
+ TiledeskChatbotConst.REQ_END_USER_IP_ADDRESS_KEY,
880
+ TiledeskChatbotConst.REQ_LAST_MESSAGE_ID_KEY,
881
+ TiledeskChatbotConst.REQ_LAST_USER_TEXT_KEY,
882
+ TiledeskChatbotConst.REQ_PROJECT_ID_KEY,
883
+ TiledeskChatbotConst.REQ_REQUEST_ID_KEY,
884
+ TiledeskChatbotConst.REQ_USER_AGENT_KEY,
885
+ TiledeskChatbotConst.REQ_USER_LANGUAGE_KEY,
886
+ TiledeskChatbotConst.REQ_USER_SOURCE_PAGE_KEY,
887
+ TiledeskChatbotConst.REQ_LAST_USER_MESSAGE_TYPE_KEY,
888
+ TiledeskChatbotConst.REQ_TRANSCRIPT_KEY,
889
+ TiledeskChatbotConst.REQ_LAST_USER_MESSAGE_KEY,
890
+ TiledeskChatbotConst.REQ_DECODED_JWT_KEY,
891
+ "lastUserImageURL", // image
892
+ "lastUserImageName", // image
893
+ "lastUserImageWidth", // image
894
+ "lastUserImageHeight", // image
895
+ "lastUserImageType", // image
896
+ "lastUserDocumentURL", // file
897
+ "lastUserDocumentName", // file
898
+ "lastUserDocumentType", // file
899
+ "ticketId",
900
+ TiledeskChatbotConst.REQ_CHAT_CHANNEL,
901
+ "user_lead_id",
902
+ "userLeadId",
903
+ "lastUserText",
904
+ TiledeskChatbotConst.REQ_REQUESTER_IS_AUTHENTICATED_KEY,
905
+ TiledeskChatbotConst.USER_INPUT,
906
+ TiledeskChatbotConst.REQ_CHATBOT_TOKEN,
907
+ TiledeskChatbotConst.REQ_CHATBOT_TOKEN_v2,
908
+ ]
909
+ let userParams = {};
910
+ if (flowAttributes) {
911
+ for (const [key, value] of Object.entries(flowAttributes)) {
912
+ // console.log(key, value);
913
+ // There is a bug that moves the requestId as a key in request attributes, so: && !key.startsWith("support-group-")
914
+ if (!key.startsWith("_") && !RESERVED.some(e => e === key) && !key.startsWith("support-group-")) {
915
+ userParams[key] = value;
916
+ }
917
+ }
918
+ }
919
+ return userParams;
920
+ }
921
+
922
+ /**
923
+ * A stub to get the request parameters, hosted by tilebot on:
924
+ * /${TILEBOT_ROUTE}/ext/parameters/requests/${requestId}?all
925
+ *
926
+ * @param {string} requestId. Tiledesk chatbot/requestId parameters
927
+ */
928
+ getChatbotParameters(requestId, callback) {
929
+ // const jwt_token = this.fixToken(token);
930
+ const url = `${process.env.TILEBOT_ENDPOINT}/ext/reserved/parameters/requests/${requestId}?all`;
931
+ const HTTPREQUEST = {
932
+ url: url,
933
+ headers: {
934
+ 'Content-Type': 'application/json'
935
+ },
936
+ method: 'get'
937
+ };
938
+ this.myrequest(
939
+ HTTPREQUEST,
940
+ function (err, resbody) {
941
+ if (err) {
942
+ if (callback) {
943
+ callback(err);
944
+ }
945
+ }
946
+ else {
947
+ if (callback) {
948
+ callback(null, resbody);
949
+ }
950
+ }
951
+ }, false
952
+ );
953
+ }
954
+
955
+ myrequest(options, callback, log) {
956
+ if (log) {
957
+ console.log("API URL:", options.url);
958
+ console.log("** Options:", JSON.stringify(options));
959
+ }
960
+ axios(
961
+ {
962
+ url: options.url,
963
+ method: options.method,
964
+ data: options.json,
965
+ params: options.params,
966
+ headers: options.headers
967
+ })
968
+ .then((res) => {
969
+ if (log) {
970
+ console.log("Response for url:", options.url);
971
+ console.log("Response headers:\n", JSON.stringify(res.headers));
972
+ }
973
+ if (res && res.status == 200 && res.data) {
974
+ if (callback) {
975
+ callback(null, res.data);
976
+ }
977
+ }
978
+ else {
979
+ if (callback) {
980
+ callback(TiledeskClient.getErr({ message: "Response status not 200" }, options, res), null, null);
981
+ }
982
+ }
983
+ })
984
+ .catch((error) => {
985
+ console.error("(TiledeskChatbotUtil) Axios error: ", JSON.stringify(error));
986
+ if (callback) {
987
+ callback(error, null, null);
988
+ }
989
+ });
990
+ }
991
+
992
+
749
993
  }
750
994
 
751
995
  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) => {