Skip to content

Commit

Permalink
Add API for proxying discord webhook calls
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksTeresh committed Jul 22, 2024
1 parent 9cae2be commit 22c9b9d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ BOT_ID=id-of-your-bot
CLIENT_SECRET=your-client-secret
DISCORD_REDIRECT_URL=your-client-oauth2-redirect-url-with-identyfy-guilds.join
DISCORD_SERVER_INVITE=your-discord-server-invite
DISCORD_WEBHOOK_URL=discord-webhook-used-in-fullstack-open-course
DISCORD_WEBHOOK_TOKEN=token-for-the-discord-webhook
PORT=your-custom-backend-port
SESSION_SECRET=server-session-secret
BACKEND_SERVER_URL=backend-server-url-without-port
Expand Down
2 changes: 2 additions & 0 deletions src/server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const discordAuthRoute = require("./routes/discordAuth");
const discordJoinRoute = require("./routes/join");
const facultyAuthRoute = require("./routes/authenticateFaculty");
const metricsRoute = require("./routes/metrics");
const webhookProxyRoute = require("./routes/webhookProxy");
const defaultRouteHandler = require("./routes/defaultRouteHandler");
const defaultRouteErrorHandler = require("./routes/defaultRouteErrorHandler");
const flash = require("connect-flash");
Expand Down Expand Up @@ -38,6 +39,7 @@ module.exports = (sequelize) => {
app.use(flash());
app.use("/discordAuth", discordAuthRoute);
app.use("/join", discordJoinRoute);
app.use("/webhooks", webhookProxyRoute);
app.use("/authenticate_faculty", facultyAuthRoute);
app.use("/metrics", metricsRoute);

Expand Down
22 changes: 22 additions & 0 deletions src/server/routes/webhookProxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require("dotenv").config()
const axios = require("axios")
const router = require("express").Router()

router.get("/:webhookId", async (req, res) => {
// only allow POST
if (req.method === "POST") {
const webhookUrl = `${process.env.DISCORD_WEBHOOK_URL}/${req.params.webhookId}/${process.env.DISCORD_WEBHOOK_TOKEN}`
const webhookData = req.body
try {
await axios.post(webhookUrl, webhookData)
res.status(200).send("Webhook sent successfully")
} catch (error) {
console.error(error)
res.status(500).send("Internal server error")
}
} else {
res.status(405).send("Method not allowed")
}
})

module.exports = router

0 comments on commit 22c9b9d

Please sign in to comment.