Skip to content

Instantly share code, notes, and snippets.

@peccu
Last active September 21, 2017 01:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peccu/58ff0b92c329575cd79cd9dd31d6b965 to your computer and use it in GitHub Desktop.
Save peccu/58ff0b92c329575cd79cd9dd31d6b965 to your computer and use it in GitHub Desktop.
send stdin or arguments to slack like chat via webhook
#!/usr/bin/env node
/* references
- [How to get node.js HTTP request promise without a single dependency](https://www.tomas-dvorak.cz/posts/nodejs-request-without-dependencies/)
- [Process | Node.js v8.5.0 Documentation](https://nodejs.org/api/process.html#process_process_stdin)
- stdin
- [HTTP | Node.js v8.5.0 Documentation](https://nodejs.org/docs/latest/api/http.html#http_http_request_options_callback)
- request
- [URL | Node.js v8.5.0 Documentation](https://nodejs.org/docs/latest/api/url.html)
- parse url
*/
const hook_url='https://webhook/url';
const channel='channel-name';
const icon=':deciduous_tree:';
const getStdin = function(){
return new Promise((resolve, reject) => {
process.stdin.setEncoding('utf8');
var input = [];
process.stdin.on('readable', () => {
const chunk = process.stdin.read();
if (chunk !== null) {
input.push(chunk);
}
});
process.stdin.on('end', () => {
process.stdout.write(`> sending data:\n${input.join('')}`);
resolve(input.join(''));
});
});
};
const postContent = function(url, data){
return new Promise((resolve, reject) => {
const postData = require('querystring').stringify(data);
const urllib = require('url');
const parsedUrl = urllib.parse(url);
const options = {
hostname: parsedUrl.host,
port: url.startsWith('https') ? 443 : 80,
path: parsedUrl.path,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postData)
}
};
const lib = url.startsWith('https') ? require('https') : require('http');
const req = lib.request(options, (res) => {
// handle http errors
if (res.statusCode < 200 || res.statusCode > 299) {
reject(new Error('Failed to load page, status code: ' + res.statusCode));
}
// console.log(`STATUS: ${res.statusCode}`);
// console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf8');
const body = [];
res.on('data', (chunk) => {
// console.log(`BODY: ${chunk}`);
body.push(chunk);
});
res.on('end', () => {
// console.log('No more data in response.');
resolve(body.join(''));
});
});
req.on('error', (e) => {
console.error(`problem with request: ${e.message}`);
reject(e);
});
// write data to request body
req.write(postData);
req.end();
});
};
getStdin().then(function(input){
const mes = input;
const data = {
payload: JSON.stringify({
icon_emoji: icon,
text: '```' + mes + '```',
channel: channel
})
};
return postContent(hook_url, data);
})
.then(function(out){
console.log('> done');
})
.catch(function(e){
console.log('error', e);
});
#!/bin/bash
HOOK_URL='https://webhook/url'
CHANNEL='channel-name'
ICON=':cat:'
post() {
local mes=$(cat - |perl -pe 's/\n/\\n/g'|perl -pe 's/"/\\"/g'|perl -pe "s/'/'\\\\''/g")
curl -X POST --data-urlencode 'payload={"icon_emoji":"'$ICON'","text":"```'$mes'```","channel":"'$CHANNEL'"}' $HOOK_URL
}
if [ -p /dev/stdin ]; then
cat -
else
echo $@
fi | post
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment