Skip to content

Instantly share code, notes, and snippets.

@designfrontier
Last active December 3, 2018 16:20
Show Gist options
  • Save designfrontier/684b484e87b1e3ca8d96ebb6b5d70605 to your computer and use it in GitHub Desktop.
Save designfrontier/684b484e87b1e3ca8d96ebb6b5d70605 to your computer and use it in GitHub Desktop.
Pipe to Slack
#!/usr/bin/env node
const http = require('https');
const url = process.env.SLACK_URL;
let data;
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(chunk) {
if (chunk) {
if (data) {
data += chunk;
} else {
data = chunk;
}
}
});
process.stdin.on('end', function() {
console.log(process.argv);
const payload = {
channel: process.argv[2] || 'test-slackbot',
username: process.argv[3] || 'pairingbot',
icon_emoji: process.argv[4] || ':pairing:',
text: data
}
const options = {
hostname: 'hooks.slack.com',
port: 443,
path: url,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
};
const req = http.request(options, (res) => {
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
});
req.on('error', (e) => {
console.error(`problem with request: ${e.message}`);
});
// write data to request body
req.write(JSON.stringify(payload));
req.end();
});
@designfrontier
Copy link
Author

Requires a slack webhook URL in your env as SLACK_URL and accepts channel, botname, bot emoji as arguments. Then takes whatever is piped into it and outputs it in the designated channel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment