javascript - 尝试在 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();
}
}```
解决方案
欢迎堆栈溢出!
您的代码中有一些错误,我将尝试一一描述/修复它们。
您CaptureLoanAmountHandler
以简单的.speak
命令结束,这意味着 Alexa 将在她完成您要求她说话的句子后立即关闭会话。为了保持会话打开,添加.reprompt
到响应构建器(您也可以使用.shouldEndSession
withfalse
参数,但从.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
.
除了看起来更接近对话框- 剧透警报 - 它只是在幕后完成所有与对话框相关的魔法,最后你会得到你要求的价值;)
推荐阅读
- lua - 试图将 https 字符串发送到 lua 中的网站
- javascript - 有人可以帮助我使用最不安全的登录页面的无效 javascript 代码吗?
- oracle - 如果 Oracle DB 位于单独的系统上,是否可以在 LINUX 上安装 Oracle APEX?
- redux - 单个史诗中的多个异步操作执行顺序
- angular - Karma 无法加载浏览器启动器
- android - 计算二维数组的总和很慢
- javascript - 失败的道具类型:道具`children`在`Sidebar`中标记为必填,但其值为`undefined`
- npm - `npm create-react-app` 命令不适用于 npm 版本 6.9.0
- typescript - 如何在打字稿中使用带有元组的array.map?
- java - 在 android Java 中实现底部抽屉