@tiledesk/tiledesk-tybot-connector 2.0.36-rc12 → 2.0.37

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 (31) hide show
  1. package/CHANGELOG.md +3 -0
  2. package/engine/TiledeskChatbot.js +21 -67
  3. package/engine/TiledeskChatbotConst.js +0 -2
  4. package/index.js +0 -26
  5. package/logs/app.log +13651 -12759
  6. package/logs/app1.log +13327 -13395
  7. package/logs/app2.log +7679 -10185
  8. package/logs/app8.log +4066 -0
  9. package/logs/app9.log +5452 -0
  10. package/package.json +4 -3
  11. package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +1 -22
  12. package/tiledeskChatbotPlugs/directives/DEPRECATED_DirOfflineHours.js +4 -3
  13. package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +12 -11
  14. package/tiledeskChatbotPlugs/directives/DirAssign.js +7 -5
  15. package/tiledeskChatbotPlugs/directives/DirAssignFromFunction.js +0 -26
  16. package/tiledeskChatbotPlugs/directives/DirCondition.js +0 -35
  17. package/tiledeskChatbotPlugs/directives/DirConnectBlock.js +0 -1
  18. package/tiledeskChatbotPlugs/directives/DirDeflectToHelpCenter.js +0 -34
  19. package/tiledeskChatbotPlugs/directives/DirDeleteVariable.js +0 -1
  20. package/tiledeskChatbotPlugs/directives/DirDisableInputText.js +0 -22
  21. package/tiledeskChatbotPlugs/directives/DirFireTiledeskEvent.js +4 -2
  22. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgents.js +0 -24
  23. package/tiledeskChatbotPlugs/directives/DirIfOpenHours.js +0 -30
  24. package/tiledeskChatbotPlugs/directives/DirIfOpenHours_OLD.js +0 -29
  25. package/tiledeskChatbotPlugs/directives/DirIntent.js +0 -1
  26. package/tiledeskChatbotPlugs/directives/DirLockIntent.js +1 -25
  27. package/tiledeskChatbotPlugs/directives/DirSendEmail.js +1 -32
  28. package/tiledeskChatbotPlugs/directives/DirWait.js +0 -8
  29. package/utils/TiledeskChatbotUtil.js +163 -139
  30. package/logs/app3.log +0 -161
  31. package/logs/app4.log +0 -46163
@@ -1,5 +1,4 @@
1
1
  const { param } = require('express/lib/request');
2
- const ms = require('minimist-string');
3
2
  const { TiledeskChatbot } = require('../../engine/TiledeskChatbot');
4
3
  const { Filler } = require('../Filler');
5
4
  const { TiledeskClient } = require('@tiledesk/tiledesk-client');
@@ -27,16 +26,7 @@ class DirSendEmail {
27
26
  let action;
28
27
  if (directive.action) {
29
28
  action = directive.action;
30
- }
31
- else if (directive.parameter && directive.parameter.trim() !== "") {
32
- const params = this.parseParams(directive.parameter);
33
- action = {
34
- subject: params.subject,
35
- text: params.text,
36
- to: params.to
37
- }
38
- }
39
- else {
29
+ } else {
40
30
  this.logger.error("Incorrect action for ", directive.name, directive)
41
31
  winston.warn("DirSendEmail Incorrect directive: ", directive);
42
32
  callback();
@@ -94,27 +84,6 @@ class DirSendEmail {
94
84
  }
95
85
  }
96
86
  }
97
-
98
- parseParams(directive_parameter) {
99
- let subject = null;
100
- let text = null;
101
- let to = null
102
- const params = ms(directive_parameter);
103
- if (params.subject) {
104
- subject = params.subject
105
- }
106
- if (params.text) {
107
- text = params.text.replace(/\\n/g, "\n");
108
- }
109
- if (params.to) {
110
- to = params.to;
111
- }
112
- return {
113
- subject: subject,
114
- to: to,
115
- text: text
116
- }
117
- }
118
87
  }
119
88
 
120
89
  module.exports = { DirSendEmail };
@@ -18,7 +18,6 @@ class DirWait {
18
18
  }
19
19
 
20
20
  execute(directive, callback) {
21
- console.log(`(GAB) called DirWait execute at : ${new Date().getTime()}, directive:`, directive)
22
21
  // 500ms < wait-time < 10.000ms
23
22
  winston.verbose("Execute Wait directive");
24
23
  let action;
@@ -48,7 +47,6 @@ class DirWait {
48
47
  }
49
48
 
50
49
  this.go(action, () => {
51
- console.log(`(DirWait) go callback exit at: ${new Date().getTime()} . . .` )
52
50
  this.logger.native("[Wait] Executed");
53
51
  callback();
54
52
  })
@@ -60,16 +58,10 @@ class DirWait {
60
58
  // const step_key = TiledeskChatbot.requestCacheKey(this.requestId) + ":step";
61
59
  if (action && action.millis >= 1000) {//2000 * 60) { // at list 2 minutes waiting time to reset the steps counter
62
60
  // await this.tdcache.set(step_key, 0);
63
- let start1= new Date()
64
61
  await TiledeskChatbot.resetStep(this.tdcache, this.requestId);
65
- let end1 = new Date()
66
- console.log(`(GAB) called DirWait go() 1-> after TiledeskChatbot.resetStep at : ${end1.getTime()}, diff: ${end1-start1}[ms]`)
67
-
68
62
  }
69
- console.log(`(GAB) called DirWait go() at :${new Date().getTime()}, millis: ${action.millis}`)
70
63
  this.logger.native("[Wait] Waiting for ", action.millis, "[ms]")
71
64
  setTimeout(() => {
72
- console.log(`(GAB) called DirWait go() settimeout at : ${new Date().getTime()}`)
73
65
  callback();
74
66
  }, action.millis);
75
67
  }
@@ -680,172 +680,189 @@ class TiledeskChatbotUtil {
680
680
  // update request context
681
681
  try {
682
682
  winston.debug("Updating request variables. Message:", message);
683
-
684
-
685
- const addQueue = [];
686
- const deleteQueue = [];
687
- const add = (k, v) => { if (v !== undefined && v !== null && k !== undefined && k !== null) addQueue.push([k, v]); };
688
- const remove = (k) => { if(k) deleteQueue.push(k);}
683
+ const messageId = message._id;
689
684
 
690
- // --- BASE ATTRIBUTES ---
691
- if (process.env.BASE_URL) {
692
- add(TiledeskChatbotConst.REQ_CHAT_URL, `${process.env.BASE_URL}/dashboard/#/project/${projectId}/wsrequest/${requestId}/messages`);
685
+ if(process.env.BASE_URL){
686
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHAT_URL, `${process.env.BASE_URL}/dashboard/#/project/${projectId}/wsrequest/${requestId}/messages`);
693
687
  }
694
688
 
695
- add(TiledeskChatbotConst.REQ_PROJECT_ID_KEY, projectId);
696
- add(TiledeskChatbotConst.REQ_REQUEST_ID_KEY, requestId);
697
-
698
- // --- CHATBOT INFO ---
689
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_PROJECT_ID_KEY, projectId);
690
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_REQUEST_ID_KEY, requestId);
691
+
699
692
  if (chatbot.bot) {
700
- add(TiledeskChatbotConst.REQ_CHATBOT_NAME_KEY, chatbot.bot.name);
701
- add(TiledeskChatbotConst.REQ_CHATBOT_ID_KEY, chatbot.bot._id);
693
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_NAME_KEY, chatbot.bot.name);
694
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_ID_KEY, chatbot.bot._id);
702
695
  }
703
-
704
- // --- TOKEN ---
705
- if (chatbotToken || process.env.TILEDESK_API) {
706
- add(TiledeskChatbotConst.REQ_CHATBOT_TOKEN, chatbotToken); // deprecated
707
- add(TiledeskChatbotConst.REQ_CHATBOT_TOKEN_v2, "JWT " + chatbotToken);
696
+
697
+ if (chatbotToken) {
698
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN, chatbotToken); // DEPRECATED
699
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN_v2, "JWT " + chatbotToken);
708
700
  }
709
-
701
+
702
+ if (process.env.TILEDESK_API) {
703
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN, chatbotToken); // DEPRECATED
704
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN_v2, "JWT " + chatbotToken);
705
+ }
706
+
710
707
  if (process.env.API_URL) {
711
- add(TiledeskChatbotConst.API_BASE_URL, process.env.API_URL);
708
+ await chatbot.addParameter(TiledeskChatbotConst.API_BASE_URL, process.env.API_URL);
712
709
  }
713
710
 
714
- // --- USER MESSAGE ---
715
711
  if (message.text && message.sender !== "_tdinternal") {
716
- remove(TiledeskChatbotConst.USER_INPUT); // REAL delete
717
-
718
- add(TiledeskChatbotConst.REQ_LAST_USER_TEXT_KEY, message.text); // deprecated
719
- add(TiledeskChatbotConst.REQ_LAST_USER_TEXT_v2_KEY, message.text);
720
-
721
- add(TiledeskChatbotConst.REQ_LAST_USER_MESSAGE_TYPE_KEY, message.type);
722
- add(TiledeskChatbotConst.REQ_LAST_USER_MESSAGE_KEY,
723
- TiledeskChatbotUtil.lastUserMessageFrom(message));
724
-
712
+ await chatbot.deleteParameter(TiledeskChatbotConst.USER_INPUT); // user wrote, delete userInput, replyv2 will not trigger timeout action
713
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_TEXT_KEY, message.text); // DEPRECATED
714
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_TEXT_v2_KEY, message.text);
725
715
  if (message.channel) {
726
- const channelName = message.channel.name === "chat21" ? "web" : message.channel.name;
727
- add(TiledeskChatbotConst.REQ_CHAT_CHANNEL, channelName);
716
+ if (message.channel.name === "chat21") {
717
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHAT_CHANNEL, "web"); // renames the channel in chat21
718
+ }
719
+ else {
720
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHAT_CHANNEL, message.channel.name);
721
+ }
728
722
  }
723
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_MESSAGE_TYPE_KEY, message.type);
724
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_MESSAGE_KEY, TiledeskChatbotUtil.lastUserMessageFrom(message)); // JSON TYPE *NEW
729
725
  }
730
726
 
731
- // --- IMAGE ---
732
- if (message.type && message.type === "image" && message.metadata?.src) {
733
- add(TiledeskChatbotConst.REQ_LAST_USER_IMAGE_URL, message.metadata.src);
734
- add(TiledeskChatbotConst.REQ_LAST_USER_IMAGE_NAME, message.metadata.name);
735
- add(TiledeskChatbotConst.REQ_LAST_USER_IMAGE_WIDTH, message.metadata.width);
736
- add(TiledeskChatbotConst.REQ_LAST_USER_IMAGE_HEIGHT, message.metadata.height);
737
- add(TiledeskChatbotConst.REQ_LAST_USER_IMAGE_TYPE, message.metadata.type);
727
+ // get image
728
+ if (message.type && message.type === "image" && message.metadata) {
729
+ if (message.metadata.src) {
730
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_IMAGE_URL, message.metadata.src);
731
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_IMAGE_NAME, message.metadata.name);
732
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_IMAGE_WIDTH, message.metadata.width);
733
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_IMAGE_HEIGHT, message.metadata.height);
734
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_IMAGE_TYPE, message.metadata.type);
735
+ }
738
736
  }
739
737
 
740
- // --- DOCUMENT ---
741
- if (message.type && message.type === "file" && message.metadata?.src) {
742
-
743
-
744
- if (message.metadata.src) {
745
- const m = message.metadata;
746
- add(TiledeskChatbotConst.REQ_LAST_USER_DOCUMENT_URL, m.src); // deprecated
747
- add(TiledeskChatbotConst.REQ_LAST_USER_DOCUMENT_AS_ATTACHMENT_URL, m.src);
748
-
749
- const inlineUrl = m.src.replace("/download", "/");
750
- add(TiledeskChatbotConst.REQ_LAST_USER_DOCUMENT_AS_INLINE_URL, inlineUrl);
751
-
752
- add(TiledeskChatbotConst.REQ_LAST_USER_DOCUMENT_NAME, m.name);
753
- add(TiledeskChatbotConst.REQ_LAST_USER_DOCUMENT_TYPE, m.type);
738
+ // get document
739
+ if (message.type && message.type === "file" && message.metadata) {
740
+ if (message.metadata.src) {
741
+ await chatbot.addParameter("lastUserDocumentURL", message.metadata.src); // legacy. will be deprecated
742
+ const url_as_attachment = message.metadata.src;
743
+ await chatbot.addParameter("lastUserDocumentAsAttachmentURL", url_as_attachment);
744
+ let url_inline = url_as_attachment;
745
+ if (url_as_attachment.match(/.*\/download.*/)) { // removing "/download" removes the "Content-disposion: attachment" HTTP header
746
+ url_inline = url_as_attachment.replace('/download', '/');
747
+ }
748
+ await chatbot.addParameter("lastUserDocumentAsInlineURL", url_inline);
749
+ await chatbot.addParameter("lastUserDocumentName", message.metadata.name);
750
+ await chatbot.addParameter("lastUserDocumentType", message.metadata.type);
754
751
  }
755
752
  }
756
753
 
757
- // --- LEAD ---
758
754
  if (message && message.request && message.request.lead) {
759
755
  winston.debug("(TiledeskChatbotUtil) Lead found with email: " + message.request.lead.email + " and lead.fullname " + message.request.lead.fullname);
760
- const lead = message.request.lead;
761
-
762
- const savedEmail = await chatbot.getParameter(TiledeskChatbotConst.REQ_LEAD_EMAIL_KEY);
763
- if (lead.email && !savedEmail) add(TiledeskChatbotConst.REQ_LEAD_EMAIL_KEY, lead.email);
764
-
765
- const savedName = await chatbot.getParameter(TiledeskChatbotConst.REQ_LEAD_USERFULLNAME_KEY);
766
- if (lead.fullname && !savedName) add(TiledeskChatbotConst.REQ_LEAD_USERFULLNAME_KEY, lead.fullname);
756
+ let currentLeadEmail = await chatbot.getParameter(TiledeskChatbotConst.REQ_LEAD_EMAIL_KEY);
757
+ winston.debug("(TiledeskChatbotUtil) You lead email from attributes: " + currentLeadEmail);
758
+ if (message.request.lead.email && !currentLeadEmail) {
759
+ // worth saving
760
+ winston.debug("(TiledeskChatbotUtil) worth saving email");
761
+ try {
762
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_LEAD_EMAIL_KEY, message.request.lead.email);
763
+ }
764
+ catch(error) {
765
+ winston.error("(TiledeskChatbotUtil) Error on setting userEmail:", error);
766
+ }
767
+ }
768
+ let currentLeadName = await chatbot.getParameter(TiledeskChatbotConst.REQ_LEAD_USERFULLNAME_KEY);
769
+ winston.debug("(TiledeskChatbotUtil) You lead email from attributes: " + currentLeadEmail);
770
+ if (message.request.lead.fullname && !currentLeadName) {
771
+ // worth saving
772
+ winston.debug("(TiledeskChatbotUtil) worth saving email");
773
+ try {
774
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_LEAD_USERFULLNAME_KEY, message.request.lead.fullname);
775
+ }
776
+ catch(error) {
777
+ winston.error("(TiledeskChatbotUtil) Error on setting userFullname: ", error);
778
+ }
779
+ }
767
780
 
768
781
  if (message.request.lead.phone) {
769
- add(TiledeskChatbotConst.REQ_USER_PHONE_KEY, lead.phone);
782
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_PHONE_KEY, message.request.lead.phone);
770
783
  }
771
- if (lead.lead_id) {
772
- const prefixes = ["wab-", "vxml-", CHANNEL_NAME.VOICE_TWILIO, CHANNEL_NAME.SMS];
773
- if (prefixes.some(pref => lead.lead_id.startsWith(pref))) {
774
- const parts = lead.lead_id.split("-");
775
- if (parts[1]) add(TiledeskChatbotConst.REQ_CURRENT_PHONE_NUMBER_KEY, parts[1]);
784
+ if (message.request.lead.lead_id && (message.request.lead.lead_id.startsWith("wab-") ||
785
+ message.request.lead.lead_id.startsWith("vxml-") ||
786
+ message.request.lead.lead_id.startsWith(CHANNEL_NAME.VOICE_TWILIO) ||
787
+ message.request.lead.lead_id.startsWith(CHANNEL_NAME.SMS))) {
788
+ const splits = message.request.lead.lead_id.split("-");
789
+ if (splits && splits.length > 1) {
790
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CURRENT_PHONE_NUMBER_KEY,splits[1]);
776
791
  }
777
792
  }
778
793
  if (message.request.lead._id) {
779
- add(TiledeskChatbotConst.REQ_USER_LEAD_ID_KEY, lead._id);
794
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_LEAD_ID_KEY, message.request.lead._id);
780
795
  }
781
796
  if (message.request.lead.company) {
782
- add(TiledeskChatbotConst.REQ_USER_COMPANY_KEY, lead.company);
797
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_COMPANY_KEY, message.request.lead.company);
783
798
  }
784
799
  if (message.request.ticket_id) {
785
- add(TiledeskChatbotConst.REQ_TICKET_ID_KEY, message.request.ticket_id);
800
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_TICKET_ID_KEY, message.request.ticket_id);
786
801
  }
787
802
  }
788
803
 
789
- // --- LAST MESSAGE ID ---
790
- const messageId = message._id;
791
- add(TiledeskChatbotConst.REQ_LAST_MESSAGE_ID_KEY, messageId);
792
-
793
- // --- LOCATION ---
804
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_MESSAGE_ID_KEY, messageId);
794
805
  if (message.request && message.request.location && message.request.location.country) {
795
- add(TiledeskChatbotConst.REQ_COUNTRY_KEY, message.request.location.country);
806
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_COUNTRY_KEY, message.request.location.country);
796
807
  }
797
808
  if (message.request && message.request.location && message.request.location.city) {
798
- add(TiledeskChatbotConst.REQ_CITY_KEY, message.request.location.city);
809
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CITY_KEY, message.request.location.city);
799
810
  }
800
-
801
- // --- USER CONTEXT ---
802
811
  if (message.request) {
803
- let userLang = message.request.language;
804
- if (userLang) {
805
- const parsed = parser.parse(userLang);
806
- if (parsed?.[0]?.code) userLang = parsed[0].code;
812
+ let user_language = message.request["language"];
813
+ if (message.request["language"]) {
814
+ var languages = parser.parse(message.request["language"]);
815
+ if (languages && languages.length > 0 && languages[0].code) {
816
+ user_language = languages[0].code;
817
+ }
807
818
  }
808
-
809
- add(TiledeskChatbotConst.REQ_USER_SOURCE_PAGE_KEY, message.request.sourcePage);
810
- add(TiledeskChatbotConst.REQ_USER_LANGUAGE_KEY, userLang);
811
- add(TiledeskChatbotConst.REQ_USER_AGENT_KEY, message.request.userAgent);
812
-
819
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_SOURCE_PAGE_KEY, message.request.sourcePage);
820
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_LANGUAGE_KEY, user_language);
821
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_AGENT_KEY, message.request.userAgent);
813
822
  if (message.request.attributes && message.request.attributes.decoded_jwt) {
814
- add(TiledeskChatbotConst.REQ_DECODED_JWT_KEY, message.request.attributes.decoded_jwt);
823
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_DECODED_JWT_KEY, message.request.attributes.decoded_jwt);
824
+ }
825
+ if (message.request.requester) {
826
+ if (message.request.requester.isAuthenticated === true) {
827
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_REQUESTER_IS_AUTHENTICATED_KEY, true);
828
+ }
829
+ else {
830
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_REQUESTER_IS_AUTHENTICATED_KEY, false);
831
+ }
815
832
  }
816
-
817
- const auth = !!message.request.requester?.isAuthenticated;
818
- add(TiledeskChatbotConst.REQ_REQUESTER_IS_AUTHENTICATED_KEY, auth);
819
833
  }
820
-
821
- // --- DEPARTMENT ---
822
- const dep = message.request?.department || message.attributes;
823
- if (dep) {
824
- add(TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY, dep.departmentId || dep._id);
825
- add(TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY, dep.departmentName || dep.name);
834
+ if (message.request && message.request.department) {
835
+ // It was an error when getting this from widget message's attributes
836
+ // await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY, message.attributes.departmentId);
837
+ // await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY, message.attributes.departmentName);
838
+ // get from request.department instead
839
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY, message.request.department._id);
840
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY, message.request.department.name);
841
+ }
842
+ else if (message.attributes && message.attributes.departmentId) {
843
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY, message.attributes.departmentId);
844
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY, message.attributes.departmentName);
826
845
  }
827
846
 
828
- // --- EMAIL ATTRIBUTES ---
829
847
  if (message.attributes) {
830
- const emailMapping = [
831
- ["email_subject", TiledeskChatbotConst.REQ_EMAIL_SUBJECT],
832
- ["email_toEmail", TiledeskChatbotConst.REQ_EMAIL_TO],
833
- ["email_fromEmail", TiledeskChatbotConst.REQ_EMAIL_FROM],
834
- ["email_messageId", TiledeskChatbotConst.REQ_EMAIL_MESSAGE_ID],
835
- ["email_replyTo", TiledeskChatbotConst.REQ_EMAIL_REPLY_TO],
836
- ["email_eml", TiledeskChatbotConst.REQ_EMAIL_EML],
837
- ["link", TiledeskChatbotConst.REQ_EMAIL_ATTACHMENTS_LINK],
838
- ["attachments", TiledeskChatbotConst.REQ_EMAIL_ATTACHMENTS_FILES]
848
+ const attrFields = [
849
+ { key: TiledeskChatbotConst.REQ_EMAIL_SUBJECT, attr: "email_subject" },
850
+ { key: TiledeskChatbotConst.REQ_EMAIL_TO, attr: "email_toEmail" },
851
+ { key: TiledeskChatbotConst.REQ_EMAIL_FROM, attr: "email_fromEmail" },
852
+ { key: TiledeskChatbotConst.REQ_EMAIL_MESSAGE_ID, attr: "email_messageId" },
853
+ { key: TiledeskChatbotConst.REQ_EMAIL_REPLY_TO, attr: "email_replyTo" },
854
+ { key: TiledeskChatbotConst.REQ_EMAIL_EML, attr: "email_eml" },
855
+ { key: TiledeskChatbotConst.REQ_EMAIL_ATTACHMENTS_LINK, attr: "link" },
856
+ { key: TiledeskChatbotConst.REQ_EMAIL_ATTACHMENTS_FILES, attr: "attachments" },
857
+ // aggiungi qui altri campi se necessario
839
858
  ];
840
-
841
- for (const [attr, key] of emailMapping) {
842
- if (message.attributes[attr] !== undefined) {
843
- add(key, message.attributes[attr]);
859
+ for (const field of attrFields) {
860
+ if (message.attributes[field.attr] !== undefined && message.attributes[field.attr] !== null) {
861
+ await chatbot.addParameter(field.key, message.attributes[field.attr]);
844
862
  }
845
863
  }
846
864
  }
847
865
 
848
- // --- PAYLOAD ---
849
866
  if (message && message.request && message.request.attributes && message.request.attributes.payload) {
850
867
  if (!message.attributes) {
851
868
  message.attributes = {}
@@ -856,15 +873,15 @@ class TiledeskChatbotUtil {
856
873
  if (message.attributes) {
857
874
  winston.debug("(TiledeskChatbotUtil) Ok message.attributes ", message.attributes);
858
875
 
859
- add(TiledeskChatbotConst.REQ_END_USER_ID_KEY, message.attributes.requester_id);
860
- add(TiledeskChatbotConst.REQ_END_USER_IP_ADDRESS_KEY, message.attributes.ipAddress);
876
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_END_USER_ID_KEY, message.attributes.requester_id);
877
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_END_USER_IP_ADDRESS_KEY, message.attributes.ipAddress);
861
878
  if (message.attributes.payload) {
862
879
  try {
863
880
  for (const [key, value] of Object.entries(message.attributes.payload)) {
864
881
  const value_type = typeof value;
865
- add(key, value);
882
+ await chatbot.addParameter(key, value);
866
883
  }
867
- add("payload", message.attributes.payload);
884
+ await chatbot.addParameter("payload", message.attributes.payload);
868
885
  }
869
886
  catch(err) {
870
887
  winston.error("(TiledeskChatbotUtil) Error importing message payload in request variables: ", err);
@@ -874,19 +891,19 @@ class TiledeskChatbotUtil {
874
891
  // TODO - REMOVE - THEY ARE IN ATTRIBUTES.PAYLOAD
875
892
  // voice-vxml attributes
876
893
  if (message.attributes.dnis) {
877
- add("dnis", message.attributes.dnis);
894
+ await chatbot.addParameter("dnis", message.attributes.dnis);
878
895
  }
879
896
  if (message.attributes.callId) {
880
- add("callId", message.attributes.callId);
897
+ await chatbot.addParameter("callId", message.attributes.callId);
881
898
  }
882
899
  if (message.attributes.ani) {
883
- add("ani", message.attributes.ani);
900
+ await chatbot.addParameter("ani", message.attributes.ani);
884
901
  }
885
902
  }
886
903
 
887
904
 
888
905
 
889
- // --- GLOBALS ---
906
+
890
907
  const _bot = chatbot.bot; // aka FaqKB
891
908
  winston.debug("(TiledeskChatbotUtil) Adding Globals to context: ", _bot);
892
909
 
@@ -894,7 +911,7 @@ class TiledeskChatbotUtil {
894
911
  winston.debug("(TiledeskChatbotUtil) Got Globals: ", _bot.attributes.globals);
895
912
  _bot.attributes.globals.forEach(async (global_var) => {
896
913
  winston.error("(TiledeskChatbotUtil) Adding global: " + global_var.key + " value: " + global_var.value);
897
- add(global_var.key, global_var.value);
914
+ await chatbot.addParameter(global_var.key, global_var.value);
898
915
  });
899
916
  }
900
917
  // await chatbot.addParameter("testVar",
@@ -905,13 +922,6 @@ class TiledeskChatbotUtil {
905
922
  // }
906
923
  // }
907
924
  // );
908
-
909
-
910
- // --- FINAL BATCH EXECUTION ---
911
- await Promise.all([
912
- ...addQueue.map(([key, value]) => chatbot.addParameter(key, value)),
913
- ...deleteQueue.map(key => chatbot.deleteParameter(key))
914
- ]);
915
925
 
916
926
  } catch(error) {
917
927
  winston.error("(TiledeskChatbotUtil) updateRequestAttributes Error: ", error);
@@ -941,14 +951,28 @@ class TiledeskChatbotUtil {
941
951
  }
942
952
 
943
953
  static validateRequestId(requestId, projectId) {
944
- if (!requestId || !projectId) return false;
945
-
946
- // Pattern for support-group-{projectId}-{id}
947
- const supportPattern = new RegExp(`^support-group-${projectId}-\\S+$`);
948
- // Pattern for automation-request-{projectId}-{id} or automation-request-{projectId}-{id}-something
949
- const automationPattern = new RegExp(`^automation-request-${projectId}-\\S+(-\\S+)?$`);
950
-
951
- return supportPattern.test(requestId) || automationPattern.test(requestId);
954
+ let isValid = false;
955
+ if (requestId.startsWith("support-group-")) {
956
+ const parts = requestId.split("-");
957
+ if (parts.length >= 4) {
958
+ isValid = (parts[0] === "support" && parts[1] === "group" && parts[2] === projectId && parts[3].length > 0);
959
+ }
960
+ else {
961
+ isValid = false;
962
+ }
963
+ } else if (requestId.startsWith("automation-request-")) {
964
+ const parts = requestId.split("-");
965
+ if (parts.length === 4 || parts.length === 5) {
966
+ isValid = (parts[0] === "automation" && parts[1] === "request" && parts[2] === projectId && parts[3].length > 0);
967
+ }
968
+ else {
969
+ isValid = false;
970
+ }
971
+ }
972
+ else {
973
+ isValid = false;
974
+ }
975
+ return isValid;
952
976
  }
953
977
 
954
978
  static userFlowAttributes(flowAttributes) {