import { telegramRequest } from './request.ts'
import { inputRecord, secretNameFrom } from './validation.ts'
type WebhookInfo = {
url?: string
has_custom_certificate?: boolean
pending_update_count?: number
last_error_date?: number
last_error_message?: string
max_connections?: number
allowed_updates?: string[]
ip_address?: string
[key: string]: unknown
}
/**
* Read whether a webhook is registered. getUpdates fails with 409 while a
* webhook URL is set.
*/
export default async function getWebhookInfo(
params: Record<string, unknown> = {},
) {
const input = inputRecord(params)
const secretName = secretNameFrom(input)
const info = await telegramRequest<WebhookInfo>({
method: 'getWebhookInfo',
secretName,
})
const url = typeof info.url === 'string' ? info.url : ''
return {
ok: true,
webhookSet: url.length > 0,
hasUrl: url.length > 0,
pendingUpdateCount:
typeof info.pending_update_count === 'number'
? info.pending_update_count
: 0,
lastErrorMessage:
typeof info.last_error_message === 'string'
? info.last_error_message
: null,
allowedUpdates: Array.isArray(info.allowed_updates)
? info.allowed_updates
: [],
pollingHint: url.length > 0
? 'A webhook is set, so getUpdates will 409. Preview deleteWebhook with dryRun: true, then confirm: true to switch to polling.'
: 'No webhook is set; getUpdates polling is available.',
}
}