Alexa API通过lambda函数请求
Alexa API request through lambda function
我正在创建Alexa技能,并通过AWS Lambda托管了我的NodeJS代码。
我需要从API访问数据,并将其提取到我的Lambda函数中,以服务于Alexa。我如何使用Express或jQuery这样的包来允许通过我的Lambda函数进行API请求?
我发现了安装npm
包的详细/复杂的方法,但我正在寻找是否有更好/不同的方法来完成
我使用了nodeJS
http
请求。包括getWelcomeResponse()
中的这个请求和Alexa Dev中的测试,我得到了
无法调用远程终结点,或者它返回的响应无效。
如果没有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
其中错误为getaddrinfo ENOTFOUND at errnoException (dns.js:37:11) at Object.onanswer
。我不太理解这个错误。
如果你只需要进行HTTP调用,它内置在NodeJS中,你不需要在Lambda中安装任何额外的包。看看这个问题的答案:在node.js 中发送http请求
相关文章:
- 暂停函数执行流程,直到ajax请求完成
- google在类内映射javascript directionsService.route请求:将类实例传递给回调函数
- 如何发送作为节点请求响应函数中的代码块的响应
- 为什么不't CasperJS'sendAJAX函数为PUT请求发送任何内容
- 如何在类函数中发送ajax请求
- JavaScriptajax请求servlet调用错误回调函数
- 如何仅在完成对gap.client.youtube的请求.execute后执行函数
- 使用 PHP 生成的按钮生成 POST 请求,这些按钮调用带有参数的 javascript 函数
- 节点 js 将变量传递到请求回调函数中
- 在导出函数 MEANJS 中编写 Http 请求
- 将对象函数传递给请求动画帧时丢失对象引用
- 将请求作为参数传递给函数
- 带有Ajax请求的Javascript JSON.parse函数n〃;新行转义字符
- 迭代node.js请求函数
- AJAX请求导致更改函数解除绑定
- AngularJS http.jsonp请求.then和.success函数不起作用
- 使用jQuery.ajax请求向PHP函数发送一个对象数组
- AJAX请求返回Javascript获胜't返回特定函数,但将返回其他函数
- var someSome=(函数(){..}());一个奇怪的javascript请求
- Alexa API通过lambda函数请求