forked from sarkonovich/Alexa-Hue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_passthrough.js
52 lines (48 loc) · 1.72 KB
/
lambda_passthrough.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
var http = require('http');
var URLParser = require('url');
exports.handler = function (json, context) {
try {
// A list of URL's to call for each applicationId
var handlers = {
'appId':'url',
'amzn1.echo-sdk-ams.app.xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx':'http://xxxxxxxx.ngrok.io/lights'
};
// Look up the url to call based on the appId
var url = handlers[json.session.application.applicationId];
if (!url) { context.fail("No url found for application id"); }
var parts = URLParser.parse(url);
var post_data = JSON.stringify(json);
// An object of options to indicate where to post to
var post_options = {
host: parts.hostname,
auth: parts.auth,
port: (parts.port || 80),
path: parts.path,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': post_data.length
}
};
// Initiate the request to the HTTP endpoint
var req = http.request(post_options,function(res) {
var body = "";
// Data may be chunked
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
// When data is done, finish the request
context.succeed(JSON.parse(body));
});
});
req.on('error', function(e) {
context.fail('problem with request: ' + e.message);
});
// Send the JSON data
req.write(post_data);
req.end();
} catch (e) {
context.fail("Exception: " + e);
}
};