This repository has been archived by the owner on Sep 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
button.js
66 lines (59 loc) · 2.15 KB
/
button.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
59
60
61
62
63
64
65
66
/**
* This is a sample that connects Lambda with IFTTT Maker channel. The event is
* sent in this format: <serialNumber>-<clickType>.
*
* The following JSON template shows what is sent as the payload:
{
"serialNumber": "GXXXXXXXXXXXXXXXXX",
"batteryVoltage": "xxmV",
"clickType": "SINGLE" | "DOUBLE" | "LONG"
}
*
* A "LONG" clickType is sent if the first press lasts longer than 1.5 seconds.
* "SINGLE" and "DOUBLE" clickType payloads are sent for short clicks.
*
* For more documentation, follow the link below.
* http://docs.aws.amazon.com/iot/latest/developerguide/iot-lambda-rule.html
*/
'use strict';
const https = require('https');
const makerKey = '{YOUR KEY GOES HERE}'; // change it to your Maker key
var AWS = require("aws-sdk");
var dynamodb = new AWS.DynamoDB.DocumentClient({region: 'us-east-1'});
exports.handler = (event, context, callback) => {
console.log('Received event:', event);
if (event.clickType != "SINGLE") {
// make sure you created a receipe for event <serialNumber>-<clickType>
const makerEvent = `${event.serialNumber}-${event.clickType}`;
const url = `https://maker.ifttt.com/trigger/${makerEvent}/with/key/${makerKey}`;
https.get(url, (res) => {
let body = '';
console.log(`STATUS: ${res.statusCode}`);
res.on('data', (chunk) => body += chunk);
res.on('end', () => {
console.log('Event has been sent to IFTTT Maker channel');
callback(null, body);
});
}).on('error', (e) => {
console.log('Failed to trigger Maker channel', e);
callback(`Failed to trigger Maker channel: ${e.message}`);
});
} else {
var date = Date.now();
var params = {
TableName: 'litterboxStatus',
Item: {
'litterbox' : event.serialNumber,
'date' : date,
}
};
// Call DynamoDB to add the item to the table
dynamodb.put(params, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data);
}
});
}
};