2

I am creating an Alexa skill and have hosted my NodeJS code through AWS Lambda.

I need to access data from an API and pull it into my Lambda function to serve back to Alexa. How can I use packages like Express or, perhaps jQuery, to allow for API requests through my Lambda function?


I've found verbose/complex ways of installing npm packages, but was looking if there was a better / different way of doing this

I used the nodeJS http request. Including this request within the getWelcomeResponse() and testing within Alexa Dev, I get

The remote endpoint could not be called, or the response it returned was invalid.

Without the HTTP request, it returns an expected json response

 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 Error logs:

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 

Where the error is getaddrinfo ENOTFOUND at errnoException (dns.js:37:11) at Object.onanswer. I don't quite understand this error.

Jedi
  • 3,088
  • 2
  • 28
  • 47
user3871
  • 12,432
  • 33
  • 128
  • 268

1 Answers1

0

If you just need to make an HTTP call, this is built into NodeJS and you don't need to install any extra packages in Lambda. Look at the answer to this question: Sending http request in node.js

Community
  • 1
  • 1
Mark B
  • 183,023
  • 24
  • 297
  • 295
  • Fair enough, it would be nice to use a package though. So I used the `node` HTTP request, placed it in Lambda `function getWelcomeResponse(callback) ` method, and when I tested it within Alexa Dev, it returned `The remote endpoint could not be called, or the response it returned was invalid.` Without the HTTP request, it returns an expected `json` response – user3871 Apr 06 '16 at 19:26
  • Why would it be nice to use a package? Just to make your deployment process more complicated? Or do you mean it would be nice to use a framework that makes things easier somehow? I feel like the built-in NodeJS function is just as easy as using the jQuery AJAX function in a browser. – Mark B Apr 06 '16 at 19:31
  • Also, you need to go get the log for the failed function call out of CloudWatch and post it here. Or just read it and see what your error is. – Mark B Apr 06 '16 at 19:32
  • Mark, I've posted above. I put the `getWelcomeResponse` within the `node js http request` callback. From the logs above, you can see that the `console.log("GET WELCOME RESPONSE")` is called, but nothing is called within the `http` request method – user3871 Apr 06 '16 at 19:53