@tiledesk/tiledesk-server 2.10.87 → 2.10.89
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 +19 -0
- package/app.js +4 -0
- package/errorCodes.js +6 -0
- package/event/botEvent.js +89 -42
- package/jobsManager.js +4 -1
- package/models/chatbotTemplates.js +22 -0
- package/models/faq.js +14 -1
- package/models/faq_kb.js +64 -2
- package/models/flowLogs.js +64 -0
- package/models/user.js +7 -0
- package/models/webhook.js +30 -0
- package/package.json +4 -4
- package/routes/auth.js +1 -1
- package/routes/faq.js +6 -0
- package/routes/faq_kb.js +123 -85
- package/routes/logs.js +80 -3
- package/routes/request.js +2 -2
- package/routes/users.js +3 -0
- package/routes/webhook.js +54 -25
- package/routes/webhooks.js +119 -2
- package/services/chatbotService.js +24 -18
- package/services/faqService.js +35 -331
- package/services/logsService.js +59 -0
- package/services/userService.js +3 -2
- package/services/webhookService.js +21 -2
- package/template/chatbot/blank.js +79 -0
- package/template/chatbot/blank_copilot.js +43 -0
- package/template/chatbot/blank_voice.js +108 -0
- package/template/chatbot/blank_voice_twilio.js +115 -0
- package/template/chatbot/blank_webhook.js +43 -0
- package/template/chatbot/empty.js +3 -0
- package/template/chatbot/example.js +88 -0
- package/template/chatbot/handoff.js +25 -0
- package/template/chatbot/index.js +12 -0
- package/template/chatbot/official_copilot.js +717 -0
- package/test/faqkbRoute.js +165 -23
- package/test/webhookRoute.js +247 -2
- package/utils/TdCache.js +3 -3
package/routes/faq_kb.js
CHANGED
@@ -18,6 +18,7 @@ const trainingService = require('../services/trainingService');
|
|
18
18
|
var roleChecker = require('../middleware/has-role');
|
19
19
|
const roleConstants = require('../models/roleConstants');
|
20
20
|
const errorCodes = require('../errorCodes');
|
21
|
+
const faq_kb = require('../models/faq_kb');
|
21
22
|
|
22
23
|
let chatbot_templates_api_url = process.env.CHATBOT_TEMPLATES_API_URL
|
23
24
|
|
@@ -26,12 +27,12 @@ let MAX_UPLOAD_FILE_SIZE = process.env.MAX_UPLOAD_FILE_SIZE;
|
|
26
27
|
let uploadlimits = undefined;
|
27
28
|
|
28
29
|
if (MAX_UPLOAD_FILE_SIZE) {
|
29
|
-
uploadlimits = {fileSize: parseInt(MAX_UPLOAD_FILE_SIZE)}
|
30
|
+
uploadlimits = { fileSize: parseInt(MAX_UPLOAD_FILE_SIZE) };
|
30
31
|
winston.debug("Max upload file size is : " + MAX_UPLOAD_FILE_SIZE);
|
31
32
|
} else {
|
32
33
|
winston.debug("Max upload file size is infinity");
|
33
34
|
}
|
34
|
-
var upload = multer({limits: uploadlimits});
|
35
|
+
var upload = multer({ limits: uploadlimits });
|
35
36
|
|
36
37
|
|
37
38
|
router.post('/', roleChecker.hasRole('admin'), async function (req, res) {
|
@@ -130,7 +131,7 @@ router.post('/train', roleChecker.hasRole('admin'), function (req, res) {
|
|
130
131
|
});
|
131
132
|
|
132
133
|
router.post('/aitrain/', roleChecker.hasRole('admin'), async (req, res) => {
|
133
|
-
|
134
|
+
|
134
135
|
let id_faq_kb = req.body.id_faq_kb;
|
135
136
|
let webhook_enabled = req.body.webhook_enabled;
|
136
137
|
|
@@ -241,6 +242,17 @@ router.put('/:faq_kbid/publish', roleChecker.hasRole('admin'), async (req, res)
|
|
241
242
|
let id_faq_kb = req.params.faq_kbid;
|
242
243
|
winston.debug('id_faq_kb: ' + id_faq_kb);
|
243
244
|
|
245
|
+
let chatbot_id;
|
246
|
+
let release_note;
|
247
|
+
|
248
|
+
if (req.body.restore_from) {
|
249
|
+
chatbot_id = req.body.restore_from;
|
250
|
+
release_note = "Restored from " + chatbot_id;
|
251
|
+
} else {
|
252
|
+
chatbot_id = id_faq_kb;
|
253
|
+
release_note = req.body.release_note || "No comment";
|
254
|
+
}
|
255
|
+
|
244
256
|
const api_url = process.env.API_URL || configGlobal.apiUrl;
|
245
257
|
winston.debug("fork --> base_url: " + api_url); // check if correct
|
246
258
|
|
@@ -253,14 +265,15 @@ router.put('/:faq_kbid/publish', roleChecker.hasRole('admin'), async (req, res)
|
|
253
265
|
|
254
266
|
try {
|
255
267
|
// fork(id_faq_kb, api_url, token, project_id)
|
256
|
-
let forked = await cs.fork(
|
268
|
+
let forked = await cs.fork(chatbot_id, api_url, token, current_project_id);
|
257
269
|
// winston.debug("forked: ", forked)
|
258
270
|
|
259
271
|
let forkedChatBotId = forked.bot_id;
|
260
|
-
winston.debug("forkedChatBotId: "+forkedChatBotId);
|
272
|
+
winston.debug("forkedChatBotId: " + forkedChatBotId);
|
261
273
|
|
262
|
-
let updatedForkedChabot = await Faq_kb.findByIdAndUpdate(forkedChatBotId, { $unset: { slug: 1 }, trashed: true, publishedBy: req.user.id, publishedAt: new Date().getTime()}, { new: true, upsert: true }).exec();
|
263
|
-
|
274
|
+
let updatedForkedChabot = await Faq_kb.findByIdAndUpdate(forkedChatBotId, { $unset: { slug: 1 }, trashed: true, root_id: id_faq_kb, release_note: release_note, publishedBy: req.user.id, publishedAt: new Date().getTime() }, { new: true, upsert: true }).exec();
|
275
|
+
//let updatedForkedChabot = await Faq_kb.findByIdAndUpdate(forkedChatBotId, { $unset: { slug: 1 }, trashed: true, root_id: id_faq_kb, publishedBy: req.user.id, publishedAt: new Date().getTime() }, { new: true }).exec();
|
276
|
+
winston.debug("updatedForkedChabot: ", updatedForkedChabot);
|
264
277
|
botEvent.emit('faqbot.update', updatedForkedChabot);
|
265
278
|
|
266
279
|
const port = process.env.PORT || '3000';
|
@@ -270,20 +283,22 @@ router.put('/:faq_kbid/publish', roleChecker.hasRole('admin'), async (req, res)
|
|
270
283
|
}
|
271
284
|
winston.debug("TILEBOT_ENDPOINT: " + TILEBOT_ENDPOINT);
|
272
285
|
|
273
|
-
let updatedOriginalChabot = await Faq_kb.findByIdAndUpdate(id_faq_kb,
|
274
|
-
winston.debug("updatedOriginalChabot: ",updatedOriginalChabot);
|
286
|
+
let updatedOriginalChabot = await Faq_kb.findByIdAndUpdate(id_faq_kb, { url: TILEBOT_ENDPOINT + forkedChatBotId }, { new: true, upsert: true }).exec();
|
287
|
+
winston.debug("updatedOriginalChabot: ", updatedOriginalChabot);
|
288
|
+
|
289
|
+
cs.setModified(id_faq_kb, false);
|
275
290
|
|
276
291
|
botEvent.emit('faqbot.update', updatedOriginalChabot);
|
277
292
|
|
278
293
|
return res.status(200).send({ message: "Chatbot published successfully", bot_id: forkedChatBotId });
|
279
294
|
|
280
|
-
} catch(e) {
|
295
|
+
} catch (e) {
|
281
296
|
winston.error("Error Unable publish chatbot: ", e);
|
282
297
|
return res.status(500).send({ success: false, message: "Unable publish chatbot" });
|
283
298
|
}
|
284
299
|
});
|
285
300
|
|
286
|
-
router.put('/:faq_kbid', roleChecker.hasRoleOrTypes('admin', ['bot','subscription']), function (req, res) {
|
301
|
+
router.put('/:faq_kbid', roleChecker.hasRoleOrTypes('admin', ['bot', 'subscription']), function (req, res) {
|
287
302
|
|
288
303
|
winston.debug(req.body);
|
289
304
|
|
@@ -349,6 +364,8 @@ router.put('/:faq_kbid', roleChecker.hasRoleOrTypes('admin', ['bot','subscriptio
|
|
349
364
|
if (req.body.slug != undefined) {
|
350
365
|
update.slug = req.body.slug;
|
351
366
|
}
|
367
|
+
|
368
|
+
update.modified = true;
|
352
369
|
|
353
370
|
winston.debug("update", update);
|
354
371
|
|
@@ -366,8 +383,8 @@ router.put('/:faq_kbid', roleChecker.hasRoleOrTypes('admin', ['bot','subscriptio
|
|
366
383
|
});
|
367
384
|
});
|
368
385
|
|
369
|
-
router.put('/:faq_kbid/language/:language', roleChecker.hasRoleOrTypes('admin', ['bot','subscription']), (req, res) => {
|
370
|
-
|
386
|
+
router.put('/:faq_kbid/language/:language', roleChecker.hasRoleOrTypes('admin', ['bot', 'subscription']), (req, res) => {
|
387
|
+
|
371
388
|
winston.debug("update language: ", req.params);
|
372
389
|
|
373
390
|
let update = {};
|
@@ -393,7 +410,7 @@ router.put('/:faq_kbid/language/:language', roleChecker.hasRoleOrTypes('admin',
|
|
393
410
|
|
394
411
|
})
|
395
412
|
|
396
|
-
router.patch('/:faq_kbid/attributes', roleChecker.hasRoleOrTypes('admin', ['bot','subscription']), function (req, res) { //TODO add cache_bot_here
|
413
|
+
router.patch('/:faq_kbid/attributes', roleChecker.hasRoleOrTypes('admin', ['bot', 'subscription']), function (req, res) { //TODO add cache_bot_here
|
397
414
|
var data = req.body;
|
398
415
|
|
399
416
|
// TODO use service method
|
@@ -403,40 +420,40 @@ router.patch('/:faq_kbid/attributes', roleChecker.hasRoleOrTypes('admin', ['bot'
|
|
403
420
|
return res.status(500).send({ success: false, msg: 'Error updating object.' });
|
404
421
|
}
|
405
422
|
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
if (!updatedBot.attributes) {
|
411
|
-
winston.debug("empty attributes")
|
412
|
-
updatedBot.attributes = {};
|
413
|
-
}
|
423
|
+
if (!updatedBot) {
|
424
|
+
return res.status(404).send({ success: false, msg: 'Object not found.' });
|
425
|
+
}
|
414
426
|
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
});
|
427
|
+
if (!updatedBot.attributes) {
|
428
|
+
winston.debug("empty attributes")
|
429
|
+
updatedBot.attributes = {};
|
430
|
+
}
|
431
|
+
|
432
|
+
winston.debug(" updatedBot attributes", updatedBot.attributes)
|
433
|
+
|
434
|
+
Object.keys(data).forEach(function (key) {
|
435
|
+
var val = data[key];
|
436
|
+
winston.debug("data attributes " + key + " " + val)
|
437
|
+
updatedBot.attributes[key] = val;
|
438
|
+
});
|
439
|
+
|
440
|
+
winston.debug("updatedBot attributes", updatedBot.attributes)
|
441
|
+
|
442
|
+
// https://stackoverflow.com/questions/24054552/mongoose-not-saving-nested-object
|
443
|
+
updatedBot.markModified('attributes');
|
444
|
+
|
445
|
+
//cacheinvalidation
|
446
|
+
updatedBot.save(function (err, savedProject) {
|
447
|
+
if (err) {
|
448
|
+
winston.error("error saving bot attributes", err)
|
449
|
+
return res.status(500).send({ success: false, msg: 'Error getting object.' });
|
450
|
+
}
|
451
|
+
winston.verbose(" saved bot attributes", updatedBot.toObject())
|
452
|
+
botEvent.emit('faqbot.update', updatedBot);
|
453
|
+
res.json(updatedBot);
|
454
|
+
});
|
438
455
|
});
|
439
|
-
|
456
|
+
|
440
457
|
});
|
441
458
|
|
442
459
|
router.delete('/:faq_kbid', roleChecker.hasRole('admin'), function (req, res) {
|
@@ -450,12 +467,12 @@ router.delete('/:faq_kbid', roleChecker.hasRole('admin'), function (req, res) {
|
|
450
467
|
/**
|
451
468
|
* WARNING: faq_kb is the operation result, not the faq_kb object. The event subscriber will not receive the object as expected.
|
452
469
|
*/
|
453
|
-
botEvent.emit('faqbot.delete', faq_kb);
|
454
|
-
res.status(200).send({ success: true, message: "Chatbot with id " + req.params.faq_kbid + " deleted successfully"})
|
470
|
+
botEvent.emit('faqbot.delete', faq_kb);
|
471
|
+
res.status(200).send({ success: true, message: "Chatbot with id " + req.params.faq_kbid + " deleted successfully" })
|
455
472
|
});
|
456
473
|
});
|
457
474
|
|
458
|
-
router.get('/:faq_kbid',
|
475
|
+
router.get('/:faq_kbid', roleChecker.hasRoleOrTypes('admin', ['bot', 'subscription']), function (req, res) {
|
459
476
|
|
460
477
|
winston.debug(req.query);
|
461
478
|
|
@@ -500,7 +517,25 @@ router.get('/:faq_kbid', roleChecker.hasRoleOrTypes('admin', ['bot','subscripti
|
|
500
517
|
});
|
501
518
|
});
|
502
519
|
|
503
|
-
router.get('/:faq_kbid/
|
520
|
+
router.get('/:faq_kbid/published', roleChecker.hasRoleOrTypes('admin', ['bot', 'subscription']), async function (req, res) {
|
521
|
+
|
522
|
+
let id_project = req.projectid;
|
523
|
+
let chatbot_id = req.params.faq_kbid;
|
524
|
+
|
525
|
+
let published_chatbots = await faq_kb.find({ id_project: id_project, root_id: chatbot_id })
|
526
|
+
.sort({ publishedAt: -1 })
|
527
|
+
.limit(100)
|
528
|
+
.populate('publishedBy', '_id firstname lastname email')
|
529
|
+
.catch((err) => {
|
530
|
+
winston.error("Error finding published chatbots: ", err);
|
531
|
+
return res.status(500).send({ success: false, error: "Error finding published chatbots from root " + chatbot_id });
|
532
|
+
})
|
533
|
+
|
534
|
+
res.status(200).send(published_chatbots);
|
535
|
+
|
536
|
+
})
|
537
|
+
|
538
|
+
router.get('/:faq_kbid/jwt', roleChecker.hasRoleOrTypes('admin', ['bot', 'subscription']), function (req, res) {
|
504
539
|
|
505
540
|
winston.debug(req.query);
|
506
541
|
|
@@ -513,10 +548,10 @@ router.get('/:faq_kbid/jwt', roleChecker.hasRoleOrTypes('admin', ['bot','subscri
|
|
513
548
|
}
|
514
549
|
|
515
550
|
var signOptions = {
|
516
|
-
issuer:
|
517
|
-
subject:
|
518
|
-
audience:
|
519
|
-
jwtid: uuidv4()
|
551
|
+
issuer: 'https://tiledesk.com',
|
552
|
+
subject: 'bot',
|
553
|
+
audience: 'https://tiledesk.com/bots/' + faq_kb._id,
|
554
|
+
jwtid: uuidv4()
|
520
555
|
};
|
521
556
|
|
522
557
|
// TODO metti bot_? a user._id
|
@@ -533,7 +568,7 @@ router.get('/:faq_kbid/jwt', roleChecker.hasRoleOrTypes('admin', ['bot','subscri
|
|
533
568
|
|
534
569
|
var token = jwt.sign(botPayload, botSecret, signOptions);
|
535
570
|
|
536
|
-
res.json({"jwt":token});
|
571
|
+
res.json({ "jwt": token });
|
537
572
|
});
|
538
573
|
});
|
539
574
|
|
@@ -541,7 +576,7 @@ router.get('/:faq_kbid/jwt', roleChecker.hasRoleOrTypes('admin', ['bot','subscri
|
|
541
576
|
* This endpoint should be the only one reachble with role agent.
|
542
577
|
* If the role is agent the response must contain only _id, name, or other non relevant info.
|
543
578
|
*/
|
544
|
-
router.get('/', roleChecker.hasRoleOrTypes('agent', ['bot','subscription']), function (req, res) {
|
579
|
+
router.get('/', roleChecker.hasRoleOrTypes('agent', ['bot', 'subscription']), function (req, res) {
|
545
580
|
|
546
581
|
|
547
582
|
winston.debug("req.query", req.query);
|
@@ -560,10 +595,10 @@ router.get('/', roleChecker.hasRoleOrTypes('agent', ['bot','subscription']), fun
|
|
560
595
|
* the bots created before the implementation of the 'trashed' property are not returned
|
561
596
|
*/
|
562
597
|
let query = { "id_project": req.projectid, "trashed": { $in: [null, false] } };
|
563
|
-
|
598
|
+
|
564
599
|
if (restricted_mode === true) {
|
565
600
|
query.agents_available = {
|
566
|
-
$in: [
|
601
|
+
$in: [null, true]
|
567
602
|
}
|
568
603
|
}
|
569
604
|
|
@@ -571,13 +606,13 @@ router.get('/', roleChecker.hasRoleOrTypes('agent', ['bot','subscription']), fun
|
|
571
606
|
query.type = { $ne: "identity" }
|
572
607
|
}
|
573
608
|
|
574
|
-
let search_obj = {"$search": req.query.text};
|
609
|
+
let search_obj = { "$search": req.query.text };
|
575
610
|
|
576
|
-
if (req.query.text) {
|
611
|
+
if (req.query.text) {
|
577
612
|
if (req.query.language) {
|
578
613
|
search_obj["$language"] = req.query.language;
|
579
614
|
}
|
580
|
-
query.$text = search_obj;
|
615
|
+
query.$text = search_obj;
|
581
616
|
}
|
582
617
|
|
583
618
|
if (req.query.public) {
|
@@ -586,7 +621,7 @@ router.get('/', roleChecker.hasRoleOrTypes('agent', ['bot','subscription']), fun
|
|
586
621
|
|
587
622
|
if (req.query.certified) {
|
588
623
|
query.certified = req.query.certified;
|
589
|
-
}
|
624
|
+
}
|
590
625
|
|
591
626
|
winston.debug("query", query);
|
592
627
|
|
@@ -595,12 +630,12 @@ router.get('/', roleChecker.hasRoleOrTypes('agent', ['bot','subscription']), fun
|
|
595
630
|
winston.error('GET FAQ-KB ERROR ', err)
|
596
631
|
return res.status(500).send({ success: false, message: "Unable to get chatbots" });
|
597
632
|
}
|
598
|
-
|
633
|
+
|
599
634
|
if (restricted_mode === true) {
|
600
635
|
// Returns only: _id, name, id_project, language
|
601
636
|
faq_kbs = faq_kbs.map(({ webhook_enabled, intentsEngine, score, url, attributes, trained, certifiedTags, createdBy, createdAt, updatedAt, __v, ...keepAttrs }) => keepAttrs)
|
602
637
|
}
|
603
|
-
|
638
|
+
|
604
639
|
res.json(faq_kbs)
|
605
640
|
|
606
641
|
})
|
@@ -642,9 +677,11 @@ router.post('/fork/:id_faq_kb', roleChecker.hasRole('admin'), async (req, res) =
|
|
642
677
|
delete chatbot.attributes.globals
|
643
678
|
}
|
644
679
|
}
|
680
|
+
|
681
|
+
delete chatbot.modified;
|
645
682
|
|
646
683
|
chatbot.template = "empty";
|
647
|
-
|
684
|
+
|
648
685
|
let savedChatbot = await cs.createBot(api_url, token, chatbot, landing_project_id);
|
649
686
|
winston.debug("savedChatbot: ", savedChatbot)
|
650
687
|
|
@@ -667,11 +704,11 @@ router.post('/importjson/:id_faq_kb', roleChecker.hasRole('admin'), upload.singl
|
|
667
704
|
|
668
705
|
let id_faq_kb = req.params.id_faq_kb;
|
669
706
|
winston.debug('import on id_faq_kb: ' + id_faq_kb);
|
670
|
-
|
707
|
+
|
671
708
|
winston.debug('import with option create: ' + req.query.create);
|
672
709
|
winston.debug('import with option replace: ' + req.query.replace);
|
673
710
|
winston.debug('import with option overwrite: ' + req.query.overwrite);
|
674
|
-
|
711
|
+
|
675
712
|
let json_string;
|
676
713
|
let json;
|
677
714
|
if (req.file) {
|
@@ -687,15 +724,16 @@ router.post('/importjson/:id_faq_kb', roleChecker.hasRole('admin'), upload.singl
|
|
687
724
|
// **** CREATE TRUE option ****
|
688
725
|
// ****************************
|
689
726
|
if (req.query.create === 'true') {
|
690
|
-
if (json.subtype && (json.subtype === 'webhook' || json.subtype === 'copilot')) {
|
691
|
-
|
692
|
-
}
|
727
|
+
// if (json.subtype && (json.subtype === 'webhook' || json.subtype === 'copilot')) {
|
728
|
+
// json.template = 'empty';
|
729
|
+
// }
|
730
|
+
json.template = 'empty';
|
693
731
|
let savedChatbot = await faqService.create(req.projectid, req.user.id, json)
|
694
732
|
.catch((err) => {
|
695
|
-
|
696
|
-
|
733
|
+
winston.error("Error creating new chatbot")
|
734
|
+
return res.status(400).send({ succes: false, message: "Error creatings new chatbot", error: err })
|
697
735
|
})
|
698
|
-
|
736
|
+
|
699
737
|
// Edit attributes.rules
|
700
738
|
let attributes = json.attributes;
|
701
739
|
if (attributes &&
|
@@ -713,7 +751,7 @@ router.post('/importjson/:id_faq_kb', roleChecker.hasRole('admin'), upload.singl
|
|
713
751
|
}
|
714
752
|
})
|
715
753
|
}
|
716
|
-
|
754
|
+
|
717
755
|
let chatbot_edited = { attributes: attributes };
|
718
756
|
|
719
757
|
let updatedChatbot = await Faq_kb.findByIdAndUpdate(savedChatbot._id, chatbot_edited, { new: true }).catch((err) => {
|
@@ -725,8 +763,8 @@ router.post('/importjson/:id_faq_kb', roleChecker.hasRole('admin'), upload.singl
|
|
725
763
|
botEvent.emit('faqbot.create', savedChatbot);
|
726
764
|
|
727
765
|
if (json.intents) {
|
728
|
-
|
729
|
-
json.intents.forEach(
|
766
|
+
|
767
|
+
json.intents.forEach(async (intent) => {
|
730
768
|
|
731
769
|
let new_faq = {
|
732
770
|
id_faq_kb: savedChatbot._id,
|
@@ -801,7 +839,7 @@ router.post('/importjson/:id_faq_kb', roleChecker.hasRole('admin'), upload.singl
|
|
801
839
|
if (json.description) {
|
802
840
|
chatbot.description = json.description;
|
803
841
|
}
|
804
|
-
|
842
|
+
|
805
843
|
if (json.attributes) {
|
806
844
|
let attributes = json.attributes;
|
807
845
|
if (attributes.rules &&
|
@@ -825,7 +863,7 @@ router.post('/importjson/:id_faq_kb', roleChecker.hasRole('admin'), upload.singl
|
|
825
863
|
return res.status(400).send({ success: false, message: "Error updating chatbot", error: err })
|
826
864
|
})
|
827
865
|
|
828
|
-
botEvent.emit('faqbot.update', updatedChatbot);
|
866
|
+
botEvent.emit('faqbot.update', updatedChatbot);
|
829
867
|
|
830
868
|
// *****************************
|
831
869
|
// **** REPLACE TRUE option ****
|
@@ -840,7 +878,7 @@ router.post('/importjson/:id_faq_kb', roleChecker.hasRole('admin'), upload.singl
|
|
840
878
|
}
|
841
879
|
|
842
880
|
if (json.intents) {
|
843
|
-
await json.intents.forEach(
|
881
|
+
await json.intents.forEach(async (intent) => {
|
844
882
|
|
845
883
|
let new_faq = {
|
846
884
|
id_faq_kb: updatedChatbot._id,
|
@@ -878,9 +916,9 @@ router.post('/importjson/:id_faq_kb', roleChecker.hasRole('admin'), upload.singl
|
|
878
916
|
}
|
879
917
|
}
|
880
918
|
|
881
|
-
|
882
|
-
|
883
|
-
|
919
|
+
// ********************************
|
920
|
+
// **** OVERWRITE FALSE option ****
|
921
|
+
// ********************************
|
884
922
|
} else {
|
885
923
|
|
886
924
|
let faq = await Faq.create(new_faq).catch((err) => {
|
@@ -893,7 +931,7 @@ router.post('/importjson/:id_faq_kb', roleChecker.hasRole('admin'), upload.singl
|
|
893
931
|
})
|
894
932
|
|
895
933
|
if (faq) {
|
896
|
-
winston.
|
934
|
+
winston.debug("new intent created: ", faq)
|
897
935
|
faqBotEvent.emit('faq.create', faq);
|
898
936
|
}
|
899
937
|
}
|
@@ -976,12 +1014,12 @@ router.get('/exportjson/:id_faq_kb', roleChecker.hasRole('admin'), (req, res) =>
|
|
976
1014
|
router.post('/:faq_kbid/training', roleChecker.hasRole('admin'), function (req, res) {
|
977
1015
|
|
978
1016
|
winston.debug(req.body);
|
979
|
-
winston.info(req.params.faq_kbid + "/training called"
|
1017
|
+
winston.info(req.params.faq_kbid + "/training called");
|
980
1018
|
|
981
1019
|
var update = {};
|
982
1020
|
update.trained = true;
|
983
1021
|
// update._id = req.params.faq_kbid;
|
984
|
-
|
1022
|
+
|
985
1023
|
winston.debug("update", update);
|
986
1024
|
// "$set": req.params.faq_kbid
|
987
1025
|
|
package/routes/logs.js
CHANGED
@@ -3,8 +3,11 @@ var router = express.Router();
|
|
3
3
|
var winston = require('../config/winston');
|
4
4
|
const { MessageLog } = require('../models/whatsappLog');
|
5
5
|
const { Transaction } = require('../models/transaction');
|
6
|
+
const logsService = require('../services/logsService');
|
7
|
+
const { v4: uuidv4 } = require('uuid');
|
8
|
+
const jwt = require("jsonwebtoken")
|
6
9
|
|
7
|
-
|
10
|
+
const jwtSecret = process.env.CHAT21_JWT_SECRET || "tokenKey";
|
8
11
|
|
9
12
|
router.get('/', function (req, res, next) {
|
10
13
|
winston.info("logs", req.body);
|
@@ -32,7 +35,6 @@ router.get('/whatsapp', async (req, res) => {
|
|
32
35
|
res.status(200).send(transactions);
|
33
36
|
})
|
34
37
|
|
35
|
-
// res.stats(200).send({ success: true });
|
36
38
|
})
|
37
39
|
|
38
40
|
|
@@ -51,7 +53,7 @@ router.get('/whatsapp/:transaction_id', async (req, res) => {
|
|
51
53
|
|
52
54
|
winston.verbose("Logs found: ", logs);
|
53
55
|
|
54
|
-
let clearLogs = logs.map(({_id, __v, ...keepAttrs}) => keepAttrs)
|
56
|
+
let clearLogs = logs.map(({ _id, __v, ...keepAttrs }) => keepAttrs)
|
55
57
|
winston.verbose("clearLogs: ", clearLogs)
|
56
58
|
|
57
59
|
res.status(200).send(clearLogs);
|
@@ -85,10 +87,85 @@ router.post('/whatsapp', async (req, res) => {
|
|
85
87
|
})
|
86
88
|
|
87
89
|
|
90
|
+
router.get('/flows/:request_id', async (req, res) => {
|
91
|
+
|
92
|
+
let request_id = req.params.request_id;
|
93
|
+
const { timestamp, direction, logLevel } = req.query;
|
94
|
+
|
95
|
+
if (!request_id) {
|
96
|
+
return res.status(400).send({ success: false, error: "Missing required parameter 'request_id'." });
|
97
|
+
}
|
88
98
|
|
99
|
+
let method;
|
100
|
+
|
101
|
+
if (!timestamp) {
|
102
|
+
method = logsService.getLastRows(request_id, 20, logLevel);
|
103
|
+
} else if (direction === 'prev') {
|
104
|
+
logsService.get
|
105
|
+
method = logsService.getOlderRows(request_id, 10, logLevel, new Date(timestamp));
|
106
|
+
} else if (direction === 'next') {
|
107
|
+
method = logsService.getNewerRows(request_id, 10, logLevel, new Date(timestamp))
|
108
|
+
} else {
|
109
|
+
return res.status(400).send({ success: false, error: "Missing or invalid 'direction' parameter. Use 'prev' or 'next'."})
|
110
|
+
}
|
111
|
+
|
112
|
+
method.then((logs) => {
|
113
|
+
res.status(200).send(logs);
|
114
|
+
}).catch((err) => {
|
115
|
+
res.status(500).send({ success: false, error: "Error fetching logs: " + err.message });
|
116
|
+
})
|
117
|
+
|
118
|
+
})
|
119
|
+
|
120
|
+
|
121
|
+
router.get('/flows/auth/:request_id', async (req, res) => {
|
122
|
+
|
123
|
+
const request_id = req.params.request_id;
|
124
|
+
const appid = "tilechat";
|
125
|
+
|
126
|
+
const scope = [
|
127
|
+
`rabbitmq.read:*/*/apps.${appid}.logs.${request_id}.*`,
|
128
|
+
`rabbitmq.write:*/*/apps.${appid}.logs.${request_id}.*`,
|
129
|
+
`rabbitmq.configure:*/*/*`
|
130
|
+
]
|
131
|
+
|
132
|
+
const now = Math.round(new Date().getTime() / 1000);
|
133
|
+
const exp = now + 60 * 60 * 24 * 30;
|
134
|
+
|
135
|
+
var payload = {
|
136
|
+
"jti": uuidv4(),
|
137
|
+
"sub": request_id,
|
138
|
+
scope: scope,
|
139
|
+
"client_id": request_id,
|
140
|
+
"cid": request_id,
|
141
|
+
"azp": request_id,
|
142
|
+
"user_id": request_id,
|
143
|
+
"app_id": appid,
|
144
|
+
"iat": now,
|
145
|
+
"exp": exp,
|
146
|
+
"aud": [
|
147
|
+
"rabbitmq",
|
148
|
+
request_id
|
149
|
+
],
|
150
|
+
"kid": "tiledesk-key",
|
151
|
+
}
|
152
|
+
|
153
|
+
var token = jwt.sign(
|
154
|
+
payload,
|
155
|
+
jwtSecret,
|
156
|
+
{
|
157
|
+
"algorithm": "HS256"
|
158
|
+
}
|
159
|
+
);
|
89
160
|
|
161
|
+
const result = {
|
162
|
+
request_id: request_id,
|
163
|
+
token: token
|
164
|
+
}
|
90
165
|
|
166
|
+
return res.status(200).send(result);
|
91
167
|
|
168
|
+
})
|
92
169
|
|
93
170
|
|
94
171
|
module.exports = router;
|
package/routes/request.js
CHANGED
@@ -23,7 +23,7 @@ var cacheEnabler = require("../services/cacheEnabler");
|
|
23
23
|
var Project_user = require("../models/project_user");
|
24
24
|
var Lead = require("../models/lead");
|
25
25
|
var UIDGenerator = require("../utils/UIDGenerator");
|
26
|
-
let
|
26
|
+
let { Publisher } = require("@tiledesk/tiledesk-multi-worker");
|
27
27
|
|
28
28
|
csv = require('csv-express');
|
29
29
|
csv.separator = ';';
|
@@ -39,7 +39,7 @@ const faq_kb = require('../models/faq_kb');
|
|
39
39
|
|
40
40
|
const AMQP_MANAGER_URL = process.env.AMQP_MANAGER_URL;
|
41
41
|
|
42
|
-
let jobManager = new
|
42
|
+
let jobManager = new Publisher(AMQP_MANAGER_URL, {
|
43
43
|
debug: false,
|
44
44
|
queueName: "conversation-tags_queue",
|
45
45
|
exchange: "tiledesk-multi",
|
package/routes/users.js
CHANGED
@@ -37,6 +37,9 @@ router.put('/', function (req, res) {
|
|
37
37
|
if (req.body.public_website != undefined) {
|
38
38
|
update.public_website = req.body.public_website;
|
39
39
|
}
|
40
|
+
if (req.body.phone != undefined) {
|
41
|
+
update.phone = req.body.phone;
|
42
|
+
}
|
40
43
|
|
41
44
|
User.findByIdAndUpdate(req.user.id, update, { new: true, upsert: true }, function (err, updatedUser) {
|
42
45
|
if (err) {
|