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

标签 json http amazon-web-services aws-lambda amazon-echo

我正在为新的 Amazon ECHO 研究一项“技能”。该技能将允许用户向 Alexa 询问有关 Enphase 太阳能系统的状态和性能的信息。 Alexa 将响应从基于 JSON 的 Enphase API 中提取的结果。例如,用户可以问,

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

问题是我已经有好几年没用 JavaScript 编程了,这是我第一次使用 AWS Lambda。我没有找到任何关于如何在 AWS Lambda 函数中将 JSON 查询嵌入到第三方服务器的信息。这是我的 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));
 }

一些指导将不胜感激。即使有人能指出我正确的方向。谢谢!

最佳答案

Request是一个非常流行的库,用于处理 node.js 中的 http 请求。以下是使用您的数据的 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));
  }
});

我没有 ECHO/Alexa 的示例,但这里有一个 Lambda calling out to get weather data to send it to Slack 的示例

关于json - 如何从 AWS Lambda 函数查询第三方 JSON API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34031878/

相关文章:

javascript - 如何从 Google Firebase 中存储的数据创建 JSON 字符串

android - 错误 Android - 收到 HTML 而不是 JSON

http - 为什么在 HTTP 请求的 'Bearer' header 中的 token 之前需要 'Authorization'?

node.js - amazon s3 deleteObjects nodejs - 无法正常工作

json - Spectron 测试产生 JScript 语法错误

Java 嵌套忽略内部属性

C++ cUrl通过api向 Telegram Bot 发送图像buff

ios - AWS 认知 : immediately signed-put after sign-in

postgresql - 在使用 AWS Data Pipeline 从 RDS postgres 数据库导出的 CSV 文件中包含列标题?

javascript - 如何访问具有 json 字符串的 javascript 对象中的特定键值对