Created
May 11, 2018 19:37
-
-
Save cdeutsch/dfc05e327b327b555cf5eb09257d1991 to your computer and use it in GitHub Desktop.
Amazon AWS IOT Button to IFTTT Webhook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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'); | |
exports.handler = (event, context, callback) => { | |
console.log('Received event:', event); | |
// make sure you created a receipe for event <serialNumber>-<clickType> | |
const makerEvent = `${event.serialNumber}-${event.clickType}`; | |
console.log('Maker Event', makerEvent); | |
const url = `https://maker.ifttt.com/trigger/${makerEvent}/with/key/${process.env.MAKER_KEY}`; | |
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}`); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment