@tiledesk/tiledesk-tybot-connector 2.0.34 → 2.0.36-rc12
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/engine/TiledeskChatbot.js +67 -21
- package/engine/TiledeskChatbotConst.js +2 -0
- package/index.js +26 -0
- package/logs/app2.log +241 -0
- package/logs/app3.log +161 -0
- package/package.json +1 -1
- package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +22 -1
- package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +25 -2
- package/tiledeskChatbotPlugs/directives/DirWait.js +8 -0
- package/utils/TiledeskChatbotUtil.js +139 -163
package/package.json
CHANGED
|
@@ -117,6 +117,7 @@ class DirectivesChatbotPlug {
|
|
|
117
117
|
}
|
|
118
118
|
|
|
119
119
|
async processDirectives(theend) {
|
|
120
|
+
console.log(`(GAB) DirectivesChatbotPlug 0--> after processDirectives at : ${new Date().getTime()}`)
|
|
120
121
|
this.theend = theend;
|
|
121
122
|
const directives = this.directives;
|
|
122
123
|
if (!directives || directives.length === 0) {
|
|
@@ -170,15 +171,23 @@ class DirectivesChatbotPlug {
|
|
|
170
171
|
this.curr_directive_index = -1;
|
|
171
172
|
winston.verbose("(DirectivesChatbotPlug) processing directives...");
|
|
172
173
|
|
|
174
|
+
let start1 = new Date()
|
|
173
175
|
const next_dir = await this.nextDirective(directives);
|
|
176
|
+
let end1 = new Date()
|
|
177
|
+
console.log(`(GAB) DirectivesChatbotPlug 1--> after processDirectives nextDirective at : ${end1.getTime()}, diff: ${end1-start1}[ms]`)
|
|
178
|
+
|
|
174
179
|
winston.debug("(DirectivesChatbotPlug) next_dir: ", next_dir);
|
|
175
180
|
await this.process(next_dir);
|
|
176
181
|
}
|
|
177
182
|
|
|
178
183
|
async nextDirective(directives) {
|
|
184
|
+
console.log(`(GAB) DirectivesChatbotPlug 0--> nextDirective at : ${new Date().getTime()}`)
|
|
185
|
+
let start1 = new Date()
|
|
179
186
|
winston.debug("(DirectivesChatbotPlug) ....nextDirective() checkStep()");
|
|
180
187
|
const go_on = await TiledeskChatbot.checkStep(this.context.tdcache, this.context.requestId, this.chatbot?.MAX_STEPS, this.chatbot?.MAX_EXECUTION_TIME);
|
|
181
|
-
|
|
188
|
+
let end1 = new Date()
|
|
189
|
+
console.log(`(GAB) DirectivesChatbotPlug 1--> nextDirective at : ${end1.getTime()}, diff: ${end1-start1}[ms]`)
|
|
190
|
+
|
|
182
191
|
if (go_on.error) {
|
|
183
192
|
winston.debug("(DirectivesChatbotPlug) go_on == false! nextDirective() Stopped!");
|
|
184
193
|
return this.errorMessage(go_on.error); //"Request error: anomaly detection. MAX ACTIONS exeeded.");
|
|
@@ -211,6 +220,7 @@ class DirectivesChatbotPlug {
|
|
|
211
220
|
}
|
|
212
221
|
|
|
213
222
|
async process(directive) {
|
|
223
|
+
console.log(`(GAB) DirectivesChatbotPlug process 0--> after process at : ${new Date().getTime()}`)
|
|
214
224
|
let context = this.context;
|
|
215
225
|
if (directive) {
|
|
216
226
|
winston.verbose("(DirectivesChatbotPlug) directive['name']: " + directive["name"]);
|
|
@@ -219,16 +229,23 @@ class DirectivesChatbotPlug {
|
|
|
219
229
|
if (directive && directive.name) {
|
|
220
230
|
directive_name = directive.name.toLowerCase();
|
|
221
231
|
}
|
|
232
|
+
let start1 = new Date();
|
|
222
233
|
if (directive && directive.action) {
|
|
223
234
|
const action_id = directive.action["_tdActionId"];
|
|
224
235
|
const locked_action_id = await this.chatbot.currentLockedAction(this.supportRequest.request_id);
|
|
236
|
+
let end1 = new Date()
|
|
237
|
+
console.log(`(GAB) DirectivesChatbotPlug process 1--> after chatbot.currentLockedAction at : ${end1.getTime()}, diff: ${end1-start1}[ms]`)
|
|
225
238
|
if ( locked_action_id && (locked_action_id !== action_id) ) {
|
|
239
|
+
let start2 = new Date();
|
|
226
240
|
let next_dir = await this.nextDirective(this.directives);
|
|
241
|
+
let end2 = new Date()
|
|
242
|
+
console.log(`(GAB) DirectivesChatbotPlug process 2--> after chatbot.currentLockedAction at : ${end2.getTime()}, diff: ${end2-start2}[ms]`)
|
|
227
243
|
this.process(next_dir);
|
|
228
244
|
return;
|
|
229
245
|
}
|
|
230
246
|
|
|
231
247
|
}
|
|
248
|
+
let start2 = new Date()
|
|
232
249
|
if (directive == null || (directive !== null && directive["name"] === undefined)) {
|
|
233
250
|
winston.debug("(DirectivesChatbotPlug) stop process(). directive is (null?): ", directive);
|
|
234
251
|
this.theend();
|
|
@@ -450,7 +467,11 @@ class DirectivesChatbotPlug {
|
|
|
450
467
|
});
|
|
451
468
|
}
|
|
452
469
|
else if (directive_name === Directives.WAIT) {
|
|
470
|
+
let end2 = new Date();
|
|
471
|
+
console.log(`(GAB) DirectivesChatbotPlug process 3--> before DirWait execute at : ${end2.getTime()}`)
|
|
453
472
|
new DirWait(context).execute(directive, async () => {
|
|
473
|
+
let endWait = new Date()
|
|
474
|
+
console.log(`(GAB) DirectivesChatbotPlug process 3--> after DirWait executed callback at : ${endWait.getTime()}, diff: ${endWait-end2}[ms]`)
|
|
454
475
|
let next_dir = await this.nextDirective(this.directives);
|
|
455
476
|
this.process(next_dir);
|
|
456
477
|
});
|
|
@@ -71,7 +71,7 @@ class DirAskGPTV2 {
|
|
|
71
71
|
// default values
|
|
72
72
|
let answer = "No answers";
|
|
73
73
|
let namespace = this.context.projectId;
|
|
74
|
-
let llm;
|
|
74
|
+
let llm = "openai";
|
|
75
75
|
let model;
|
|
76
76
|
let temperature;
|
|
77
77
|
let max_tokens;
|
|
@@ -99,6 +99,9 @@ class DirAskGPTV2 {
|
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
let source = null;
|
|
102
|
+
if (!action.llm) {
|
|
103
|
+
action.llm = "openai";
|
|
104
|
+
}
|
|
102
105
|
|
|
103
106
|
await this.checkMandatoryParameters(action).catch( async (missing_param) => {
|
|
104
107
|
this.logger.error(`[Ask Knowledge Base] missing attribute '${missing_param}'`);
|
|
@@ -116,6 +119,9 @@ class DirAskGPTV2 {
|
|
|
116
119
|
if (action.namespace) {
|
|
117
120
|
namespace = action.namespace;
|
|
118
121
|
}
|
|
122
|
+
if (action.llm) {
|
|
123
|
+
llm = action.llm;
|
|
124
|
+
}
|
|
119
125
|
if (action.model) {
|
|
120
126
|
model = action.model;
|
|
121
127
|
}
|
|
@@ -172,8 +178,10 @@ class DirAskGPTV2 {
|
|
|
172
178
|
let key;
|
|
173
179
|
let publicKey = false;
|
|
174
180
|
let ollama_integration;
|
|
181
|
+
let vllm_integration;
|
|
175
182
|
|
|
176
183
|
if (action.llm === 'ollama') {
|
|
184
|
+
key = process.env.GPTKEY;
|
|
177
185
|
ollama_integration = await integrationService.getIntegration(this.projectId, action.llm, this.token).catch( async (err) => {
|
|
178
186
|
this.logger.error("[Ask Knowledge Base] Error getting ollama integration.");
|
|
179
187
|
await this.chatbot.addParameter("flowError", "Ollama integration not found");
|
|
@@ -185,7 +193,22 @@ class DirAskGPTV2 {
|
|
|
185
193
|
callback();
|
|
186
194
|
return;
|
|
187
195
|
})
|
|
188
|
-
}
|
|
196
|
+
}
|
|
197
|
+
else if (action.llm === 'vllm') {
|
|
198
|
+
key = process.env.GPTKEY;
|
|
199
|
+
vllm_integration = await integrationService.getIntegration(this.projectId, action.llm, this.token).catch( async (err) => {
|
|
200
|
+
this.logger.error("[Ask Knowledge Base] Error getting vllm integration.");
|
|
201
|
+
await this.chatbot.addParameter("flowError", "vLLM integration not found");
|
|
202
|
+
if (falseIntent) {
|
|
203
|
+
await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
|
|
204
|
+
callback(true);
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
callback();
|
|
208
|
+
return;
|
|
209
|
+
})
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
189
212
|
key = await integrationService.getKeyFromIntegrations(this.projectId, action.llm, this.token);
|
|
190
213
|
|
|
191
214
|
if (!key && action.llm === 'openai') {
|
|
@@ -18,6 +18,7 @@ class DirWait {
|
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
execute(directive, callback) {
|
|
21
|
+
console.log(`(GAB) called DirWait execute at : ${new Date().getTime()}, directive:`, directive)
|
|
21
22
|
// 500ms < wait-time < 10.000ms
|
|
22
23
|
winston.verbose("Execute Wait directive");
|
|
23
24
|
let action;
|
|
@@ -47,6 +48,7 @@ class DirWait {
|
|
|
47
48
|
}
|
|
48
49
|
|
|
49
50
|
this.go(action, () => {
|
|
51
|
+
console.log(`(DirWait) go callback exit at: ${new Date().getTime()} . . .` )
|
|
50
52
|
this.logger.native("[Wait] Executed");
|
|
51
53
|
callback();
|
|
52
54
|
})
|
|
@@ -58,10 +60,16 @@ class DirWait {
|
|
|
58
60
|
// const step_key = TiledeskChatbot.requestCacheKey(this.requestId) + ":step";
|
|
59
61
|
if (action && action.millis >= 1000) {//2000 * 60) { // at list 2 minutes waiting time to reset the steps counter
|
|
60
62
|
// await this.tdcache.set(step_key, 0);
|
|
63
|
+
let start1= new Date()
|
|
61
64
|
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
|
+
|
|
62
68
|
}
|
|
69
|
+
console.log(`(GAB) called DirWait go() at :${new Date().getTime()}, millis: ${action.millis}`)
|
|
63
70
|
this.logger.native("[Wait] Waiting for ", action.millis, "[ms]")
|
|
64
71
|
setTimeout(() => {
|
|
72
|
+
console.log(`(GAB) called DirWait go() settimeout at : ${new Date().getTime()}`)
|
|
65
73
|
callback();
|
|
66
74
|
}, action.millis);
|
|
67
75
|
}
|
|
@@ -680,189 +680,172 @@ class TiledeskChatbotUtil {
|
|
|
680
680
|
// update request context
|
|
681
681
|
try {
|
|
682
682
|
winston.debug("Updating request variables. Message:", message);
|
|
683
|
-
|
|
683
|
+
|
|
684
684
|
|
|
685
|
-
|
|
686
|
-
|
|
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);}
|
|
689
|
+
|
|
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`);
|
|
687
693
|
}
|
|
688
694
|
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
695
|
+
add(TiledeskChatbotConst.REQ_PROJECT_ID_KEY, projectId);
|
|
696
|
+
add(TiledeskChatbotConst.REQ_REQUEST_ID_KEY, requestId);
|
|
697
|
+
|
|
698
|
+
// --- CHATBOT INFO ---
|
|
692
699
|
if (chatbot.bot) {
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
}
|
|
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);
|
|
700
|
+
add(TiledeskChatbotConst.REQ_CHATBOT_NAME_KEY, chatbot.bot.name);
|
|
701
|
+
add(TiledeskChatbotConst.REQ_CHATBOT_ID_KEY, chatbot.bot._id);
|
|
700
702
|
}
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
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);
|
|
705
708
|
}
|
|
706
|
-
|
|
709
|
+
|
|
707
710
|
if (process.env.API_URL) {
|
|
708
|
-
|
|
711
|
+
add(TiledeskChatbotConst.API_BASE_URL, process.env.API_URL);
|
|
709
712
|
}
|
|
710
713
|
|
|
714
|
+
// --- USER MESSAGE ---
|
|
711
715
|
if (message.text && message.sender !== "_tdinternal") {
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
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
|
+
|
|
715
725
|
if (message.channel) {
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
}
|
|
719
|
-
else {
|
|
720
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_CHAT_CHANNEL, message.channel.name);
|
|
721
|
-
}
|
|
726
|
+
const channelName = message.channel.name === "chat21" ? "web" : message.channel.name;
|
|
727
|
+
add(TiledeskChatbotConst.REQ_CHAT_CHANNEL, channelName);
|
|
722
728
|
}
|
|
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
|
|
725
729
|
}
|
|
726
730
|
|
|
727
|
-
//
|
|
728
|
-
if (message.type && message.type === "image" && message.metadata) {
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_IMAGE_TYPE, message.metadata.type);
|
|
735
|
-
}
|
|
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);
|
|
736
738
|
}
|
|
737
739
|
|
|
738
|
-
//
|
|
739
|
-
if (message.type && message.type === "file" && message.metadata) {
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
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);
|
|
751
754
|
}
|
|
752
755
|
}
|
|
753
756
|
|
|
757
|
+
// --- LEAD ---
|
|
754
758
|
if (message && message.request && message.request.lead) {
|
|
755
759
|
winston.debug("(TiledeskChatbotUtil) Lead found with email: " + message.request.lead.email + " and lead.fullname " + message.request.lead.fullname);
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
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
|
-
}
|
|
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);
|
|
780
767
|
|
|
781
768
|
if (message.request.lead.phone) {
|
|
782
|
-
|
|
769
|
+
add(TiledeskChatbotConst.REQ_USER_PHONE_KEY, lead.phone);
|
|
783
770
|
}
|
|
784
|
-
if (
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
if (splits && splits.length > 1) {
|
|
790
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_CURRENT_PHONE_NUMBER_KEY,splits[1]);
|
|
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]);
|
|
791
776
|
}
|
|
792
777
|
}
|
|
793
778
|
if (message.request.lead._id) {
|
|
794
|
-
|
|
779
|
+
add(TiledeskChatbotConst.REQ_USER_LEAD_ID_KEY, lead._id);
|
|
795
780
|
}
|
|
796
781
|
if (message.request.lead.company) {
|
|
797
|
-
|
|
782
|
+
add(TiledeskChatbotConst.REQ_USER_COMPANY_KEY, lead.company);
|
|
798
783
|
}
|
|
799
784
|
if (message.request.ticket_id) {
|
|
800
|
-
|
|
785
|
+
add(TiledeskChatbotConst.REQ_TICKET_ID_KEY, message.request.ticket_id);
|
|
801
786
|
}
|
|
802
787
|
}
|
|
803
788
|
|
|
804
|
-
|
|
789
|
+
// --- LAST MESSAGE ID ---
|
|
790
|
+
const messageId = message._id;
|
|
791
|
+
add(TiledeskChatbotConst.REQ_LAST_MESSAGE_ID_KEY, messageId);
|
|
792
|
+
|
|
793
|
+
// --- LOCATION ---
|
|
805
794
|
if (message.request && message.request.location && message.request.location.country) {
|
|
806
|
-
|
|
795
|
+
add(TiledeskChatbotConst.REQ_COUNTRY_KEY, message.request.location.country);
|
|
807
796
|
}
|
|
808
797
|
if (message.request && message.request.location && message.request.location.city) {
|
|
809
|
-
|
|
798
|
+
add(TiledeskChatbotConst.REQ_CITY_KEY, message.request.location.city);
|
|
810
799
|
}
|
|
800
|
+
|
|
801
|
+
// --- USER CONTEXT ---
|
|
811
802
|
if (message.request) {
|
|
812
|
-
let
|
|
813
|
-
if (
|
|
814
|
-
|
|
815
|
-
if (
|
|
816
|
-
user_language = languages[0].code;
|
|
817
|
-
}
|
|
803
|
+
let userLang = message.request.language;
|
|
804
|
+
if (userLang) {
|
|
805
|
+
const parsed = parser.parse(userLang);
|
|
806
|
+
if (parsed?.[0]?.code) userLang = parsed[0].code;
|
|
818
807
|
}
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
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
|
+
|
|
822
813
|
if (message.request.attributes && message.request.attributes.decoded_jwt) {
|
|
823
|
-
|
|
814
|
+
add(TiledeskChatbotConst.REQ_DECODED_JWT_KEY, message.request.attributes.decoded_jwt);
|
|
824
815
|
}
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
}
|
|
829
|
-
else {
|
|
830
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_REQUESTER_IS_AUTHENTICATED_KEY, false);
|
|
831
|
-
}
|
|
832
|
-
}
|
|
833
|
-
}
|
|
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);
|
|
816
|
+
|
|
817
|
+
const auth = !!message.request.requester?.isAuthenticated;
|
|
818
|
+
add(TiledeskChatbotConst.REQ_REQUESTER_IS_AUTHENTICATED_KEY, auth);
|
|
841
819
|
}
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
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);
|
|
845
826
|
}
|
|
846
827
|
|
|
828
|
+
// --- EMAIL ATTRIBUTES ---
|
|
847
829
|
if (message.attributes) {
|
|
848
|
-
const
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
// aggiungi qui altri campi se necessario
|
|
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]
|
|
858
839
|
];
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
840
|
+
|
|
841
|
+
for (const [attr, key] of emailMapping) {
|
|
842
|
+
if (message.attributes[attr] !== undefined) {
|
|
843
|
+
add(key, message.attributes[attr]);
|
|
862
844
|
}
|
|
863
845
|
}
|
|
864
846
|
}
|
|
865
847
|
|
|
848
|
+
// --- PAYLOAD ---
|
|
866
849
|
if (message && message.request && message.request.attributes && message.request.attributes.payload) {
|
|
867
850
|
if (!message.attributes) {
|
|
868
851
|
message.attributes = {}
|
|
@@ -873,15 +856,15 @@ class TiledeskChatbotUtil {
|
|
|
873
856
|
if (message.attributes) {
|
|
874
857
|
winston.debug("(TiledeskChatbotUtil) Ok message.attributes ", message.attributes);
|
|
875
858
|
|
|
876
|
-
|
|
877
|
-
|
|
859
|
+
add(TiledeskChatbotConst.REQ_END_USER_ID_KEY, message.attributes.requester_id);
|
|
860
|
+
add(TiledeskChatbotConst.REQ_END_USER_IP_ADDRESS_KEY, message.attributes.ipAddress);
|
|
878
861
|
if (message.attributes.payload) {
|
|
879
862
|
try {
|
|
880
863
|
for (const [key, value] of Object.entries(message.attributes.payload)) {
|
|
881
864
|
const value_type = typeof value;
|
|
882
|
-
|
|
865
|
+
add(key, value);
|
|
883
866
|
}
|
|
884
|
-
|
|
867
|
+
add("payload", message.attributes.payload);
|
|
885
868
|
}
|
|
886
869
|
catch(err) {
|
|
887
870
|
winston.error("(TiledeskChatbotUtil) Error importing message payload in request variables: ", err);
|
|
@@ -891,19 +874,19 @@ class TiledeskChatbotUtil {
|
|
|
891
874
|
// TODO - REMOVE - THEY ARE IN ATTRIBUTES.PAYLOAD
|
|
892
875
|
// voice-vxml attributes
|
|
893
876
|
if (message.attributes.dnis) {
|
|
894
|
-
|
|
877
|
+
add("dnis", message.attributes.dnis);
|
|
895
878
|
}
|
|
896
879
|
if (message.attributes.callId) {
|
|
897
|
-
|
|
880
|
+
add("callId", message.attributes.callId);
|
|
898
881
|
}
|
|
899
882
|
if (message.attributes.ani) {
|
|
900
|
-
|
|
883
|
+
add("ani", message.attributes.ani);
|
|
901
884
|
}
|
|
902
885
|
}
|
|
903
886
|
|
|
904
887
|
|
|
905
888
|
|
|
906
|
-
|
|
889
|
+
// --- GLOBALS ---
|
|
907
890
|
const _bot = chatbot.bot; // aka FaqKB
|
|
908
891
|
winston.debug("(TiledeskChatbotUtil) Adding Globals to context: ", _bot);
|
|
909
892
|
|
|
@@ -911,7 +894,7 @@ class TiledeskChatbotUtil {
|
|
|
911
894
|
winston.debug("(TiledeskChatbotUtil) Got Globals: ", _bot.attributes.globals);
|
|
912
895
|
_bot.attributes.globals.forEach(async (global_var) => {
|
|
913
896
|
winston.error("(TiledeskChatbotUtil) Adding global: " + global_var.key + " value: " + global_var.value);
|
|
914
|
-
|
|
897
|
+
add(global_var.key, global_var.value);
|
|
915
898
|
});
|
|
916
899
|
}
|
|
917
900
|
// await chatbot.addParameter("testVar",
|
|
@@ -922,6 +905,13 @@ class TiledeskChatbotUtil {
|
|
|
922
905
|
// }
|
|
923
906
|
// }
|
|
924
907
|
// );
|
|
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
|
+
]);
|
|
925
915
|
|
|
926
916
|
} catch(error) {
|
|
927
917
|
winston.error("(TiledeskChatbotUtil) updateRequestAttributes Error: ", error);
|
|
@@ -951,28 +941,14 @@ class TiledeskChatbotUtil {
|
|
|
951
941
|
}
|
|
952
942
|
|
|
953
943
|
static validateRequestId(requestId, projectId) {
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
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;
|
|
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);
|
|
976
952
|
}
|
|
977
953
|
|
|
978
954
|
static userFlowAttributes(flowAttributes) {
|