async function handleRequest()

in lambda/fulfillment/lib/middleware/lexRouter.js [110:198]


async function handleRequest(req, res, botName, botAlias) {
    function mapFromSimpleName(botName) {
        const bName = process.env[botName];
        return bName ? bName : botName;
    }
    function getFreeTextResponse(inputText, sentiment, sentimentScore) {
        let response = {
            message: "",
            slots: { 'FreeText' : inputText,
                'Sentiment' : sentiment,
                'SentimentPositive': _.get(sentimentScore, 'Positive', ''),
                'SentimentNegative': _.get(sentimentScore, 'Negative', ''),
                'SentimentNeutral': _.get(sentimentScore, 'Neutral', ''),
                'SentimentMixed': _.get(sentimentScore, 'Mixed', '')
            },
            dialogState: 'Fulfilled',
        } ;
        return response;
    }

    let tempBotUserID = _.get(req,"_userInfo.UserId","nouser");
    tempBotUserID = tempBotUserID.substring(0, 100); // Lex has max userId length of 100
    if (botName === FREE_TEXT_ELICIT_RESPONSE_NAME) {
        return getFreeTextResponse(_.get(req, "question"), _.get(req, "sentiment"), _.get(req, "sentimentScore"));
    } else {

        // if a connect client and an elicitResponse bot such as QNANumber and the user is confirming the response
        // from the bot, proxy a key pad press (phone touch) of 1 for Yes and 2 for No. This helps accessibility
        // when confirming responses to a Lex intent.
        let respText = _.get(req, "question");
        let progress = _.get(req, "session.qnabotcontext.elicitResponse.progress", undefined);
        if (isConnectClient(req) && ( botName != QNAYesNo && botName != QNAYesNoExit) && progress === 'ConfirmIntent') {
            if (respText === '1' || respText.toLowerCase() === 'one' || respText.toLowerCase() === 'correct' ) respText = 'Yes';
            if (respText === '2' || respText.toLowerCase() === 'two' ) respText = 'No';
        }
        if ((botName === QNAPhoneNumber || botName === QNAPhoneNumberNoConfirm) && ( progress === 'ElicitSlot' || progress === 'ElicitIntent' || progress === "" || progress === undefined ) ) {
            respText = 'my number is ' + respText;
        }
        if ((botName === QNADate || botName === QNADateNoConfirm) && ( progress === 'ElicitSlot' || progress === 'ElicitIntent' || progress === "" || progress === undefined ) ) {
            respText = 'the date is ' + respText;
        }
        
        // Resolve bot details from environment, if using simple name for built-in bots
        const botIdentity = mapFromSimpleName(botName);
        
        // Determine if we using LexV1 or LexV2.. LexV2 bot is identified by "lexv2::BotId/BotAliasId/LocaleId"
        let response = {};
        if (botIdentity.toLowerCase().startsWith("lexv2::")) {
            // lex v2 response bot
            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,
                text: respText

            };
            qnabot.log("Lex V2 parameters: " + JSON.stringify(params));
            const lexv2response = await lexV2ClientRequester(params); 
            qnabot.log("Lex V2 response: " + JSON.stringify(lexv2response));
            response.message = _.get(lexv2response, 'messages[0].content', '');
            // lex v2 FallbackIntent match means it failed to fill desired slot(s).
            if (lexv2response.sessionState.intent.name === "FallbackIntent" || 
                lexv2response.sessionState.intent.state === "Failed") {
                response.dialogState = "Failed";
            } else {
                response.dialogState = lexv2response.sessionState.dialogAction.type;
            }
            let slots = _.get(lexv2response,"sessionState.intent.slots");
            if (slots) {
                response.slots = _.mapValues(slots, x => { return _.get(x,"value.interpretedValue") } );
            }
        } else {
            // lex v1 response bot
            const params = {
                botAlias: botAlias,
                botName: mapFromSimpleName(botName),
                inputText: respText,
                userId: tempBotUserID,
            };
            qnabot.log("Lex V1 parameters: " + JSON.stringify(params));
            response = await lexV1ClientRequester(params);
        }
        return response;
    }
};