Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query third party JSON API from AWS Lambda function

I am working on a "Skill" for the new Amazon ECHO. The skill will allow a user to ask Alexa for information on the status and performance of an Enphase solar system. Alexa will respond with results extracted from the JSON based Enphase API. For example, the user could ask,

 "Alexa.  Ask Enphase how much solar energy I have produced in the last week."
 ALEXA <"Your array has produced 152kWh in the last week.">

Problem is it has been years since I've programmed in JavaScript and this is my first time using AWS Lambda. I have not been very successful finding any information on how to embed a JSON query to a third party server within AWS Lambda function. Here is a relevant section of code in my Lambda function:

 /**
  * Gets power from Enphase API and prepares speach
  */
 function GetPowerFromEnphase(intent, session, callback) {
      var Power = 0;
      var repromptText = null;
      var sessionAttributes = {};
      var shouldEndSession = false;
      var speechOutput = "";

      //////////////////////////////////////////////////////////////////////
      // Need code here for sending JSON query to Enphase server to get power
      // Request:
      // https://api.enphaseenergy.com/api/v2/systems/67/summary
      // key=5e01e16f7134519e70e02c80ef61b692&user_id=4d7a45774e6a41320a
      // Response:
      // HTTP/1.1 200 OK
      // Content-Type: application/json; charset=utf-8
      // Status: 200
      // {"system_id":67,"modules":35,"size_w":6270,"current_power":271,
      // "energy_today":30030,"energy_lifetime":59847036,
      // "summary_date":"2015-03 04","source":"microinverters",
      // "status":"normal","operational_at":1201362300,
      // "last_report_at":1425517225}
      //////////////////////////////////////////////////////////////////////

      speechOutput = "Your array is producing " + Power + " kW, goodbye";
      shouldEndSession = true;

      // Setting repromptText to null signifies that we do not want to reprompt the user.
      // If the user does not respond or says something that is not understood, the session
      // will end.
      callback(sessionAttributes,
         buildSpeechletResponse(intent.name, speechOutput, repromptText,
         shouldEndSession));
 }

Some guidance would be much appreciated. Even if someone could point me in the right direction. Thanks!

like image 671
Darko Avatar asked Dec 01 '15 23:12

Darko


People also ask

Can I call external API from AWS Lambda?

Now, to call an external REST API, we'll modify the Lambda Function as given below and will invoke this Lambda function from our Client-side using AWS-SDK. The example function returns a 200 response to clients, with data from the requested external API. statusCode: 200, body: JSON.


1 Answers

Request is a very popular library for handling http requests in node.js. Here is an example of a POST using your data:

var request = require('request');

request({
  url: 'https://api.enphaseenergy.com/api/v2/systems/67/summary',
  method: 'POST',
  headers: {
    Accept: 'application/json',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    key: '5e01e16f7134519e70e02c80ef61b692',
    user_id: '4d7a45774e6a41320a'
  })
}, function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log('BODY: ', body);
    var jsonResponse = JSON.parse(body); // turn response into JSON

    // do stuff with the response and pass it to the callback...

    callback(sessionAttributes, 
        buildSpeechletResponse(intent.name, speechOutput, repromptText,
        shouldEndSession));
  }
});

I don't have an example of ECHO/Alexa but here is an example of Lambda calling out to get weather data to send it to Slack

like image 198
Ryan Avatar answered Oct 12 '22 14:10

Ryan