Skip to content

Instantly share code, notes, and snippets.

@benbjurstrom
Created May 16, 2016 01:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benbjurstrom/3a2e545fb3c0325840fe67b46114a3c3 to your computer and use it in GitHub Desktop.
Save benbjurstrom/3a2e545fb3c0325840fe67b46114a3c3 to your computer and use it in GitHub Desktop.
Amazon IOT Button To IFTTT
/**
* An Amazon Lambda function to send an IOT button request to an IFTTT Maker endpoint.
* Code adapted from SNS2IFTTT by Danilo Poccia [https://github.com/danilop/SNS2IFTTT/blob/master/index.js]
*/
var http = require('http');
var iftttMakerSecretKey = '<YOUR IFTTT MAKER SECRET KEY>';
/**
* When invoked from an IOT Button press the event payload contains the following:
{
"serialNumber": "GXXXXXXXXXXXXXXXXX",
"batteryVoltage": "xxmV",
"clickType": "SINGLE" | "DOUBLE" | "LONG"
}
*/
exports.handler = (event, context) => {
console.log('Received event:', event.clickType);
/**
* To allow the use of multiple IOT Buttons with three distinct functions per Button we'll compose our
* Maker event name from the device serial and the click type ("SINGLE" | "DOUBLE" | "LONG")
*/
var iftttMakerEventName = 'iot_' + event.serialNumber +'_'+ event.clickType;
// Compose the final Maker url
var iftttMakerUrl =
'http://maker.ifttt.com/trigger/'
+ iftttMakerEventName
+ '/with/key/'
+ iftttMakerSecretKey;
// Make the http request
console.log('start request to ' + iftttMakerUrl)
http.get(encodeURI(iftttMakerUrl), function(res) {
console.log("Got response: " + res.statusCode);
context.succeed('SUCCESS');
}).on('error', function(e) {
console.log("Got error: " + e.message);
context.done(null, 'FAILURE');
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment