尝试在 Alexa 中获取贷款金额和利率并计算 EMI

Trying to capture Loan Amount and Interest rate in Alexa and compute 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();
    }
}```

欢迎加入堆栈溢出!

您的代码中有一些错误,我会尝试 describe/fix 一个一个地解决它们。

您的 CaptureLoanAmountHandler 以简单的 .speak 命令结束,这意味着 Alexa 将在她说完您要她说话的句子后立即关闭会话。为了保持会话打开,将 .reprompt 添加到响应构建器(您也可以使用 .shouldEndSessionfalse 参数,但从用户体验的角度来看 .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.

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