首页 > 解决方案 > 尝试在 Alexa 中捕获贷款金额和利率并计算 EMI

问题描述

我正在尝试构建一个非常基本的 EMI 计算器。我想以不同的意图捕获贷款金额和利率,然后做数学部分。但是,在捕获贷款金额并确认相同之后,程序不会移动以捕获利率。

我只能达到 Alexa 确认贷款金额的价值。

请帮我理解为什么?

const Alexa = require('ask-sdk-core');


//Launch request and welcome message.
const LaunchRequestHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
    },
    handle(handlerInput) {
        const speakOutput = 'Hello! Welcome to your E.M.I. Calculator. Please tell me the loan amount you want to calculate the E.M.I. for';
        return handlerInput.responseBuilder
            .speak(speakOutput)
            .reprompt(speakOutput)
            .getResponse();
    }
};

//capture the loan amount, save it in local variable and confirm to user the amount.
const captureLoanAmountHandler = {
    canHandle(handlerInput){
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'captureloanamount';
    },
    async handle(handlerInput){
        const currencyName = handlerInput.requestEnvelope.request.intent.slots.currency.value;
        const loanAmount = handlerInput.requestEnvelope.request.intent.slots.loanamount.value;
        
        const speakOutput = `Ok, I have captured the loan amount as ${currencyName} ${loanAmount}.`;
 //       return handlerInput.responseBuilder.speak(speakOutput);
        
 //       return handlerInput.responseBuilder.speak(speakOutput).getResponse();
        
    }
    };
    
//Prompt user for interest rate and capture it
const captureInterestRateHandler = {
    canHandle(handlerInput){
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'captureinterestrate';
    },
    
    async handle(handlerInput){
        let interestRateCaptured = false;
        while (!interestRateCaptured){
            const speakOutput = 'Please tell me the interest rate';
               interestRateCaptured = true; 
        return handlerInput.responseBuilder
            .speak(speakOutput).getResponse();
        }    
            
        const iRate = handlerInput.requestEnvelope.request.intent.slots.roi.value;    
        const speakOutput1 = `Ok, I have captured the interest rate as ${iRate}`;
        return handlerInput.responseBuilder.speak(speakOutput1).getResponse();
    }
}```

标签: javascriptnode.jsalexaalexa-skills-kitalexa-skill

解决方案


欢迎堆栈溢出

您的代码中有一些错误,我将尝试一一描述/修复它们。

CaptureLoanAmountHandler以简单的.speak命令结束,这意味着 Alexa 将在她完成您要求她说话的句子后立即关闭会话。为了保持会话打开,添加.reprompt到响应构建器(您也可以使用.shouldEndSessionwithfalse参数,但从.reprompt用户体验的角度来看更好)并在那里添加一些线索,让用户如何与您的技能进行交互:

//capture the loan amount, save it in local variable and confirm to user the amount.
const CaptureLoanAmountHandler = {
    canHandle(handlerInput){
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'captureloanamount';
    },
    async handle(handlerInput){
        const currencyName = handlerInput.requestEnvelope.request.intent.slots.currency.value;
        const loanAmount = handlerInput.requestEnvelope.request.intent.slots.loanamount.value;
        
        const speakOutput = `Ok, I have captured the loan amount as ${currencyName} ${loanAmount}. Now tell me your interest rate`;
        
        return handlerInput
                 .responseBuilder
                 .speak(speakOutput)
                 .reprompt('Say: interest rate is...')
                 .getResponse();
        
    }
};

CaptureInterestRateHandler不应该包含while循环。在您的代码中,它只会运行一次,因为您true在第一次运行时将防护设置为;)

//Prompt user for interest rate and capture it
const CaptureInterestRateHandler = {
    canHandle(handlerInput){
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'captureinterestrate';
    },
    
    async handle(handlerInput){
        const iRate = handlerInput.requestEnvelope.request.intent.slots.roi.value;    
        const speakOutput1 = `Ok, I have captured the interest rate as ${iRate}`;
        return handlerInput.responseBuilder.speak(speakOutput1).getResponse();
    }
}

我想当您收集所有输入数据时应该进行一些计算。

根据您的评论:

//capture the loan amount, save it in local variable and confirm to user the amount.

我假设您希望稍后在其他意图处理程序中看到贷款金额值 - 恐怕您不会:(。所有变量,甚至const在单个处理程序运行中都可用。为了在其他意图中访问它们,您需要存储他们在SessionAttributes.

除了看起来更接近对话框- 剧透警报 - 它只是在幕后完成所有与对话框相关的魔法,最后你会得到你要求的价值;)


推荐阅读