2015-12-01 42 views
6

我正在为新的Amazon ECHO“技能”工作。该技能将允许用户询问Alexa有关Enphase太阳能系统状态和性能的信息。 Alexa将回应从基于JSON的Enphase API中提取的结果。例如,用户可能会问,如何从AWS Lambda函数查询第三方JSON 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)); 
} 

某些指南将不胜感激。即使有人能指引我正确的方向。谢谢!

回答

6

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

+0

一个例子。 js Lambda代码? – Darko

+0

您必须在您上传的zip文件中包含它(node_modules)以及index.js。如果您只是在aws控制台中编辑代码,则无法执行此操作。这篇文章谈论它:https://aws.amazon.com/blogs/compute/nodejs-packages-in-lambda/或者你可以使用这个启动器,它有一些内置的命令,将它压缩为你: https://github.com/ryanray/aws-lambda-starter – Ryan