in lambda/export/createFAQ.js [153:281]
async function createFAQ(params) {
// create kendra and s3 clients
let kendraClient = (process.env.REGION ?
new AWSKendra({apiVersion:'2019-02-03', region:process.env.REGION}) :
new AWSKendra({apiVersion:'2019-02-03', region:params.region})
);
let s3Client = (process.env.REGION ?
new AWSS3({apiVersion:'2006-03-01', region:process.env.REGION}) :
new AWSS3({apiVersion:'2006-03-01', region:params.region}));
qnabot.log('clients created');
// read in JSON and upload to S3 bucket
var fs = require('fs');
var s3_params = {
Bucket: params.s3_bucket,
Key: params.s3_key,
ACL: 'bucket-owner-read', // TODO: should this param be public?
Body: fs.createReadStream(params.json_path), // use read stream option in case file is large
};
let count=0;
while (count<1) {
try {
var s3_response = await s3Uploader(s3Client, s3_params);
count++;
} catch (error) {
if (error.code=='ThrottlingException') {
qnabot.log(`Throttling exception: trying upload CSV again in 10 seconds`);
await sleep(10000);
continue;
} else {
throw error;
}
}
}
await sleep(10000);
// if FAQ exists already, delete the old one and update it
var index_params = {
IndexId: params.faq_index_id,
MaxResults: '30' // default max number of FAQs in developer edition
};
count=0;
while (count<1) {
try {
var list_faq_response = await faqLister(kendraClient, index_params);
count++;
} catch (error) {
if (error.code=='ThrottlingException') {
qnabot.log(`Throttling exception: trying list FAQs again in 10 seconds`);
await sleep(10000);
continue;
} else {
throw error;
}
}
}
await sleep(10000);
var j, elem, index=null;
for (j=0; j<list_faq_response.FaqSummaryItems.length; j++) {
elem = list_faq_response.FaqSummaryItems[j];
if (elem.Name == params.faq_name) {
index = elem.Id;
break;
}
}
if (index != null) {
var delete_faq_params = {
Id: index,
IndexId: params.faq_index_id
}
count=0;
while (count<1) {
try {
var del_faq_response = await faqDeleter(kendraClient, delete_faq_params);
count++;
} catch (error) {
if (error.code=='ThrottlingException') {
qnabot.log(`Throttling exception: trying delete FAQ again in 10 seconds`);
await sleep(10000);
continue;
} else {
throw error;
}
}
}
} else {
qnabot.log("No old FAQ to delete");
}
await sleep(10000);
// create the FAQ
var faq_params = {
IndexId: params.faq_index_id,
Name: params.faq_name,
RoleArn: params.kendra_s3_access_role,
FileFormat: "JSON",
S3Path: {
Bucket: params.s3_bucket,
Key: params.s3_key
},
Description: 'Exported FAQ of questions from QnABot designer console'
// if no tags, delete parameter because empty arrays cause throttling exceptions
};
count=0;
while (count<1) {
try {
var faq_response = await faqConverter(kendraClient, faq_params,delete_faq_params);
count++;
} catch (error) {
if (error.code=='ThrottlingException') {
qnabot.log(`Throttling exception: trying convert to FAQ again in 10 seconds`);
await sleep(10000);
continue;
} else {
throw error;
}
}
}
await sleep(10000);
return faq_response;
}