Created
May 16, 2016 16:30
-
-
Save jed/57dfb5f234d88534dd74fc9f22191cc3 to your computer and use it in GitHub Desktop.
Break Bot: pushing AWS IoT button events to Slack
This file contains 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
'use strict' | |
// for context, see https://twitter.com/jedschmidt/status/732244756491816960 | |
const https = require('https') | |
const qs = require('querystring') | |
const WEBHOOK_PATH = '/services/YOUR_WEBHOOK_PATH_HERE' | |
const EVENTS_BY_CLICKTYPE = { | |
SINGLE : {emoji: ':coffee:', type: 'beverages'}, | |
DOUBLE : {emoji: ':beer:', type: 'libations'}, | |
LONG : {emoji: ':fork_and_knife:', type: 'food'} | |
} | |
exports.handler = function(data, context, cb) { | |
let event = EVENTS_BY_CLICKTYPE[data.clickType] | |
if (!event) return cb() | |
let payload = JSON.stringify({ | |
channel: '#coffee-break', | |
username: 'Break Bot', | |
text: `Come on down to the kitchen, we're enjoying some ${event.type}!`, | |
icon_emoji: event.emoji | |
}) | |
let body = qs.stringify({payload}) | |
let options = { | |
method: 'POST', | |
host: 'hooks.slack.com', | |
path: WEBHOOK_PATH, | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'Content-Length': Buffer.byteLength(body) | |
} | |
} | |
https.request(options) | |
.on('error', cb) | |
.on('response', res => { | |
res.setEncoding('utf8') | |
res.on('data', msg => { | |
res.statusCode == 200 ? cb(null, msg) : cb(msg) | |
}) | |
}) | |
.end(body) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment