我正在创建 Alexa 技能,并通过 AWS Lambda 托管我的 NodeJS 代码。
我需要从 API 访问数据并将其拉入我的 Lambda 函数以返回给 Alexa。如何使用 Express 或 jQuery 等包来允许通过我的 Lambda 函数发出 API 请求?
<小时/>我找到了verbose/complex ways安装 npm
软件包,但正在寻找是否有更好/不同的方法来执行此操作
我使用了 nodeJS
http
请求。将此请求包含在 getWelcomeResponse()
中并在 Alexa Dev 中进行测试,我得到
The remote endpoint could not be called, or the response it returned was invalid.
没有 HTTP 请求,它返回预期的 json 响应
function getWelcomeResponse(callback) {
console.log('GET WELCOME RESPONSE');
var options = {
host: 'http://clg-api-dev.elasticbeanstalk.com',
port: 7474,
path: '/1.0/leveldata/3',
method: 'GET',
headers: {
accept: 'application/json'
}
};
var x = http.request(options,function(res){
console.log("Connected");
res.on('data',function(data){
console.log('My Data: ', data);
var sessionAttributes = {},
speechOutput = "Reindeer Games. I will ask you " + GAME_LENGTH.toString()
+ " questions, try to get as many right as you can. Just say the number of the answer. Let's begin. ",
shouldEndSession = false,
gameQuestions = populateGameQuestions(),
sessionAttributes = {
"speechOutput": repromptText,
"repromptText": repromptText,
"currentQuestionIndex": currentQuestionIndex,
"correctAnswerIndex": correctAnswerIndex + 1,
"questions": gameQuestions,
"score": 0,
"correctAnswerText":
questions[gameQuestions[currentQuestionIndex]][Object.keys(questions[gameQuestions[currentQuestionIndex]])[0]][0]
};
callback(sessionAttributes, buildSpeechletResponse(CARD_TITLE, speechOutput, repromptText, shouldEndSession));
});
});
x.end();
}
Cloudwatch 错误日志:
START RequestId: e293453f-fc30-11e5-ae18-8723f88b4bb0 Version: $LATEST
2016-04-06T19:50:59.657Z e293453f-fc30-11e5-ae18-8723f88b4bb0 event.session.application.applicationId=amzn1.echo-sdk-ams.app.e8233bb6-ce2d-4a6c-8f82-e947d58d3bad
2016-04-06T19:50:59.767Z e293453f-fc30-11e5-ae18-8723f88b4bb0 onLaunch requestId=EdwRequestId.f6baa34c-bfc1-4758-b74d-9874d970c10e, sessionId=SessionId.7063c3b5-b2c0-4b1d-9180-d79aaeed9a49
2016-04-06T19:50:59.768Z e293453f-fc30-11e5-ae18-8723f88b4bb0 GET WELCOME RESPONSE
2016-04-06T19:50:59.934Z e293453f-fc30-11e5-ae18-8723f88b4bb0 Error: getaddrinfo ENOTFOUND at errnoException (dns.js:37:11) at Object.onanswer [as oncomplete] (dns.js:126:16)
END RequestId: e293453f-fc30-11e5-ae18-8723f88b4bb0
REPORT RequestId: e293453f-fc30-11e5-ae18-8723f88b4bb0 Duration: 315.42 ms Billed Duration: 400 ms Memory Size: 128 MB Max Memory Used: 14 MB
Process exited before completing request
其中错误是 Object.onanswer 处的 getaddrinfo ENOTFOUND at errnoException (dns.js:37:11)
。我不太明白这个错误。
最佳答案
如果您只需要进行 HTTP 调用,这是内置于 NodeJS 中的,您无需在 Lambda 中安装任何额外的软件包。看一下这个问题的答案:Sending http request in node.js
关于javascript - 通过 lambda 函数进行 Alexa API 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36459865/