-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathalexa_proxy.js
58 lines (52 loc) · 2.01 KB
/
alexa_proxy.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
53
54
55
56
57
58
// A proxy for passing Amazon Alexa requests to your smarthouse web server using the Amazon Lambda service as a proxy.
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://your_smarthouse_ip:80/your_smarthouse_code.pl'
};
// Look up the url to call based on the appId
var url = handlers[json.session.application.applicationId];
console.log("db url:" + url)
if (!url) { context.fail("No url found for application id:" + json.session.application.applicationID); }
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,
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);
console.log("db json:" + post_data)
req.end();
} catch (e) {
console.log("db error:" +e)
context.fail("Exception: " + e);
}
console.log("db all done.")
};