add discord webhook

This commit is contained in:
HPL-JesusCastro
2026-05-23 02:45:21 +08:00
parent 71d06c12ad
commit 92a1fd1573
2 changed files with 15 additions and 0 deletions

View File

@@ -9,3 +9,6 @@ READER_PASSWORD=your-reader-password-here
# A long random string used to sign JWT tokens. # A long random string used to sign JWT tokens.
# Generate one with: node -e "console.log(require('crypto').randomBytes(32).toString('hex'))" # Generate one with: node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"
JWT_SECRET=change-this-to-a-random-secret JWT_SECRET=change-this-to-a-random-secret
# Discord webhook URL for first-read notifications (optional)
DISCORD_WEBHOOK_URL=

View File

@@ -128,6 +128,18 @@ export async function letterRoutes(app: FastifyInstance) {
if (role === "reader" && !rows[0].readAt) { if (role === "reader" && !rows[0].readAt) {
const readAt = new Date().toISOString(); const readAt = new Date().toISOString();
await db.update(letters).set({ readAt }).where(eq(letters.id, id)); await db.update(letters).set({ readAt }).where(eq(letters.id, id));
const webhookUrl = process.env.DISCORD_WEBHOOK_URL;
if (webhookUrl) {
const date = new Date(rows[0].letterDate ?? rows[0].createdAt.slice(0, 10))
.toLocaleDateString("en-US", { weekday: "long", year: "numeric", month: "long", day: "numeric" });
fetch(webhookUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ content: `💌 Your letter from **${date}** was just opened!` }),
}).catch(() => {});
}
return { ...rows[0], readAt }; return { ...rows[0], readAt };
} }