feat: add authentication for webhook endpoint using password verification

This commit is contained in:
Kyle Fang
2025-08-03 19:03:57 +08:00
parent 1586ad9000
commit 524cc2a468
2 changed files with 13 additions and 1 deletions

View File

@@ -19,6 +19,8 @@ declare global {
const WEBHOOK_PREFIX: string;
const WEBHOOK_PASSWORD: string;
const TG_GROUPS: KVNamespace;
}
@@ -32,6 +34,16 @@ router.get("/", (context) => {
});
router.post("/bot", async (context) => {
// Check for authentication password in query parameter
const password = context.req.url.searchParams.get('password');
const expectedPassword = WEBHOOK_PASSWORD;
if (!password || password !== expectedPassword) {
context.res.status = 401;
context.res.body = { ok: false, error: "Unauthorized" };
return;
}
const result: Update = await context.req.body.json();
await processUpdate(result);
console.log(JSON.stringify(result, null, 2));

View File

@@ -32,7 +32,7 @@ export function formatRichMessage(message: RichMessage): string {
`
: ""
}<b>${escapeHTML(message.event)}</b>${
}<b>${escapeHTML(message.event ?? "Unknown event")}</b>${
(message.text ?? message.message)
? `