@tiledesk/tiledesk-tybot-connector 0.2.21 → 0.2.22
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 +3 -0
- package/index.js +1 -1
- package/models/MockTdCache.js +9 -0
- package/models/TiledeskChatbot.js +46 -43
- package/models/TiledeskChatbotUtil.js +4 -4
- package/package.json +1 -1
- package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +4 -4
- package/tiledeskChatbotPlugs/directives/DirAskGPT.js +4 -4
- package/tiledeskChatbotPlugs/directives/DirCaptureUserReply.js +12 -12
- package/tiledeskChatbotPlugs/directives/DirGptTask.js +1 -1
- package/tiledeskChatbotPlugs/directives/DirIfOnlineAgents.js +11 -3
- package/tiledeskChatbotPlugs/directives/DirReply.js +3 -3
package/CHANGELOG.md
CHANGED
package/index.js
CHANGED
|
@@ -10,7 +10,7 @@ const { MongodbBotsDataSource } = require('./models/MongodbBotsDataSource.js');
|
|
|
10
10
|
// const { MongodbIntentsMachine } = require('./models/MongodbIntentsMachine.js');
|
|
11
11
|
// const { TiledeskIntentsMachine } = require('./models/TiledeskIntentsMachine.js');
|
|
12
12
|
const { MockBotsDataSource } = require('./models/MockBotsDataSource.js');
|
|
13
|
-
|
|
13
|
+
const { TiledeskChatbotConst } = require('./models/TiledeskChatbotConst');
|
|
14
14
|
const { IntentsMachineFactory } = require('./models/IntentsMachineFactory');
|
|
15
15
|
// let parser = require('accept-language-parser');
|
|
16
16
|
|
package/models/MockTdCache.js
CHANGED
|
@@ -51,6 +51,15 @@ class MockTdCache {
|
|
|
51
51
|
resolve();
|
|
52
52
|
});
|
|
53
53
|
}
|
|
54
|
+
|
|
55
|
+
async setJSON(key, value, options) {
|
|
56
|
+
return new Promise( (resolve, reject) => {
|
|
57
|
+
const _string = JSON.stringify(value);
|
|
58
|
+
this.set(key, _string);
|
|
59
|
+
resolve();
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
54
63
|
}
|
|
55
64
|
|
|
56
65
|
module.exports = { MockTdCache };
|
|
@@ -80,27 +80,31 @@ class TiledeskChatbot {
|
|
|
80
80
|
if (this.log) {console.log("RESETTING LOCKED INTENT. Intent was explicitly invoked with an action:", message.attributes.action);}
|
|
81
81
|
await this.unlockIntent(this.requestId);
|
|
82
82
|
await this.unlockAction(this.requestId);
|
|
83
|
+
console.log("RESET LOCKED INTENT.");
|
|
83
84
|
if (this.log) {console.log("RESET LOCKED INTENT. Intent was explicitly invoked with an action:", message.attributes.action);}
|
|
84
85
|
}
|
|
85
|
-
}
|
|
86
|
-
catch(error) {
|
|
86
|
+
} catch(error) {
|
|
87
87
|
console.error("Error resetting locked intent:", error);
|
|
88
88
|
}
|
|
89
89
|
}
|
|
90
90
|
|
|
91
91
|
// any external invocation restarts the steps counter
|
|
92
|
-
|
|
93
|
-
if (
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
if (this.
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
92
|
+
try {
|
|
93
|
+
if (message.sender != "_tdinternal") {
|
|
94
|
+
if (this.log) {
|
|
95
|
+
console.log("Resetting current step by request message:", message.text);
|
|
96
|
+
}
|
|
97
|
+
await TiledeskChatbot.resetStep(this.tdcache, this.requestId);
|
|
98
|
+
if (this.log) {
|
|
99
|
+
if (this.tdcache) {
|
|
100
|
+
let currentStep =
|
|
101
|
+
await TiledeskChatbot.currentStep(this.tdcache, this.requestId);
|
|
102
|
+
if (this.log) {console.log("after reset currentStep:", currentStep)}
|
|
103
|
+
}
|
|
102
104
|
}
|
|
103
105
|
}
|
|
106
|
+
} catch(error) {
|
|
107
|
+
console.error("Error resetting locked intent:", error);
|
|
104
108
|
}
|
|
105
109
|
// Emergency stop :)
|
|
106
110
|
// if (message.text === "/anomaly") {
|
|
@@ -111,38 +115,38 @@ class TiledeskChatbot {
|
|
|
111
115
|
// Checking locked intent (for non-internal intents)
|
|
112
116
|
// internal intents always "skip" the locked intent
|
|
113
117
|
// if (message.text.startsWith("/") && message.sender != "_tdinternal") {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
}
|
|
118
|
+
const locked_intent = await this.currentLockedIntent(this.requestId);
|
|
119
|
+
if (this.log) {console.log("got locked intent: -" + locked_intent + "-")}
|
|
120
|
+
if (locked_intent) {
|
|
121
|
+
// const tdclient = new TiledeskClient({
|
|
122
|
+
// projectId: this.projectId,
|
|
123
|
+
// token: this.token,
|
|
124
|
+
// APIURL: this.APIURL,
|
|
125
|
+
// APIKEY: this.APIKEY,
|
|
126
|
+
// log: false
|
|
127
|
+
// });
|
|
128
|
+
// it only gets the locked_intent
|
|
129
|
+
// const faq = await this.botsDataSource.getByIntentDisplayName(this.botId, locked_intent);
|
|
130
|
+
const faq = await this.botsDataSource.getByIntentDisplayNameCache(this.botId, locked_intent, this.tdcache);
|
|
131
|
+
if (this.log) {console.log("locked intent. got faqs", JSON.stringify(faq))}
|
|
132
|
+
let reply;
|
|
133
|
+
if (faq) {
|
|
134
|
+
reply = await this.execIntent(faq, message, lead);//, bot);
|
|
135
|
+
// if (!reply.attributes) {
|
|
136
|
+
// reply.attributes = {}
|
|
137
|
+
// }
|
|
138
|
+
// // used by the Clients to get some info about the intent that generated this reply
|
|
139
|
+
// reply.attributes.intent_display_name = faq.intent_display_name;
|
|
140
|
+
// reply.attributes.intent_id = faq.intent_id;
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
reply = {
|
|
144
|
+
"text": "An error occurred while getting locked intent:'" + locked_intent + "'"
|
|
142
145
|
}
|
|
143
|
-
resolve(reply);
|
|
144
|
-
return;
|
|
145
146
|
}
|
|
147
|
+
resolve(reply);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
146
150
|
// }
|
|
147
151
|
// else if (message.text.startsWith("/")) {
|
|
148
152
|
// if (this.log) {
|
|
@@ -555,7 +559,6 @@ class TiledeskChatbot {
|
|
|
555
559
|
async lockAction(requestId, action_id) {
|
|
556
560
|
if (this.tdcache != null && requestId != null && action_id != null) {
|
|
557
561
|
await this.tdcache.set("tilebot:requests:" + requestId + ":action:locked", action_id);
|
|
558
|
-
|
|
559
562
|
}
|
|
560
563
|
else {
|
|
561
564
|
console.error("lockAction recoverable error, one of requestId:", requestId, "action_id:", action_id, "is null");
|
|
@@ -241,11 +241,11 @@ class TiledeskChatbotUtil {
|
|
|
241
241
|
if (!message) {
|
|
242
242
|
return;
|
|
243
243
|
}
|
|
244
|
-
console.log("compute delay...", message)
|
|
244
|
+
// console.log("compute delay...", message)
|
|
245
245
|
if (message.attributes.commands.length > 0) {
|
|
246
|
-
console.log("going on delay")
|
|
246
|
+
// console.log("going on delay")
|
|
247
247
|
let commands = message.attributes.commands;
|
|
248
|
-
console.log("got commands", commands)
|
|
248
|
+
// console.log("got commands", commands)
|
|
249
249
|
let totalWaitTime = 0;
|
|
250
250
|
for (let i = commands.length - 1; i >= 0; i--) {
|
|
251
251
|
if (commands[i].type === "wait") { // is a wait
|
|
@@ -327,7 +327,7 @@ class TiledeskChatbotUtil {
|
|
|
327
327
|
const chat_url = `https://panel.tiledesk.com/v3/dashboard/#/project/${projectId}/wsrequest/${requestId}/messages`
|
|
328
328
|
// await chatbot.addParameter("chatbot", chatbot);
|
|
329
329
|
await chatbot.addParameter(TiledeskChatbotConst.REQ_CHAT_URL, chat_url);
|
|
330
|
-
console.log("Adding proj_", projectId);
|
|
330
|
+
// console.log("Adding proj_", projectId);
|
|
331
331
|
await chatbot.addParameter(TiledeskChatbotConst.REQ_PROJECT_ID_KEY, projectId);
|
|
332
332
|
// TODO add projectName too
|
|
333
333
|
await chatbot.addParameter(TiledeskChatbotConst.REQ_REQUEST_ID_KEY, requestId);
|
package/package.json
CHANGED
|
@@ -106,7 +106,7 @@ class DirectivesChatbotPlug {
|
|
|
106
106
|
return;
|
|
107
107
|
}
|
|
108
108
|
const supportRequest = this.supportRequest;
|
|
109
|
-
console.log("supportRequest is:", JSON.stringify(supportRequest))
|
|
109
|
+
// console.log("supportRequest is:", JSON.stringify(supportRequest))
|
|
110
110
|
|
|
111
111
|
const token = this.token;
|
|
112
112
|
const API_URL = this.API_URL;
|
|
@@ -199,7 +199,7 @@ class DirectivesChatbotPlug {
|
|
|
199
199
|
}
|
|
200
200
|
|
|
201
201
|
async process(directive) {
|
|
202
|
-
console.log(".process(directive):", JSON.stringify(directive));
|
|
202
|
+
// console.log(".process(directive):", JSON.stringify(directive));
|
|
203
203
|
let context = this.context;
|
|
204
204
|
// console.log(".this.context.reply", JSON.stringify(this.context.reply));
|
|
205
205
|
if (directive) {
|
|
@@ -278,7 +278,7 @@ class DirectivesChatbotPlug {
|
|
|
278
278
|
});
|
|
279
279
|
}
|
|
280
280
|
else if (directive_name === Directives.REPLY) {
|
|
281
|
-
console.log("...DirReply");
|
|
281
|
+
// console.log("...DirReply");
|
|
282
282
|
new DirReply(context).execute(directive, async () => {
|
|
283
283
|
let next_dir = await this.nextDirective(this.directives);
|
|
284
284
|
this.process(next_dir);
|
|
@@ -543,7 +543,7 @@ class DirectivesChatbotPlug {
|
|
|
543
543
|
});
|
|
544
544
|
}
|
|
545
545
|
else if (directive_name === Directives.CAPTURE_USER_REPLY) {
|
|
546
|
-
console.log("...DirCaptureUserReply");
|
|
546
|
+
// console.log("...DirCaptureUserReply");
|
|
547
547
|
new DirCaptureUserReply(context).execute(directive, async () => {
|
|
548
548
|
let next_dir = await this.nextDirective(this.directives);
|
|
549
549
|
this.process(next_dir);
|
|
@@ -208,10 +208,10 @@ class DirAskGPT {
|
|
|
208
208
|
if (action.assignReplyTo && answer) {
|
|
209
209
|
await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignReplyTo, answer);
|
|
210
210
|
}
|
|
211
|
-
console.log("--> action.assignSourceTo: ", action.assignSourceTo)
|
|
212
|
-
console.log("--> source: ", source)
|
|
211
|
+
// console.log("--> action.assignSourceTo: ", action.assignSourceTo)
|
|
212
|
+
// console.log("--> source: ", source)
|
|
213
213
|
if (action.assignSourceTo && source) {
|
|
214
|
-
console.log("--> source: ", source)
|
|
214
|
+
// console.log("--> source: ", source)
|
|
215
215
|
await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignSourceTo, source);
|
|
216
216
|
}
|
|
217
217
|
// Debug log
|
|
@@ -265,7 +265,7 @@ class DirAskGPT {
|
|
|
265
265
|
}
|
|
266
266
|
})
|
|
267
267
|
.catch((error) => {
|
|
268
|
-
console.error("An error occurred:", JSON.stringify(error.data));
|
|
268
|
+
// console.error("An error occurred:", JSON.stringify(error.data));
|
|
269
269
|
if (callback) {
|
|
270
270
|
callback(error, null);
|
|
271
271
|
}
|
|
@@ -62,33 +62,33 @@ class DirCaptureUserReply {
|
|
|
62
62
|
|
|
63
63
|
async go(action, callback) {
|
|
64
64
|
const goToIntent = action.goToIntent;
|
|
65
|
-
console.log("(DirCaptureUserReply) goToIntent:", goToIntent);
|
|
65
|
+
// console.log("(DirCaptureUserReply) goToIntent:", goToIntent);
|
|
66
66
|
let lockedAction = await this.chatbot.currentLockedAction(this.requestId);
|
|
67
|
-
console.log("(DirCaptureUserReply) lockedAction:", lockedAction);
|
|
67
|
+
// console.log("(DirCaptureUserReply) lockedAction:", lockedAction);
|
|
68
68
|
if (!lockedAction) {
|
|
69
|
-
console.log("(DirCaptureUserReply) !lockedAction");
|
|
69
|
+
// console.log("(DirCaptureUserReply) !lockedAction");
|
|
70
70
|
const intent_name = this.reply.attributes.intent_info.intent_name
|
|
71
71
|
const actionId = action["_tdActionId"];
|
|
72
|
-
console.log("(DirCaptureUserReply) intent_name:", intent_name);
|
|
73
|
-
console.log("(DirCaptureUserReply) actionId:", actionId);
|
|
72
|
+
// console.log("(DirCaptureUserReply) intent_name:", intent_name);
|
|
73
|
+
// console.log("(DirCaptureUserReply) actionId:", actionId);
|
|
74
74
|
await this.chatbot.lockIntent(this.requestId, intent_name);
|
|
75
|
-
console.log("(DirCaptureUserReply) lockIntent");
|
|
75
|
+
// console.log("(DirCaptureUserReply) lockIntent");
|
|
76
76
|
await this.chatbot.lockAction(this.requestId, actionId);
|
|
77
|
-
console.log("(DirCaptureUserReply) lockAction");
|
|
77
|
+
// console.log("(DirCaptureUserReply) lockAction");
|
|
78
78
|
let _lockedAction = await this.chatbot.currentLockedAction(this.requestId);
|
|
79
79
|
let _lockedIntent = await this.chatbot.currentLockedIntent(this.requestId);
|
|
80
|
-
console.log("(DirCaptureUserReply) _lockedAction", _lockedAction)
|
|
81
|
-
console.log("(DirCaptureUserReply) _lockedIntent", _lockedIntent)
|
|
80
|
+
// console.log("(DirCaptureUserReply) _lockedAction", _lockedAction)
|
|
81
|
+
// console.log("(DirCaptureUserReply) _lockedIntent", _lockedIntent)
|
|
82
82
|
callback();
|
|
83
83
|
return;
|
|
84
84
|
} else {
|
|
85
85
|
try {
|
|
86
86
|
await this.chatbot.unlockIntent(this.requestId);
|
|
87
87
|
await this.chatbot.unlockAction(this.requestId);
|
|
88
|
-
console.log("unlo")
|
|
88
|
+
// console.log("unlo")
|
|
89
89
|
}
|
|
90
90
|
catch(e) {
|
|
91
|
-
console.error("
|
|
91
|
+
console.error("Error", e)
|
|
92
92
|
}
|
|
93
93
|
|
|
94
94
|
}
|
|
@@ -102,7 +102,7 @@ class DirCaptureUserReply {
|
|
|
102
102
|
}
|
|
103
103
|
|
|
104
104
|
if (callback) {
|
|
105
|
-
console.log("(DirCaptureUserReply) #executeGoTo(goToIntent)", goToIntent)
|
|
105
|
+
// console.log("(DirCaptureUserReply) #executeGoTo(goToIntent)", goToIntent)
|
|
106
106
|
this.#executeGoTo(goToIntent, () => {
|
|
107
107
|
callback(); // continue the flow
|
|
108
108
|
});
|
|
@@ -76,13 +76,15 @@ class DirIfOnlineAgents {
|
|
|
76
76
|
}
|
|
77
77
|
const trueIntent = action.trueIntent;
|
|
78
78
|
const falseIntent = action.falseIntent;
|
|
79
|
+
console.log("IfOnlineAgents:trueIntent:", trueIntent);
|
|
80
|
+
console.log("IfOnlineAgents:falseIntent:", falseIntent);
|
|
79
81
|
const trueIntentAttributes = action.trueIntentAttributes;
|
|
80
82
|
const falseIntentAttributes = action.falseIntentAttributes;
|
|
81
83
|
let stopOnConditionMet = action.stopOnConditionMet;
|
|
82
84
|
this.tdclient.openNow((err, result) => {
|
|
83
85
|
if (this.log) {console.log("openNow():", result);}
|
|
84
86
|
if (err) {
|
|
85
|
-
console.error("tdclient.openNow Error:", err);
|
|
87
|
+
console.error("IfOnlineAgents:tdclient.openNow Error:", err);
|
|
86
88
|
callback();
|
|
87
89
|
return;
|
|
88
90
|
}
|
|
@@ -90,8 +92,9 @@ class DirIfOnlineAgents {
|
|
|
90
92
|
if (result && result.isopen) {
|
|
91
93
|
this.tdclient.getProjectAvailableAgents((err, agents) => {
|
|
92
94
|
if (this.log) {console.log("Agents", agents);}
|
|
95
|
+
console.log("IfOnlineAgents:Agents", agents); // PROD
|
|
93
96
|
if (err) {
|
|
94
|
-
console.error("Error getting available agents:", err);
|
|
97
|
+
console.error("IfOnlineAgents:Error getting available agents:", err);
|
|
95
98
|
callback();
|
|
96
99
|
}
|
|
97
100
|
else {
|
|
@@ -100,18 +103,21 @@ class DirIfOnlineAgents {
|
|
|
100
103
|
if (trueIntent) {
|
|
101
104
|
let intentDirective = DirIntent.intentDirectiveFor(trueIntent, trueIntentAttributes);
|
|
102
105
|
if (this.log) {console.log("agents (openHours) => trueIntent");}
|
|
106
|
+
console.log("IfOnlineAgents:agents (openHours) => trueIntent", intentDirective) // prod
|
|
103
107
|
this.intentDir.execute(intentDirective, () => {
|
|
104
108
|
callback(stopOnConditionMet);
|
|
105
109
|
});
|
|
106
110
|
}
|
|
107
111
|
else {
|
|
112
|
+
console.log("NO IfOnlineAgents trueIntent defined. callback()") // prod
|
|
108
113
|
callback();
|
|
109
114
|
return;
|
|
110
115
|
}
|
|
111
116
|
}
|
|
112
117
|
else if (falseIntent) {
|
|
113
118
|
let intentDirective = DirIntent.intentDirectiveFor(falseIntent, falseIntentAttributes);
|
|
114
|
-
if (this.log) {console.log("!agents (openHours) => falseIntent",
|
|
119
|
+
if (this.log) {console.log("!agents (openHours) => falseIntent", intentDirective);}
|
|
120
|
+
console.log("IfOnlineAgents:agents (openHours) => falseIntent", intentDirective) // prod
|
|
115
121
|
this.intentDir.execute(intentDirective, () => {
|
|
116
122
|
callback(stopOnConditionMet);
|
|
117
123
|
});
|
|
@@ -126,6 +132,7 @@ class DirIfOnlineAgents {
|
|
|
126
132
|
if (falseIntent) {
|
|
127
133
|
let intentDirective = DirIntent.intentDirectiveFor(falseIntent, falseIntentAttributes);
|
|
128
134
|
if (this.log) {console.log("!agents (!openHours) => falseIntent");}
|
|
135
|
+
console.log("!agents (!openHours) => falseIntent BECAUSE CLOSED"); //PROD
|
|
129
136
|
this.intentDir.execute(intentDirective, () => {
|
|
130
137
|
callback();
|
|
131
138
|
});
|
|
@@ -136,6 +143,7 @@ class DirIfOnlineAgents {
|
|
|
136
143
|
}
|
|
137
144
|
else {
|
|
138
145
|
if (this.log) {console.log("undeterminate result.");}
|
|
146
|
+
console.log("undeterminate result.");
|
|
139
147
|
callback();
|
|
140
148
|
}
|
|
141
149
|
}
|
|
@@ -109,15 +109,15 @@ class DirReply {
|
|
|
109
109
|
}
|
|
110
110
|
if (this.log) {console.log("Reply message sent");}
|
|
111
111
|
const delay = TiledeskChatbotUtil.totalMessageWait(message);
|
|
112
|
-
console.log("got total delay:", delay)
|
|
112
|
+
// console.log("got total delay:", delay)
|
|
113
113
|
if (delay > 0 && delay <= 30000) { // prevent long delays
|
|
114
114
|
setTimeout(() => {
|
|
115
|
-
console.log("callback after delay")
|
|
115
|
+
// console.log("callback after delay")
|
|
116
116
|
callback();
|
|
117
117
|
}, delay);
|
|
118
118
|
}
|
|
119
119
|
else {
|
|
120
|
-
console.log("invalid delay.")
|
|
120
|
+
// console.log("invalid delay.")
|
|
121
121
|
callback();
|
|
122
122
|
}
|
|
123
123
|
});
|