@tiledesk/tiledesk-tybot-connector 0.2.38 → 0.2.40
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/TiledeskChatbotUtil.js +203 -184
- package/package.json +2 -2
- package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +10 -3
- package/tiledeskChatbotPlugs/directives/DirAskGPT.js +7 -2
- package/tiledeskChatbotPlugs/directives/DirGptTask.js +5 -0
- package/tiledeskChatbotPlugs/directives/DirMake.js +202 -91
- package/tiledeskChatbotPlugs/directives/DirQapla.js +1 -1
- package/tiledeskChatbotPlugs/directives/DirSetConversationTags.js +127 -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.40
|
|
9
|
+
- refactorized Make action
|
|
10
|
+
- added support for public gptkey for GptTask and AskGPT actions
|
|
11
|
+
- fixes bug: AskGPT action not working
|
|
12
|
+
|
|
13
|
+
# v0.2.39
|
|
14
|
+
- fixed bug: wrong update of userFullname based on lead.fullname
|
|
15
|
+
|
|
8
16
|
# v0.2.38
|
|
9
17
|
- improved Qapla' action with condition
|
|
10
18
|
|
|
@@ -373,207 +373,226 @@ class TiledeskChatbotUtil {
|
|
|
373
373
|
static async updateRequestAttributes(chatbot, message, projectId, requestId) {
|
|
374
374
|
// update request context
|
|
375
375
|
try {
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
376
|
+
if (chatbot.log) {console.log("Updating request variables. Message:", JSON.stringify(message));}
|
|
377
|
+
const messageId = message._id;
|
|
378
|
+
const chat_url = `https://panel.tiledesk.com/v3/dashboard/#/project/${projectId}/wsrequest/${requestId}/messages`
|
|
379
|
+
// await chatbot.addParameter("chatbot", chatbot);
|
|
380
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_CHAT_URL, chat_url);
|
|
381
|
+
// console.log("Adding proj_", projectId);
|
|
382
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_PROJECT_ID_KEY, projectId);
|
|
383
|
+
// TODO add projectName too
|
|
384
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_REQUEST_ID_KEY, requestId);
|
|
385
|
+
if (chatbot.bot) {
|
|
386
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_NAME_KEY, chatbot.bot.name);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
if (message.text && message.sender !== "_tdinternal") {
|
|
390
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_TEXT_KEY, message.text); // DEPRECATED
|
|
391
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_TEXT_v2_KEY, message.text);
|
|
392
|
+
if (message.channel) {
|
|
393
|
+
if (message.channel.name === "chat21") {
|
|
394
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_CHAT_CHANNEL, "web"); // renames the channel in chat21
|
|
395
|
+
}
|
|
396
|
+
else {
|
|
397
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_CHAT_CHANNEL, message.channel.name);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_MESSAGE_TYPE_KEY, message.type);
|
|
401
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_MESSAGE_KEY, TiledeskChatbotUtil.lastUserMessageFrom(message)); // JSON TYPE *NEW
|
|
402
|
+
// get image
|
|
403
|
+
if (message.type && message.type === "image" && message.metadata) {
|
|
404
|
+
// "text": "\nimage text",
|
|
405
|
+
// "id_project": "65203e12f8c0cf002cf4110b",
|
|
406
|
+
// "createdBy": "8ac52a30-133f-4ee1-8b4b-96055bb81757",
|
|
407
|
+
// "metadata": {
|
|
408
|
+
// "height": 905,
|
|
409
|
+
// "name": "tiledesk_Open graph_general.png",
|
|
410
|
+
// "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",
|
|
411
|
+
// "type": "image/png",
|
|
412
|
+
// "uid": "lo68iyq5",
|
|
413
|
+
// "width": 1724
|
|
414
|
+
// }
|
|
415
|
+
if (message.metadata.src) {
|
|
416
|
+
await chatbot.addParameter("lastUserImageURL", message.metadata.src);
|
|
417
|
+
await chatbot.addParameter("lastUserImageName", message.metadata.name);
|
|
418
|
+
await chatbot.addParameter("lastUserImageWidth", message.metadata.width);
|
|
419
|
+
await chatbot.addParameter("lastUserImageHeight", message.metadata.height);
|
|
420
|
+
await chatbot.addParameter("lastUserImageType", message.metadata.type);
|
|
421
|
+
}
|
|
395
422
|
}
|
|
396
423
|
else {
|
|
397
|
-
await chatbot.addParameter(
|
|
424
|
+
await chatbot.addParameter("lastUserImageURL", null);
|
|
425
|
+
await chatbot.addParameter("lastUserImageName", null);
|
|
426
|
+
await chatbot.addParameter("lastUserImageWidth", null);
|
|
427
|
+
await chatbot.addParameter("lastUserImageHeight", null);
|
|
428
|
+
await chatbot.addParameter("lastUserImageType", null);
|
|
398
429
|
}
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
430
|
+
// get document
|
|
431
|
+
if (message.type && message.type === "file" && message.metadata) {
|
|
432
|
+
// "type": "file",
|
|
433
|
+
// "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",
|
|
434
|
+
// "metadata": {
|
|
435
|
+
// "name": "LIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf",
|
|
436
|
+
// "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",
|
|
437
|
+
// "type": "application/pdf",
|
|
438
|
+
// "uid": "lo68oz8i"
|
|
439
|
+
// }
|
|
440
|
+
if (message.metadata.src) {
|
|
441
|
+
await chatbot.addParameter("lastUserDocumentURL", message.metadata.src);
|
|
442
|
+
await chatbot.addParameter("lastUserDocumentName", message.metadata.name);
|
|
443
|
+
await chatbot.addParameter("lastUserDocumentType", message.metadata.type);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
else {
|
|
447
|
+
await chatbot.addParameter("lastUserDocumentURL", null);
|
|
448
|
+
await chatbot.addParameter("lastUserDocumentName", null);
|
|
449
|
+
await chatbot.addParameter("lastUserDocumentType", null);
|
|
450
|
+
}
|
|
451
|
+
if (message && message.request && message.request.lead) {
|
|
452
|
+
if (message.request.lead.email) {
|
|
453
|
+
await chatbot.addParameter("userEmail", message.request.lead.email);
|
|
454
|
+
}
|
|
455
|
+
if (message.request.lead.fullname) {
|
|
456
|
+
console.log("got lead.fullname:", message.request.lead.fullname);
|
|
457
|
+
try {
|
|
458
|
+
const current_userFullname = await chatbot.getParameter("userFullname");
|
|
459
|
+
console.log("current_userFullname:", current_userFullname);
|
|
460
|
+
if (current_userFullname && current_userFullname.startsWith("guest#")) {
|
|
461
|
+
console.log("current_userFullname && current_userFullname.startsWith('guest#')");
|
|
462
|
+
await chatbot.addParameter("userFullname", message.request.lead.fullname);
|
|
463
|
+
}
|
|
464
|
+
else if (!current_userFullname) {
|
|
465
|
+
console.log("no current_userFullname");
|
|
466
|
+
await chatbot.addParameter("userFullname", message.request.lead.fullname);
|
|
467
|
+
console.log("it was null, set to the incoming one");
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
catch(error) {
|
|
471
|
+
console.error("Error on setting userFullname:", error);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
else {
|
|
475
|
+
console.log("!lead.fullname");
|
|
476
|
+
}
|
|
477
|
+
if (message.request.lead.phone) {
|
|
478
|
+
await chatbot.addParameter("userPhone", message.request.lead.phone);
|
|
479
|
+
}
|
|
480
|
+
if (message.request.lead.lead_id) {
|
|
481
|
+
await chatbot.addParameter("userLeadId", message.request.lead.lead_id);
|
|
482
|
+
}
|
|
483
|
+
if (message.request.lead.company) {
|
|
484
|
+
await chatbot.addParameter("userCompany", message.request.lead.company);
|
|
485
|
+
}
|
|
421
486
|
}
|
|
422
487
|
}
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_MESSAGE_ID_KEY, messageId);
|
|
491
|
+
if (message.request && message.request.location && message.request.location.country) {
|
|
492
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_COUNTRY_KEY, message.request.location.country);
|
|
493
|
+
}
|
|
494
|
+
if (message.request && message.request.location && message.request.location.city) {
|
|
495
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_CITY_KEY, message.request.location.city);
|
|
429
496
|
}
|
|
430
|
-
//
|
|
431
|
-
if (message.
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
// "
|
|
435
|
-
|
|
436
|
-
//
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
// }
|
|
440
|
-
if (message.metadata.src) {
|
|
441
|
-
await chatbot.addParameter("lastUserDocumentURL", message.metadata.src);
|
|
442
|
-
await chatbot.addParameter("lastUserDocumentName", message.metadata.name);
|
|
443
|
-
await chatbot.addParameter("lastUserDocumentType", message.metadata.type);
|
|
497
|
+
// console.log("message.request.language", message.request["language"]);
|
|
498
|
+
if (message.request) {
|
|
499
|
+
let user_language = message.request["language"];
|
|
500
|
+
if (message.request["language"]) {
|
|
501
|
+
// console.log("HTTP language:", message.request["language"]);
|
|
502
|
+
var languages = parser.parse(message.request["language"]);
|
|
503
|
+
// console.log("languages:", languages);
|
|
504
|
+
if (languages && languages.length > 0 && languages[0].code) {
|
|
505
|
+
user_language = languages[0].code;
|
|
444
506
|
}
|
|
445
507
|
}
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
await chatbot.addParameter("lastUserDocumentType", null);
|
|
508
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_SOURCE_PAGE_KEY, message.request.sourcePage);
|
|
509
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_LANGUAGE_KEY, user_language);
|
|
510
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_AGENT_KEY, message.request.userAgent);
|
|
450
511
|
}
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
512
|
+
// console.log("message.request.language", message.request["language"])
|
|
513
|
+
if (message.request && message.request.department) {
|
|
514
|
+
// It was an error when getting this from widget message's attributes
|
|
515
|
+
// await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY, message.attributes.departmentId);
|
|
516
|
+
// await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY, message.attributes.departmentName);
|
|
517
|
+
// get from request.department instead
|
|
518
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY, message.request.department._id);
|
|
519
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY, message.request.department.name);
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
if (projectId === "641864da99c1fb00131ba495") {
|
|
523
|
+
console.log("641864da99c1fb00131ba495 > for projectId:", JSON.stringify(message))
|
|
524
|
+
}
|
|
525
|
+
// for BUG
|
|
526
|
+
// if (chatbot.log) {console.log("message.request.attributes.payload", JSON.stringify(message.request.attributes.payload))}
|
|
527
|
+
if (message && message.request && message.request.attributes && message.request.attributes.payload) {
|
|
528
|
+
if (!message.attributes) {
|
|
529
|
+
message.attributes = {}
|
|
530
|
+
}
|
|
531
|
+
message.attributes.payload = message.request.attributes.payload
|
|
532
|
+
if (chatbot.log) {console.log("FORCED SET message.attributes.payload:", JSON.stringify(message.attributes.payload))}
|
|
533
|
+
// if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > FORCED SET message.attributes.payload:", JSON.stringify(message.attributes.payload))}
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
if (message.attributes) {
|
|
537
|
+
if (chatbot.log) {console.log("Ok message.attributes", JSON.stringify(message.attributes));}
|
|
538
|
+
if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > Ok message.attributes", JSON.stringify(message.attributes));}
|
|
539
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_END_USER_ID_KEY, message.attributes.requester_id);
|
|
540
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_END_USER_IP_ADDRESS_KEY, message.attributes.ipAddress);
|
|
541
|
+
if (message.attributes.payload) {
|
|
542
|
+
try {
|
|
543
|
+
for (const [key, value] of Object.entries(message.attributes.payload)) {
|
|
544
|
+
// const value = all_parameters[key];
|
|
545
|
+
const value_type = typeof value;
|
|
546
|
+
//if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > importing payload parameter:", key, "value:", value, "type:", value_type);}
|
|
547
|
+
//await chatbot.addParameter(key, String(value));
|
|
548
|
+
await chatbot.addParameter(key, value);
|
|
460
549
|
}
|
|
461
|
-
if (message.request.lead.lead_id) {
|
|
462
|
-
await chatbot.addParameter("userLeadId", message.request.lead.lead_id);
|
|
463
550
|
}
|
|
464
|
-
|
|
465
|
-
|
|
551
|
+
catch(err) {
|
|
552
|
+
console.error("Error importing message payload in request variables:", err);
|
|
466
553
|
}
|
|
467
554
|
}
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_MESSAGE_ID_KEY, messageId);
|
|
472
|
-
if (message.request && message.request.location && message.request.location.country) {
|
|
473
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_COUNTRY_KEY, message.request.location.country);
|
|
474
|
-
}
|
|
475
|
-
if (message.request && message.request.location && message.request.location.city) {
|
|
476
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_CITY_KEY, message.request.location.city);
|
|
477
|
-
}
|
|
478
|
-
// console.log("message.request.language", message.request["language"]);
|
|
479
|
-
if (message.request) {
|
|
480
|
-
let user_language = message.request["language"];
|
|
481
|
-
if (message.request["language"]) {
|
|
482
|
-
// console.log("HTTP language:", message.request["language"]);
|
|
483
|
-
var languages = parser.parse(message.request["language"]);
|
|
484
|
-
// console.log("languages:", languages);
|
|
485
|
-
if (languages && languages.length > 0 && languages[0].code) {
|
|
486
|
-
user_language = languages[0].code;
|
|
487
555
|
}
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
}
|
|
493
|
-
// console.log("message.request.language", message.request["language"])
|
|
494
|
-
if (message.request && message.request.department) {
|
|
495
|
-
// It was an error when getting this from widget message's attributes
|
|
496
|
-
// await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY, message.attributes.departmentId);
|
|
497
|
-
// await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY, message.attributes.departmentName);
|
|
498
|
-
// get from request.department instead
|
|
499
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY, message.request.department._id);
|
|
500
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY, message.request.department.name);
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
if (projectId === "641864da99c1fb00131ba495") {
|
|
504
|
-
console.log("641864da99c1fb00131ba495 > for projectId:", JSON.stringify(message))
|
|
505
|
-
}
|
|
506
|
-
// for BUG
|
|
507
|
-
// if (chatbot.log) {console.log("message.request.attributes.payload", JSON.stringify(message.request.attributes.payload))}
|
|
508
|
-
if (message && message.request && message.request.attributes && message.request.attributes.payload) {
|
|
509
|
-
if (!message.attributes) {
|
|
510
|
-
message.attributes = {}
|
|
511
|
-
}
|
|
512
|
-
message.attributes.payload = message.request.attributes.payload
|
|
513
|
-
if (chatbot.log) {console.log("FORCED SET message.attributes.payload:", JSON.stringify(message.attributes.payload))}
|
|
514
|
-
// if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > FORCED SET message.attributes.payload:", JSON.stringify(message.attributes.payload))}
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
if (message.attributes) {
|
|
518
|
-
if (chatbot.log) {console.log("Ok message.attributes", JSON.stringify(message.attributes));}
|
|
519
|
-
if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > Ok message.attributes", JSON.stringify(message.attributes));}
|
|
520
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_END_USER_ID_KEY, message.attributes.requester_id);
|
|
521
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_END_USER_IP_ADDRESS_KEY, message.attributes.ipAddress);
|
|
522
|
-
if (message.attributes.payload) {
|
|
523
|
-
try {
|
|
524
|
-
for (const [key, value] of Object.entries(message.attributes.payload)) {
|
|
525
|
-
// const value = all_parameters[key];
|
|
526
|
-
const value_type = typeof value;
|
|
527
|
-
//if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > importing payload parameter:", key, "value:", value, "type:", value_type);}
|
|
528
|
-
//await chatbot.addParameter(key, String(value));
|
|
529
|
-
await chatbot.addParameter(key, value);
|
|
530
|
-
}
|
|
556
|
+
|
|
557
|
+
const _bot = chatbot.bot; // aka FaqKB
|
|
558
|
+
if (chatbot.log) {
|
|
559
|
+
console.log("Adding Globals to context..., chatbot.attributes?", JSON.stringify(_bot));
|
|
531
560
|
}
|
|
532
|
-
|
|
533
|
-
|
|
561
|
+
|
|
562
|
+
if (_bot.attributes && _bot.attributes.globals) {
|
|
563
|
+
if (chatbot.log) {console.log("Got Globals:", JSON.stringify(_bot.attributes.globals));}
|
|
564
|
+
_bot.attributes.globals.forEach(async (global_var) => {
|
|
565
|
+
if (chatbot.log) {console.log("Adding global:", global_var.key, "value:", global_var.value);}
|
|
566
|
+
await chatbot.addParameter(global_var.key, global_var.value);
|
|
567
|
+
});
|
|
534
568
|
}
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
if (chatbot.log) {
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
console.log("requestId:", requestId);
|
|
562
|
-
console.log("KEY:", TiledeskChatbotConst.REQ_PROJECT_ID_KEY);
|
|
563
|
-
// console.log("TiledeskChatbot:", TiledeskChatbot);
|
|
564
|
-
let proj_ = await chatbot.getParameter(TiledeskChatbotConst.REQ_PROJECT_ID_KEY);
|
|
565
|
-
console.log("request parameter proj_:", proj_);
|
|
566
|
-
const all_parameters = await chatbot.allParameters();
|
|
567
|
-
for (const [key, value] of Object.entries(all_parameters)) {
|
|
568
|
-
// const value = all_parameters[key];
|
|
569
|
-
const value_type = typeof value;
|
|
570
|
-
if (chatbot.log) {console.log("REQUEST ATTRIBUTE:", key, "VALUE:", value, "TYPE:", value_type)}
|
|
571
|
-
}
|
|
569
|
+
// await chatbot.addParameter("testVar",
|
|
570
|
+
// {
|
|
571
|
+
// name: "Andrea",
|
|
572
|
+
// coords: {
|
|
573
|
+
// x: 2, y: 1
|
|
574
|
+
// }
|
|
575
|
+
// }
|
|
576
|
+
// );
|
|
577
|
+
|
|
578
|
+
if (chatbot.log) {
|
|
579
|
+
// console.log("tdcache:", chatbot.tdcache);
|
|
580
|
+
console.log("requestId:", requestId);
|
|
581
|
+
console.log("KEY:", TiledeskChatbotConst.REQ_PROJECT_ID_KEY);
|
|
582
|
+
// console.log("TiledeskChatbot:", TiledeskChatbot);
|
|
583
|
+
let proj_ = await chatbot.getParameter(TiledeskChatbotConst.REQ_PROJECT_ID_KEY);
|
|
584
|
+
console.log("request parameter proj_:", proj_);
|
|
585
|
+
const all_parameters = await chatbot.allParameters();
|
|
586
|
+
for (const [key, value] of Object.entries(all_parameters)) {
|
|
587
|
+
// const value = all_parameters[key];
|
|
588
|
+
const value_type = typeof value;
|
|
589
|
+
if (chatbot.log) {console.log("REQUEST ATTRIBUTE:", key, "VALUE:", value, "TYPE:", value_type)}
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
} catch(error) {
|
|
593
|
+
console.error("Error", error)
|
|
594
|
+
process.exit(1)
|
|
572
595
|
}
|
|
573
|
-
} catch(error) {
|
|
574
|
-
console.error("Error", error)
|
|
575
|
-
process.exit(1)
|
|
576
|
-
}
|
|
577
596
|
// message["attributes"]: {
|
|
578
597
|
// "departmentId": "63c980054f857c00350535bc",
|
|
579
598
|
// "departmentName": "Default Department",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tiledesk/tiledesk-tybot-connector",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.40",
|
|
4
4
|
"description": "Tiledesk Tybot connector",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"@tiledesk/helpcenter-query-client": "^0.1.8",
|
|
15
15
|
"@tiledesk/tiledesk-chatbot-client": "^0.5.30",
|
|
16
16
|
"@tiledesk/tiledesk-chatbot-util": "^0.8.39",
|
|
17
|
-
"@tiledesk/tiledesk-client": "^0.10.
|
|
17
|
+
"@tiledesk/tiledesk-client": "^0.10.5",
|
|
18
18
|
"accept-language-parser": "^1.5.0",
|
|
19
19
|
"axios": "^0.27.2",
|
|
20
20
|
"body-parser": "^1.19.0",
|
|
@@ -632,9 +632,16 @@ class DirectivesChatbotPlug {
|
|
|
632
632
|
})
|
|
633
633
|
}
|
|
634
634
|
else if (directive_name === Directives.MAKE) {
|
|
635
|
-
new DirMake(context).execute(directive, async () => {
|
|
636
|
-
|
|
637
|
-
|
|
635
|
+
new DirMake(context).execute(directive, async (stop) => {
|
|
636
|
+
if (context.log) { console.log("DirMake stop?", stop);}
|
|
637
|
+
if (stop == true) {
|
|
638
|
+
if (context.log) { console.log("Stopping Actions on:", JSON.stringify(directive));}
|
|
639
|
+
this.theend();
|
|
640
|
+
}
|
|
641
|
+
else {
|
|
642
|
+
let next_dir = await this.nextDirective(this.directives);
|
|
643
|
+
this.process(next_dir);
|
|
644
|
+
}
|
|
638
645
|
})
|
|
639
646
|
}
|
|
640
647
|
else {
|
|
@@ -100,6 +100,11 @@ class DirAskGPT {
|
|
|
100
100
|
key = await this.getKeyFromKbSettings(server_base_url);
|
|
101
101
|
}
|
|
102
102
|
|
|
103
|
+
if (!key) {
|
|
104
|
+
if (this.log) { console.log("DirGptTask - Retrieve public gptkey")}
|
|
105
|
+
key = process.env.GPTKEY;
|
|
106
|
+
}
|
|
107
|
+
|
|
103
108
|
if (!key) {
|
|
104
109
|
console.error("Error: DirAskGPT gptkey is mandatory");
|
|
105
110
|
await this.#assignAttributes(action, answer);
|
|
@@ -299,7 +304,7 @@ class DirAskGPT {
|
|
|
299
304
|
this.#myrequest(
|
|
300
305
|
INTEGRATIONS_HTTPREQUEST, async (err, integration) => {
|
|
301
306
|
if (err) {
|
|
302
|
-
console.error("DirAskGPT Get integrations error ",
|
|
307
|
+
if (this.log) { console.error("DirAskGPT Get integrations error ", err); }
|
|
303
308
|
resolve(null);
|
|
304
309
|
} else {
|
|
305
310
|
|
|
@@ -331,7 +336,7 @@ class DirAskGPT {
|
|
|
331
336
|
this.#myrequest(
|
|
332
337
|
KB_HTTPREQUEST, async (err, resbody) => {
|
|
333
338
|
if (err) {
|
|
334
|
-
console.error("DirGptTask Get kb settings error ",
|
|
339
|
+
if (this.log) { console.error("DirGptTask Get kb settings error ", err); }
|
|
335
340
|
resolve(null);
|
|
336
341
|
} else {
|
|
337
342
|
if (!resbody.gptkey) {
|
|
@@ -99,6 +99,11 @@ class DirGptTask {
|
|
|
99
99
|
key = await this.getKeyFromKbSettings(server_base_url);
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
+
if (!key) {
|
|
103
|
+
if (this.log) { console.log("DirGptTask - Retrieve public gptkey")}
|
|
104
|
+
key = process.env.GPTKEY;
|
|
105
|
+
}
|
|
106
|
+
|
|
102
107
|
if (!key) {
|
|
103
108
|
console.error("DirGptTask gptkey is mandatory");
|
|
104
109
|
await this.#assignAttributes(action, answer);
|
|
@@ -5,7 +5,6 @@ const { DirIntent } = require("./DirIntent");
|
|
|
5
5
|
let https = require("https");
|
|
6
6
|
require('dotenv').config();
|
|
7
7
|
|
|
8
|
-
|
|
9
8
|
class DirMake {
|
|
10
9
|
|
|
11
10
|
constructor(context) {
|
|
@@ -15,9 +14,8 @@ class DirMake {
|
|
|
15
14
|
this.context = context;
|
|
16
15
|
this.tdcache = this.context.tdcache;
|
|
17
16
|
this.requestId = this.context.requestId;
|
|
18
|
-
this.log = context.log;
|
|
19
17
|
this.intentDir = new DirIntent(context);
|
|
20
|
-
|
|
18
|
+
this.log = context.log;
|
|
21
19
|
}
|
|
22
20
|
|
|
23
21
|
execute(directive, callback) {
|
|
@@ -31,80 +29,97 @@ class DirMake {
|
|
|
31
29
|
callback();
|
|
32
30
|
return;
|
|
33
31
|
}
|
|
34
|
-
this.go(action, () => {
|
|
35
|
-
callback();
|
|
32
|
+
this.go(action, (stop) => {
|
|
33
|
+
callback(stop);
|
|
36
34
|
})
|
|
37
35
|
}
|
|
38
36
|
|
|
39
37
|
async go(action, callback) {
|
|
40
38
|
if (this.log) { console.log("DirMake action:", JSON.stringify(action)); }
|
|
41
|
-
let trueIntent = action.trueIntent;
|
|
42
|
-
let falseIntent = action.falseIntent;
|
|
43
|
-
if (this.log) {console.log('DirMake trueIntent',trueIntent)}
|
|
44
39
|
if (!this.tdcache) {
|
|
45
40
|
console.error("Error: DirMake tdcache is mandatory");
|
|
46
41
|
callback();
|
|
47
42
|
return;
|
|
48
43
|
}
|
|
49
|
-
|
|
44
|
+
|
|
45
|
+
let trueIntent = action.trueIntent;
|
|
46
|
+
let falseIntent = action.falseIntent;
|
|
47
|
+
let trueIntentAttributes = action.trueIntentAttributes;
|
|
48
|
+
let falseIntentAttributes = action.falseIntentAttributes;
|
|
49
|
+
|
|
50
|
+
if (this.log) {
|
|
51
|
+
console.log("DirAskGPT trueIntent", trueIntent)
|
|
52
|
+
console.log("DirAskGPT falseIntent", falseIntent)
|
|
53
|
+
console.log("DirAskGPT trueIntentAttributes", trueIntentAttributes)
|
|
54
|
+
console.log("DirAskGPT falseIntentAttributes", falseIntentAttributes)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// default values?
|
|
58
|
+
let status = null;
|
|
59
|
+
let error = null;
|
|
60
|
+
|
|
50
61
|
let requestVariables = null;
|
|
51
62
|
requestVariables =
|
|
52
63
|
await TiledeskChatbot.allParametersStatic(
|
|
53
64
|
this.tdcache, this.requestId
|
|
54
65
|
)
|
|
55
66
|
|
|
56
|
-
if (this.log) {
|
|
57
|
-
const all_parameters = await TiledeskChatbot.allParametersStatic(this.context.tdcache, this.context.requestId);
|
|
58
|
-
for (const [key, value] of Object.entries(all_parameters)) {
|
|
59
|
-
if (this.log) { console.log("DirMake request parameter:", key, "value:", value, "type:", typeof value) }
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
67
|
let webhook_url = action.url;
|
|
64
68
|
let bodyParameters = action.bodyParameters;
|
|
69
|
+
|
|
65
70
|
if (this.log) {
|
|
66
71
|
console.log("DirMake webhook_url: ", webhook_url);
|
|
67
|
-
console.log("DirMake bodyParameters: ", bodyParameters);
|
|
72
|
+
console.log("DirMake bodyParameters: ", JSON.stringify(bodyParameters));
|
|
68
73
|
}
|
|
69
|
-
|
|
70
|
-
|
|
74
|
+
|
|
75
|
+
if (!bodyParameters) {
|
|
76
|
+
console.error("Error: DirMake bodyParameters is undefined");
|
|
77
|
+
error = "Missing body parameters";
|
|
78
|
+
await this.#assignAttributes(action, status, error);
|
|
79
|
+
if (falseIntent) {
|
|
80
|
+
await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
|
|
81
|
+
callback(true);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
71
84
|
callback();
|
|
72
85
|
return;
|
|
73
86
|
}
|
|
87
|
+
|
|
74
88
|
if (!webhook_url || webhook_url === '') {
|
|
75
|
-
|
|
76
|
-
let status = 422;
|
|
89
|
+
console.error("Error: DirMake Webhook Url is not defined");
|
|
90
|
+
let status = 422;
|
|
77
91
|
let error = 'Missing make webhook url';
|
|
78
92
|
await this.#assignAttributes(action, status, error);
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
93
|
+
if (falseIntent) {
|
|
94
|
+
await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
|
|
95
|
+
callback(true);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
callback();
|
|
82
99
|
return;
|
|
83
100
|
}
|
|
84
|
-
|
|
85
|
-
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* process.env.MAKE_ENDPOINT is used for testing purposes only.
|
|
104
|
+
* This variable must be not defined in Production Env.
|
|
105
|
+
*/
|
|
86
106
|
let make_base_url = process.env.MAKE_ENDPOINT;
|
|
87
|
-
|
|
88
|
-
url = make_base_url + "/make/";
|
|
89
|
-
} else {
|
|
90
|
-
url = action.url;
|
|
91
|
-
}
|
|
92
|
-
const filler = new Filler();
|
|
93
|
-
for (const [key, value] of Object.entries(bodyParameters)) {
|
|
94
|
-
//if (this.log) {console.log("bodyParam:", key, "value:", value)}
|
|
95
|
-
let filled_value = filler.fill(value, requestVariables);
|
|
96
|
-
bodyParameters[key] = filled_value;
|
|
97
|
-
}
|
|
98
|
-
if (this.log) {console.log('DirMake bodyParameters filler: ',bodyParameters)}
|
|
99
|
-
|
|
107
|
+
let url;
|
|
100
108
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
109
|
+
if (make_base_url) {
|
|
110
|
+
url = make_base_url + "/make/";
|
|
111
|
+
} else {
|
|
112
|
+
url = action.url;
|
|
113
|
+
}
|
|
107
114
|
if (this.log) { console.log("DirMake MakeEndpoint URL: ", url); }
|
|
115
|
+
|
|
116
|
+
const filler = new Filler();
|
|
117
|
+
for (const [key, value] of Object.entries(bodyParameters)) {
|
|
118
|
+
let filled_value = filler.fill(value, requestVariables);
|
|
119
|
+
bodyParameters[key] = filled_value;
|
|
120
|
+
}
|
|
121
|
+
if (this.log) { console.log('DirMake bodyParameters filler: ', bodyParameters) }
|
|
122
|
+
|
|
108
123
|
const MAKE_HTTPREQUEST = {
|
|
109
124
|
url: url,
|
|
110
125
|
headers: {
|
|
@@ -113,37 +128,46 @@ class DirMake {
|
|
|
113
128
|
json: bodyParameters,
|
|
114
129
|
method: "POST"
|
|
115
130
|
}
|
|
116
|
-
|
|
117
|
-
if (this.log) { console.log("myrequest/DirMake MAKE_HTTPREQUEST", MAKE_HTTPREQUEST); }
|
|
131
|
+
if (this.log) { console.log("DirMake MAKE_HTTPREQUEST", MAKE_HTTPREQUEST); }
|
|
118
132
|
this.#myrequest(
|
|
119
|
-
MAKE_HTTPREQUEST, async (err,
|
|
133
|
+
MAKE_HTTPREQUEST, async (err, res) => {
|
|
120
134
|
if (err) {
|
|
121
135
|
if (callback) {
|
|
122
|
-
|
|
123
|
-
let status = 404;
|
|
124
|
-
let error = 'Make url not found';
|
|
136
|
+
console.error("(httprequest) DirMake err:", err);
|
|
137
|
+
// let status = 404;
|
|
138
|
+
// let error = 'Make url not found';
|
|
139
|
+
status = res.status;
|
|
140
|
+
error = res.error;
|
|
125
141
|
await this.#assignAttributes(action, status, error);
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
142
|
+
if (falseIntent) {
|
|
143
|
+
await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
|
|
144
|
+
callback(true);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
129
147
|
callback();
|
|
148
|
+
return;
|
|
130
149
|
}
|
|
131
150
|
} else if (callback) {
|
|
132
|
-
if (this.log) { console.log("
|
|
133
|
-
let status = 200;
|
|
134
|
-
let error = null;
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
151
|
+
if (this.log) { console.log("(httprequest) DirMake resbody ", res); }
|
|
152
|
+
// let status = 200;
|
|
153
|
+
// let error = null;
|
|
154
|
+
status = res.status;
|
|
155
|
+
error = null;
|
|
156
|
+
if (res.error) {
|
|
157
|
+
error = res.error
|
|
158
|
+
}
|
|
159
|
+
await this.#assignAttributes(action, status, error);
|
|
160
|
+
if (trueIntent) {
|
|
161
|
+
await this.#executeCondition(true, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
|
|
162
|
+
callback(true);
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
callback();
|
|
166
|
+
return;
|
|
141
167
|
}
|
|
142
168
|
}
|
|
143
169
|
);
|
|
144
|
-
|
|
145
|
-
console.error('error: ', e)
|
|
146
|
-
}
|
|
170
|
+
|
|
147
171
|
}
|
|
148
172
|
|
|
149
173
|
async #assignAttributes(action, status, error) {
|
|
@@ -170,6 +194,7 @@ class DirMake {
|
|
|
170
194
|
}
|
|
171
195
|
}
|
|
172
196
|
|
|
197
|
+
// Advanced #myrequest function
|
|
173
198
|
#myrequest(options, callback) {
|
|
174
199
|
if (this.log) {
|
|
175
200
|
console.log("API URL:", options.url);
|
|
@@ -179,7 +204,8 @@ class DirMake {
|
|
|
179
204
|
url: options.url,
|
|
180
205
|
method: options.method,
|
|
181
206
|
params: options.params,
|
|
182
|
-
headers: options.headers
|
|
207
|
+
headers: options.headers,
|
|
208
|
+
timeout: 20000
|
|
183
209
|
}
|
|
184
210
|
if (options.json !== null) {
|
|
185
211
|
axios_options.data = options.json
|
|
@@ -197,61 +223,146 @@ class DirMake {
|
|
|
197
223
|
.then((res) => {
|
|
198
224
|
if (this.log) {
|
|
199
225
|
console.log("Response for url:", options.url);
|
|
200
|
-
console.log("Response status:", res.status);
|
|
201
226
|
console.log("Response headers:\n", JSON.stringify(res.headers));
|
|
202
227
|
}
|
|
203
|
-
if (
|
|
204
|
-
|
|
205
|
-
callback(null, res.data);
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
else {
|
|
209
|
-
if (callback) {
|
|
210
|
-
callback(new Error("Response status is not 200"), null);
|
|
211
|
-
}
|
|
228
|
+
if (callback) {
|
|
229
|
+
callback(null, res);
|
|
212
230
|
}
|
|
231
|
+
|
|
213
232
|
})
|
|
214
|
-
.catch((
|
|
215
|
-
if (this.log) {
|
|
233
|
+
.catch((err) => {
|
|
234
|
+
if (this.log) {
|
|
235
|
+
// FIX THE STRINGIFY OF CIRCULAR STRUCTURE BUG - START
|
|
236
|
+
let cache = [];
|
|
237
|
+
let error_log = JSON.stringify(err, function (key, value) { // try to use a separate function
|
|
238
|
+
if (typeof value === 'object' && value != null) {
|
|
239
|
+
if (cache.indexOf(value) !== -1) {
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
cache.push(value);
|
|
243
|
+
}
|
|
244
|
+
return value;
|
|
245
|
+
});
|
|
246
|
+
console.error("An error occurred: ", error_log);
|
|
247
|
+
// FIX THE STRINGIFY OF CIRCULAR STRUCTURE BUG - END
|
|
248
|
+
// console.error("An error occurred:", JSON.stringify(err));
|
|
249
|
+
}
|
|
216
250
|
if (callback) {
|
|
217
|
-
|
|
251
|
+
let status = 1000;
|
|
252
|
+
let cache = [];
|
|
253
|
+
let str_error = JSON.stringify(err, function (key, value) { // try to use a separate function
|
|
254
|
+
if (typeof value === 'object' && value != null) {
|
|
255
|
+
if (cache.indexOf(value) !== -1) {
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
cache.push(value);
|
|
259
|
+
}
|
|
260
|
+
return value;
|
|
261
|
+
});
|
|
262
|
+
let error = JSON.parse(str_error) // "status" disappears without this trick
|
|
263
|
+
let errorMessage = JSON.stringify(error);
|
|
264
|
+
if (error.status) {
|
|
265
|
+
status = error.status;
|
|
266
|
+
}
|
|
267
|
+
if (error.message) {
|
|
268
|
+
errorMessage = error.message;
|
|
269
|
+
}
|
|
270
|
+
callback(
|
|
271
|
+
null, {
|
|
272
|
+
status: status,
|
|
273
|
+
data: null,
|
|
274
|
+
error: errorMessage
|
|
275
|
+
}
|
|
276
|
+
);
|
|
218
277
|
}
|
|
219
278
|
});
|
|
220
279
|
}
|
|
280
|
+
|
|
281
|
+
// #myrequest(options, callback) {
|
|
282
|
+
// if (this.log) {
|
|
283
|
+
// console.log("API URL:", options.url);
|
|
284
|
+
// console.log("** Options:", JSON.stringify(options));
|
|
285
|
+
// }
|
|
286
|
+
// let axios_options = {
|
|
287
|
+
// url: options.url,
|
|
288
|
+
// method: options.method,
|
|
289
|
+
// params: options.params,
|
|
290
|
+
// headers: options.headers
|
|
291
|
+
// }
|
|
292
|
+
// if (options.json !== null) {
|
|
293
|
+
// axios_options.data = options.json
|
|
294
|
+
// }
|
|
295
|
+
// if (this.log) {
|
|
296
|
+
// console.log("axios_options:", JSON.stringify(axios_options));
|
|
297
|
+
// }
|
|
298
|
+
// if (options.url.startsWith("https:")) {
|
|
299
|
+
// const httpsAgent = new https.Agent({
|
|
300
|
+
// rejectUnauthorized: false,
|
|
301
|
+
// });
|
|
302
|
+
// axios_options.httpsAgent = httpsAgent;
|
|
303
|
+
// }
|
|
304
|
+
// axios(axios_options)
|
|
305
|
+
// .then((res) => {
|
|
306
|
+
// if (this.log) {
|
|
307
|
+
// console.log("Response for url:", options.url);
|
|
308
|
+
// console.log("Response headers:\n", JSON.stringify(res.headers));
|
|
309
|
+
// }
|
|
310
|
+
// if (res && res.status == 200 && res.data) {
|
|
311
|
+
// if (callback) {
|
|
312
|
+
// callback(null, res.data);
|
|
313
|
+
// }
|
|
314
|
+
// }
|
|
315
|
+
// else {
|
|
316
|
+
// if (callback) {
|
|
317
|
+
// callback(new Error("Response status is not 200"), null);
|
|
318
|
+
// }
|
|
319
|
+
// }
|
|
320
|
+
// })
|
|
321
|
+
// .catch((error) => {
|
|
322
|
+
// if (this.log) { console.error("An error occurred:", JSON.stringify(error.message)) };
|
|
323
|
+
// if (callback) {
|
|
324
|
+
// callback(error, null);
|
|
325
|
+
// }
|
|
326
|
+
// });
|
|
327
|
+
// }
|
|
328
|
+
|
|
221
329
|
async #executeCondition(result, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes, callback) {
|
|
222
330
|
let trueIntentDirective = null;
|
|
223
|
-
|
|
224
331
|
if (trueIntent) {
|
|
225
|
-
//console.log('executeCondition/trueIntent',trueIntent)
|
|
226
332
|
trueIntentDirective = DirIntent.intentDirectiveFor(trueIntent, trueIntentAttributes);
|
|
227
|
-
//console.log('executeCondition/trueIntentDirective',trueIntentDirective)
|
|
228
|
-
//console.log('executeCondition/trueIntentAttributes',trueIntentAttributes)
|
|
229
333
|
}
|
|
230
334
|
let falseIntentDirective = null;
|
|
231
335
|
if (falseIntent) {
|
|
232
336
|
falseIntentDirective = DirIntent.intentDirectiveFor(falseIntent, falseIntentAttributes);
|
|
233
337
|
}
|
|
234
|
-
if (this.log) {console.log('DirMake executeCondition/result',result)}
|
|
235
338
|
if (result === true) {
|
|
236
339
|
if (trueIntentDirective) {
|
|
237
340
|
this.intentDir.execute(trueIntentDirective, () => {
|
|
238
|
-
callback
|
|
341
|
+
if (callback) {
|
|
342
|
+
callback();
|
|
343
|
+
}
|
|
239
344
|
});
|
|
240
345
|
}
|
|
241
346
|
else {
|
|
242
|
-
if (this.log) {console.log("No trueIntentDirective specified");}
|
|
243
|
-
callback
|
|
347
|
+
if (this.log) { console.log("No trueIntentDirective specified"); }
|
|
348
|
+
if (callback) {
|
|
349
|
+
callback();
|
|
350
|
+
}
|
|
244
351
|
}
|
|
245
352
|
}
|
|
246
353
|
else {
|
|
247
354
|
if (falseIntentDirective) {
|
|
248
355
|
this.intentDir.execute(falseIntentDirective, () => {
|
|
249
|
-
callback
|
|
356
|
+
if (callback) {
|
|
357
|
+
callback();
|
|
358
|
+
}
|
|
250
359
|
});
|
|
251
360
|
}
|
|
252
361
|
else {
|
|
253
|
-
if (this.log) {console.log("No falseIntentDirective specified");}
|
|
254
|
-
callback
|
|
362
|
+
if (this.log) { console.log("No falseIntentDirective specified"); }
|
|
363
|
+
if (callback) {
|
|
364
|
+
callback();
|
|
365
|
+
}
|
|
255
366
|
}
|
|
256
367
|
}
|
|
257
368
|
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
const { Filler } = require('../Filler');
|
|
2
|
+
|
|
3
|
+
class DirSetConversationTags {
|
|
4
|
+
|
|
5
|
+
constructor(context) {
|
|
6
|
+
if (!context) {
|
|
7
|
+
throw new Error('context object is mandatory.');
|
|
8
|
+
}
|
|
9
|
+
this.context = context;
|
|
10
|
+
this.log = context.log;
|
|
11
|
+
this.tdclient = context.tdclient;
|
|
12
|
+
this.requestId = context.requestId;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
execute(directive, callback) {
|
|
16
|
+
let action;
|
|
17
|
+
if (directive.action) {
|
|
18
|
+
action = directive.action
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
callback();
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
this.go(action, () => {
|
|
25
|
+
callback();
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async go(action, callback) {
|
|
30
|
+
if (this.log) {console.log("(DirSetConversationTags) Adding conversation tags:", action.depName);}
|
|
31
|
+
let tagsString = action.tags;
|
|
32
|
+
tagsString = tagsString.replace(/ /g,"");
|
|
33
|
+
if (tagsString.length === 0) {
|
|
34
|
+
if (this.log) {console.error("(DirSetConversationTags) Invalid action: tags string is empty")};
|
|
35
|
+
callback();
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
try {
|
|
39
|
+
if (this.tdcache) {
|
|
40
|
+
const requestAttributes =
|
|
41
|
+
await TiledeskChatbot.allParametersStatic(this.tdcache, this.context.requestId);
|
|
42
|
+
const filler = new Filler();
|
|
43
|
+
tags = filler.fill(tags, requestAttributes);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
catch(error) {
|
|
47
|
+
console.error("Error while filling operands:", error);
|
|
48
|
+
}
|
|
49
|
+
this.moveToDepartment(this.requestId, depName, (deps) => {
|
|
50
|
+
if (!deps) {
|
|
51
|
+
if (this.log) {console.log("Dep not found");}
|
|
52
|
+
callback();
|
|
53
|
+
return
|
|
54
|
+
}
|
|
55
|
+
if (this.log) {console.log("Switched to dept:", depName, "action:", JSON.stringify(action));}
|
|
56
|
+
if (action.triggerBot) {
|
|
57
|
+
let dep = null;
|
|
58
|
+
let i;
|
|
59
|
+
for (i = 0; i < deps.length; i++) {
|
|
60
|
+
let d = deps[i];
|
|
61
|
+
if (d.name.toLowerCase() === depName.toLowerCase()) {
|
|
62
|
+
dep = d;
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (dep && dep.hasBot === true && dep.id_bot) {
|
|
67
|
+
if (this.log) {console.log("Sending hidden /start message to bot in dept");}
|
|
68
|
+
const message = {
|
|
69
|
+
type: "text",
|
|
70
|
+
text: "/start",
|
|
71
|
+
attributes : {
|
|
72
|
+
subtype: "info"
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
this.tdclient.sendSupportMessage(
|
|
76
|
+
this.requestId,
|
|
77
|
+
message, (err) => {
|
|
78
|
+
if (err) {
|
|
79
|
+
console.error("Error sending hidden message:", err.message);
|
|
80
|
+
}
|
|
81
|
+
if (this.log) {console.log("Hidden message sent.");}
|
|
82
|
+
callback();
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
if (this.log) {console.log("No action.triggerBot");}
|
|
88
|
+
callback();
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
moveToDepartment(requestId, depName, callback) {
|
|
94
|
+
this.tdclient.getAllDepartments((err, deps) => {
|
|
95
|
+
if (this.log) {console.log("deps:", JSON.stringify(deps));}
|
|
96
|
+
if (err) {
|
|
97
|
+
console.error("getAllDepartments() error:", err);
|
|
98
|
+
callback();
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
let dep = null;
|
|
102
|
+
let i;
|
|
103
|
+
for (i = 0; i < deps.length; i++) {
|
|
104
|
+
let d = deps[i];
|
|
105
|
+
if (d.name.toLowerCase() === depName.toLowerCase()) {
|
|
106
|
+
dep = d;
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (dep) {
|
|
111
|
+
this.tdclient.updateRequestDepartment(requestId, dep._id, null, (err, res) => {
|
|
112
|
+
if (err) {
|
|
113
|
+
console.error("DirDepartment error:", err);
|
|
114
|
+
callback();
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
console.log("DirDepartment response:",JSON.stringify(res));
|
|
118
|
+
callback(deps);
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
module.exports = { DirSetConversationTags };
|