Skip to content

Instantly share code, notes, and snippets.

@jed
Created May 16, 2016 16:30
Show Gist options
  • Save jed/57dfb5f234d88534dd74fc9f22191cc3 to your computer and use it in GitHub Desktop.
Save jed/57dfb5f234d88534dd74fc9f22191cc3 to your computer and use it in GitHub Desktop.
Break Bot: pushing AWS IoT button events to Slack
'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