Skip to content

Instantly share code, notes, and snippets.

@carlbennett
Created October 13, 2018 23:58
Show Gist options
  • Save carlbennett/d3bf560b56d620b77d62c1f55e551dde to your computer and use it in GitHub Desktop.
Save carlbennett/d3bf560b56d620b77d62c1f55e551dde to your computer and use it in GitHub Desktop.
Discord Webhook Relay for Grafana (deprecated now that Grafana has this built-in)
[Unit]
Description=webhookRelay.js - a Slack to Discord webhook relay for Grafana
Documentation=https://gist.github.com/brussell98/a865ecc2284e958226596accc852ee28
After=network.target
[Service]
Type=simple
User=nobody
ExecStart=/usr/bin/node /opt/discordWebhookRelay/webhookRelay.js
Restart=on-failure
[Install]
WantedBy=multi-user.target
const express = require('express'),
bodyParser = require('body-parser'),
request = require('unirest'),
app = express();
app.disable('x-powered-by');
app.set('env', 'production');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/relay/:id/:token', log, (req, res) => {
if (!req.params.id || !req.params.token || !req.body)
res.sendStatus(400);
if (req.body.payload) {
req.body = JSON.parse(req.body.payload);
}
for (let attachment of req.body.attachments) {
if (!attachment.fields)
continue;
for (let field of attachment.fields) {
field.name = field.title;
delete field.title;
delete field.short;
field.value = field.value.toString();
}
}
var embed = { "embeds": [{
"url": req.body.attachments[0].title_link.replace(/ /g,''),
"image": { "url": req.body.attachments[0].image_url.replace(/ /g,'') },
"title": req.body.attachments[0].title,
"description": req.body.attachments[0].text,
"color": parseInt((req.body.attachments[0].color.replace(/#/g,'')), 16),
"fields": req.body.attachments[0].fields,
"timestamp": new Date().toISOString(),
"footer": {
"icon_url": req.body.attachments[0].footer_icon.replace(/ /g,''),
"text": req.body.attachments[0].footer
}
}]};
console.log("Relaying " + req.body.attachments[0].title + " it is " + new Date().toISOString());
//console.log('Relaying data: ' + require('util').inspect(embed, { depth: null }));
request.post(`https://canary.discordapp.com/api/webhooks/${req.params.id}/${req.params.token}?wait=true`)
.type('application/json')
.header('User-Agent', 'Webhook-Relay v1')
.send(embed)
.end(response => {
if (response.ok) {
//console.log('Relay Response: ' + require('util').inspect(response.body, { depth: null }));
res.status(200).send(response.body);
} else {
console.error('Relay Error: ' + require('util').inspect(response.body, { depth: null }));
res.sendStatus(500);
}
});
});
app.use((req, res) => {
res.status(404);
if (req.accepts('html'))
res.send('<h1>' + req.url + ' not found</h1>');
else if (req.accepts('json'))
res.send({ error: 'Not found' });
else
res.type('txt').send('Not found');
});
function log(req, res, next) {
console.log(`${req.method} ${req.path} from ${req.headers['x-forwarded-for'] || req.connection.remoteAddress || req.socket.remoteAddress || req.connection.socket.remoteAddress}`);
return next();
}
app.listen(4321, '127.0.0.1', error => {
if (error)
return console.log(error)
console.log('Webhook Relay online');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment