-
-
Save brussell98/a865ecc2284e958226596accc852ee28 to your computer and use it in GitHub Desktop.
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.title = field.title || ''; | |
field.value = field.value || ''; | |
} | |
} | |
console.log('Relaying data: ' + require('util').inspect(req.body, { depth: null })); | |
request.post(`https://canary.discordapp.com/api/webhooks/${req.params.id}/${req.params.token}/slack?wait=true`) | |
.type('application/json') | |
.header('User-Agent', 'Webhook-Relay v1') | |
.send(req.body) | |
.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'); | |
}); |
@jmarcos-cano - A few facts that may help you out and anyone else that finds this.
The script binds to 0.0.0.0
on port 8080
(changeable on line 61).
To configure Grafana, create a new slack
alert channel, then take the Discord webhook link:
https://discordapp.com/api/webhooks/:id/:token
and convert it to:
http://<bind-address>:<bind-port>/relay/:id/:token
while leaving the other fields empty/unset.
The script must be running for the webhook to succeed, of course.
To run the script, you'll need to install nodejs and a few node modules:
npm install express unirest
then:
node ./webhookRelay.js
Good luck!
Here's a SystemD service unit file for this.
[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 /data/discordWebhookRelay/webhookRelay.js
Restart=on-failure
[Install]
WantedBy=multi-user.target
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
is this to send webhooks to discord via slack webhooks? or viceversa?