mirror of
https://github.com/pupperpowell/bibdle.git
synced 2026-04-05 17:33:31 -04:00
feat: add /api/send-daily-verse endpoint for daily Discord verse posting
Protected by CRON_SECRET bearer token. Fetches today's verse in America/New_York timezone and POSTs it to DISCORD_DAILY_WEBHOOK. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
36
src/routes/api/send-daily-verse/+server.ts
Normal file
36
src/routes/api/send-daily-verse/+server.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import type { RequestHandler } from './$types';
|
||||
import { getVerseForDate } from '$lib/server/daily-verse';
|
||||
import { CRON_SECRET, DISCORD_DAILY_WEBHOOK } from '$env/dynamic/private';
|
||||
|
||||
export const POST: RequestHandler = async ({ request }) => {
|
||||
const authHeader = request.headers.get('Authorization');
|
||||
if (!authHeader || authHeader !== `Bearer ${CRON_SECRET}`) {
|
||||
return json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
|
||||
const dateStr = new Date().toLocaleDateString('en-CA', { timeZone: 'America/New_York' });
|
||||
|
||||
const verse = await getVerseForDate(dateStr);
|
||||
|
||||
const fullDate = new Date(dateStr + 'T00:00:00Z').toLocaleDateString('en-US', {
|
||||
year: 'numeric',
|
||||
month: 'long',
|
||||
day: 'numeric',
|
||||
timeZone: 'UTC',
|
||||
});
|
||||
|
||||
const message = `Today's BIBDLE Verse (${fullDate}): "${verse.verseText}" — ${verse.reference}`;
|
||||
|
||||
const discordResponse = await fetch(DISCORD_DAILY_WEBHOOK, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ content: message }),
|
||||
});
|
||||
|
||||
if (!discordResponse.ok) {
|
||||
return json({ error: 'Failed to post to Discord' }, { status: 500 });
|
||||
}
|
||||
|
||||
return json({ ok: true });
|
||||
};
|
||||
Reference in New Issue
Block a user