简体   繁体   English

如何从AWS Lambda函数查询第三方JSON API

[英]How to query third party JSON API from AWS Lambda function

I am working on a "Skill" for the new Amazon ECHO. 我正在为新的亚马逊ECHO工作“技能”。 The skill will allow a user to ask Alexa for information on the status and performance of an Enphase solar system. 该技能将允许用户向Alexa询问有关Enphase太阳系的状态和性能的信息。 Alexa will respond with results extracted from the JSON based Enphase API. Alexa将回应从基于JSON的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. 问题是我用JavaScript编程已经多年了,这是我第一次使用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. 我没有非常成功地找到有关如何将JSON查询嵌入AWS Lambda函数中的第三方服务器的任何信息。 Here is a relevant section of code in my Lambda function: 以下是我的Lambda函数中相关的代码部分:

 /**
  * 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! 谢谢!

Request is a very popular library for handling http requests in node.js. Request是一个非常流行的库,用于处理node.js中的http请求。 Here is an example of a POST using your data: 以下是使用您的数据的POST示例:

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 我没有ECHO / Alexa的例子,但这里有一个Lambda呼叫获取天气数据发送给Slack的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将JSON对象从第三方api转换为本地POJO - How to convert JSON object from third party api into local POJO Json没有将\\ n解析为来自第三方API的换行 - Json not parsing \n as new line from third party API 从第三方API获取固定数量的JSON对象 - Get fixed number of JSON objects from third-party API 来自第三方API的Json数据显示在HTML表中 - Json data from third party API display in HTML table 如何从shopify自定义app调用第三方站点的json api - How to call third party site's json api from shopify custom app 从AWS Lambda函数解析JSON答案 - Parsing JSON answer from an AWS Lambda function Lambda function to import json data from AWS API Gateway and insert into Mysql table - Lambda function to import json data from AWS API Gateway and insert into Mysql table 将JSON对象发布给属性名称具有空间的第三方API(来自C#) - Post json Object which property name has space to third party api from c# 如何使用 Nodejs 从 RDS 数据库中获取数据的列名作为 AWS Lambda Function 中 JSON 中的响应 - How to get column name of data from RDS database as response in JSON in AWS Lambda Function using Nodejs Access json request from python in python AWS lambda function - Access json request from python in python AWS lambda function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM