'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