Miki P
Miki P

Reputation: 652

Node JS request.post not firing in AWS Lambda

I'm attempting to build an Alexa skill, which at its launch has to get data by issuing a post request, via the Node JS request module.

The code excerpt which actually issues the request works, as I've tested it by itself. However, when I try to include it in the AWS Lambda Function, it doesn't get executed.

As I'm a beginner in JS and AWS, debugging this is extremely difficult for me, and I've been unable to figure it out even remotely. Any help it truly appreciated.

    exports.handler = (event, context) => {
    try {
        // New session
        if (event.session.new) {
            // New Session
            console.log("NEW SESSION");
        }

        // Launch Request
        switch (event.request.type) {
            case "LaunchRequest":
                var url = "https://api.random.org/json-rpc/1/invoke";
                var myRequest = {
                    "jsonrpc": "2.0",
                    "method": "generateStrings",
                    "params": {
                        "apiKey": "my-api-key",
                        "n": "1",
                        "length": "3",
                        "characters": "abcdefghijklmnopqrstuvwxyz0123456789"
                    },
                    "id": 96
                }
                var pin;
                request.post(
                    url,
                    {json: myRequest},
                    function (error, response, body) {
                        if (!error && response.statusCode == 200) {
                            pin = body.result.random.data[0];
                            console.log(pin);
                        }
                        else {
                            console.log(error);
                        }
                    }
                );
                console.log("LAUNCH REQUEST");
                var welcome = "Welcome. ";
                var pinStatement = "Your 3 letter or number pin is: " + pin
                context.succeed(
                    generateResponse(
                        buildSpeechletReponse(welcome + pinStatement, true),
                        {}
                    )
                );
                break;
            // Intent Request
            case "IntentRequest":
                console.log("INTENT REQUEST");
                break;

            // Session Ended Request
            case "SessionEndedRequest":
                console.log("SESSION ENDED REQUEST");
                break;

                default:
                    context.fail(`INVALID REQUEST TYPE: ${event.request.type}`);
            }
        }
        catch (error) {
            context.fail(`Exception: ${error}`);
        }
    }

//Here are the helper functions: 

  buildSpeechletReponse = (outputText, shouldEndSession) => {
    return {
        outputSpeech : {
            type: "PlainText",
            text: outputText
        },
        shouldEndSession: shouldEndSession
    };
}

    generateResponse = (sessionAttributes, speechletResponse) => {

    return {
        version: "1.0",
        sessionAttributes: sessionAttributes,
        response: speechletResponse
    };
}

Upvotes: 0

Views: 1272

Answers (1)

idbehold
idbehold

Reputation: 17168

You're calling context.succeed() before your request receives it's response.

case "LaunchRequest":
  var url = "https://api.random.org/json-rpc/1/invoke";
  var myRequest = {
      "jsonrpc": "2.0",
      "method": "generateStrings",
      "params": {
          "apiKey": "my-api-key",
          "n": "1",
          "length": "3",
          "characters": "abcdefghijklmnopqrstuvwxyz0123456789"
      },
      "id": 96
  }
  var pin;
  request.post(
      url,
      {json: myRequest},
      function (error, response, body) {
          if (!error && response.statusCode == 200) {
              pin = body.result.random.data[0];
              console.log(pin);
              var response = buildSpeechletReponse("Welcome. Your 3 letter or number pin is: " + pin, true)
              context.succeed(generateResponse(response, {}));
          }
          else {
              context.fail(error || body);
          }
      }
  );
  console.log("LAUNCH REQUEST");
  break;

Upvotes: 2

Related Questions