in lambda/fulfillment/lib/middleware/specialtyBotRouter.js [157:239]
async function handleRequest(req, res, botName, botAlias) {
if (botName.toLowerCase().startsWith("lambda::")) {
// target bot is a Lambda Function
const lambdaName = botName.split("::")[1];
qnabot.log("Calling Lambda:", lambdaName);
let response = await lambdaClientRequester(lambdaName, req);
qnabot.log("lambda response: " + JSON.stringify(response,null,2));
return response;
} else {
function mapFromSimpleName(botName) {
const bName = process.env[botName];
return bName ? bName : botName;
}
// Resolve bot details from environment, if using simple name for built-in bots
const botIdentity = mapFromSimpleName(botName);
let tempBotUserID = _.get(req, "_userInfo.UserId", "nouser");
tempBotUserID = tempBotUserID.substring(0, 100); // Lex has max userId length of 100
// Determine if we using LexV1 or LexV2.. LexV2 bot is identified by "lexv2::BotId/BotAliasId/LocaleId"
if (botIdentity.toLowerCase().startsWith("lexv2::")) {
let res = {};
const ids = botIdentity.split("::")[1];
let [botId,botAliasId,localeId]=ids.split("/")
localeId = localeId || "en_US";
const params = {
botId: botId,
botAliasId: botAliasId,
localeId: localeId,
sessionId: tempBotUserID,
sessionState: {
sessionAttributes: generateMergedAttributes(req),
},
text: _.get(req, "question")
};
const lexv2response = await lexV2ClientRequester(params);
res.intentName = lexv2response.sessionState.intent.name;
res.sessionAttributes = lexv2response.sessionState.sessionAttributes;
res.dialogState = lexv2response.sessionState.intent.state;
res.slotToElicit = lexv2response.sessionState.dialogAction.slotToElicit;
let finalMessage = "";
if (lexv2response.messages && lexv2response.messages.length > 0) {
lexv2response.messages.forEach((mes) => {
if (mes.contentType === 'ImageResponseCard') {
res.responseCard = {};
res.responseCard.version = '1';
res.responseCard.contentType = 'application/vnd.amazonaws.card.generic';
res.responseCard.genericAttachments = [];
res.responseCard.genericAttachments.push(mes.imageResponseCard);
} else {
finalMessage += mes.content + " ";
}
});
}
res.message = finalMessage.trim();
// lex v2 FallbackIntent match means it failed to fill desired slot(s).
if (lexv2response.sessionState.intent.name === "FallbackIntent" ||
lexv2response.sessionState.intent.state === "Failed") {
res.dialogState = "Failed";
} else {
res.dialogState = lexv2response.sessionState.dialogAction.type;
}
let slots = _.get(lexv2response,"sessionState.intent.slots");
if (slots) {
res.slots = _.mapValues(slots, x => { return _.get(x,"value.interpretedValue") } );
}
return res;
} else {
const params = {
botAlias: botAlias,
botName: botIdentity,
inputText: _.get(req, "question"),
sessionAttributes: generateMergedAttributes(req),
userId: getBotUserId(req),
};
const response = await lexV1ClientRequester(params);
return response;
}
}
};