gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 09:33:48 25 4
gpt4 key购买 nike

我正在尝试构建一个非常基本的 EMI 计算器。我想在不同的 intent 中捕获贷款金额和利率,然后做数学部分。但是,在捕获贷款金额并确认相同后,程序不会移动以捕获利率。
我只能达到 Alexa 确认贷款金额值(value)的程度。
请帮我理解为什么?

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 会在她说完您让她说话的句子后立即关闭 session 。为了保持 session 打开,添加 .reprompt到响应构建器(您也可以将 .shouldEndSessionfalse 参数一起使用,但从 UX 的 Angular 来看 .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.
我假设您希望稍后在其他 intent 处理程序中看到贷款金额值 - 恐怕您不会:(。所有变量,甚至 const 在单个处理程序运行中都可用。为了在其他 intent 中访问它们,您需要将它们存储在 SessionAttributes 中。
除此之外,看起来更接近 Dialog - 剧透警报 - 它只是在幕后执行所有与对话相关的魔法,最后你会得到你要求的值;)

关于javascript - 尝试在 Alexa 中获取贷款金额和利率并计算 EMI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62688137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com