Skip to content

Instantly share code, notes, and snippets.

@masawada
Created December 10, 2018 03:56
Show Gist options
  • Save masawada/a96f1a7e6e617457d4ec3dc1f8b1f4ff to your computer and use it in GitHub Desktop.
Save masawada/a96f1a7e6e617457d4ec3dc1f8b1f4ff to your computer and use it in GitHub Desktop.
const url = require('url');
const https = require('https');
const slack_url = url.parse(process.env.SLACK_URL);
const channel = process.env.CHANNEL;
const username = process.env.USERNAME;
const icon_emoji = process.env.ICON_EMOJI;
const messages = {
SINGLE : process.env.SINGLE_CLICK_MESSAGE,
DOUBLE : process.env.DOUBLE_CLICK_MESSAGE,
LONG : process.env.LONG_CLICK_MESSAGE,
};
exports.handler = async (event) => {
return new Promise((resolve, reject) => {
const clickType = event['deviceEvent']['buttonClicked']['clickType'];
const text = messages[clickType];
const payload = JSON.stringify({ text, channel, username, icon_emoji });
const options = {
host: slack_url.hostname,
path: slack_url.pathname,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': payload.length,
},
};
const req = https.request(options, () => resolve('message sent'));
req.on('error', (error) => reject(`error: ${error.message}`));
req.write(payload);
req.end();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment