【发布时间】:2019-09-23 15:57:49
【问题描述】:
我的代码没有运行,谁能帮忙。 无法说出文本,我可以返回处理程序输入响应吗?测试函数是一个http调用,可能需要时间。
function test(url, number)
{
return 5;
}
function speak(handlerInput) {
return handlerInput.responseBuilder
.getResponse();
}
const NumberFactIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'NumberFactIntent';
},
handle(handlerInput) {
const theNumber = handlerInput.requestEnvelope.request.intent.slots.number.value;
const repromptOutput = " Would you like another fact?";
const URL = "http://numbersapi.com";
test(URL, theNumber).then(function (data) {
console.log("data is " + data);
handlerInput.responseBuilder
.speak("Test Data")
.reprompt(repromptOutput)
return speak(handlerInput);
}).catch(function (data) {
console.log("error is " + data);
handlerInput.responseBuilder
.speak(`I wasn't able to find a fact for ${theNumber}` )
.reprompt(repromptOutput)
return speak(handlerInput);
});
}
};
【问题讨论】:
-
"测试函数是一个可能需要时间的 http 调用" - http 调用需要多长时间。 Alexa 将在 8 秒后超时,因此您需要在此之前返回响应。
-
如果您将代码部署到 AWS Lambda,我还建议您检查 CloudWatch 日志是否有任何错误。
标签: node.js promise alexa alexa-app alexa-sdk-nodejs