Skip to content

Commit 1788fcd

Browse files
committed
Merge branch 'master' of github.com:Tiledesk/tiledesk-server
* 'master' of github.com:Tiledesk/tiledesk-server: adds possibility to create a bot and import json in one shot adds test for importson?create=true
2 parents d1d8bc8 + d2b0bf0 commit 1788fcd

File tree

3 files changed

+154
-42
lines changed

3 files changed

+154
-42
lines changed

routes/faq_kb.js

Lines changed: 108 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ router.post('/fork/:id_faq_kb', async (req, res) => {
402402
router.post('/importjson/:id_faq_kb', upload.single('uploadFile'), (req, res) => {
403403

404404
let id_faq_kb = req.params.id_faq_kb;
405-
winston.debug('id_faq_kb: ', id_faq_kb);
405+
winston.info('import on id_faq_kb: ', id_faq_kb);
406406

407407
let json_string;
408408
let json;
@@ -413,8 +413,6 @@ router.post('/importjson/:id_faq_kb', upload.single('uploadFile'), (req, res) =>
413413
json = req.body;
414414
}
415415

416-
winston.info("json_string: " + json_string);
417-
418416
if (req.query.intentsOnly == "true") {
419417

420418
winston.info("intents only")
@@ -473,44 +471,15 @@ router.post('/importjson/:id_faq_kb', upload.single('uploadFile'), (req, res) =>
473471

474472
} else {
475473

476-
Faq_kb.findById(id_faq_kb, (err, faq_kb) => {
477-
if (err) {
478-
winston.error("GET FAQ-KB ERROR", err);
479-
return res.status(500).send({ success: false, msg: "Error getting bot." });
480-
}
481-
if (!faq_kb) {
482-
return res.status(404).send({ success: false, msg: 'Bot not found.' });
483-
}
484-
485-
const json = JSON.parse(json_string);
486-
487-
if (json.webhook_enabled) {
488-
faq_kb.webhook_enabled = json.webhook_enabled;
489-
}
490-
if (json.webhook_url) {
491-
faq_kb.webhook_url = json.webhook_url;
492-
}
493-
if (json.language) {
494-
faq_kb.language = json.language;
495-
}
496-
if (json.name) {
497-
faq_kb.name = json.name;
498-
}
499-
if (json.description) {
500-
faq_kb.description = json.description;
501-
}
502-
503-
Faq_kb.findByIdAndUpdate(id_faq_kb, faq_kb, { new: true }, (err, updatedFaq_kb) => {
504-
if (err) {
505-
return res.status(500).send({ success: false, msg: "Error updating bot." });
506-
}
507-
508-
botEvent.emit('faqbot.update', updatedFaq_kb);
474+
if (req.query.create == 'true') {
475+
faqService.create(json.name, undefined, req.projectid, req.user.id, "tilebot", json.description, json.webhook_url, json.webhook_enabled, json.language, undefined, undefined, undefined).then((savedFaq_kb) => {
476+
winston.debug("saved (and imported) faq kb: ", savedFaq_kb);
477+
botEvent.emit('faqbot.create', savedFaq_kb);
509478

510479
json.intents.forEach((intent) => {
511-
480+
512481
let new_faq = {
513-
id_faq_kb: updatedFaq_kb._id,
482+
id_faq_kb: savedFaq_kb._id,
514483
id_project: req.projectid,
515484
createdBy: req.user.id,
516485
intent_display_name: intent.intent_display_name,
@@ -523,7 +492,7 @@ router.post('/importjson/:id_faq_kb', upload.single('uploadFile'), (req, res) =>
523492
language: intent.language
524493
}
525494

526-
// overwrite duplicated intents
495+
// TO DELETE: no used when req.query.create = 'true'
527496
if (req.query.overwrite == "true") {
528497
Faq.findOneAndUpdate({ id_faq_kb: id_faq_kb, intent_display_name: intent.intent_display_name }, new_faq, { new: true, upsert: true, rawResult: true }, (err, savingResult) => {
529498
if (err) {
@@ -559,12 +528,110 @@ router.post('/importjson/:id_faq_kb', upload.single('uploadFile'), (req, res) =>
559528
}
560529

561530
})
531+
return res.status(200).send(savedFaq_kb);
562532

563-
return res.send(updatedFaq_kb);
533+
}).catch((err) => {
534+
console.error("error saving faq_kb: ", err);
535+
return res.status(500).send(err);
536+
})
564537

538+
} else {
539+
540+
Faq_kb.findById(id_faq_kb, (err, faq_kb) => {
541+
if (err) {
542+
winston.error("GET FAQ-KB ERROR", err);
543+
return res.status(500).send({ success: false, msg: "Error getting bot." });
544+
}
545+
if (!faq_kb) {
546+
return res.status(404).send({ success: false, msg: 'Bot not found.' });
547+
}
548+
549+
// should be wrong
550+
//const json = JSON.parse(json_string);
551+
552+
if (json.webhook_enabled) {
553+
faq_kb.webhook_enabled = json.webhook_enabled;
554+
}
555+
if (json.webhook_url) {
556+
faq_kb.webhook_url = json.webhook_url;
557+
}
558+
if (json.language) {
559+
faq_kb.language = json.language;
560+
}
561+
if (json.name) {
562+
faq_kb.name = json.name;
563+
}
564+
if (json.description) {
565+
faq_kb.description = json.description;
566+
}
567+
568+
Faq_kb.findByIdAndUpdate(id_faq_kb, faq_kb, { new: true }, (err, updatedFaq_kb) => {
569+
if (err) {
570+
return res.status(500).send({ success: false, msg: "Error updating bot." });
571+
}
572+
573+
botEvent.emit('faqbot.update', updatedFaq_kb);
574+
575+
json.intents.forEach((intent) => {
576+
577+
let new_faq = {
578+
id_faq_kb: updatedFaq_kb._id,
579+
id_project: req.projectid,
580+
createdBy: req.user.id,
581+
intent_display_name: intent.intent_display_name,
582+
question: intent.question,
583+
answer: intent.answer,
584+
reply: intent.reply,
585+
form: intent.form,
586+
enabled: intent.enabled,
587+
webhook_enabled: intent.webhook_enabled,
588+
language: intent.language
589+
}
590+
591+
// overwrite duplicated intents
592+
if (req.query.overwrite == "true") {
593+
Faq.findOneAndUpdate({ id_faq_kb: id_faq_kb, intent_display_name: intent.intent_display_name }, new_faq, { new: true, upsert: true, rawResult: true }, (err, savingResult) => {
594+
if (err) {
595+
winston.error("findOneAndUpdate (upsert) FAQ ERROR ", err);
596+
} else {
597+
598+
if (savingResult.lastErrorObject.updatedExisting == true) {
599+
winston.info("updated existing intent")
600+
faqBotEvent.emit('faq.update', savingResult.value);
601+
} else {
602+
winston.info("new intent crated")
603+
faqBotEvent.emit('faq.create', savingResult.value);
604+
}
605+
606+
}
607+
608+
})
609+
610+
// don't overwrite duplicated intents
611+
} else {
612+
Faq.create(new_faq, (err, savedFaq) => {
613+
if (err) {
614+
winston.debug("create new FAQ ERROR ", err);
615+
if (err.code == 11000) {
616+
winston.error("Duplicate intent_display_name.");
617+
winston.info("Skip duplicated intent_display_name");
618+
} else {
619+
winston.info("new intent crated")
620+
faqBotEvent.emit('faq.create', savedFaq);
621+
}
622+
}
623+
})
624+
}
625+
626+
})
627+
628+
return res.send(updatedFaq_kb);
629+
630+
})
631+
565632
})
633+
}
566634

567-
})
568635
}
569636

570637
})

test/example.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"webhook_enabled":false,"language":"en","name":"examplebot","intents":[{"webhook_enabled":false,"enabled":true,"question":"\\start","answer":"Hi","intent_display_name":"start","language":"en"},{"webhook_enabled":false,"enabled":true,"question":"question1","answer":"question1","intent_display_name":"question1","language":"en"},{"webhook_enabled":false,"enabled":true,"question":"defaultFallback","answer":"I can not provide an adequate answer. Write a new question or talk to a human agent.\n* Back to start tdAction:start\n* See the docs https://docs.tiledesk.com/\n* 👨🏻‍🦰 I want an agent","intent_display_name":"defaultFallback","language":"en"}]}

test/faqkbRoute.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,50 @@ describe('FaqKBRoute', () => {
232232
})
233233
})
234234

235+
it('create bot and import json', (done) => {
236+
237+
var email = "test-signup-" + Date.now() + "@email.com";
238+
var pwd = "pwd";
239+
240+
userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
241+
projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
242+
243+
chai.request(server)
244+
.post('/' + savedProject._id + '/faq_kb/importjson/' + null + "?create=true")
245+
.auth(email, pwd)
246+
.set('Content-Type', 'text/plain')
247+
.attach('uploadFile', fs.readFileSync(path.resolve(__dirname, './example.json')), 'example.json')
248+
.end((err, res) => {
249+
if (log) {
250+
console.log("import json res: ", res.body);
251+
}
252+
res.should.have.status(200);
253+
res.should.be.a('object');
254+
expect(res.body.name).to.equal("examplebot");
255+
expect(res.body.language).to.equal("en");
256+
257+
let id_faq_kb = res.body._id;
258+
259+
chai.request(server)
260+
.get('/' + savedProject._id + '/faq?id_faq_kb=' + id_faq_kb)
261+
.auth(email, pwd)
262+
.end((err, res) => {
263+
if (log) {
264+
console.log("faq_list: ", res.body);
265+
}
266+
res.should.have.status(200);
267+
res.body.should.be.an('array').that.is.not.empty;
268+
269+
done();
270+
271+
})
272+
})
273+
274+
})
275+
})
276+
277+
})
278+
235279

236280
it('import json (overwrite true)', (done) => {
237281

@@ -259,7 +303,7 @@ describe('FaqKBRoute', () => {
259303
.post('/' + savedProject._id + '/faq_kb/importjson/' + id_faq_kb + "?overwrite=true")
260304
.auth(email, pwd)
261305
.set('Content-Type', 'text/plain')
262-
.attach('uploadFile', fs.readFileSync(path.resolve(__dirname, './example-json.txt')), 'example-json.txt')
306+
.attach('uploadFile', fs.readFileSync(path.resolve(__dirname, './example.json')), 'example.json')
263307
.end((err, res) => {
264308
if (log) {
265309
console.log("import json res: ", res.body);

0 commit comments

Comments
 (0)