@tiledesk/tiledesk-server 2.9.13 → 2.9.15
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +7 -0
- package/package.json +1 -1
- package/routes/faq_kb.js +501 -243
- package/routes/kb.js +6 -2
- package/routes/project.js +8 -2
- package/test/faqkbRoute.js +190 -133
- package/test/projectRoute.js +42 -39
- package/utils/jobs-worker-queue-manager/JobManagerV2.js +11 -2
- package/utils/jobs-worker-queue-manager/queueManagerClassV2.js +17 -7
package/routes/faq_kb.js
CHANGED
@@ -689,12 +689,15 @@ router.post('/fork/:id_faq_kb', async (req, res) => {
|
|
689
689
|
|
690
690
|
})
|
691
691
|
|
692
|
-
|
693
692
|
router.post('/importjson/:id_faq_kb', upload.single('uploadFile'), async (req, res) => {
|
694
693
|
|
695
694
|
let id_faq_kb = req.params.id_faq_kb;
|
696
695
|
winston.debug('import on id_faq_kb: ' + id_faq_kb);
|
697
|
-
|
696
|
+
|
697
|
+
winston.debug('import with option create: ' + req.query.create);
|
698
|
+
winston.debug('import with option replace: ' + req.query.replace);
|
699
|
+
winston.debug('import with option overwrite: ' + req.query.overwrite);
|
700
|
+
|
698
701
|
let json_string;
|
699
702
|
let json;
|
700
703
|
if (req.file) {
|
@@ -706,291 +709,546 @@ router.post('/importjson/:id_faq_kb', upload.single('uploadFile'), async (req, r
|
|
706
709
|
|
707
710
|
winston.debug("json source " + json_string)
|
708
711
|
|
709
|
-
//
|
710
|
-
|
711
|
-
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
|
716
|
-
|
717
|
-
|
718
|
-
|
719
|
-
|
720
|
-
|
721
|
-
|
722
|
-
|
723
|
-
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
-
|
728
|
-
|
729
|
-
|
730
|
-
|
731
|
-
|
712
|
+
// ****************************
|
713
|
+
// **** CREATE TRUE option ****
|
714
|
+
// ****************************
|
715
|
+
if (req.query.create === 'true') {
|
716
|
+
let savedChatbot = await 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)
|
717
|
+
.catch((err) => {
|
718
|
+
winston.error("Error creating new chatbot")
|
719
|
+
return res.status(400).send({ succes: false, message: "Error creatings new chatbot", error: err })
|
720
|
+
})
|
721
|
+
|
722
|
+
// Edit attributes.rules
|
723
|
+
let attributes = json.attributes;
|
724
|
+
if (attributes &&
|
725
|
+
attributes.rules &&
|
726
|
+
attributes.rules.length > 0) {
|
727
|
+
|
728
|
+
await attributes.rules.forEach((rule) => {
|
729
|
+
if (rule.do &&
|
730
|
+
rule.do[0] &&
|
731
|
+
rule.do[0].message &&
|
732
|
+
rule.do[0].message.participants &&
|
733
|
+
rule.do[0].message.participants[0]) {
|
734
|
+
rule.do[0].message.participants[0] = "bot_" + savedChatbot._id
|
735
|
+
winston.debug("attributes rule new participant: ", rule.do[0].message.participants[0])
|
736
|
+
}
|
737
|
+
})
|
738
|
+
}
|
739
|
+
|
740
|
+
let chatbot_edited = { attributes: attributes };
|
732
741
|
|
733
|
-
|
734
|
-
|
735
|
-
|
736
|
-
|
737
|
-
|
742
|
+
let updatedChatbot = await Faq_kb.findByIdAndUpdate(savedChatbot._id, chatbot_edited, { new: true }).catch((err) => {
|
743
|
+
winston.error("Error updating chatbot attributes: ", err);
|
744
|
+
winston.debug("Skip Error updating chatbot attributes: ", err);
|
745
|
+
// return res.status(400).send({ success: false, message: "Error updating chatbot attributes", error: err })
|
746
|
+
})
|
747
|
+
|
748
|
+
botEvent.emit('faqbot.create', savedChatbot);
|
749
|
+
|
750
|
+
if (json.intents) {
|
751
|
+
|
752
|
+
json.intents.forEach( async (intent) => {
|
753
|
+
|
754
|
+
let new_faq = {
|
755
|
+
id_faq_kb: savedChatbot._id,
|
756
|
+
id_project: req.projectid,
|
757
|
+
createdBy: req.user.id,
|
758
|
+
intent_display_name: intent.intent_display_name,
|
759
|
+
intent_id: intent.intent_id,
|
760
|
+
question: intent.question,
|
761
|
+
answer: intent.answer,
|
762
|
+
reply: intent.reply,
|
763
|
+
form: intent.form,
|
764
|
+
enabled: intent.enabled,
|
765
|
+
webhook_enabled: intent.webhook_enabled,
|
766
|
+
language: intent.language,
|
767
|
+
actions: intent.actions,
|
768
|
+
attributes: intent.attributes
|
769
|
+
}
|
770
|
+
|
771
|
+
let faq = await Faq.create(new_faq).catch((err) => {
|
772
|
+
if (err.code == 11000) { // should never happen
|
773
|
+
winston.error("Duplicate intent_display_name.");
|
774
|
+
winston.debug("Skip duplicated intent_display_name"); // Don't stop the flow
|
738
775
|
} else {
|
739
|
-
|
740
|
-
|
776
|
+
winston.error("Error saving new faq: ", err)
|
777
|
+
}
|
778
|
+
})
|
779
|
+
|
780
|
+
if (faq) {
|
781
|
+
winston.debug("new intent created")
|
782
|
+
faqBotEvent.emit('faq.create', savedFaq);
|
783
|
+
}
|
784
|
+
})
|
785
|
+
}
|
786
|
+
|
787
|
+
if (updatedChatbot) {
|
788
|
+
return res.status(200).send(updatedChatbot)
|
789
|
+
} else {
|
790
|
+
return res.status(200).send(savedChatbot)
|
791
|
+
}
|
792
|
+
}
|
793
|
+
// *****************************
|
794
|
+
// **** CREATE FALSE option ****
|
795
|
+
// *****************************
|
796
|
+
else {
|
797
|
+
|
798
|
+
if (!id_faq_kb) {
|
799
|
+
return res.status(400).send({ success: false, message: "With replace or overwrite option a id_faq_kb must be provided" })
|
800
|
+
}
|
801
|
+
|
802
|
+
let chatbot = Faq_kb.findById(id_faq_kb).catch((err) => {
|
803
|
+
winston.error("Error finding chatbot with id " + id_faq_kb);
|
804
|
+
return res.status(404).send({ success: false, message: "Error finding chatbot with id " + id_faq_kb, error: err });
|
805
|
+
})
|
806
|
+
|
807
|
+
if (!chatbot) {
|
808
|
+
winston.error("Chatbot not found with id " + id_faq_kb);
|
809
|
+
return res.status(404).send({ success: false, message: "Chatbot not found with id " + id_faq_kb });
|
810
|
+
}
|
811
|
+
|
812
|
+
if (json.webhook_enabled) {
|
813
|
+
chatbot.webhook_enabled = json.webhook_enabled;
|
814
|
+
}
|
815
|
+
if (json.webhook_url) {
|
816
|
+
chatbot.webhook_url = json.webhook_url;
|
817
|
+
}
|
818
|
+
if (json.language) {
|
819
|
+
chatbot.language = json.language;
|
820
|
+
}
|
821
|
+
if (json.name) {
|
822
|
+
chatbot.name = json.name;
|
823
|
+
}
|
824
|
+
if (json.description) {
|
825
|
+
chatbot.description = json.description;
|
826
|
+
}
|
827
|
+
|
828
|
+
if (json.attributes) {
|
829
|
+
let attributes = json.attributes;
|
830
|
+
if (attributes.rules &&
|
831
|
+
attributes.rules.length > 0) {
|
832
|
+
await attributes.rules.forEach((rule) => {
|
833
|
+
if (rule.do &&
|
834
|
+
rule.do[0] &&
|
835
|
+
rule.do[0].message &&
|
836
|
+
rule.do[0].message.participants &&
|
837
|
+
rule.do[0].message.participants[0]) {
|
838
|
+
rule.do[0].message.participants[0] = "bot_" + chatbot._id
|
839
|
+
winston.debug("attributes rule new participant: " + rule.do[0].message.participants[0])
|
840
|
+
}
|
841
|
+
})
|
842
|
+
chatbot.attributes = json.attributes;
|
843
|
+
}
|
844
|
+
}
|
845
|
+
|
846
|
+
let updatedChatbot = await Faq_kb.findByIdAndUpdate(id_faq_kb, chatbot, { new: true }).catch((err) => {
|
847
|
+
winston.error("Error updating chatbot");
|
848
|
+
return res.status(400).send({ success: false, message: "Error updating chatbot", error: err })
|
849
|
+
})
|
850
|
+
|
851
|
+
botEvent.emit('faqbot.update', updatedChatbot);
|
852
|
+
|
853
|
+
// *****************************
|
854
|
+
// **** REPLACE TRUE option ****
|
855
|
+
// *****************************
|
856
|
+
if (req.query.replace === 'true') {
|
857
|
+
let result = await Faq.deleteMany({ id_faq_kb: id_faq_kb }).catch((err) => {
|
858
|
+
winston.error("Unable to delete all existing faqs with id_faq_kb " + id_faq_kb);
|
859
|
+
})
|
860
|
+
|
861
|
+
winston.verbose("All faq for chatbot " + id_faq_kb + " deleted successfully")
|
862
|
+
winston.debug("DeleteMany faqs result ", result);
|
863
|
+
}
|
864
|
+
|
865
|
+
if (json.intents) {
|
866
|
+
await json.intents.forEach( async (intent) => {
|
867
|
+
|
868
|
+
let new_faq = {
|
869
|
+
id_faq_kb: updatedChatbot._id,
|
870
|
+
id_project: req.projectid,
|
871
|
+
createdBy: req.user.id,
|
872
|
+
intent_display_name: intent.intent_display_name,
|
873
|
+
intent_id: intent.intent_id,
|
874
|
+
question: intent.question,
|
875
|
+
answer: intent.answer,
|
876
|
+
reply: intent.reply,
|
877
|
+
form: intent.form,
|
878
|
+
enabled: intent.enabled,
|
879
|
+
webhook_enabled: intent.webhook_enabled,
|
880
|
+
language: intent.language,
|
881
|
+
actions: intent.actions,
|
882
|
+
attributes: intent.attributes
|
883
|
+
}
|
884
|
+
|
885
|
+
// *******************************
|
886
|
+
// **** OVERWRITE TRUE option ****
|
887
|
+
// *******************************
|
888
|
+
if (req.query.overwrite == "true") {
|
889
|
+
|
890
|
+
let savingResult = await Faq.findOneAndUpdate({ id_faq_kb: id_faq_kb, intent_display_name: intent.intent_display_name }, new_faq, { new: true, upsert: true, rawResult: true }).catch((err) => {
|
891
|
+
winston.error("Unable to create faq: ", err);
|
892
|
+
})
|
893
|
+
|
894
|
+
if (savingResult) {
|
895
|
+
if (savingResult.lastErrorObject.updatedExisting === true) {
|
896
|
+
winston.info("updated existing intent")
|
741
897
|
faqBotEvent.emit('faq.update', savingResult.value);
|
742
898
|
} else {
|
743
|
-
winston.
|
899
|
+
winston.info("new intent created")
|
744
900
|
faqBotEvent.emit('faq.create', savingResult.value);
|
745
901
|
}
|
746
902
|
}
|
747
|
-
})
|
748
903
|
|
749
|
-
|
750
|
-
|
751
|
-
|
752
|
-
|
753
|
-
|
904
|
+
// ********************************
|
905
|
+
// **** OVERWRITE FALSE option ****
|
906
|
+
// ********************************
|
907
|
+
} else {
|
908
|
+
|
909
|
+
let faq = await Faq.create(new_faq).catch((err) => {
|
754
910
|
if (err.code == 11000) {
|
755
911
|
winston.error("Duplicate intent_display_name.");
|
756
|
-
winston.
|
912
|
+
winston.info("Skip duplicated intent_display_name");
|
757
913
|
} else {
|
758
|
-
winston.
|
759
|
-
faqBotEvent.emit('faq.create', savedFaq);
|
914
|
+
winston.error("Error creating new intent: ", err);
|
760
915
|
}
|
916
|
+
winston.error("Error creating new intent: ", err);
|
917
|
+
})
|
918
|
+
|
919
|
+
if (faq) {
|
920
|
+
winston.info("new intent created")
|
921
|
+
faqBotEvent.emit('faq.create', savedFaq);
|
761
922
|
}
|
762
|
-
}
|
763
|
-
}
|
923
|
+
}
|
764
924
|
|
765
|
-
|
766
|
-
//faqBotEvent.emit('faq_train.importedall', id_faq_kb);
|
767
|
-
return res.status(200).send({ success: true, msg: "Intents imported successfully" })
|
925
|
+
})
|
768
926
|
|
769
|
-
|
927
|
+
}
|
770
928
|
|
771
|
-
if (
|
929
|
+
if (updatedChatbot) {
|
930
|
+
return res.send(updatedChatbot);
|
931
|
+
} else {
|
932
|
+
return res.send(chatbot);
|
933
|
+
}
|
934
|
+
|
935
|
+
}
|
936
|
+
|
937
|
+
})
|
938
|
+
|
939
|
+
// OLD
|
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') {
|
772
1022
|
|
773
1023
|
|
774
|
-
|
775
|
-
|
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);
|
776
1026
|
|
777
|
-
|
778
|
-
|
779
|
-
|
780
|
-
|
781
|
-
|
1027
|
+
// // edit attributes.rules
|
1028
|
+
// let attributes = json.attributes;
|
1029
|
+
// if (attributes &&
|
1030
|
+
// attributes.rules &&
|
1031
|
+
// attributes.rules.length > 0) {
|
782
1032
|
|
783
|
-
|
784
|
-
|
785
|
-
|
786
|
-
|
787
|
-
|
788
|
-
|
789
|
-
|
790
|
-
|
791
|
-
|
792
|
-
|
793
|
-
|
794
|
-
|
795
|
-
|
796
|
-
|
797
|
-
|
798
|
-
|
799
|
-
|
800
|
-
|
801
|
-
|
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) {
|
802
1052
|
|
803
|
-
|
1053
|
+
// json.intents.forEach((intent) => {
|
804
1054
|
|
805
|
-
|
806
|
-
|
807
|
-
|
808
|
-
|
809
|
-
|
810
|
-
|
811
|
-
|
812
|
-
|
813
|
-
|
814
|
-
|
815
|
-
|
816
|
-
|
817
|
-
|
818
|
-
|
819
|
-
|
820
|
-
|
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
|
+
// }
|
821
1071
|
|
822
|
-
|
823
|
-
|
824
|
-
|
825
|
-
|
826
|
-
|
827
|
-
|
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 {
|
828
1078
|
|
829
|
-
|
830
|
-
|
831
|
-
|
832
|
-
|
833
|
-
|
834
|
-
|
835
|
-
|
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
|
+
// }
|
836
1086
|
|
837
|
-
|
1087
|
+
// }
|
838
1088
|
|
839
|
-
|
840
|
-
|
841
|
-
|
842
|
-
|
843
|
-
|
844
|
-
|
845
|
-
|
846
|
-
|
847
|
-
|
848
|
-
|
849
|
-
|
850
|
-
|
851
|
-
|
852
|
-
|
853
|
-
|
854
|
-
|
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
|
+
// }
|
855
1105
|
|
856
|
-
|
857
|
-
|
858
|
-
|
859
|
-
|
860
|
-
|
861
|
-
|
862
|
-
|
863
|
-
|
864
|
-
|
865
|
-
|
866
|
-
|
867
|
-
|
868
|
-
|
869
|
-
|
870
|
-
|
871
|
-
|
872
|
-
|
873
|
-
|
874
|
-
|
875
|
-
|
876
|
-
|
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
|
+
// }
|
877
1131
|
|
878
|
-
|
879
|
-
|
1132
|
+
// // should be wrong
|
1133
|
+
// //const json = JSON.parse(json_string);
|
880
1134
|
|
881
|
-
|
882
|
-
|
883
|
-
|
884
|
-
|
885
|
-
|
886
|
-
|
887
|
-
|
888
|
-
|
889
|
-
|
890
|
-
|
891
|
-
|
892
|
-
|
893
|
-
|
894
|
-
|
895
|
-
|
896
|
-
|
897
|
-
|
898
|
-
|
899
|
-
|
900
|
-
|
901
|
-
|
902
|
-
|
903
|
-
|
904
|
-
|
905
|
-
|
906
|
-
|
907
|
-
|
908
|
-
|
909
|
-
|
910
|
-
|
911
|
-
|
912
|
-
|
913
|
-
|
914
|
-
|
915
|
-
|
916
|
-
|
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
|
+
// // }
|
917
1171
|
|
918
1172
|
|
919
|
-
|
920
|
-
|
921
|
-
|
922
|
-
|
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
|
+
// }
|
923
1177
|
|
924
|
-
|
1178
|
+
// botEvent.emit('faqbot.update', updatedFaq_kb);
|
925
1179
|
|
926
|
-
|
927
|
-
|
1180
|
+
// if (json.intents) {
|
1181
|
+
// await json.intents.forEach((intent) => {
|
928
1182
|
|
929
|
-
|
930
|
-
|
931
|
-
|
932
|
-
|
933
|
-
|
934
|
-
|
935
|
-
|
936
|
-
|
937
|
-
|
938
|
-
|
939
|
-
|
940
|
-
|
941
|
-
|
942
|
-
|
943
|
-
|
944
|
-
|
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
|
+
// }
|
945
1199
|
|
946
|
-
|
947
|
-
|
948
|
-
|
949
|
-
|
950
|
-
|
951
|
-
|
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 {
|
952
1208
|
|
953
|
-
|
954
|
-
|
955
|
-
|
956
|
-
|
957
|
-
|
958
|
-
|
959
|
-
|
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
|
+
// }
|
960
1216
|
|
961
|
-
|
1217
|
+
// }
|
962
1218
|
|
963
|
-
|
1219
|
+
// })
|
964
1220
|
|
965
|
-
|
966
|
-
|
967
|
-
|
968
|
-
|
969
|
-
|
970
|
-
|
971
|
-
|
972
|
-
|
973
|
-
|
974
|
-
|
975
|
-
|
976
|
-
|
977
|
-
|
978
|
-
|
979
|
-
|
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
|
+
// }
|
980
1238
|
|
981
|
-
|
1239
|
+
// })
|
982
1240
|
|
983
|
-
|
984
|
-
|
985
|
-
|
1241
|
+
// }
|
1242
|
+
// //faqBotEvent.emit('faq_train.importedall', id_faq_kb);
|
1243
|
+
// return res.send(updatedFaq_kb);
|
986
1244
|
|
987
|
-
|
1245
|
+
// })
|
988
1246
|
|
989
|
-
|
990
|
-
|
1247
|
+
// })
|
1248
|
+
// }
|
991
1249
|
|
992
|
-
|
993
|
-
})
|
1250
|
+
// }
|
1251
|
+
// })
|
994
1252
|
|
995
1253
|
router.get('/exportjson/:id_faq_kb', (req, res) => {
|
996
1254
|
|