Skip to content

Instantly share code, notes, and snippets.

@mooyoul
Created October 24, 2017 22:56
Show Gist options
  • Save mooyoul/1d5a188fed7ef7abbce27171652f1bee to your computer and use it in GitHub Desktop.
Save mooyoul/1d5a188fed7ef7abbce27171652f1bee to your computer and use it in GitHub Desktop.
Bosun Slack Proxy
const { text, send } = require('micro');
const request = require('request');
const debug = require('debug');
const LOG_TAG = 'bosun-slack-proxy';
debug.enable(LOG_TAG);
const log = debug(LOG_TAG);
module.exports = async (req, res) => {
const requestBody = await text(req);
log('Got body: %s', requestBody);
const { response, body } = await sendNotification(requestBody);
if (response.statusCode.toString().slice(0, 1) !== '2') {
log('Unexpected response code %s', response.statusCode);
await sendNotification(JSON.stringify({
// tslint:disable-next-line
text: [
`Slack respond with status code ${response.statusCode}.`,
`Request body: ${requestBody}`,
].join('\n'),
}));
}
res.setHeader('Content-Type', response.headers['content-type']);
send(res, response.statusCode, body);
};
function sendNotification(payload) {
return new Promise((resolve, reject) => {
request({
method: 'POST',
url: 'YOUR_SLACK_INCOMING_WEBHOOK_URL',
headers: {
'Content-Type': 'application/json',
},
body: payload,
}, (e, response, body) => {
if (e) {
return reject(e);
}
resolve({
response,
body,
});
});
});
}
{
"name": "bosun-slack-proxy",
"version": "1.0.0",
"description": "Proxies bosun notification to slack",
"main": "index.js",
"scripts": {
"start": "micro --port=9000",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "MooYeol Lee <[email protected]>",
"license": "ISC",
"dependencies": {
"debug": "^3.0.0",
"micro": "^8.0.3",
"request": "^2.81.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment