mirror of
https://github.com/whekin/household-bot.git
synced 2026-03-31 15:44:02 +00:00
feat(ops): add first deployment runbook tooling
This commit is contained in:
86
scripts/ops/telegram-webhook.ts
Normal file
86
scripts/ops/telegram-webhook.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
type WebhookCommand = 'info' | 'set' | 'delete'
|
||||
|
||||
function requireEnv(name: string): string {
|
||||
const value = process.env[name]?.trim()
|
||||
if (!value) {
|
||||
throw new Error(`${name} is required`)
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
async function telegramRequest(
|
||||
botToken: string,
|
||||
method: string,
|
||||
body?: URLSearchParams
|
||||
): Promise<any> {
|
||||
const response = await fetch(`https://api.telegram.org/bot${botToken}/${method}`, {
|
||||
method: body ? 'POST' : 'GET',
|
||||
body
|
||||
})
|
||||
|
||||
const payload = (await response.json()) as {
|
||||
ok?: boolean
|
||||
result?: unknown
|
||||
}
|
||||
if (!response.ok || payload.ok !== true) {
|
||||
throw new Error(`Telegram ${method} failed: ${JSON.stringify(payload)}`)
|
||||
}
|
||||
|
||||
return payload.result
|
||||
}
|
||||
|
||||
async function run(): Promise<void> {
|
||||
const command = (process.argv[2] ?? 'info') as WebhookCommand
|
||||
const botToken = requireEnv('TELEGRAM_BOT_TOKEN')
|
||||
|
||||
switch (command) {
|
||||
case 'info': {
|
||||
const result = await telegramRequest(botToken, 'getWebhookInfo')
|
||||
console.log(JSON.stringify(result, null, 2))
|
||||
return
|
||||
}
|
||||
case 'set': {
|
||||
const params = new URLSearchParams({
|
||||
url: requireEnv('TELEGRAM_WEBHOOK_URL')
|
||||
})
|
||||
|
||||
const secretToken = process.env.TELEGRAM_WEBHOOK_SECRET?.trim()
|
||||
if (secretToken) {
|
||||
params.set('secret_token', secretToken)
|
||||
}
|
||||
|
||||
const maxConnections = process.env.TELEGRAM_MAX_CONNECTIONS?.trim()
|
||||
if (maxConnections) {
|
||||
params.set('max_connections', maxConnections)
|
||||
}
|
||||
|
||||
const dropPendingUpdates = process.env.TELEGRAM_DROP_PENDING_UPDATES?.trim()
|
||||
if (dropPendingUpdates) {
|
||||
params.set('drop_pending_updates', dropPendingUpdates)
|
||||
}
|
||||
|
||||
const result = await telegramRequest(botToken, 'setWebhook', params)
|
||||
console.log(JSON.stringify({ ok: true, result }, null, 2))
|
||||
return
|
||||
}
|
||||
case 'delete': {
|
||||
const params = new URLSearchParams()
|
||||
const dropPendingUpdates = process.env.TELEGRAM_DROP_PENDING_UPDATES?.trim()
|
||||
if (dropPendingUpdates) {
|
||||
params.set('drop_pending_updates', dropPendingUpdates)
|
||||
}
|
||||
|
||||
const result = await telegramRequest(botToken, 'deleteWebhook', params)
|
||||
console.log(JSON.stringify({ ok: true, result }, null, 2))
|
||||
return
|
||||
}
|
||||
default:
|
||||
throw new Error(`Unsupported command: ${command}`)
|
||||
}
|
||||
}
|
||||
|
||||
run().catch((error) => {
|
||||
console.error(error instanceof Error ? error.message : String(error))
|
||||
process.exitCode = 1
|
||||
})
|
||||
Reference in New Issue
Block a user