Skip to content

Instantly share code, notes, and snippets.

@phit
Forked from brussell98/webhookRelay.js
Last active February 21, 2022 11:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phit/6d48d2703dc3f57e0e20d86386cd10c8 to your computer and use it in GitHub Desktop.
Save phit/6d48d2703dc3f57e0e20d86386cd10c8 to your computer and use it in GitHub Desktop.
Make slack Grafana webhooks discord compatible
const express = require('express'),
bodyParser = require('body-parser'),
request = require('unirest'),
app = express();
function HEXToVBColor(rrggbb) {
return parseInt(rrggbb, 16);
}
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,
"image": { "url": req.body.attachments[0].image_url },
"title": req.body.attachments[0].title,
"description": req.body.attachments[0].text,
"color": HEXToVBColor(req.body.attachments[0].color.replace(/#/g,'')),
"fields": req.body.attachments[0].fields,
"timestamp": new Date().toISOString(),
"footer": {
"icon_url": req.body.attachments[0].footer_icon,
"text": req.body.attachments[0].footer
}
}]};
//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(8080, '0.0.0.0', error => {
if (error)
return console.log(error)
console.log('Webhook Relay online');
});
@samwiseg0
Copy link

You don't really need this... Discord already converts Slack on their end. just add /slack to the URL ie. https://discordapp.com/api/webhooks/XXXXX/XXXXXXXX/slack

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