@tiledesk/tiledesk-tybot-connector 0.2.84 → 0.2.86
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/models/MockBotsDataSource.js +1 -1
- package/models/TiledeskChatbot.js +1 -8
- package/models/TiledeskChatbotConst.js +5 -0
- package/models/TiledeskChatbotUtil.js +11 -10
- package/package.json +1 -1
- package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +8 -88
- package/tiledeskChatbotPlugs/directives/DirContactUpdate.js +98 -0
- package/tiledeskChatbotPlugs/directives/DirIfOnlineAgentsV2.js +74 -15
- package/tiledeskChatbotPlugs/directives/DirIfOpenHours.js +152 -27
- package/tiledeskChatbotPlugs/directives/DirIfOpenHours_OLD.js +139 -0
- package/tiledeskChatbotPlugs/directives/Directives.js +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,14 @@
|
|
|
5
5
|
available on:
|
|
6
6
|
▶️ https://www.npmjs.com/package/@tiledesk/tiledesk-tybot-connector
|
|
7
7
|
|
|
8
|
+
# v0.2.86
|
|
9
|
+
- Added to ifOnlineAgents Action: ignoreProjectWideOperatingHours = action.ignoreOperatingHours;
|
|
10
|
+
- Updated ifOnlineAgents with get available agents with 'raw' option
|
|
11
|
+
|
|
12
|
+
# v0.2.85
|
|
13
|
+
- Improved "If operating hours" action with time slots
|
|
14
|
+
- Other fix (?)
|
|
15
|
+
|
|
8
16
|
# v0.2.84
|
|
9
17
|
- Fixed "guest#" bug
|
|
10
18
|
- Fixed default_context in AskGPTv2
|
|
@@ -78,7 +78,7 @@ class MockBotsDataSource {
|
|
|
78
78
|
async getByIntentDisplayNameCache(botId, key, tdcache) {
|
|
79
79
|
let faq = null;
|
|
80
80
|
if (tdcache) {
|
|
81
|
-
// console.log("mock
|
|
81
|
+
// console.log("mock cache. anyway in mock getting faq from datasource...");
|
|
82
82
|
faq = await this.getByIntentDisplayName(botId, key);
|
|
83
83
|
// console.log("faq found in datasource.:", JSON.stringify(faq));
|
|
84
84
|
}
|
|
@@ -787,15 +787,8 @@ class TiledeskChatbot {
|
|
|
787
787
|
fullname: all_parameters['userFullname'],
|
|
788
788
|
phone: all_parameters['userPhone']
|
|
789
789
|
}
|
|
790
|
-
|
|
791
|
-
tdclient.updateLead(leadId, nativeAttributes, null, null, () => {
|
|
790
|
+
tdclient.updateLead(leadId, nativeAttributes, null, null, () => {
|
|
792
791
|
if (this.log) {console.log("Lead updated.")}
|
|
793
|
-
// tdclient.updateRequestAttributes(requestId, {
|
|
794
|
-
// preChatForm: all_parameters,
|
|
795
|
-
// updated: Date.now
|
|
796
|
-
// }, () => {
|
|
797
|
-
// if (this.log) {console.log("Prechat updated.");}
|
|
798
|
-
// });
|
|
799
792
|
});
|
|
800
793
|
};
|
|
801
794
|
}
|
|
@@ -25,6 +25,11 @@ class TiledeskChatbotConst {
|
|
|
25
25
|
static USER_INPUT = "_userInput";
|
|
26
26
|
static REQ_LEAD_USERFULLNAME_KEY = "userFullname";
|
|
27
27
|
static REQ_LEAD_EMAIL_KEY = "userEmail";
|
|
28
|
+
static REQ_USER_PHONE_KEY = "userPhone";
|
|
29
|
+
static REQ_CURRENT_PHONE_NUMBER_KEY = "currentPhoneNumber";
|
|
30
|
+
static REQ_USER_LEAD_ID_KEY = "userLeadId";
|
|
31
|
+
static REQ_USER_COMPANY_KEY = "userCompany";
|
|
32
|
+
static REQ_TICKET_ID_KEY = "ticketId";
|
|
28
33
|
|
|
29
34
|
|
|
30
35
|
// static REQ_DEPARTMENT_ID_KEY = "tdDepartmentId";
|
|
@@ -598,8 +598,8 @@ class TiledeskChatbotUtil {
|
|
|
598
598
|
if (message.request.lead.email) {
|
|
599
599
|
await chatbot.addParameter(TiledeskChatbotConst.REQ_LEAD_EMAIL_KEY, message.request.lead.email);
|
|
600
600
|
}
|
|
601
|
-
|
|
602
|
-
if (message.request.lead.fullname) {
|
|
601
|
+
if (message.request.lead.fullname && !message.request.lead.fullname.startsWith("guest#")) {
|
|
602
|
+
// if (message.request.lead.fullname) {
|
|
603
603
|
// worth saving
|
|
604
604
|
console.log("worth saving. lead found. lead.email:", message.request.lead.email, "lead.fullname:", message.request.lead.fullname)
|
|
605
605
|
try {
|
|
@@ -608,28 +608,29 @@ class TiledeskChatbotUtil {
|
|
|
608
608
|
catch(error) {
|
|
609
609
|
console.error("Error on setting userFullname:", error);
|
|
610
610
|
}
|
|
611
|
-
}
|
|
612
|
-
else {
|
|
613
|
-
|
|
611
|
+
// }
|
|
612
|
+
// else {
|
|
613
|
+
// // console.log("!lead.fullname");
|
|
614
|
+
// }
|
|
614
615
|
}
|
|
615
616
|
// console.log("Getting userPhone:", JSON.stringify(message.request));
|
|
616
617
|
if (message.request.lead.phone) {
|
|
617
|
-
await chatbot.addParameter(
|
|
618
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_PHONE_KEY, message.request.lead.phone);
|
|
618
619
|
}
|
|
619
620
|
if (message.request.lead.lead_id && message.request.lead.lead_id.startsWith("wab-")) {
|
|
620
621
|
const splits = message.request.lead.lead_id.split("-");
|
|
621
622
|
if (splits && splits.length > 1) {
|
|
622
|
-
await chatbot.addParameter(
|
|
623
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_CURRENT_PHONE_NUMBER_KEY,splits[1]);
|
|
623
624
|
}
|
|
624
625
|
}
|
|
625
626
|
if (message.request.lead._id) {
|
|
626
|
-
await chatbot.addParameter(
|
|
627
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_LEAD_ID_KEY, message.request.lead._id);
|
|
627
628
|
}
|
|
628
629
|
if (message.request.lead.company) {
|
|
629
|
-
await chatbot.addParameter(
|
|
630
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_COMPANY_KEY, message.request.lead.company);
|
|
630
631
|
}
|
|
631
632
|
if (message.request.ticket_id) {
|
|
632
|
-
await chatbot.addParameter(
|
|
633
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_TICKET_ID_KEY, message.request.ticket_id);
|
|
633
634
|
}
|
|
634
635
|
}
|
|
635
636
|
|
package/package.json
CHANGED
|
@@ -49,6 +49,7 @@ const { DirAskGPTV2 } = require('./directives/DirAskGPTV2');
|
|
|
49
49
|
const { DirAssistant } = require('./directives/DirAssistant');
|
|
50
50
|
const { DirReplyV2 } = require('./directives/DirReplyV2');
|
|
51
51
|
const { DirIfOnlineAgentsV2 } = require('./directives/DirIfOnlineAgentsV2');
|
|
52
|
+
const { DirContactUpdate } = require('./directives/DirContactUpdate');
|
|
52
53
|
|
|
53
54
|
class DirectivesChatbotPlug {
|
|
54
55
|
|
|
@@ -239,10 +240,6 @@ class DirectivesChatbotPlug {
|
|
|
239
240
|
// go on
|
|
240
241
|
// console.log("Going on to next directive...");
|
|
241
242
|
}
|
|
242
|
-
// }
|
|
243
|
-
// catch(error) {
|
|
244
|
-
// console.error("Error on locks:", error);
|
|
245
|
-
// }
|
|
246
243
|
|
|
247
244
|
}
|
|
248
245
|
if (directive == null || (directive !== null && directive["name"] === undefined)) {
|
|
@@ -272,8 +269,6 @@ class DirectivesChatbotPlug {
|
|
|
272
269
|
let next_dir = await this.nextDirective(this.directives);
|
|
273
270
|
this.process(next_dir);
|
|
274
271
|
}
|
|
275
|
-
// let next_dir = await this.nextDirective(this.directives);
|
|
276
|
-
// this.process(next_dir);
|
|
277
272
|
});
|
|
278
273
|
}
|
|
279
274
|
else if (directive_name === Directives.MESSAGE) {
|
|
@@ -360,8 +355,6 @@ class DirectivesChatbotPlug {
|
|
|
360
355
|
let next_dir = await this.nextDirective(this.directives);
|
|
361
356
|
this.process(next_dir);
|
|
362
357
|
}
|
|
363
|
-
// let next_dir = await this.nextDirective(this.directives);
|
|
364
|
-
// this.process(next_dir);
|
|
365
358
|
});
|
|
366
359
|
}
|
|
367
360
|
else if (directive_name === Directives.IF_ONLINE_AGENTS) {
|
|
@@ -448,57 +441,6 @@ class DirectivesChatbotPlug {
|
|
|
448
441
|
this.process(next_dir);
|
|
449
442
|
});
|
|
450
443
|
}
|
|
451
|
-
// else if (directive_name === Directives.WHEN_OPEN) {
|
|
452
|
-
// // DEPRECATED
|
|
453
|
-
// const whenOpenDir = new DirWhenOpen(
|
|
454
|
-
// {
|
|
455
|
-
// tdclient: this.context.tdclient, // matches open hours
|
|
456
|
-
// log: false
|
|
457
|
-
// });
|
|
458
|
-
// whenOpenDir.execute(directive, directives, curr_directive_index, async () => {
|
|
459
|
-
// let next_dir = await this.nextDirective(this.directives);
|
|
460
|
-
// this.process(next_dir);
|
|
461
|
-
// });
|
|
462
|
-
// }
|
|
463
|
-
// else if (directive_name === Directives.WHEN_CLOSED) {
|
|
464
|
-
// // DEPRECATED
|
|
465
|
-
// const whenOpenDir = new DirWhenOpen(
|
|
466
|
-
// {
|
|
467
|
-
// tdclient: this.context.tdclient,
|
|
468
|
-
// checkOpen: false, // matches closed hours
|
|
469
|
-
// log: false
|
|
470
|
-
// });
|
|
471
|
-
// whenOpenDir.execute(directive, directives, curr_directive_index, async () => {
|
|
472
|
-
// let next_dir = await this.nextDirective(this.directives);
|
|
473
|
-
// this.process(next_dir);
|
|
474
|
-
// });
|
|
475
|
-
// }
|
|
476
|
-
// else if (directive_name === Directives.IF_AGENTS) {
|
|
477
|
-
// // DEPRECATED
|
|
478
|
-
// const ifNoAgentsDir = new DirIfAvailableAgents(
|
|
479
|
-
// {
|
|
480
|
-
// tdclient: this.context.tdclient,
|
|
481
|
-
// checkAgents: true, // check available agents > 0
|
|
482
|
-
// log: false
|
|
483
|
-
// });
|
|
484
|
-
// ifNoAgentsDir.execute(directive, directives, curr_directive_index, async () => {
|
|
485
|
-
// let next_dir = await this.nextDirective(this.directives);
|
|
486
|
-
// this.process(next_dir);
|
|
487
|
-
// });
|
|
488
|
-
// }
|
|
489
|
-
// else if (directive_name === Directives.IF_NO_AGENTS) {
|
|
490
|
-
// // DEPRECATED
|
|
491
|
-
// const ifNoAgentsDir = new DirIfAvailableAgents(
|
|
492
|
-
// {
|
|
493
|
-
// tdclient: this.context.tdclient,
|
|
494
|
-
// checkAgents: false, // check no available agents
|
|
495
|
-
// log: false
|
|
496
|
-
// });
|
|
497
|
-
// ifNoAgentsDir.execute(directive, directives, curr_directive_index, async () => {
|
|
498
|
-
// let next_dir = await this.nextDirective(this.directives);
|
|
499
|
-
// this.process(next_dir);
|
|
500
|
-
// });
|
|
501
|
-
// }
|
|
502
444
|
else if (directive_name === Directives.AGENT) {
|
|
503
445
|
// console.log("...DirMoveToAgent");
|
|
504
446
|
new DirMoveToAgent(context).execute(directive, async () => {
|
|
@@ -506,35 +448,6 @@ class DirectivesChatbotPlug {
|
|
|
506
448
|
this.process(next_dir);
|
|
507
449
|
});
|
|
508
450
|
}
|
|
509
|
-
// else if (directive_name === Directives.WHEN_ONLINE_MOVE_TO_AGENT) { // DEPRECATED?
|
|
510
|
-
// // let depId;
|
|
511
|
-
// // if (context.supportRequest && context.supportRequest.department && context.supportRequest.department._id) {
|
|
512
|
-
// // depId = context.supportRequest.department._id;
|
|
513
|
-
// // console.log("context.supportRequest", JSON.stringify(context.supportRequest));
|
|
514
|
-
// // const agentDir = new DirMoveToAgent(
|
|
515
|
-
// // {
|
|
516
|
-
// // tdclient: context.tdclient,
|
|
517
|
-
// // requestId: context.requestId,
|
|
518
|
-
// // depId: depId
|
|
519
|
-
// // }
|
|
520
|
-
// // );
|
|
521
|
-
// // if (!directive.action) {
|
|
522
|
-
// // directive.action = {}
|
|
523
|
-
// // directive.action = {
|
|
524
|
-
// // whenOnlineOnly: true
|
|
525
|
-
// // }
|
|
526
|
-
// // }
|
|
527
|
-
// new DirMoveToAgent(context).execute(directive, async () => {
|
|
528
|
-
// let next_dir = await this.nextDirective(this.directives);
|
|
529
|
-
// this.process(next_dir);
|
|
530
|
-
// });
|
|
531
|
-
// // }
|
|
532
|
-
// // else {
|
|
533
|
-
// // console.log("Warning. DepId null while calling 'WHEN_ONLINE_MOVE_TO_AGENT' directive")
|
|
534
|
-
// // let next_dir = await this.nextDirective(this.directives);
|
|
535
|
-
// // this.process(next_dir);
|
|
536
|
-
// // }
|
|
537
|
-
// }
|
|
538
451
|
else if (directive_name === Directives.CLOSE) {
|
|
539
452
|
// console.log("Exec close()")
|
|
540
453
|
new DirClose(context).execute(directive, async () => {
|
|
@@ -770,6 +683,13 @@ class DirectivesChatbotPlug {
|
|
|
770
683
|
}
|
|
771
684
|
});
|
|
772
685
|
}
|
|
686
|
+
else if (directive_name === Directives.CONTACT_UPDATE) {
|
|
687
|
+
console.log("...CONTACT_UPDATE");
|
|
688
|
+
new DirContactUpdate(context).execute(directive, async () => {
|
|
689
|
+
let next_dir = await this.nextDirective(this.directives);
|
|
690
|
+
this.process(next_dir);
|
|
691
|
+
});
|
|
692
|
+
}
|
|
773
693
|
else {
|
|
774
694
|
//console.log("Unhandled Post-message Directive:", directive_name);
|
|
775
695
|
let next_dir = await this.nextDirective(this.directives);
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
const { Filler } = require('../Filler');
|
|
2
|
+
const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
|
|
3
|
+
const { TiledeskChatbotUtil } = require('../../models/TiledeskChatbotUtil');
|
|
4
|
+
let axios = require('axios');
|
|
5
|
+
const { TiledeskChatbotConst } = require('../../models/TiledeskChatbotConst');
|
|
6
|
+
|
|
7
|
+
class DirContactUpdate {
|
|
8
|
+
|
|
9
|
+
constructor(context) {
|
|
10
|
+
if (!context) {
|
|
11
|
+
throw new Error('context object is mandatory.');
|
|
12
|
+
}
|
|
13
|
+
this.context = context;
|
|
14
|
+
this.projectId = context.projectId;
|
|
15
|
+
this.requestId = context.requestId;
|
|
16
|
+
this.supportRequest = context.supportRequest;
|
|
17
|
+
this.token = context.token;
|
|
18
|
+
this.tdcache = context.tdcache;
|
|
19
|
+
this.tdclient = context.tdclient;
|
|
20
|
+
this.log = context.log;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
execute(directive, callback) {
|
|
24
|
+
let action;
|
|
25
|
+
if (directive.action) {
|
|
26
|
+
action = directive.action;
|
|
27
|
+
if (!action.attributes) {
|
|
28
|
+
action.attributes = {}
|
|
29
|
+
}
|
|
30
|
+
action.attributes.fillParams = true;
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
console.error("Incorrect directive (no action provided):", directive);
|
|
34
|
+
callback();
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
this.go(action, () => {
|
|
38
|
+
callback();
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async go(action, callback) {
|
|
43
|
+
if (this.log) {console.log("(DirContactUpdate) start. Update properties:", action.update); }
|
|
44
|
+
const contactProperties = action.update;
|
|
45
|
+
|
|
46
|
+
// fill
|
|
47
|
+
let requestAttributes = null;
|
|
48
|
+
if (this.tdcache) {
|
|
49
|
+
requestAttributes =
|
|
50
|
+
await TiledeskChatbot.allParametersStatic(
|
|
51
|
+
this.tdcache, this.requestId
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
const filler = new Filler();
|
|
55
|
+
let updateProperties = {}
|
|
56
|
+
for (const [key, value] of Object.entries(contactProperties)) {
|
|
57
|
+
updateProperties[key] = filler.fill(value, requestAttributes);
|
|
58
|
+
if (this.log) {console.log("(DirContactUpdate) updating property:", key, "value:", value); }
|
|
59
|
+
}
|
|
60
|
+
const leadId = requestAttributes[TiledeskChatbotConst.REQ_USER_LEAD_ID_KEY];
|
|
61
|
+
this.tdclient.updateLead(leadId, updateProperties, null, null, () => {
|
|
62
|
+
if (this.log) {console.log("(DirContactUpdate) Lead updated.", updateProperties);}
|
|
63
|
+
// send hidden info to update widget lead fullname only if it is a conversation!
|
|
64
|
+
if (this.log) {console.log("(DirContactUpdate) requestId:", this.requestId); }
|
|
65
|
+
if (this.log) {console.log("(DirContactUpdate) updateProperties:", updateProperties); }
|
|
66
|
+
if (this.log) {console.log("(DirContactUpdate) updateProperties['fullname']:", updateProperties['fullname']); }
|
|
67
|
+
callback();
|
|
68
|
+
// if (this.requestId.startsWith("support-group") && updateProperties['userFullname']) {
|
|
69
|
+
// if (this.log) {console.log("(DirContactUpdate) send hidden info to update widget lead fullname"); }
|
|
70
|
+
// const userFullname = updateProperties['fullname'];
|
|
71
|
+
// const updateLeadDataOnWidgetMessage = {
|
|
72
|
+
// type: "text",
|
|
73
|
+
// text: "Updated lead fullname on widget with: " + userFullname,
|
|
74
|
+
// attributes: {
|
|
75
|
+
// // subtype: "info",
|
|
76
|
+
// updateUserFullname: userFullname
|
|
77
|
+
// }
|
|
78
|
+
// };
|
|
79
|
+
// if (this.log) {console.log("(DirContactUpdate) sending updateLeadDataOnWidgetMessage:", updateLeadDataOnWidgetMessage); }
|
|
80
|
+
// this.context.tdclient.sendSupportMessage(
|
|
81
|
+
// this.requestId,
|
|
82
|
+
// updateLeadDataOnWidgetMessage,
|
|
83
|
+
// (err) => {
|
|
84
|
+
// if (err) {
|
|
85
|
+
// console.error("(DirContactUpdate) Error sending reply:", err);
|
|
86
|
+
// }
|
|
87
|
+
// if (this.log) {console.log("(DirContactUpdate) hidden message sent:", updateLeadDataOnWidgetMessage);}
|
|
88
|
+
// callback();
|
|
89
|
+
// });
|
|
90
|
+
// }
|
|
91
|
+
// else {
|
|
92
|
+
// callback();
|
|
93
|
+
// }
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
module.exports = { DirContactUpdate };
|
|
@@ -52,8 +52,24 @@ class DirIfOnlineAgentsV2 {
|
|
|
52
52
|
let stopOnConditionMet = true; //action.stopOnConditionMet;
|
|
53
53
|
|
|
54
54
|
try {
|
|
55
|
-
const
|
|
56
|
-
|
|
55
|
+
const ignoreProjectWideOperatingHours = action.ignoreOperatingHours;
|
|
56
|
+
let isOpen = false;
|
|
57
|
+
if (ignoreProjectWideOperatingHours === true) {
|
|
58
|
+
// always go on to only check agents availability
|
|
59
|
+
isOpen = true;
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
const result = await this.openNow();
|
|
63
|
+
if (result && result.isopen) {
|
|
64
|
+
isOpen = true;
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
isOpen = false;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// if (result && result.isopen) {
|
|
72
|
+
if (isOpen === true) { // always true if ignoreProjectWideOperatingHours = true
|
|
57
73
|
const selectedOption = action.selectedOption;
|
|
58
74
|
|
|
59
75
|
let agents;
|
|
@@ -102,11 +118,10 @@ class DirIfOnlineAgentsV2 {
|
|
|
102
118
|
}
|
|
103
119
|
else { // if (checkAll) => go project-wide
|
|
104
120
|
if (this.log) {console.log("(DirIfOnlineAgents) selectedOption === all"); }
|
|
105
|
-
agents = await this.getProjectAvailableAgents();
|
|
121
|
+
agents = await this.getProjectAvailableAgents(true);
|
|
106
122
|
if (this.log) {console.log("(DirIfOnlineAgents) agents:", agents); }
|
|
107
123
|
}
|
|
108
124
|
|
|
109
|
-
console.log("anyway qui...");
|
|
110
125
|
if (agents && agents.length > 0) {
|
|
111
126
|
if (trueIntent) {
|
|
112
127
|
let intentDirective = DirIntent.intentDirectiveFor(trueIntent, trueIntentAttributes);
|
|
@@ -134,6 +149,18 @@ class DirIfOnlineAgentsV2 {
|
|
|
134
149
|
this.chatbot.addParameter("flowError", "(If online Agents) No path for 'no available agents' defined.");
|
|
135
150
|
callback();
|
|
136
151
|
}
|
|
152
|
+
} else {
|
|
153
|
+
if (falseIntent) {
|
|
154
|
+
let intentDirective = DirIntent.intentDirectiveFor(falseIntent, falseIntentAttributes);
|
|
155
|
+
if (this.log) {console.log("!agents (!openHours) => falseIntent");}
|
|
156
|
+
console.log("!agents (!openHours) => falseIntent BECAUSE CLOSED"); //PROD
|
|
157
|
+
this.intentDir.execute(intentDirective, () => {
|
|
158
|
+
callback();
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
callback();
|
|
163
|
+
}
|
|
137
164
|
}
|
|
138
165
|
}
|
|
139
166
|
catch(err) {
|
|
@@ -157,19 +184,51 @@ class DirIfOnlineAgentsV2 {
|
|
|
157
184
|
});
|
|
158
185
|
}
|
|
159
186
|
|
|
160
|
-
async getProjectAvailableAgents() {
|
|
187
|
+
async getProjectAvailableAgents(raw, callback) {
|
|
161
188
|
return new Promise( (resolve, reject) => {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
|
|
189
|
+
const URL = `${this.context.TILEDESK_APIURL}/projects/${this.context.projectId}/users/availables?raw=${raw}`
|
|
190
|
+
const HTTPREQUEST = {
|
|
191
|
+
url: URL,
|
|
192
|
+
headers: {
|
|
193
|
+
'Content-Type' : 'application/json',
|
|
194
|
+
'Authorization': this.fixToken(this.context.token)
|
|
195
|
+
},
|
|
196
|
+
// json: true,
|
|
197
|
+
method: 'GET',
|
|
198
|
+
};
|
|
199
|
+
this.#myrequest(
|
|
200
|
+
HTTPREQUEST,
|
|
201
|
+
function(err, resbody) {
|
|
202
|
+
if (err) {
|
|
203
|
+
if (callback) {
|
|
204
|
+
callback(err);
|
|
205
|
+
}
|
|
206
|
+
reject(err);
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
if (callback) {
|
|
210
|
+
callback(null, resbody);
|
|
211
|
+
}
|
|
212
|
+
resolve(resbody);
|
|
213
|
+
}
|
|
214
|
+
}, this.log);
|
|
215
|
+
});
|
|
216
|
+
|
|
171
217
|
}
|
|
172
218
|
|
|
219
|
+
// async getProjectAvailableAgents() {
|
|
220
|
+
// return new Promise( (resolve, reject) => {
|
|
221
|
+
// this.tdclient.getProjectAvailableAgents((err, agents) => {
|
|
222
|
+
// if (err) {
|
|
223
|
+
// reject(err);
|
|
224
|
+
// }
|
|
225
|
+
// else {
|
|
226
|
+
// resolve(agents);
|
|
227
|
+
// }
|
|
228
|
+
// });
|
|
229
|
+
// })
|
|
230
|
+
// }
|
|
231
|
+
|
|
173
232
|
async getDepartmentAvailableAgents(depId) {
|
|
174
233
|
return new Promise( (resolve, reject) => {
|
|
175
234
|
this.tdclient.getDepartment(depId, async (error, dep) => {
|
|
@@ -237,7 +296,7 @@ class DirIfOnlineAgentsV2 {
|
|
|
237
296
|
}
|
|
238
297
|
else {
|
|
239
298
|
// no group => assigned to all teammates
|
|
240
|
-
const agents = await this.getProjectAvailableAgents();
|
|
299
|
+
const agents = await this.getProjectAvailableAgents(true);
|
|
241
300
|
resolve(agents);
|
|
242
301
|
}
|
|
243
302
|
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// const { TiledeskClient } = require('@tiledesk/tiledesk-client');
|
|
2
|
+
let axios = require('axios');
|
|
2
3
|
const { DirIntent } = require('./DirIntent');
|
|
4
|
+
let https = require("https");
|
|
3
5
|
const ms = require('minimist-string');
|
|
4
6
|
|
|
5
7
|
class DirIfOpenHours {
|
|
@@ -9,6 +11,7 @@ class DirIfOpenHours {
|
|
|
9
11
|
throw new Error('context object is mandatory.');
|
|
10
12
|
}
|
|
11
13
|
this.tdclient = context.tdclient;
|
|
14
|
+
this.context = context;
|
|
12
15
|
// this.tdclient = new TiledeskClient({
|
|
13
16
|
// projectId: context.projectId,
|
|
14
17
|
// token: context.token,
|
|
@@ -34,7 +37,8 @@ class DirIfOpenHours {
|
|
|
34
37
|
// log: context.log
|
|
35
38
|
// }
|
|
36
39
|
// );
|
|
37
|
-
this.log = context.log;
|
|
40
|
+
//this.log = context.log;
|
|
41
|
+
this.log = true;
|
|
38
42
|
}
|
|
39
43
|
|
|
40
44
|
execute(directive, callback) {
|
|
@@ -67,11 +71,13 @@ class DirIfOpenHours {
|
|
|
67
71
|
}
|
|
68
72
|
|
|
69
73
|
go(action, callback) {
|
|
74
|
+
|
|
70
75
|
const trueIntent = action.trueIntent;
|
|
71
76
|
const falseIntent = action.falseIntent;
|
|
72
77
|
const trueIntentAttributes = action.trueIntentAttributes;
|
|
73
78
|
const falseIntentAttributes = action.falseIntentAttributes;
|
|
74
79
|
const stopOnConditionMet = action.stopOnConditionMet;
|
|
80
|
+
|
|
75
81
|
if (trueIntent && trueIntent.trim() === "") {
|
|
76
82
|
trueIntent = null;
|
|
77
83
|
}
|
|
@@ -84,36 +90,155 @@ class DirIfOpenHours {
|
|
|
84
90
|
callback();
|
|
85
91
|
return;
|
|
86
92
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
93
|
+
|
|
94
|
+
let slot_id = null;
|
|
95
|
+
if (action.slotId) {
|
|
96
|
+
slot_id = action.slotId;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const server_base_url = process.env.API_ENDPOINT || process.env.API_URL;
|
|
100
|
+
if (this.log) { console.log("DirAskGPT ApiEndpoint URL: ", server_base_url); }
|
|
101
|
+
|
|
102
|
+
let isopen_url = server_base_url + "/projects/" + this.context.projectId + "/isopen";
|
|
103
|
+
if (slot_id) {
|
|
104
|
+
isopen_url = isopen_url.concat("?timeSlot=" + slot_id);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const HTTPREQUEST = {
|
|
108
|
+
url: isopen_url,
|
|
109
|
+
headers: {
|
|
110
|
+
'Content-Type': 'application/json',
|
|
111
|
+
'Authorization': 'JWT ' + this.context.token
|
|
112
|
+
},
|
|
113
|
+
method: 'GET'
|
|
114
|
+
}
|
|
115
|
+
if (this.log) { console.log("DirIfOpenHours HTTPREQUEST", HTTPREQUEST); }
|
|
116
|
+
|
|
117
|
+
this.#myrequest(
|
|
118
|
+
HTTPREQUEST, async (err, resbody) => {
|
|
119
|
+
if (this.log && err) {
|
|
120
|
+
console.log("DirIfOpenHours error: ", err);
|
|
121
|
+
}
|
|
122
|
+
if (this.log) { console.log("DirIfOpenHours resbody:", resbody); }
|
|
123
|
+
|
|
124
|
+
if (err) {
|
|
125
|
+
if (callback) {
|
|
126
|
+
if (falseIntent) {
|
|
127
|
+
let intentDirective = DirIntent.intentDirectiveFor(falseIntent);
|
|
128
|
+
if (this.log) {console.log("!agents (openHours) => falseIntent", falseIntent);}
|
|
129
|
+
this.intentDir.execute(intentDirective, () => {
|
|
130
|
+
callback(stopOnConditionMet);
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
} else {
|
|
135
|
+
if (resbody.isopen && resbody.isopen === true) {
|
|
136
|
+
if (trueIntent) {
|
|
137
|
+
let intentDirective = DirIntent.intentDirectiveFor(trueIntent);
|
|
138
|
+
if (this.log) {console.log("agents (openHours) => trueIntent");}
|
|
139
|
+
this.intentDir.execute(intentDirective, () => {
|
|
140
|
+
callback(stopOnConditionMet);
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
callback();
|
|
144
|
+
return;
|
|
145
|
+
} else {
|
|
146
|
+
if (falseIntent) {
|
|
147
|
+
let intentDirective = DirIntent.intentDirectiveFor(falseIntent);
|
|
148
|
+
if (this.log) {console.log("!agents (openHours) => falseIntent", falseIntent);}
|
|
149
|
+
this.intentDir.execute(intentDirective, () => {
|
|
150
|
+
callback(stopOnConditionMet);
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
callback();
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
92
157
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
// this.tdclient.openNow(action.slot_id, (err, result) => {
|
|
161
|
+
// console.log("openNow():", result);
|
|
162
|
+
// if (this.log) {console.log("openNow():", result);}
|
|
163
|
+
// if (err) {
|
|
164
|
+
// console.error("*** DirIfOpenHours Error:", err);
|
|
165
|
+
// callback();
|
|
166
|
+
// }
|
|
167
|
+
// else if (result && result.isopen) {
|
|
168
|
+
// console.log("yes is open")
|
|
169
|
+
// if (trueIntent) {
|
|
170
|
+
// let intentDirective = DirIntent.intentDirectiveFor(trueIntent);
|
|
171
|
+
// if (this.log) {console.log("agents (openHours) => trueIntent");}
|
|
172
|
+
// this.intentDir.execute(intentDirective, () => {
|
|
173
|
+
// callback(stopOnConditionMet);
|
|
174
|
+
// });
|
|
175
|
+
// }
|
|
176
|
+
// else {
|
|
177
|
+
// callback();
|
|
178
|
+
// return;
|
|
179
|
+
// }
|
|
180
|
+
// }
|
|
181
|
+
// else if (falseIntent) {
|
|
182
|
+
// let intentDirective = DirIntent.intentDirectiveFor(falseIntent);
|
|
183
|
+
// if (this.log) {console.log("!agents (openHours) => falseIntent", falseIntent);}
|
|
184
|
+
// this.intentDir.execute(intentDirective, () => {
|
|
185
|
+
// callback(stopOnConditionMet);
|
|
186
|
+
// });
|
|
187
|
+
// }
|
|
188
|
+
// else {
|
|
189
|
+
// callback();
|
|
190
|
+
// }
|
|
191
|
+
// });
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
#myrequest(options, callback) {
|
|
195
|
+
if (this.log) {
|
|
196
|
+
console.log("API URL:", options.url);
|
|
197
|
+
console.log("** Options:", JSON.stringify(options));
|
|
198
|
+
}
|
|
199
|
+
let axios_options = {
|
|
200
|
+
url: options.url,
|
|
201
|
+
method: options.method,
|
|
202
|
+
params: options.params,
|
|
203
|
+
headers: options.headers
|
|
204
|
+
}
|
|
205
|
+
if (options.json !== null) {
|
|
206
|
+
axios_options.data = options.json
|
|
207
|
+
}
|
|
208
|
+
if (this.log) {
|
|
209
|
+
console.log("axios_options:", JSON.stringify(axios_options));
|
|
210
|
+
}
|
|
211
|
+
if (options.url.startsWith("https:")) {
|
|
212
|
+
const httpsAgent = new https.Agent({
|
|
213
|
+
rejectUnauthorized: false,
|
|
214
|
+
});
|
|
215
|
+
axios_options.httpsAgent = httpsAgent;
|
|
216
|
+
}
|
|
217
|
+
axios(axios_options)
|
|
218
|
+
.then((res) => {
|
|
219
|
+
console.log("siamo quiii")
|
|
220
|
+
if (this.log) {
|
|
221
|
+
console.log("Response for url:", options.url);
|
|
222
|
+
console.log("Response headers:\n", JSON.stringify(res.headers));
|
|
223
|
+
}
|
|
224
|
+
if (res && res.status == 200 && res.data) {
|
|
225
|
+
if (callback) {
|
|
226
|
+
callback(null, res.data);
|
|
227
|
+
}
|
|
100
228
|
}
|
|
101
229
|
else {
|
|
102
|
-
callback
|
|
103
|
-
|
|
230
|
+
if (callback) {
|
|
231
|
+
callback(new Error("Response status is not 200"), null);
|
|
232
|
+
}
|
|
104
233
|
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
callback(
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
else {
|
|
114
|
-
callback();
|
|
115
|
-
}
|
|
116
|
-
});
|
|
234
|
+
})
|
|
235
|
+
.catch((error) => {
|
|
236
|
+
console.log("siamo quiii")
|
|
237
|
+
// console.error("An error occurred:", JSON.stringify(error.data));
|
|
238
|
+
if (callback) {
|
|
239
|
+
callback(error, null);
|
|
240
|
+
}
|
|
241
|
+
});
|
|
117
242
|
}
|
|
118
243
|
|
|
119
244
|
parseParams(directive_parameter) {
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
// const { TiledeskClient } = require('@tiledesk/tiledesk-client');
|
|
2
|
+
const { DirIntent } = require('./DirIntent');
|
|
3
|
+
const ms = require('minimist-string');
|
|
4
|
+
|
|
5
|
+
class DirIfOpenHours {
|
|
6
|
+
|
|
7
|
+
constructor(context) {
|
|
8
|
+
if (!context) {
|
|
9
|
+
throw new Error('context object is mandatory.');
|
|
10
|
+
}
|
|
11
|
+
this.tdclient = context.tdclient;
|
|
12
|
+
// this.tdclient = new TiledeskClient({
|
|
13
|
+
// projectId: context.projectId,
|
|
14
|
+
// token: context.token,
|
|
15
|
+
// APIURL: context.TILEDESK_APIURL,
|
|
16
|
+
// APIKEY: "___",
|
|
17
|
+
// log: context.log
|
|
18
|
+
// });
|
|
19
|
+
// let context = {
|
|
20
|
+
// projectId: projectId,
|
|
21
|
+
// token: token,
|
|
22
|
+
// requestId: supportRequest,
|
|
23
|
+
// APIURL: API_URL,
|
|
24
|
+
// TILEBOT_ENDPOINT:TILEBOT_ENDPOINT,
|
|
25
|
+
// departmentId: depId,
|
|
26
|
+
// log: false
|
|
27
|
+
// }
|
|
28
|
+
this.intentDir = new DirIntent(context);
|
|
29
|
+
// {
|
|
30
|
+
// API_ENDPOINT: context.TILEDESK_APIURL,
|
|
31
|
+
// TILEBOT_ENDPOINT: context.TILEBOT_ENDPOINT,
|
|
32
|
+
// supportRequest: context.supportRequest,
|
|
33
|
+
// token: context.token,
|
|
34
|
+
// log: context.log
|
|
35
|
+
// }
|
|
36
|
+
// );
|
|
37
|
+
this.log = context.log;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
execute(directive, callback) {
|
|
41
|
+
let action;
|
|
42
|
+
if (directive.action) {
|
|
43
|
+
action = directive.action
|
|
44
|
+
}
|
|
45
|
+
else if (directive.parameter) {
|
|
46
|
+
let params;
|
|
47
|
+
params = this.parseParams(directive.parameter);
|
|
48
|
+
if (!params.trueIntent && !params.falseIntent) {
|
|
49
|
+
if (this.log) {
|
|
50
|
+
console.log("missing both params.trueIntent & params.falseIntent");
|
|
51
|
+
}
|
|
52
|
+
callback();
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
action = {
|
|
56
|
+
trueIntent: params.trueIntent,
|
|
57
|
+
falseIntent: params.falseIntent
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
callback();
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
this.go(action, (stop) => {
|
|
65
|
+
callback(stop);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
go(action, callback) {
|
|
70
|
+
|
|
71
|
+
const trueIntent = action.trueIntent;
|
|
72
|
+
const falseIntent = action.falseIntent;
|
|
73
|
+
const trueIntentAttributes = action.trueIntentAttributes;
|
|
74
|
+
const falseIntentAttributes = action.falseIntentAttributes;
|
|
75
|
+
const stopOnConditionMet = action.stopOnConditionMet;
|
|
76
|
+
|
|
77
|
+
if (trueIntent && trueIntent.trim() === "") {
|
|
78
|
+
trueIntent = null;
|
|
79
|
+
}
|
|
80
|
+
if (falseIntent && falseIntent.trim() === "") {
|
|
81
|
+
falseIntent = null;
|
|
82
|
+
}
|
|
83
|
+
if (this.log) {console.log("condition action:", action);}
|
|
84
|
+
if (!trueIntent && !falseIntent) {
|
|
85
|
+
if (this.log) {console.log("Invalid condition, no intents specified");}
|
|
86
|
+
callback();
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
this.tdclient.openNow((err, result) => {
|
|
90
|
+
if (this.log) {console.log("openNow():", result);}
|
|
91
|
+
if (err) {
|
|
92
|
+
console.error("*** DirIfOpenHours Error:", err);
|
|
93
|
+
callback();
|
|
94
|
+
}
|
|
95
|
+
else if (result && result.isopen) {
|
|
96
|
+
if (trueIntent) {
|
|
97
|
+
let intentDirective = DirIntent.intentDirectiveFor(trueIntent);
|
|
98
|
+
if (this.log) {console.log("DirIfOpenHours (openHours) => trueIntent");}
|
|
99
|
+
this.intentDir.execute(intentDirective, () => {
|
|
100
|
+
callback(stopOnConditionMet);
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
callback();
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
else if (falseIntent) {
|
|
109
|
+
let intentDirective = DirIntent.intentDirectiveFor(falseIntent);
|
|
110
|
+
if (this.log) {console.log("DirIfOpenHours (openHours) => falseIntent", falseIntent);}
|
|
111
|
+
this.intentDir.execute(intentDirective, () => {
|
|
112
|
+
callback(stopOnConditionMet);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
callback();
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
parseParams(directive_parameter) {
|
|
122
|
+
let trueIntent = null;
|
|
123
|
+
let falseIntent = null;
|
|
124
|
+
const params = ms(directive_parameter);
|
|
125
|
+
if (params.trueIntent) {
|
|
126
|
+
trueIntent = params.trueIntent;
|
|
127
|
+
}
|
|
128
|
+
if (params.falseIntent) {
|
|
129
|
+
falseIntent = params.falseIntent;
|
|
130
|
+
}
|
|
131
|
+
return {
|
|
132
|
+
trueIntent: trueIntent,
|
|
133
|
+
falseIntent: falseIntent
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
module.exports = { DirIfOpenHours };
|
|
@@ -49,6 +49,7 @@ class Directives {
|
|
|
49
49
|
static GPT_ASSISTANT = 'gpt_assistant';
|
|
50
50
|
static REPLY_V2 = 'replyv2';
|
|
51
51
|
static IF_ONLINE_AGENTS_V2 = "ifonlineagentsv2";
|
|
52
|
+
static CONTACT_UPDATE = "leadupdate";
|
|
52
53
|
|
|
53
54
|
// static WHEN_ONLINE_MOVE_TO_AGENT = "whenonlinemovetoagent"; // DEPRECATED?
|
|
54
55
|
// static WHEN_OFFLINE_HOURS = "whenofflinehours"; // DEPRECATED // adds a message on top of the original message when offline hours opts: --replace
|