feat(WHE-21): scaffold grammy webhook bot server

This commit is contained in:
2026-03-05 04:17:04 +04:00
parent eef54ac183
commit f8c3e4ccf5
9 changed files with 286 additions and 3 deletions

52
apps/bot/src/server.ts Normal file
View File

@@ -0,0 +1,52 @@
export interface BotWebhookServerOptions {
webhookPath: string
webhookSecret: string
webhookHandler: (request: Request) => Promise<Response> | Response
}
function json(body: object, status = 200): Response {
return new Response(JSON.stringify(body), {
status,
headers: {
'content-type': 'application/json; charset=utf-8'
}
})
}
function isAuthorized(request: Request, expectedSecret: string): boolean {
const secretHeader = request.headers.get('x-telegram-bot-api-secret-token')
return secretHeader === expectedSecret
}
export function createBotWebhookServer(options: BotWebhookServerOptions): {
fetch: (request: Request) => Promise<Response>
} {
const normalizedWebhookPath = options.webhookPath.startsWith('/')
? options.webhookPath
: `/${options.webhookPath}`
return {
fetch: async (request: Request) => {
const url = new URL(request.url)
if (url.pathname === '/healthz') {
return json({ ok: true })
}
if (url.pathname !== normalizedWebhookPath) {
return new Response('Not Found', { status: 404 })
}
if (request.method !== 'POST') {
return new Response('Method Not Allowed', { status: 405 })
}
if (!isAuthorized(request, options.webhookSecret)) {
return new Response('Unauthorized', { status: 401 })
}
return await options.webhookHandler(request)
}
}
}