@tiledesk/tiledesk-tybot-connector 0.2.20 → 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 +9 -0
- package/index.js +10 -220
- package/models/MockTdCache.js +9 -0
- package/models/MongodbBotsDataSource.js +18 -18
- package/models/TiledeskChatbot.js +46 -43
- package/models/TiledeskChatbotUtil.js +227 -3
- package/package.json +1 -1
- package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +9 -9
- 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/tiledeskChatbotPlugs/directives/{DirIfAvailableAgents.js → DEPRECATED_DirIfAvailableAgents.js} +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,15 @@
|
|
|
5
5
|
available on:
|
|
6
6
|
▶️ https://www.npmjs.com/package/@tiledesk/tiledesk-tybot-connector
|
|
7
7
|
|
|
8
|
+
### v0.2.22
|
|
9
|
+
- Fixed "TildeskChatConst.js is not defined" in index.js
|
|
10
|
+
|
|
11
|
+
### v0.2.21
|
|
12
|
+
- log clean
|
|
13
|
+
- added log for production in IfOnlineAgent
|
|
14
|
+
- Deprecated IfAvailableAgents
|
|
15
|
+
- index.js refactored. Moved static utility methods in TiledeskChatbotUtil.js
|
|
16
|
+
|
|
8
17
|
### v0.2.20
|
|
9
18
|
- updateRequestVariables renamed in updateRequestAttributes
|
|
10
19
|
- Added TildeskChatbot instance method async getParameter(parameter_name)
|
package/index.js
CHANGED
|
@@ -7,12 +7,12 @@ const { ExtUtil } = require('./ExtUtil.js');
|
|
|
7
7
|
const { TdCache } = require('./TdCache.js');
|
|
8
8
|
const { TiledeskChatbot } = require('./models/TiledeskChatbot.js');
|
|
9
9
|
const { MongodbBotsDataSource } = require('./models/MongodbBotsDataSource.js');
|
|
10
|
-
const { MongodbIntentsMachine } = require('./models/MongodbIntentsMachine.js');
|
|
11
|
-
const { TiledeskIntentsMachine } = require('./models/TiledeskIntentsMachine.js');
|
|
10
|
+
// const { MongodbIntentsMachine } = require('./models/MongodbIntentsMachine.js');
|
|
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
|
-
let parser = require('accept-language-parser');
|
|
15
|
+
// let parser = require('accept-language-parser');
|
|
16
16
|
|
|
17
17
|
router.use(bodyParser.json({limit: '50mb'}));
|
|
18
18
|
router.use(bodyParser.urlencoded({ extended: true , limit: '50mb'}));
|
|
@@ -26,7 +26,7 @@ const { DirectivesChatbotPlug } = require('./tiledeskChatbotPlugs/DirectivesChat
|
|
|
26
26
|
|
|
27
27
|
// THE IMPORT
|
|
28
28
|
let mongoose = require('mongoose');
|
|
29
|
-
const { Directives } = require('./tiledeskChatbotPlugs/directives/Directives.js');
|
|
29
|
+
// const { Directives } = require('./tiledeskChatbotPlugs/directives/Directives.js');
|
|
30
30
|
const { TiledeskChatbotUtil } = require('./models/TiledeskChatbotUtil.js'); //require('@tiledesk/tiledesk-chatbot-util');
|
|
31
31
|
let APIURL = null;
|
|
32
32
|
let staticBots;
|
|
@@ -93,7 +93,7 @@ router.post('/ext/:botid', async (req, res) => {
|
|
|
93
93
|
// bot = await botsDS.getBotById(botId);
|
|
94
94
|
// bot = await botById(botId, projectId, tdcache, botsDS);
|
|
95
95
|
bot = await botsDS.getBotByIdCache(botId, tdcache);
|
|
96
|
-
console.log("getBotByIdCache ---> bot: ", JSON.stringify(bot, null, 2))
|
|
96
|
+
// console.log("getBotByIdCache ---> bot: ", JSON.stringify(bot, null, 2))
|
|
97
97
|
}
|
|
98
98
|
catch(error) {
|
|
99
99
|
console.error("Error getting botId:", botId);
|
|
@@ -147,7 +147,7 @@ router.post('/ext/:botid', async (req, res) => {
|
|
|
147
147
|
projectId: projectId,
|
|
148
148
|
log: log
|
|
149
149
|
});
|
|
150
|
-
await updateRequestAttributes(chatbot, message, projectId, requestId);
|
|
150
|
+
await TiledeskChatbotUtil.updateRequestAttributes(chatbot, message, projectId, requestId);
|
|
151
151
|
await TiledeskChatbotUtil.updateConversationTranscript(chatbot, message);
|
|
152
152
|
|
|
153
153
|
let reply = await chatbot.replyToMessage(message);
|
|
@@ -162,12 +162,10 @@ router.post('/ext/:botid', async (req, res) => {
|
|
|
162
162
|
// process.exit(1)
|
|
163
163
|
// }
|
|
164
164
|
if (reply.actions && reply.actions.length > 0) { // structured actions (coming from chatbot designer)
|
|
165
|
-
if (log) {console.log("the actions:", JSON.stringify(reply.actions));}
|
|
166
|
-
let directives = actionsToDirectives(reply.actions);
|
|
167
|
-
if (log) {console.log("the directives:", JSON.stringify(directives));}
|
|
168
165
|
try {
|
|
169
|
-
|
|
170
|
-
|
|
166
|
+
if (log) {console.log("the actions:", JSON.stringify(reply.actions));}
|
|
167
|
+
let directives = TiledeskChatbotUtil.actionsToDirectives(reply.actions);
|
|
168
|
+
if (log) {console.log("the directives:", JSON.stringify(directives));}
|
|
171
169
|
let directivesPlug = new DirectivesChatbotPlug(
|
|
172
170
|
{
|
|
173
171
|
message: message,
|
|
@@ -188,7 +186,7 @@ router.post('/ext/:botid', async (req, res) => {
|
|
|
188
186
|
});
|
|
189
187
|
}
|
|
190
188
|
catch (error) {
|
|
191
|
-
console.error("
|
|
189
|
+
console.error("Error while processing actions:", error);
|
|
192
190
|
}
|
|
193
191
|
}
|
|
194
192
|
else { // text answer (parse text directives to get actions)
|
|
@@ -218,214 +216,6 @@ router.post('/ext/:botid', async (req, res) => {
|
|
|
218
216
|
|
|
219
217
|
});
|
|
220
218
|
|
|
221
|
-
async function updateRequestAttributes(chatbot, message, projectId, requestId) {
|
|
222
|
-
// update request context
|
|
223
|
-
if (chatbot.log) {console.log("Updating request variables. Message:", JSON.stringify(message));}
|
|
224
|
-
const messageId = message._id;
|
|
225
|
-
const chat_url = `https://panel.tiledesk.com/v3/dashboard/#/project/${projectId}/wsrequest/${requestId}/messages`
|
|
226
|
-
// await chatbot.addParameter("chatbot", chatbot);
|
|
227
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_CHAT_URL, chat_url);
|
|
228
|
-
console.log("Adding proj_", projectId);
|
|
229
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_PROJECT_ID_KEY, projectId);
|
|
230
|
-
// TODO add projectName too
|
|
231
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_REQUEST_ID_KEY, requestId);
|
|
232
|
-
if (chatbot.bot) {
|
|
233
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_NAME_KEY, chatbot.bot.name);
|
|
234
|
-
}
|
|
235
|
-
if (message.text && message.sender !== "_tdinternal") {
|
|
236
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_TEXT_KEY, message.text);
|
|
237
|
-
await chatbot.addParameter("lastUserMessageType", message.type);
|
|
238
|
-
await chatbot.addParameter("lastUserMessage", lastUserMessageFrom(message)); // JSON TYPE *NEW
|
|
239
|
-
// get image
|
|
240
|
-
if (message.type && message.type === "image" && message.metadata) {
|
|
241
|
-
// "text": "\nimage text",
|
|
242
|
-
// "id_project": "65203e12f8c0cf002cf4110b",
|
|
243
|
-
// "createdBy": "8ac52a30-133f-4ee1-8b4b-96055bb81757",
|
|
244
|
-
// "metadata": {
|
|
245
|
-
// "height": 905,
|
|
246
|
-
// "name": "tiledesk_Open graph_general.png",
|
|
247
|
-
// "src": "https://firebasestorage.googleapis.com/v0/b/chat21-pre-01.appspot.com/o/public%2Fimages%2F8ac52a30-133f-4ee1-8b4b-96055bb81757%2Fda5bbc8d-5174-49a8-a041-3d9355242da5%2Ftiledesk_Open%20graph_general.png?alt=media&token=be82fecb-3cd1-45b9-a135-c2c57a932862",
|
|
248
|
-
// "type": "image/png",
|
|
249
|
-
// "uid": "lo68iyq5",
|
|
250
|
-
// "width": 1724
|
|
251
|
-
// }
|
|
252
|
-
if (message.metadata.src) {
|
|
253
|
-
await chatbot.addParameter("lastUserImageURL", message.metadata.src);
|
|
254
|
-
await chatbot.addParameter("lastUserImageName", message.metadata.name);
|
|
255
|
-
await chatbot.addParameter("lastUserImageWidth", message.metadata.width);
|
|
256
|
-
await chatbot.addParameter("lastUserImageHeight", message.metadata.height);
|
|
257
|
-
await chatbot.addParameter("lastUserImageType", message.metadata.type);
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
else {
|
|
261
|
-
await chatbot.addParameter("lastUserImageURL", null);
|
|
262
|
-
await chatbot.addParameter("lastUserImageName", null);
|
|
263
|
-
await chatbot.addParameter("lastUserImageWidth", null);
|
|
264
|
-
await chatbot.addParameter("lastUserImageHeight", null);
|
|
265
|
-
await chatbot.addParameter("lastUserImageType", null);
|
|
266
|
-
}
|
|
267
|
-
// get document
|
|
268
|
-
if (message.type && message.type === "file" && message.metadata) {
|
|
269
|
-
// "type": "file",
|
|
270
|
-
// "text": "[LIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf](https://firebasestorage.googleapis.com/v0/b/chat21-pre-01.appspot.com/o/public%2Fimages%2F8ac52a30-133f-4ee1-8b4b-96055bb81757%2F502265ee-4f4a-47a4-9375-172bb0e6bf39%2FLIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf?alt=media&token=a09d065a-9b56-4507-8960-344cc294e4d1)\nistruzioni",
|
|
271
|
-
// "metadata": {
|
|
272
|
-
// "name": "LIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf",
|
|
273
|
-
// "src": "https://firebasestorage.googleapis.com/v0/b/chat21-pre-01.appspot.com/o/public%2Fimages%2F8ac52a30-133f-4ee1-8b4b-96055bb81757%2F502265ee-4f4a-47a4-9375-172bb0e6bf39%2FLIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf?alt=media&token=a09d065a-9b56-4507-8960-344cc294e4d1",
|
|
274
|
-
// "type": "application/pdf",
|
|
275
|
-
// "uid": "lo68oz8i"
|
|
276
|
-
// }
|
|
277
|
-
if (message.metadata.src) {
|
|
278
|
-
await chatbot.addParameter("lastUserDocumentURL", message.metadata.src);
|
|
279
|
-
await chatbot.addParameter("lastUserDocumentName", message.metadata.name);
|
|
280
|
-
await chatbot.addParameter("lastUserDocumentType", message.metadata.type);
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
else {
|
|
284
|
-
await chatbot.addParameter("lastUserDocumentURL", null);
|
|
285
|
-
await chatbot.addParameter("lastUserDocumentName", null);
|
|
286
|
-
await chatbot.addParameter("lastUserDocumentType", null);
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_MESSAGE_ID_KEY, messageId);
|
|
292
|
-
if (message.request && message.request.location && message.request.location.country) {
|
|
293
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_COUNTRY_KEY, message.request.location.country);
|
|
294
|
-
}
|
|
295
|
-
if (message.request && message.request.location && message.request.location.city) {
|
|
296
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_CITY_KEY, message.request.location.city);
|
|
297
|
-
}
|
|
298
|
-
// console.log("message.request.language", message.request["language"]);
|
|
299
|
-
if (message.request) {
|
|
300
|
-
let user_language = message.request["language"];
|
|
301
|
-
if (message.request["language"]) {
|
|
302
|
-
// console.log("HTTP language:", message.request["language"]);
|
|
303
|
-
var languages = parser.parse(message.request["language"]);
|
|
304
|
-
// console.log("languages:", languages);
|
|
305
|
-
if (languages && languages.length > 0 && languages[0].code) {
|
|
306
|
-
user_language = languages[0].code;
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_SOURCE_PAGE_KEY, message.request.sourcePage);
|
|
310
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_LANGUAGE_KEY, user_language);
|
|
311
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_AGENT_KEY, message.request.userAgent);
|
|
312
|
-
}
|
|
313
|
-
// console.log("message.request.language", message.request["language"])
|
|
314
|
-
if (message.request && message.request.department) {
|
|
315
|
-
// It was an error when getting this from widget message's attributes
|
|
316
|
-
// await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY, message.attributes.departmentId);
|
|
317
|
-
// await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY, message.attributes.departmentName);
|
|
318
|
-
// get from request.department instead
|
|
319
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY, message.request.department._id);
|
|
320
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY, message.request.department.name);
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
if (projectId === "641864da99c1fb00131ba495") {
|
|
324
|
-
console.log("641864da99c1fb00131ba495 > for projectId:", JSON.stringify(message))
|
|
325
|
-
}
|
|
326
|
-
// for BUG
|
|
327
|
-
// if (chatbot.log) {console.log("message.request.attributes.payload", JSON.stringify(message.request.attributes.payload))}
|
|
328
|
-
if (message && message.request && message.request.attributes && message.request.attributes.payload) {
|
|
329
|
-
if (!message.attributes) {
|
|
330
|
-
message.attributes = {}
|
|
331
|
-
}
|
|
332
|
-
message.attributes.payload = message.request.attributes.payload
|
|
333
|
-
if (chatbot.log) {console.log("FORCED SET message.attributes.payload:", JSON.stringify(message.attributes.payload))}
|
|
334
|
-
// if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > FORCED SET message.attributes.payload:", JSON.stringify(message.attributes.payload))}
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
if (message.attributes) {
|
|
338
|
-
if (chatbot.log) {console.log("Ok message.attributes", JSON.stringify(message.attributes));}
|
|
339
|
-
if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > Ok message.attributes", JSON.stringify(message.attributes));}
|
|
340
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_END_USER_ID_KEY, message.attributes.requester_id);
|
|
341
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_END_USER_IP_ADDRESS_KEY, message.attributes.ipAddress);
|
|
342
|
-
if (message.attributes.payload) {
|
|
343
|
-
try {
|
|
344
|
-
for (const [key, value] of Object.entries(message.attributes.payload)) {
|
|
345
|
-
// const value = all_parameters[key];
|
|
346
|
-
const value_type = typeof value;
|
|
347
|
-
//if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > importing payload parameter:", key, "value:", value, "type:", value_type);}
|
|
348
|
-
//await chatbot.addParameter(key, String(value));
|
|
349
|
-
await chatbot.addParameter(key, value);
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
catch(err) {
|
|
353
|
-
console.error("Error importing message payload in request variables:", err);
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
if (chatbot.log) {
|
|
358
|
-
// console.log("tdcache:", chatbot.tdcache);
|
|
359
|
-
console.log("requestId:", requestId);
|
|
360
|
-
console.log("KEY:", TiledeskChatbotConst.REQ_PROJECT_ID_KEY);
|
|
361
|
-
let proj_ = await TiledeskChatbot.getParameterStatic(chatbot.tdcache, requestId, TiledeskChatbotConst.REQ_PROJECT_ID_KEY);
|
|
362
|
-
console.log("request parameter proj_:", proj_);
|
|
363
|
-
const all_parameters = await TiledeskChatbot.allParametersStatic(chatbot.tdcache, requestId);
|
|
364
|
-
for (const [key, value] of Object.entries(all_parameters)) {
|
|
365
|
-
// const value = all_parameters[key];
|
|
366
|
-
const value_type = typeof value;
|
|
367
|
-
if (chatbot.log) {console.log("request parameter:", key, "value:", value, "type:", value_type)}
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
// message["attributes"]: {
|
|
371
|
-
// "departmentId": "63c980054f857c00350535bc",
|
|
372
|
-
// "departmentName": "Default Department",
|
|
373
|
-
// "client": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36",
|
|
374
|
-
// "sourcePage": "https://tiledesk-html-site.tiledesk.repl.co/custom-attributes.html",
|
|
375
|
-
// "projectId": "63c980054f857c00350535b8",
|
|
376
|
-
// "payload": {
|
|
377
|
-
// "user_country": "Italy",
|
|
378
|
-
// "user_code": "E001"
|
|
379
|
-
// },
|
|
380
|
-
// "userFullname": "guest#7216 ",
|
|
381
|
-
// "requester_id": "7216926a-84c3-4bd5-aa79-8bd763094dc0",
|
|
382
|
-
// "ipAddress": "79.8.190.172",
|
|
383
|
-
// "sourceTitle": "Custom attributes",
|
|
384
|
-
// "widgetVer": "v.5.0.53-rc.4",
|
|
385
|
-
// "subtype": "info",
|
|
386
|
-
// "decoded_jwt": {
|
|
387
|
-
// "_id": "7216926a-84c3-4bd5-aa79-8bd763094dc0",
|
|
388
|
-
// "firstname": "guest#7216",
|
|
389
|
-
// "id": "7216926a-84c3-4bd5-aa79-8bd763094dc0",
|
|
390
|
-
// "fullName": "guest#7216 ",
|
|
391
|
-
// "iat": 1674201892,
|
|
392
|
-
// "aud": "https://tiledesk.com",
|
|
393
|
-
// "iss": "https://tiledesk.com",
|
|
394
|
-
// "sub": "guest",
|
|
395
|
-
// "jti": "f053af3d-14ca-411b-9903-78bd74e24218"
|
|
396
|
-
// }
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
function lastUserMessageFrom(msg) {
|
|
400
|
-
let message = {};
|
|
401
|
-
message["senderFullname"] = msg["senderFullname"]; // ex. "Bot"
|
|
402
|
-
message["type"] = msg["type"]; // ex. "text",
|
|
403
|
-
message["channel_type"] = msg["channel_type"]; // ex. "group",
|
|
404
|
-
message["status"] = msg["status"]; // ex. 0,
|
|
405
|
-
message["id"] = msg["_id"]; // ex. "6538cda46cb4d8002cf2317a",
|
|
406
|
-
message["sender"] = msg["sender"]; // ex. "system",
|
|
407
|
-
message["recipient"] = msg["recipient"]; // ex. "support-group-65203e12f8c0cf002cf4110b-4066a69c8b464646a3ff25f9f41575bb",
|
|
408
|
-
message["text"] = msg["text"]; // ex. "\\start",
|
|
409
|
-
message["createdBy"] = msg["createdBy"]; // ex. "system",
|
|
410
|
-
message["attributes"] = msg["attributes"]; // ex. { "subtype": "info" }
|
|
411
|
-
message["metadata"] = msg["metadata"];
|
|
412
|
-
message["channel"] = msg["channel"]; // ex. { "name": "chat21" }
|
|
413
|
-
return message;
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
function actionsToDirectives(actions) {
|
|
417
|
-
let directives = [];
|
|
418
|
-
if (actions && actions.length > 0) {
|
|
419
|
-
actions.forEach(action => {
|
|
420
|
-
let directive = Directives.actionToDirective(action);
|
|
421
|
-
if (directive) {
|
|
422
|
-
directives.push(directive);
|
|
423
|
-
}
|
|
424
|
-
});
|
|
425
|
-
}
|
|
426
|
-
return directives;
|
|
427
|
-
}
|
|
428
|
-
|
|
429
219
|
router.post('/ext/:projectId/requests/:requestId/messages', async (req, res) => {
|
|
430
220
|
res.json({success:true});
|
|
431
221
|
const projectId = req.params.projectId;
|
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 };
|
|
@@ -22,33 +22,33 @@ class MongodbBotsDataSource {
|
|
|
22
22
|
async getBotByIdCache(botId, tdcache) {
|
|
23
23
|
let bot = null;
|
|
24
24
|
if (tdcache) {
|
|
25
|
-
console.log("getBotByIdCache cache ok");
|
|
25
|
+
// console.log("getBotByIdCache cache ok");
|
|
26
26
|
let botCacheKey = "cacheman:cachegoose-cache:faq_kbs:id:" + botId;
|
|
27
27
|
try {
|
|
28
28
|
let _bot_as_string = await tdcache.get(botCacheKey);
|
|
29
29
|
const value_type = typeof _bot_as_string;
|
|
30
|
-
console.log("__bot_as_string found in chache:", _bot_as_string);
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
// console.log("__bot_as_string found in chache:", _bot_as_string);
|
|
31
|
+
if (this.log) {
|
|
32
|
+
console.log("__bot_as_string found in chache:", _bot_as_string);
|
|
33
|
+
console.log("value_type:", value_type);
|
|
34
|
+
}
|
|
35
35
|
if (_bot_as_string) {
|
|
36
36
|
bot = JSON.parse(_bot_as_string);
|
|
37
|
-
console.log("got bot from cache:", JSON.stringify(bot));
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
37
|
+
// console.log("got bot from cache:", JSON.stringify(bot));
|
|
38
|
+
if (this.log) {
|
|
39
|
+
console.log("got bot from cache:", JSON.stringify(bot));
|
|
40
|
+
}
|
|
41
41
|
}
|
|
42
42
|
else {
|
|
43
|
-
console.log("bot not found, getting from datasource...");
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
43
|
+
// console.log("bot not found, getting from datasource...");
|
|
44
|
+
if (this.log) {
|
|
45
|
+
console.log("bot not found, getting from datasource...");
|
|
46
|
+
}
|
|
47
47
|
bot = await this.getBotById(botId);
|
|
48
|
-
console.log("bot found in datasource:", JSON.stringify(bot));
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
48
|
+
// console.log("bot found in datasource:", JSON.stringify(bot));
|
|
49
|
+
if (this.log) {
|
|
50
|
+
console.log("bot found in datasource:", JSON.stringify(bot));
|
|
51
|
+
}
|
|
52
52
|
await tdcache.set(botCacheKey, JSON.stringify(bot));
|
|
53
53
|
// DEBUG CODE REMOVE
|
|
54
54
|
// let bot_ = await tdcache.get(botCacheKey);
|
|
@@ -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");
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
const { TiledeskExpression } = require('../TiledeskExpression');
|
|
2
2
|
const { Filler } = require('../tiledeskChatbotPlugs/Filler');
|
|
3
|
+
const { TiledeskChatbotConst } = require('./TiledeskChatbotConst');
|
|
4
|
+
const { TiledeskChatbot } = require('./TiledeskChatbot.js');
|
|
5
|
+
let parser = require('accept-language-parser');
|
|
6
|
+
const { Directives } = require('../tiledeskChatbotPlugs/directives/Directives.js');
|
|
3
7
|
|
|
4
8
|
class TiledeskChatbotUtil {
|
|
5
9
|
|
|
@@ -237,11 +241,11 @@ class TiledeskChatbotUtil {
|
|
|
237
241
|
if (!message) {
|
|
238
242
|
return;
|
|
239
243
|
}
|
|
240
|
-
console.log("compute delay...", message)
|
|
244
|
+
// console.log("compute delay...", message)
|
|
241
245
|
if (message.attributes.commands.length > 0) {
|
|
242
|
-
console.log("going on delay")
|
|
246
|
+
// console.log("going on delay")
|
|
243
247
|
let commands = message.attributes.commands;
|
|
244
|
-
console.log("got commands", commands)
|
|
248
|
+
// console.log("got commands", commands)
|
|
245
249
|
let totalWaitTime = 0;
|
|
246
250
|
for (let i = commands.length - 1; i >= 0; i--) {
|
|
247
251
|
if (commands[i].type === "wait") { // is a wait
|
|
@@ -298,6 +302,226 @@ class TiledeskChatbotUtil {
|
|
|
298
302
|
return false;
|
|
299
303
|
}
|
|
300
304
|
|
|
305
|
+
static lastUserMessageFrom(msg) {
|
|
306
|
+
let message = {};
|
|
307
|
+
message["senderFullname"] = msg["senderFullname"]; // ex. "Bot"
|
|
308
|
+
message["type"] = msg["type"]; // ex. "text",
|
|
309
|
+
message["channel_type"] = msg["channel_type"]; // ex. "group",
|
|
310
|
+
message["status"] = msg["status"]; // ex. 0,
|
|
311
|
+
message["id"] = msg["_id"]; // ex. "6538cda46cb4d8002cf2317a",
|
|
312
|
+
message["sender"] = msg["sender"]; // ex. "system",
|
|
313
|
+
message["recipient"] = msg["recipient"]; // ex. "support-group-65203e12f8c0cf002cf4110b-4066a69c8b464646a3ff25f9f41575bb",
|
|
314
|
+
message["text"] = msg["text"]; // ex. "\\start",
|
|
315
|
+
message["createdBy"] = msg["createdBy"]; // ex. "system",
|
|
316
|
+
message["attributes"] = msg["attributes"]; // ex. { "subtype": "info" }
|
|
317
|
+
message["metadata"] = msg["metadata"];
|
|
318
|
+
message["channel"] = msg["channel"]; // ex. { "name": "chat21" }
|
|
319
|
+
return message;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
static async updateRequestAttributes(chatbot, message, projectId, requestId) {
|
|
323
|
+
// update request context
|
|
324
|
+
try {
|
|
325
|
+
if (chatbot.log) {console.log("Updating request variables. Message:", JSON.stringify(message));}
|
|
326
|
+
const messageId = message._id;
|
|
327
|
+
const chat_url = `https://panel.tiledesk.com/v3/dashboard/#/project/${projectId}/wsrequest/${requestId}/messages`
|
|
328
|
+
// await chatbot.addParameter("chatbot", chatbot);
|
|
329
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_CHAT_URL, chat_url);
|
|
330
|
+
// console.log("Adding proj_", projectId);
|
|
331
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_PROJECT_ID_KEY, projectId);
|
|
332
|
+
// TODO add projectName too
|
|
333
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_REQUEST_ID_KEY, requestId);
|
|
334
|
+
if (chatbot.bot) {
|
|
335
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_NAME_KEY, chatbot.bot.name);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
if (message.text && message.sender !== "_tdinternal") {
|
|
339
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_TEXT_KEY, message.text); // DEPRECATED
|
|
340
|
+
await chatbot.addParameter("lastUserText", message.text);
|
|
341
|
+
|
|
342
|
+
await chatbot.addParameter("lastUserMessageType", message.type);
|
|
343
|
+
await chatbot.addParameter("lastUserMessage", TiledeskChatbotUtil.lastUserMessageFrom(message)); // JSON TYPE *NEW
|
|
344
|
+
// get image
|
|
345
|
+
if (message.type && message.type === "image" && message.metadata) {
|
|
346
|
+
// "text": "\nimage text",
|
|
347
|
+
// "id_project": "65203e12f8c0cf002cf4110b",
|
|
348
|
+
// "createdBy": "8ac52a30-133f-4ee1-8b4b-96055bb81757",
|
|
349
|
+
// "metadata": {
|
|
350
|
+
// "height": 905,
|
|
351
|
+
// "name": "tiledesk_Open graph_general.png",
|
|
352
|
+
// "src": "https://firebasestorage.googleapis.com/v0/b/chat21-pre-01.appspot.com/o/public%2Fimages%2F8ac52a30-133f-4ee1-8b4b-96055bb81757%2Fda5bbc8d-5174-49a8-a041-3d9355242da5%2Ftiledesk_Open%20graph_general.png?alt=media&token=be82fecb-3cd1-45b9-a135-c2c57a932862",
|
|
353
|
+
// "type": "image/png",
|
|
354
|
+
// "uid": "lo68iyq5",
|
|
355
|
+
// "width": 1724
|
|
356
|
+
// }
|
|
357
|
+
if (message.metadata.src) {
|
|
358
|
+
await chatbot.addParameter("lastUserImageURL", message.metadata.src);
|
|
359
|
+
await chatbot.addParameter("lastUserImageName", message.metadata.name);
|
|
360
|
+
await chatbot.addParameter("lastUserImageWidth", message.metadata.width);
|
|
361
|
+
await chatbot.addParameter("lastUserImageHeight", message.metadata.height);
|
|
362
|
+
await chatbot.addParameter("lastUserImageType", message.metadata.type);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
else {
|
|
366
|
+
await chatbot.addParameter("lastUserImageURL", null);
|
|
367
|
+
await chatbot.addParameter("lastUserImageName", null);
|
|
368
|
+
await chatbot.addParameter("lastUserImageWidth", null);
|
|
369
|
+
await chatbot.addParameter("lastUserImageHeight", null);
|
|
370
|
+
await chatbot.addParameter("lastUserImageType", null);
|
|
371
|
+
}
|
|
372
|
+
// get document
|
|
373
|
+
if (message.type && message.type === "file" && message.metadata) {
|
|
374
|
+
// "type": "file",
|
|
375
|
+
// "text": "[LIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf](https://firebasestorage.googleapis.com/v0/b/chat21-pre-01.appspot.com/o/public%2Fimages%2F8ac52a30-133f-4ee1-8b4b-96055bb81757%2F502265ee-4f4a-47a4-9375-172bb0e6bf39%2FLIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf?alt=media&token=a09d065a-9b56-4507-8960-344cc294e4d1)\nistruzioni",
|
|
376
|
+
// "metadata": {
|
|
377
|
+
// "name": "LIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf",
|
|
378
|
+
// "src": "https://firebasestorage.googleapis.com/v0/b/chat21-pre-01.appspot.com/o/public%2Fimages%2F8ac52a30-133f-4ee1-8b4b-96055bb81757%2F502265ee-4f4a-47a4-9375-172bb0e6bf39%2FLIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf?alt=media&token=a09d065a-9b56-4507-8960-344cc294e4d1",
|
|
379
|
+
// "type": "application/pdf",
|
|
380
|
+
// "uid": "lo68oz8i"
|
|
381
|
+
// }
|
|
382
|
+
if (message.metadata.src) {
|
|
383
|
+
await chatbot.addParameter("lastUserDocumentURL", message.metadata.src);
|
|
384
|
+
await chatbot.addParameter("lastUserDocumentName", message.metadata.name);
|
|
385
|
+
await chatbot.addParameter("lastUserDocumentType", message.metadata.type);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
else {
|
|
389
|
+
await chatbot.addParameter("lastUserDocumentURL", null);
|
|
390
|
+
await chatbot.addParameter("lastUserDocumentName", null);
|
|
391
|
+
await chatbot.addParameter("lastUserDocumentType", null);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_MESSAGE_ID_KEY, messageId);
|
|
397
|
+
if (message.request && message.request.location && message.request.location.country) {
|
|
398
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_COUNTRY_KEY, message.request.location.country);
|
|
399
|
+
}
|
|
400
|
+
if (message.request && message.request.location && message.request.location.city) {
|
|
401
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_CITY_KEY, message.request.location.city);
|
|
402
|
+
}
|
|
403
|
+
// console.log("message.request.language", message.request["language"]);
|
|
404
|
+
if (message.request) {
|
|
405
|
+
let user_language = message.request["language"];
|
|
406
|
+
if (message.request["language"]) {
|
|
407
|
+
// console.log("HTTP language:", message.request["language"]);
|
|
408
|
+
var languages = parser.parse(message.request["language"]);
|
|
409
|
+
// console.log("languages:", languages);
|
|
410
|
+
if (languages && languages.length > 0 && languages[0].code) {
|
|
411
|
+
user_language = languages[0].code;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_SOURCE_PAGE_KEY, message.request.sourcePage);
|
|
415
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_LANGUAGE_KEY, user_language);
|
|
416
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_AGENT_KEY, message.request.userAgent);
|
|
417
|
+
}
|
|
418
|
+
// console.log("message.request.language", message.request["language"])
|
|
419
|
+
if (message.request && message.request.department) {
|
|
420
|
+
// It was an error when getting this from widget message's attributes
|
|
421
|
+
// await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY, message.attributes.departmentId);
|
|
422
|
+
// await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY, message.attributes.departmentName);
|
|
423
|
+
// get from request.department instead
|
|
424
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY, message.request.department._id);
|
|
425
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY, message.request.department.name);
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
if (projectId === "641864da99c1fb00131ba495") {
|
|
429
|
+
console.log("641864da99c1fb00131ba495 > for projectId:", JSON.stringify(message))
|
|
430
|
+
}
|
|
431
|
+
// for BUG
|
|
432
|
+
// if (chatbot.log) {console.log("message.request.attributes.payload", JSON.stringify(message.request.attributes.payload))}
|
|
433
|
+
if (message && message.request && message.request.attributes && message.request.attributes.payload) {
|
|
434
|
+
if (!message.attributes) {
|
|
435
|
+
message.attributes = {}
|
|
436
|
+
}
|
|
437
|
+
message.attributes.payload = message.request.attributes.payload
|
|
438
|
+
if (chatbot.log) {console.log("FORCED SET message.attributes.payload:", JSON.stringify(message.attributes.payload))}
|
|
439
|
+
// if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > FORCED SET message.attributes.payload:", JSON.stringify(message.attributes.payload))}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
if (message.attributes) {
|
|
443
|
+
if (chatbot.log) {console.log("Ok message.attributes", JSON.stringify(message.attributes));}
|
|
444
|
+
if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > Ok message.attributes", JSON.stringify(message.attributes));}
|
|
445
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_END_USER_ID_KEY, message.attributes.requester_id);
|
|
446
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_END_USER_IP_ADDRESS_KEY, message.attributes.ipAddress);
|
|
447
|
+
if (message.attributes.payload) {
|
|
448
|
+
try {
|
|
449
|
+
for (const [key, value] of Object.entries(message.attributes.payload)) {
|
|
450
|
+
// const value = all_parameters[key];
|
|
451
|
+
const value_type = typeof value;
|
|
452
|
+
//if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > importing payload parameter:", key, "value:", value, "type:", value_type);}
|
|
453
|
+
//await chatbot.addParameter(key, String(value));
|
|
454
|
+
await chatbot.addParameter(key, value);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
catch(err) {
|
|
458
|
+
console.error("Error importing message payload in request variables:", err);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
if (chatbot.log) {
|
|
466
|
+
// console.log("tdcache:", chatbot.tdcache);
|
|
467
|
+
console.log("requestId:", requestId);
|
|
468
|
+
console.log("KEY:", TiledeskChatbotConst.REQ_PROJECT_ID_KEY);
|
|
469
|
+
console.log("TiledeskChatbot:", TiledeskChatbot);
|
|
470
|
+
let proj_ = await chatbot.getParameter(TiledeskChatbotConst.REQ_PROJECT_ID_KEY);
|
|
471
|
+
console.log("request parameter proj_:", proj_);
|
|
472
|
+
const all_parameters = await chatbot.allParameters();
|
|
473
|
+
for (const [key, value] of Object.entries(all_parameters)) {
|
|
474
|
+
// const value = all_parameters[key];
|
|
475
|
+
const value_type = typeof value;
|
|
476
|
+
if (chatbot.log) {console.log("request parameter:", key, "value:", value, "type:", value_type)}
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
} catch(error) {
|
|
480
|
+
console.error("Error", error)
|
|
481
|
+
process.exit(1)
|
|
482
|
+
}
|
|
483
|
+
// message["attributes"]: {
|
|
484
|
+
// "departmentId": "63c980054f857c00350535bc",
|
|
485
|
+
// "departmentName": "Default Department",
|
|
486
|
+
// "client": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36",
|
|
487
|
+
// "sourcePage": "https://tiledesk-html-site.tiledesk.repl.co/custom-attributes.html",
|
|
488
|
+
// "projectId": "63c980054f857c00350535b8",
|
|
489
|
+
// "payload": {
|
|
490
|
+
// "user_country": "Italy",
|
|
491
|
+
// "user_code": "E001"
|
|
492
|
+
// },
|
|
493
|
+
// "userFullname": "guest#7216 ",
|
|
494
|
+
// "requester_id": "7216926a-84c3-4bd5-aa79-8bd763094dc0",
|
|
495
|
+
// "ipAddress": "79.8.190.172",
|
|
496
|
+
// "sourceTitle": "Custom attributes",
|
|
497
|
+
// "widgetVer": "v.5.0.53-rc.4",
|
|
498
|
+
// "subtype": "info",
|
|
499
|
+
// "decoded_jwt": {
|
|
500
|
+
// "_id": "7216926a-84c3-4bd5-aa79-8bd763094dc0",
|
|
501
|
+
// "firstname": "guest#7216",
|
|
502
|
+
// "id": "7216926a-84c3-4bd5-aa79-8bd763094dc0",
|
|
503
|
+
// "fullName": "guest#7216 ",
|
|
504
|
+
// "iat": 1674201892,
|
|
505
|
+
// "aud": "https://tiledesk.com",
|
|
506
|
+
// "iss": "https://tiledesk.com",
|
|
507
|
+
// "sub": "guest",
|
|
508
|
+
// "jti": "f053af3d-14ca-411b-9903-78bd74e24218"
|
|
509
|
+
// }
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
static actionsToDirectives(actions) {
|
|
513
|
+
let directives = [];
|
|
514
|
+
if (actions && actions.length > 0) {
|
|
515
|
+
actions.forEach(action => {
|
|
516
|
+
let directive = Directives.actionToDirective(action);
|
|
517
|
+
if (directive) {
|
|
518
|
+
directives.push(directive);
|
|
519
|
+
}
|
|
520
|
+
});
|
|
521
|
+
}
|
|
522
|
+
return directives;
|
|
523
|
+
}
|
|
524
|
+
|
|
301
525
|
}
|
|
302
526
|
|
|
303
527
|
module.exports = { TiledeskChatbotUtil };
|
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) {
|
|
@@ -212,21 +212,21 @@ class DirectivesChatbotPlug {
|
|
|
212
212
|
directive_name = directive.name.toLowerCase();
|
|
213
213
|
}
|
|
214
214
|
if (directive && directive.action) {
|
|
215
|
-
console.log("Checking locks", JSON.stringify(directive));
|
|
215
|
+
// console.log("Checking locks", JSON.stringify(directive));
|
|
216
216
|
// try {
|
|
217
217
|
const action_id = directive.action["_tdActionId"];
|
|
218
|
-
console.log("Checking locked directive:", action_id, "for request:", this.supportRequest.request_id);
|
|
218
|
+
// console.log("Checking locked directive:", action_id, "for request:", this.supportRequest.request_id);
|
|
219
219
|
const locked_action_id = await this.chatbot.currentLockedAction(this.supportRequest.request_id);
|
|
220
|
-
console.log("locked_action_id:", locked_action_id);
|
|
220
|
+
// console.log("locked_action_id:", locked_action_id);
|
|
221
221
|
if ( locked_action_id && (locked_action_id !== action_id) ) {
|
|
222
|
-
console.log("Found locked action:", locked_action_id, "Skipping this action:", action_id);
|
|
222
|
+
// console.log("Found locked action:", locked_action_id, "Skipping this action:", action_id);
|
|
223
223
|
let next_dir = await this.nextDirective(this.directives);
|
|
224
224
|
this.process(next_dir);
|
|
225
225
|
return;
|
|
226
226
|
}
|
|
227
227
|
else {
|
|
228
228
|
// go on
|
|
229
|
-
console.log("Going on to next directive...");
|
|
229
|
+
// console.log("Going on to next directive...");
|
|
230
230
|
}
|
|
231
231
|
// }
|
|
232
232
|
// catch(error) {
|
|
@@ -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
|
});
|
|
File without changes
|