Last active
December 3, 2018 16:20
-
-
Save designfrontier/684b484e87b1e3ca8d96ebb6b5d70605 to your computer and use it in GitHub Desktop.
Pipe 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
#!/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(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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