mirror of
https://github.com/whekin/household-bot.git
synced 2026-03-31 11:54:03 +00:00
feat(WHE-21): scaffold grammy webhook bot server
This commit is contained in:
36
apps/bot/src/config.ts
Normal file
36
apps/bot/src/config.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
export interface BotRuntimeConfig {
|
||||
port: number
|
||||
telegramBotToken: string
|
||||
telegramWebhookSecret: string
|
||||
telegramWebhookPath: string
|
||||
}
|
||||
|
||||
function parsePort(raw: string | undefined): number {
|
||||
if (raw === undefined) {
|
||||
return 3000
|
||||
}
|
||||
|
||||
const parsed = Number(raw)
|
||||
if (!Number.isInteger(parsed) || parsed <= 0 || parsed > 65535) {
|
||||
throw new Error(`Invalid PORT value: ${raw}`)
|
||||
}
|
||||
|
||||
return parsed
|
||||
}
|
||||
|
||||
function requireValue(value: string | undefined, key: string): string {
|
||||
if (!value || value.trim().length === 0) {
|
||||
throw new Error(`${key} environment variable is required`)
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
export function getBotRuntimeConfig(env: NodeJS.ProcessEnv = process.env): BotRuntimeConfig {
|
||||
return {
|
||||
port: parsePort(env.PORT),
|
||||
telegramBotToken: requireValue(env.TELEGRAM_BOT_TOKEN, 'TELEGRAM_BOT_TOKEN'),
|
||||
telegramWebhookSecret: requireValue(env.TELEGRAM_WEBHOOK_SECRET, 'TELEGRAM_WEBHOOK_SECRET'),
|
||||
telegramWebhookPath: env.TELEGRAM_WEBHOOK_PATH ?? '/webhook/telegram'
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user