@tiledesk/tiledesk-server 2.10.2 → 2.10.4
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +8 -0
- package/app.js +3 -3
- package/errorCodes.js +32 -0
- package/middleware/has-role.js +6 -5
- package/package.json +2 -2
- package/pubmodules/canned/cannedResponseRoute.js +116 -11
- package/pubmodules/pubModulesManager.js +1 -1
- package/routes/auth.js +56 -14
- package/routes/faq_kb.js +61 -439
- package/routes/images.js +43 -10
- package/routes/openai.js +12 -8
- package/routes/request.js +76 -15
- package/routes/users.js +20 -1
- package/services/emailService.js +3 -2
- package/services/requestService.js +1 -1
- package/template/email/verify.html +1 -1
- package/test/authentication.js +26 -4
- package/test/authorization.js +1 -1
- package/test/faqkbRoute.js +126 -10
- package/test/requestRoute.js +50 -0
package/routes/faq_kb.js
CHANGED
@@ -16,13 +16,16 @@ const faq = require('../models/faq');
|
|
16
16
|
var jwt = require('jsonwebtoken');
|
17
17
|
const uuidv4 = require('uuid/v4');
|
18
18
|
const trainingService = require('../services/trainingService');
|
19
|
+
var roleChecker = require('../middleware/has-role');
|
20
|
+
const roleConstants = require('../models/roleConstants');
|
19
21
|
|
20
22
|
let chatbot_templates_api_url = process.env.CHATBOT_TEMPLATES_API_URL
|
21
23
|
|
22
|
-
|
24
|
+
|
25
|
+
// Eliminare ? ['bot','subscription']
|
26
|
+
router.post('/', roleChecker.hasRole('admin'), async function (req, res) {
|
23
27
|
winston.debug('create BOT ', req.body);
|
24
|
-
|
25
|
-
//faqService.create(req.body.name, req.body.url, req.projectid, req.user.id, req.body.type, req.body.description, undefined, undefined, req.body.language, req.body.template, req.body.mainCategory, req.body.intentsEngine).then(function (savedFaq_kb) {
|
28
|
+
|
26
29
|
let quoteManager = req.app.get('quote_manager');
|
27
30
|
let limits = await quoteManager.getPlanLimits(req.project);
|
28
31
|
let chatbots_limit = limits.chatbots;
|
@@ -32,8 +35,8 @@ router.post('/', async function (req, res) {
|
|
32
35
|
winston.debug("chatbots_count for project " + req.projectid + ": " + chatbots_count);
|
33
36
|
|
34
37
|
if (chatbots_count >= chatbots_limit) {
|
35
|
-
//return res.status(403).send({ success: false, error: "Maximum number of chatbots reached for the current plan", plan_limit: chatbots_limit })
|
36
38
|
winston.info("Chatbots limit reached for project " + req.projectid + ". Block currently disabled.");
|
39
|
+
//return res.status(403).send({ success: false, error: "Maximum number of chatbots reached for the current plan", plan_limit: chatbots_limit })
|
37
40
|
}
|
38
41
|
|
39
42
|
faqService.create(req.body.name, req.body.url, req.projectid, req.user.id, req.body.type, req.body.description, req.body.webhook_url, req.body.webhook_enabled, req.body.language, req.body.template, req.body.mainCategory, req.body.intentsEngine, req.body.attributes).then(function (savedFaq_kb) {
|
@@ -42,8 +45,7 @@ router.post('/', async function (req, res) {
|
|
42
45
|
|
43
46
|
});
|
44
47
|
|
45
|
-
|
46
|
-
router.post('/train', function (req, res) {
|
48
|
+
router.post('/train', roleChecker.hasRoleOrTypes('admin', ['bot','subscription']), function (req, res) {
|
47
49
|
|
48
50
|
winston.info('train BOT ', req.body);
|
49
51
|
|
@@ -59,15 +61,12 @@ router.post('/train', function (req, res) {
|
|
59
61
|
winston.debug('faq_kb.type :' + faq_kb.type);
|
60
62
|
if (faq_kb.type == "internal" && faq_kb.url) {
|
61
63
|
|
62
|
-
|
63
|
-
|
64
64
|
var train = {
|
65
65
|
language: faq_kb.language,
|
66
66
|
nlu: []
|
67
67
|
};
|
68
68
|
winston.info("train", train);
|
69
69
|
|
70
|
-
|
71
70
|
var query = { "id_project": req.projectid, "id_faq_kb": req.body.id_faq_kb };
|
72
71
|
|
73
72
|
Faq.find(query)
|
@@ -103,9 +102,8 @@ router.post('/train', function (req, res) {
|
|
103
102
|
winston.error("error training", e);
|
104
103
|
}
|
105
104
|
|
106
|
-
|
107
105
|
return res.json({ train: train, httpResponse: trainHttp });
|
108
|
-
|
106
|
+
|
109
107
|
} else {
|
110
108
|
return res.status(400).send({ success: false, msg: 'no faq to train on external bot.' });
|
111
109
|
}
|
@@ -116,34 +114,9 @@ router.post('/train', function (req, res) {
|
|
116
114
|
}
|
117
115
|
|
118
116
|
});
|
119
|
-
|
120
|
-
/*
|
121
|
-
{
|
122
|
-
"language":"it",
|
123
|
-
"nlu":[
|
124
|
-
{
|
125
|
-
"intent":"eta",
|
126
|
-
"examples":[
|
127
|
-
"quanti anni hai",
|
128
|
-
"dimmi la tua età",
|
129
|
-
"quanto sei grande",
|
130
|
-
"parlami della tua età"
|
131
|
-
]
|
132
|
-
},
|
133
|
-
{
|
134
|
-
"intent":"brutteparole",
|
135
|
-
"examples":[
|
136
|
-
"non dire parolacce",
|
137
|
-
"le brutte parole non dovrebbero dirsi"
|
138
|
-
]
|
139
|
-
}
|
140
|
-
]
|
141
|
-
}
|
142
|
-
*/
|
143
|
-
|
144
117
|
});
|
145
118
|
|
146
|
-
router.post('/aitrain/', async (req, res) => {
|
119
|
+
router.post('/aitrain/', roleChecker.hasRoleOrTypes('admin', ['bot','subscription']), async (req, res) => {
|
147
120
|
|
148
121
|
let id_faq_kb = req.body.id_faq_kb;
|
149
122
|
let webhook_enabled = req.body.webhook_enabled;
|
@@ -177,18 +150,13 @@ router.post('/aitrain/', async (req, res) => {
|
|
177
150
|
return res.status(200).send({ success: false, message: "Trained not started", error: err });
|
178
151
|
})
|
179
152
|
|
180
|
-
|
181
|
-
|
182
153
|
} else {
|
183
154
|
return res.status(200).send({ success: true, message: "Trained not started", reason: "Training available for intentsEngine equal to tiledesk-ai only" })
|
184
155
|
}
|
185
156
|
})
|
186
|
-
|
187
|
-
|
188
157
|
})
|
189
158
|
|
190
|
-
|
191
|
-
router.post('/askbot', function (req, res) {
|
159
|
+
router.post('/askbot', roleChecker.hasRoleOrTypes('admin', ['bot','subscription']), function (req, res) {
|
192
160
|
|
193
161
|
winston.debug('ASK BOT ', req.body);
|
194
162
|
|
@@ -203,10 +171,6 @@ router.post('/askbot', function (req, res) {
|
|
203
171
|
winston.debug('faq_kb.type :' + faq_kb.type);
|
204
172
|
if (faq_kb.type == "internal" || faq_kb.type == "tilebot") {
|
205
173
|
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
174
|
var query = { "id_project": req.projectid, "id_faq_kb": req.body.id_faq_kb, "question": req.body.question };
|
211
175
|
|
212
176
|
Faq.find(query)
|
@@ -223,7 +187,6 @@ router.post('/askbot', function (req, res) {
|
|
223
187
|
});
|
224
188
|
var result = { hits: faqs };
|
225
189
|
|
226
|
-
|
227
190
|
res.json(result);
|
228
191
|
} else {
|
229
192
|
query = { "id_project": req.projectid, "id_faq_kb": req.body.id_faq_kb };
|
@@ -235,8 +198,6 @@ router.post('/askbot', function (req, res) {
|
|
235
198
|
}
|
236
199
|
query.$text = search_obj;
|
237
200
|
winston.debug("fulltext search query", query);
|
238
|
-
|
239
|
-
|
240
201
|
winston.debug('internal ft query: ' + query);
|
241
202
|
|
242
203
|
Faq.find(query, { score: { $meta: "textScore" } })
|
@@ -253,27 +214,17 @@ router.post('/askbot', function (req, res) {
|
|
253
214
|
var result = { hits: faqs };
|
254
215
|
res.json(result);
|
255
216
|
});
|
256
|
-
|
257
|
-
|
258
217
|
}
|
259
|
-
|
260
|
-
|
261
218
|
});
|
262
|
-
|
263
|
-
|
264
219
|
} else {
|
265
220
|
winston.debug('external query: ');
|
266
221
|
return res.status(400).send({ success: false, msg: 'askbot on external bot.' });
|
267
222
|
}
|
268
|
-
|
269
|
-
|
270
223
|
});
|
271
|
-
|
272
224
|
});
|
273
225
|
|
274
|
-
|
275
|
-
|
276
|
-
router.put('/:faq_kbid/publish', async (req, res) => {
|
226
|
+
// Eliminare ? ['bot','subscription']
|
227
|
+
router.put('/:faq_kbid/publish', roleChecker.hasRole('admin'), async (req, res) => {
|
277
228
|
|
278
229
|
let id_faq_kb = req.params.faq_kbid;
|
279
230
|
winston.debug('id_faq_kb: ' + id_faq_kb);
|
@@ -289,19 +240,17 @@ router.put('/:faq_kbid/publish', async (req, res) => {
|
|
289
240
|
let cs = req.app.get('chatbot_service')
|
290
241
|
|
291
242
|
try {
|
292
|
-
|
243
|
+
// fork(id_faq_kb, api_url, token, project_id)
|
293
244
|
let forked = await cs.fork(id_faq_kb, api_url, token, current_project_id);
|
294
|
-
|
245
|
+
// winston.debug("forked: ", forked)
|
295
246
|
|
296
247
|
let forkedChatBotId = forked.bot_id;
|
297
248
|
winston.debug("forkedChatBotId: "+forkedChatBotId);
|
298
249
|
|
299
|
-
|
300
250
|
let updatedForkedChabot = await Faq_kb.findByIdAndUpdate(forkedChatBotId, {trashed: true, publishedBy: req.user.id, publishedAt: new Date().getTime()}, { new: true, upsert: true }).exec();
|
301
251
|
winston.debug("updatedForkedChabot: ",updatedForkedChabot);
|
302
252
|
botEvent.emit('faqbot.update', updatedForkedChabot);
|
303
253
|
|
304
|
-
|
305
254
|
const port = process.env.PORT || '3000';
|
306
255
|
const TILEBOT_ENDPOINT = process.env.TILEBOT_ENDPOINT || "http://localhost:" + port+ "/modules/tilebot/ext/";
|
307
256
|
winston.debug("TILEBOT_ENDPOINT: " + TILEBOT_ENDPOINT);
|
@@ -311,23 +260,16 @@ router.put('/:faq_kbid/publish', async (req, res) => {
|
|
311
260
|
|
312
261
|
botEvent.emit('faqbot.update', updatedOriginalChabot);
|
313
262
|
|
314
|
-
|
315
263
|
return res.status(200).send({ message: "Chatbot published successfully", bot_id: forkedChatBotId });
|
316
264
|
|
317
265
|
} catch(e) {
|
318
266
|
winston.error("Error Unable publish chatbot: ", e);
|
319
267
|
return res.status(500).send({ success: false, message: "Unable publish chatbot" });
|
320
268
|
}
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
269
|
});
|
326
270
|
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
router.put('/:faq_kbid', function (req, res) {
|
271
|
+
// Eliminare ? ['bot','subscription']
|
272
|
+
router.put('/:faq_kbid', roleChecker.hasRole('admin'), function (req, res) {
|
331
273
|
|
332
274
|
winston.debug(req.body);
|
333
275
|
|
@@ -385,10 +327,12 @@ router.put('/:faq_kbid', function (req, res) {
|
|
385
327
|
if (req.body.certifiedTags != undefined) {
|
386
328
|
update.certifiedTags = req.body.certifiedTags
|
387
329
|
}
|
388
|
-
|
330
|
+
|
331
|
+
if (req.body.agents_visible != undefined) {
|
332
|
+
update.agents_visible = req.body.agents_visible
|
333
|
+
}
|
389
334
|
|
390
335
|
winston.debug("update", update);
|
391
|
-
// "$set": req.params.faq_kbid
|
392
336
|
|
393
337
|
Faq_kb.findByIdAndUpdate(req.params.faq_kbid, update, { new: true, upsert: true }, function (err, updatedFaq_kb) { //TODO add cache_bot_here
|
394
338
|
if (err) {
|
@@ -400,7 +344,8 @@ router.put('/:faq_kbid', function (req, res) {
|
|
400
344
|
});
|
401
345
|
});
|
402
346
|
|
403
|
-
|
347
|
+
// Eliminare ? ['bot','subscription']
|
348
|
+
router.put('/:faq_kbid/language/:language', roleChecker.hasRole('admin'), (req, res) => {
|
404
349
|
|
405
350
|
winston.debug("update language: ", req.params);
|
406
351
|
|
@@ -427,18 +372,11 @@ router.put('/:faq_kbid/language/:language', (req, res) => {
|
|
427
372
|
|
428
373
|
})
|
429
374
|
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
router.patch('/:faq_kbid/attributes', function (req, res) { //TODO add cache_bot_here
|
375
|
+
// Eliminare ? ['bot','subscription']
|
376
|
+
router.patch('/:faq_kbid/attributes', roleChecker.hasRole('admin'), function (req, res) { //TODO add cache_bot_here
|
438
377
|
var data = req.body;
|
439
378
|
|
440
379
|
// TODO use service method
|
441
|
-
|
442
380
|
Faq_kb.findById(req.params.faq_kbid, function (err, updatedBot) {
|
443
381
|
if (err) {
|
444
382
|
winston.error('--- > ERROR ', err);
|
@@ -481,22 +419,24 @@ router.patch('/:faq_kbid/attributes', function (req, res) { //TODO add cache_b
|
|
481
419
|
|
482
420
|
});
|
483
421
|
|
484
|
-
|
422
|
+
// Eliminare ? ['bot','subscription']
|
423
|
+
router.delete('/:faq_kbid', roleChecker.hasRole('admin'), function (req, res) {
|
485
424
|
|
486
425
|
winston.debug(req.body);
|
487
426
|
|
488
|
-
|
489
427
|
Faq_kb.remove({ _id: req.params.faq_kbid }, function (err, faq_kb) {
|
490
428
|
if (err) {
|
491
429
|
return res.status(500).send({ success: false, msg: 'Error deleting object.' });
|
492
430
|
}
|
493
|
-
|
494
|
-
|
431
|
+
/**
|
432
|
+
* WARNING: faq_kb is the operation result, not the faq_kb object. The event subscriber will not receive the object as expected.
|
433
|
+
*/
|
434
|
+
botEvent.emit('faqbot.delete', faq_kb);
|
435
|
+
res.status(200).send({ success: true, message: "Chatbot with id " + req.params.faq_kbid + " deleted successfully"})
|
495
436
|
});
|
496
437
|
});
|
497
438
|
|
498
|
-
|
499
|
-
router.get('/:faq_kbid', function (req, res) {
|
439
|
+
router.get('/:faq_kbid', roleChecker.hasRole('admin'), function (req, res) {
|
500
440
|
|
501
441
|
winston.debug(req.query);
|
502
442
|
|
@@ -541,9 +481,7 @@ router.get('/:faq_kbid', function (req, res) {
|
|
541
481
|
});
|
542
482
|
});
|
543
483
|
|
544
|
-
|
545
|
-
|
546
|
-
router.get('/:faq_kbid/jwt', function (req, res) {
|
484
|
+
router.get('/:faq_kbid/jwt', roleChecker.hasRole('admin'), function (req, res) {
|
547
485
|
|
548
486
|
winston.debug(req.query);
|
549
487
|
|
@@ -555,7 +493,6 @@ router.get('/:faq_kbid/jwt', function (req, res) {
|
|
555
493
|
return res.status(404).send({ success: false, msg: 'Object not found.' });
|
556
494
|
}
|
557
495
|
|
558
|
-
|
559
496
|
var signOptions = {
|
560
497
|
issuer: 'https://tiledesk.com',
|
561
498
|
subject: 'bot',
|
@@ -577,20 +514,28 @@ router.get('/:faq_kbid/jwt', function (req, res) {
|
|
577
514
|
|
578
515
|
var token = jwt.sign(botPayload, botSecret, signOptions);
|
579
516
|
|
580
|
-
|
581
517
|
res.json({"jwt":token});
|
582
|
-
|
583
518
|
});
|
584
519
|
});
|
585
520
|
|
521
|
+
/**
|
522
|
+
* This endpoint should be the only one reachble with role agent.
|
523
|
+
* If the role is agent the response must contain only _id, name, or other non relevant info.
|
524
|
+
*/
|
525
|
+
router.get('/', roleChecker.hasRole('agent'), function (req, res) {
|
586
526
|
|
587
|
-
// NEW - GET ALL FAQKB WITH THE PASSED PROJECT ID
|
588
|
-
router.get('/', function (req, res) {
|
589
527
|
|
590
528
|
winston.debug("req.query", req.query);
|
529
|
+
winston.debug("GET FAQ-KB req projectid", req.projectid);
|
530
|
+
|
531
|
+
let restricted_mode = false;
|
532
|
+
|
533
|
+
let project_user = req.projectuser;
|
534
|
+
if (project_user.role === roleConstants.AGENT) {
|
535
|
+
restricted_mode = true;
|
536
|
+
}
|
591
537
|
|
592
538
|
|
593
|
-
winston.debug("GET FAQ-KB req projectid", req.projectid);
|
594
539
|
/**
|
595
540
|
* if filter only for 'trashed = false',
|
596
541
|
* the bots created before the implementation of the 'trashed' property are not returned
|
@@ -616,24 +561,27 @@ router.get('/', function (req, res) {
|
|
616
561
|
|
617
562
|
if (req.query.certified) {
|
618
563
|
query.certified = req.query.certified;
|
619
|
-
}
|
620
|
-
|
564
|
+
}
|
621
565
|
|
622
566
|
winston.debug("query", query);
|
623
567
|
|
624
|
-
Faq_kb.find(query
|
568
|
+
Faq_kb.find(query).lean().exec((err, faq_kbs) => {
|
625
569
|
if (err) {
|
626
570
|
winston.error('GET FAQ-KB ERROR ', err)
|
627
571
|
return res.status(500).send({ success: false, message: "Unable to get chatbots" });
|
628
572
|
}
|
573
|
+
|
574
|
+
if (restricted_mode === true) {
|
575
|
+
// Returns only: _id, name, id_project, language
|
576
|
+
faq_kbs = faq_kbs.map(({ webhook_enabled, type, public, certified, intentsEngine, tags, score, url, attributes, trained, certifiedTags, trashed, createdBy, createdAt, updatedAt, __v, ...keepAttrs }) => keepAttrs)
|
577
|
+
}
|
578
|
+
|
579
|
+
res.json(faq_kbs)
|
629
580
|
|
630
|
-
|
631
|
-
|
632
|
-
});
|
633
|
-
|
581
|
+
})
|
634
582
|
});
|
635
583
|
|
636
|
-
router.post('/fork/:id_faq_kb', async (req, res) => {
|
584
|
+
router.post('/fork/:id_faq_kb', roleChecker.hasRole('admin'), async (req, res) => {
|
637
585
|
|
638
586
|
let id_faq_kb = req.params.id_faq_kb;
|
639
587
|
winston.debug('id_faq_kb: ' + id_faq_kb);
|
@@ -653,7 +601,6 @@ router.post('/fork/:id_faq_kb', async (req, res) => {
|
|
653
601
|
let globals = req.query.globals;
|
654
602
|
winston.debug("export globals " + globals);
|
655
603
|
|
656
|
-
|
657
604
|
let token = req.headers.authorization;
|
658
605
|
|
659
606
|
let cs = req.app.get('chatbot_service')
|
@@ -689,7 +636,7 @@ router.post('/fork/:id_faq_kb', async (req, res) => {
|
|
689
636
|
|
690
637
|
})
|
691
638
|
|
692
|
-
router.post('/importjson/:id_faq_kb', upload.single('uploadFile'), async (req, res) => {
|
639
|
+
router.post('/importjson/:id_faq_kb', roleChecker.hasRole('admin'), upload.single('uploadFile'), async (req, res) => {
|
693
640
|
|
694
641
|
let id_faq_kb = req.params.id_faq_kb;
|
695
642
|
winston.debug('import on id_faq_kb: ' + id_faq_kb);
|
@@ -921,9 +868,7 @@ router.post('/importjson/:id_faq_kb', upload.single('uploadFile'), async (req, r
|
|
921
868
|
faqBotEvent.emit('faq.create', faq);
|
922
869
|
}
|
923
870
|
}
|
924
|
-
|
925
871
|
})
|
926
|
-
|
927
872
|
}
|
928
873
|
|
929
874
|
if (updatedChatbot) {
|
@@ -931,326 +876,10 @@ router.post('/importjson/:id_faq_kb', upload.single('uploadFile'), async (req, r
|
|
931
876
|
} else {
|
932
877
|
return res.send(chatbot);
|
933
878
|
}
|
934
|
-
|
935
879
|
}
|
936
|
-
|
937
880
|
})
|
938
881
|
|
939
|
-
|
940
|
-
// router.post('/importjson/:id_faq_kb', upload.single('uploadFile'), async (req, res) => {
|
941
|
-
|
942
|
-
// let id_faq_kb = req.params.id_faq_kb;
|
943
|
-
// winston.debug('import on id_faq_kb: ' + id_faq_kb);
|
944
|
-
|
945
|
-
// let json_string;
|
946
|
-
// let json;
|
947
|
-
// if (req.file) {
|
948
|
-
// json_string = req.file.buffer.toString('utf-8');
|
949
|
-
// json = JSON.parse(json_string);
|
950
|
-
// } else {
|
951
|
-
// json = req.body;
|
952
|
-
// }
|
953
|
-
|
954
|
-
// winston.debug("json source " + json_string)
|
955
|
-
|
956
|
-
// // intentOnly still existing?
|
957
|
-
// if (req.query.intentsOnly && req.query.intentsOnly == "true") {
|
958
|
-
|
959
|
-
// winston.debug("intents only")
|
960
|
-
|
961
|
-
// await json.intents.forEach((intent) => {
|
962
|
-
|
963
|
-
// let new_faq = {
|
964
|
-
// id_faq_kb: id_faq_kb,
|
965
|
-
// id_project: req.projectid,
|
966
|
-
// createdBy: req.user.id,
|
967
|
-
// intent_display_name: intent.intent_display_name,
|
968
|
-
// intent_id: intent.intent_id,
|
969
|
-
// question: intent.question,
|
970
|
-
// answer: intent.answer,
|
971
|
-
// reply: intent.reply,
|
972
|
-
// form: intent.form,
|
973
|
-
// enabled: intent.enabled,
|
974
|
-
// webhook_enabled: intent.webhook_enabled,
|
975
|
-
// language: intent.language,
|
976
|
-
// actions: intent.actions,
|
977
|
-
// attributes: intent.attributes
|
978
|
-
// }
|
979
|
-
|
980
|
-
// // overwrite duplicated intents
|
981
|
-
// if (req.query.overwrite == "true") {
|
982
|
-
// 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) => {
|
983
|
-
// if (err) {
|
984
|
-
// winston.error("findOneAndUpdate (upsert) FAQ ERROR ", err);
|
985
|
-
// } else {
|
986
|
-
// if (savingResult.lastErrorObject.updatedExisting == true) {
|
987
|
-
// winston.debug("updated existing intent")
|
988
|
-
// faqBotEvent.emit('faq.update', savingResult.value);
|
989
|
-
// } else {
|
990
|
-
// winston.debug("new intent created")
|
991
|
-
// faqBotEvent.emit('faq.create', savingResult.value);
|
992
|
-
// }
|
993
|
-
// }
|
994
|
-
// })
|
995
|
-
|
996
|
-
// // don't overwrite duplicated intents
|
997
|
-
// } else {
|
998
|
-
// Faq.create(new_faq, (err, savedFaq) => {
|
999
|
-
// if (err) {
|
1000
|
-
// winston.debug("create new FAQ ERROR ", err);
|
1001
|
-
// if (err.code == 11000) {
|
1002
|
-
// winston.error("Duplicate intent_display_name.");
|
1003
|
-
// winston.debug("Skip duplicated intent_display_name");
|
1004
|
-
// } else {
|
1005
|
-
// winston.debug("new intent created")
|
1006
|
-
// faqBotEvent.emit('faq.create', savedFaq);
|
1007
|
-
// }
|
1008
|
-
// }
|
1009
|
-
// })
|
1010
|
-
// }
|
1011
|
-
|
1012
|
-
// })
|
1013
|
-
// //faqBotEvent.emit('faq_train.importedall', id_faq_kb);
|
1014
|
-
// return res.status(200).send({ success: true, msg: "Intents imported successfully" })
|
1015
|
-
|
1016
|
-
// } else {
|
1017
|
-
|
1018
|
-
// // ****************************
|
1019
|
-
// // **** CREATE TRUE option ****
|
1020
|
-
// // ****************************
|
1021
|
-
// if (req.query.create && req.query.create == 'true') {
|
1022
|
-
|
1023
|
-
|
1024
|
-
// faqService.create(json.name, undefined, req.projectid, req.user.id, "tilebot", json.description, json.webhook_url, json.webhook_enabled, json.language, undefined, undefined, undefined, json.attributes).then( async (savedFaq_kb) => {
|
1025
|
-
// winston.debug("saved (and imported) faq kb: ", savedFaq_kb);
|
1026
|
-
|
1027
|
-
// // edit attributes.rules
|
1028
|
-
// let attributes = json.attributes;
|
1029
|
-
// if (attributes &&
|
1030
|
-
// attributes.rules &&
|
1031
|
-
// attributes.rules.length > 0) {
|
1032
|
-
|
1033
|
-
// await attributes.rules.forEach((rule) => {
|
1034
|
-
// if (rule.do &&
|
1035
|
-
// rule.do[0] &&
|
1036
|
-
// rule.do[0].message &&
|
1037
|
-
// rule.do[0].message.participants &&
|
1038
|
-
// rule.do[0].message.participants[0]) {
|
1039
|
-
// rule.do[0].message.participants[0] = "bot_" + savedFaq_kb._id
|
1040
|
-
// winston.debug("attributes rule new participant: ", rule.do[0].message.participants[0])
|
1041
|
-
// }
|
1042
|
-
// })
|
1043
|
-
// }
|
1044
|
-
// let chatbot_edited = { attributes: attributes };
|
1045
|
-
// Faq_kb.findByIdAndUpdate(savedFaq_kb._id, chatbot_edited, { new: true }, (err, savedEditedFaq_kb) => {
|
1046
|
-
// if (err) {
|
1047
|
-
// winston.error("error during saving edited faq_kb: ", err)
|
1048
|
-
// }
|
1049
|
-
// botEvent.emit('faqbot.create', savedFaq_kb);
|
1050
|
-
|
1051
|
-
// if (json.intents) {
|
1052
|
-
|
1053
|
-
// json.intents.forEach((intent) => {
|
1054
|
-
|
1055
|
-
// let new_faq = {
|
1056
|
-
// id_faq_kb: savedFaq_kb._id,
|
1057
|
-
// id_project: req.projectid,
|
1058
|
-
// createdBy: req.user.id,
|
1059
|
-
// intent_display_name: intent.intent_display_name,
|
1060
|
-
// intent_id: intent.intent_id,
|
1061
|
-
// question: intent.question,
|
1062
|
-
// answer: intent.answer,
|
1063
|
-
// reply: intent.reply,
|
1064
|
-
// form: intent.form,
|
1065
|
-
// enabled: intent.enabled,
|
1066
|
-
// webhook_enabled: intent.webhook_enabled,
|
1067
|
-
// language: intent.language,
|
1068
|
-
// actions: intent.actions,
|
1069
|
-
// attributes: intent.attributes
|
1070
|
-
// }
|
1071
|
-
|
1072
|
-
// // TO DELETE: no used when req.query.create = 'true'
|
1073
|
-
// if (req.query.overwrite == "true") {
|
1074
|
-
// 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) => {
|
1075
|
-
// if (err) {
|
1076
|
-
// winston.error("findOneAndUpdate (upsert) FAQ ERROR ", err);
|
1077
|
-
// } else {
|
1078
|
-
|
1079
|
-
// if (savingResult.lastErrorObject.updatedExisting == true) {
|
1080
|
-
// winston.debug("updated existing intent")
|
1081
|
-
// faqBotEvent.emit('faq.update', savingResult.value);
|
1082
|
-
// } else {
|
1083
|
-
// winston.debug("new intent created")
|
1084
|
-
// faqBotEvent.emit('faq.create', savingResult.value);
|
1085
|
-
// }
|
1086
|
-
|
1087
|
-
// }
|
1088
|
-
|
1089
|
-
// })
|
1090
|
-
// // don't overwrite duplicated intents
|
1091
|
-
// } else {
|
1092
|
-
// Faq.create(new_faq, (err, savedFaq) => {
|
1093
|
-
// if (err) {
|
1094
|
-
// winston.debug("create new FAQ ERROR ", err);
|
1095
|
-
// if (err.code == 11000) {
|
1096
|
-
// winston.error("Duplicate intent_display_name.");
|
1097
|
-
// winston.debug("Skip duplicated intent_display_name");
|
1098
|
-
// } else {
|
1099
|
-
// winston.debug("new intent created")
|
1100
|
-
// faqBotEvent.emit('faq.create', savedFaq);
|
1101
|
-
// }
|
1102
|
-
// }
|
1103
|
-
// })
|
1104
|
-
// }
|
1105
|
-
|
1106
|
-
// })
|
1107
|
-
// }
|
1108
|
-
// //faqBotEvent.emit('faq_train.importedall', savedEditedFaq_kb._id);
|
1109
|
-
// return res.status(200).send(savedEditedFaq_kb);
|
1110
|
-
// })
|
1111
|
-
|
1112
|
-
// }).catch((err) => {
|
1113
|
-
// winston.error("error saving faq_kb: ", err);
|
1114
|
-
// return res.status(500).send(err);
|
1115
|
-
// })
|
1116
|
-
|
1117
|
-
// }
|
1118
|
-
// // ****************************
|
1119
|
-
// // **** CREATE FALSE option ****
|
1120
|
-
// // ****************************
|
1121
|
-
// else {
|
1122
|
-
|
1123
|
-
// Faq_kb.findById(id_faq_kb, async (err, faq_kb) => {
|
1124
|
-
// if (err) {
|
1125
|
-
// winston.error("GET FAQ-KB ERROR", err);
|
1126
|
-
// return res.status(500).send({ success: false, msg: "Error getting bot." });
|
1127
|
-
// }
|
1128
|
-
// if (!faq_kb) {
|
1129
|
-
// return res.status(404).send({ success: false, msg: 'Bot not found.' });
|
1130
|
-
// }
|
1131
|
-
|
1132
|
-
// // should be wrong
|
1133
|
-
// //const json = JSON.parse(json_string);
|
1134
|
-
|
1135
|
-
// if (json.webhook_enabled) {
|
1136
|
-
// faq_kb.webhook_enabled = json.webhook_enabled;
|
1137
|
-
// }
|
1138
|
-
// if (json.webhook_url) {
|
1139
|
-
// faq_kb.webhook_url = json.webhook_url;
|
1140
|
-
// }
|
1141
|
-
// if (json.language) {
|
1142
|
-
// faq_kb.language = json.language;
|
1143
|
-
// }
|
1144
|
-
// if (json.name) {
|
1145
|
-
// faq_kb.name = json.name;
|
1146
|
-
// }
|
1147
|
-
// if (json.description) {
|
1148
|
-
// faq_kb.description = json.description;
|
1149
|
-
// }
|
1150
|
-
|
1151
|
-
// if (json.attributes) {
|
1152
|
-
// let attributes = json.attributes;
|
1153
|
-
// if (attributes.rules &&
|
1154
|
-
// attributes.rules.length > 0) {
|
1155
|
-
// await attributes.rules.forEach((rule) => {
|
1156
|
-
// if (rule.do &&
|
1157
|
-
// rule.do[0] &&
|
1158
|
-
// rule.do[0].message &&
|
1159
|
-
// rule.do[0].message.participants &&
|
1160
|
-
// rule.do[0].message.participants[0]) {
|
1161
|
-
// rule.do[0].message.participants[0] = "bot_" + faq_kb._id
|
1162
|
-
// winston.debug("attributes rule new participant: " + rule.do[0].message.participants[0])
|
1163
|
-
// }
|
1164
|
-
// })
|
1165
|
-
// faq_kb.attributes = json.attributes;
|
1166
|
-
// }
|
1167
|
-
// }
|
1168
|
-
// // if (json.intentsEngine) {
|
1169
|
-
// // faq_kb.intentsEngine = json.intentsEngine;
|
1170
|
-
// // }
|
1171
|
-
|
1172
|
-
|
1173
|
-
// Faq_kb.findByIdAndUpdate(id_faq_kb, faq_kb, { new: true }, async (err, updatedFaq_kb) => { //TODO add cache_bot_here
|
1174
|
-
// if (err) {
|
1175
|
-
// return res.status(500).send({ success: false, msg: "Error updating bot." });
|
1176
|
-
// }
|
1177
|
-
|
1178
|
-
// botEvent.emit('faqbot.update', updatedFaq_kb);
|
1179
|
-
|
1180
|
-
// if (json.intents) {
|
1181
|
-
// await json.intents.forEach((intent) => {
|
1182
|
-
|
1183
|
-
// let new_faq = {
|
1184
|
-
// id_faq_kb: updatedFaq_kb._id,
|
1185
|
-
// id_project: req.projectid,
|
1186
|
-
// createdBy: req.user.id,
|
1187
|
-
// intent_display_name: intent.intent_display_name,
|
1188
|
-
// intent_id: intent.intent_id,
|
1189
|
-
// question: intent.question,
|
1190
|
-
// answer: intent.answer,
|
1191
|
-
// reply: intent.reply,
|
1192
|
-
// form: intent.form,
|
1193
|
-
// enabled: intent.enabled,
|
1194
|
-
// webhook_enabled: intent.webhook_enabled,
|
1195
|
-
// language: intent.language,
|
1196
|
-
// actions: intent.actions,
|
1197
|
-
// attributes: intent.attributes
|
1198
|
-
// }
|
1199
|
-
|
1200
|
-
// // *******************************
|
1201
|
-
// // **** OVERWRITE TRUE option ****
|
1202
|
-
// // *******************************
|
1203
|
-
// if (req.query.overwrite == "true") {
|
1204
|
-
// 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) => {
|
1205
|
-
// if (err) {
|
1206
|
-
// winston.error("findOneAndUpdate (upsert) FAQ ERROR ", err);
|
1207
|
-
// } else {
|
1208
|
-
|
1209
|
-
// if (savingResult.lastErrorObject.updatedExisting == true) {
|
1210
|
-
// winston.info("updated existing intent")
|
1211
|
-
// faqBotEvent.emit('faq.update', savingResult.value);
|
1212
|
-
// } else {
|
1213
|
-
// winston.info("new intent created")
|
1214
|
-
// faqBotEvent.emit('faq.create', savingResult.value);
|
1215
|
-
// }
|
1216
|
-
|
1217
|
-
// }
|
1218
|
-
|
1219
|
-
// })
|
1220
|
-
|
1221
|
-
// // ********************************
|
1222
|
-
// // **** OVERWRITE FALSE option ****
|
1223
|
-
// // ********************************
|
1224
|
-
// } else {
|
1225
|
-
// Faq.create(new_faq, (err, savedFaq) => {
|
1226
|
-
// if (err) {
|
1227
|
-
// winston.debug("create new FAQ ERROR ", err);
|
1228
|
-
// if (err.code == 11000) {
|
1229
|
-
// winston.error("Duplicate intent_display_name.");
|
1230
|
-
// winston.info("Skip duplicated intent_display_name");
|
1231
|
-
// } else {
|
1232
|
-
// winston.info("new intent created")
|
1233
|
-
// faqBotEvent.emit('faq.create', savedFaq);
|
1234
|
-
// }
|
1235
|
-
// }
|
1236
|
-
// })
|
1237
|
-
// }
|
1238
|
-
|
1239
|
-
// })
|
1240
|
-
|
1241
|
-
// }
|
1242
|
-
// //faqBotEvent.emit('faq_train.importedall', id_faq_kb);
|
1243
|
-
// return res.send(updatedFaq_kb);
|
1244
|
-
|
1245
|
-
// })
|
1246
|
-
|
1247
|
-
// })
|
1248
|
-
// }
|
1249
|
-
|
1250
|
-
// }
|
1251
|
-
// })
|
1252
|
-
|
1253
|
-
router.get('/exportjson/:id_faq_kb', (req, res) => {
|
882
|
+
router.get('/exportjson/:id_faq_kb', roleChecker.hasRole('admin'), (req, res) => {
|
1254
883
|
|
1255
884
|
winston.debug("exporting bot...")
|
1256
885
|
|
@@ -1304,7 +933,6 @@ router.get('/exportjson/:id_faq_kb', (req, res) => {
|
|
1304
933
|
return res.send(json_string);
|
1305
934
|
}
|
1306
935
|
|
1307
|
-
|
1308
936
|
}).catch((err) => {
|
1309
937
|
winston.error('GET FAQ ERROR: ', err)
|
1310
938
|
return res.status(500).send({ success: false, msg: 'Error getting faqs.' });
|
@@ -1314,10 +942,7 @@ router.get('/exportjson/:id_faq_kb', (req, res) => {
|
|
1314
942
|
|
1315
943
|
})
|
1316
944
|
|
1317
|
-
|
1318
|
-
|
1319
|
-
|
1320
|
-
router.post('/:faq_kbid/training', function (req, res) {
|
945
|
+
router.post('/:faq_kbid/training', roleChecker.hasRole('admin'), function (req, res) {
|
1321
946
|
|
1322
947
|
winston.debug(req.body);
|
1323
948
|
winston.info(req.params.faq_kbid + "/training called" );
|
@@ -1339,7 +964,4 @@ router.post('/:faq_kbid/training', function (req, res) {
|
|
1339
964
|
});
|
1340
965
|
});
|
1341
966
|
|
1342
|
-
|
1343
|
-
|
1344
|
-
|
1345
967
|
module.exports = router;
|