Skip to content

Commit 22c9b9d

Browse files
committed
Add API for proxying discord webhook calls
1 parent 9cae2be commit 22c9b9d

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ BOT_ID=id-of-your-bot
3535
CLIENT_SECRET=your-client-secret
3636
DISCORD_REDIRECT_URL=your-client-oauth2-redirect-url-with-identyfy-guilds.join
3737
DISCORD_SERVER_INVITE=your-discord-server-invite
38+
DISCORD_WEBHOOK_URL=discord-webhook-used-in-fullstack-open-course
39+
DISCORD_WEBHOOK_TOKEN=token-for-the-discord-webhook
3840
PORT=your-custom-backend-port
3941
SESSION_SECRET=server-session-secret
4042
BACKEND_SERVER_URL=backend-server-url-without-port

src/server/app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const discordAuthRoute = require("./routes/discordAuth");
66
const discordJoinRoute = require("./routes/join");
77
const facultyAuthRoute = require("./routes/authenticateFaculty");
88
const metricsRoute = require("./routes/metrics");
9+
const webhookProxyRoute = require("./routes/webhookProxy");
910
const defaultRouteHandler = require("./routes/defaultRouteHandler");
1011
const defaultRouteErrorHandler = require("./routes/defaultRouteErrorHandler");
1112
const flash = require("connect-flash");
@@ -38,6 +39,7 @@ module.exports = (sequelize) => {
3839
app.use(flash());
3940
app.use("/discordAuth", discordAuthRoute);
4041
app.use("/join", discordJoinRoute);
42+
app.use("/webhooks", webhookProxyRoute);
4143
app.use("/authenticate_faculty", facultyAuthRoute);
4244
app.use("/metrics", metricsRoute);
4345

src/server/routes/webhookProxy.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require("dotenv").config()
2+
const axios = require("axios")
3+
const router = require("express").Router()
4+
5+
router.get("/:webhookId", async (req, res) => {
6+
// only allow POST
7+
if (req.method === "POST") {
8+
const webhookUrl = `${process.env.DISCORD_WEBHOOK_URL}/${req.params.webhookId}/${process.env.DISCORD_WEBHOOK_TOKEN}`
9+
const webhookData = req.body
10+
try {
11+
await axios.post(webhookUrl, webhookData)
12+
res.status(200).send("Webhook sent successfully")
13+
} catch (error) {
14+
console.error(error)
15+
res.status(500).send("Internal server error")
16+
}
17+
} else {
18+
res.status(405).send("Method not allowed")
19+
}
20+
})
21+
22+
module.exports = router

0 commit comments

Comments
 (0)