2015-12-01 7 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ラムダを使用して、私の最初の時間です。私は、AWS Lambda関数内のサードパーティサーバにJSONクエリを埋め込む方法に関する情報を見つけるのに成功しませんでした。ここで私のラムダ関数のコードの関連するセクションです:

/** 
    * 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要求を処理するための非常に一般的なライブラリです。

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 /アレクサの例を持っていないが、ここで私はノード内で使用するための要求をインストールするにはどうすればよいLambda calling out to get weather data to send it to Slack

+0

の例である:ここでは、あなたのデータを使用してPOSTの例があります。 jsラムダコード? – Darko

+0

アップロードするzipファイルにindex.jsとともにnode_modulesを含める必要があります。 awsコンソールでコードを編集しているだけなら、それはできません。この記事では、https://aws.amazon.com/blogs/compute/nodejs-packages-in-lambda/の講演や、このスターターを使用することができます。このスターターには、いくつかのコマンドが組み込まれています。 https://github.com/ryanray/aws-lambda-starter – Ryan

関連する問題