Last active
July 6, 2018 17:42
-
-
Save brussell98/a865ecc2284e958226596accc852ee28 to your computer and use it in GitHub Desktop.
Make slack webhooks discord compatible
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
}); |
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
Here's a SystemD service unit file for this.