Webhook API

class peyk.webhook.IPFilter(networks: Sequence[str], *, trust_forwarded_for: bool = False)[source]

Bases: object

Allow webhook requests only when their source IP is in configured networks.

trust_forwarded_for is deliberately opt-in. When disabled, only the transport peer address is considered, so an untrusted client cannot choose its own address through X-Forwarded-For.

allows(peer_ip: str, headers: Mapping[str, str]) → bool[source]

Return whether the request source belongs to an allowed network.

class peyk.webhook.WebhookOptions(allowed_updates: list[str] | None = None, drop_pending_updates: bool = False, max_connections: int | None = None)[source]

Bases: object

Platform-neutral webhook registration options.

allowed_updates: list[str] | None = None
drop_pending_updates: bool = False
max_connections: int | None = None
class peyk.webhook.WebhookProcessor(dispatcher: object, *, secret: str | None = None, max_body_size: int = MAX_DEFAULT_BODY_SIZE, handle_in_background: bool = True, endpoint_type: str | None = None, max_concurrent_updates: int | Semaphore | None = None, update_timeout: float | None = None, max_queue_wait: float | None = None, max_pending: int | None = None)[source]

Bases: object

Parse and dispatch webhook requests without depending on an HTTP server.

__init__(dispatcher: object, *, secret: str | None = None, max_body_size: int = MAX_DEFAULT_BODY_SIZE, handle_in_background: bool = True, endpoint_type: str | None = None, max_concurrent_updates: int | Semaphore | None = None, update_timeout: float | None = None, max_queue_wait: float | None = None, max_pending: int | None = None) → None[source]

max_queue_wait (seconds, default off) drops an update that waited longer than that for a free slot: under overload a message handled minutes late is worth less than keeping up with the new ones. max_pending (default off) answers 503 once that many updates are already waiting or running, so the platform retries later instead of the bot piling up tasks (and memory) without limit.

max_concurrent_updates (a number, or a semaphore shared by several processors) bounds the updates handled at the same time and update_timeout (seconds) cancels one update that hangs, so a handler stuck on a single message can neither freeze the others nor hold a slot forever. Both are off (unbounded) by default.

async handle(bot: Bot[object], body: bytes, headers: Mapping[str, str]) → WebhookResult[source]

Verify, parse and dispatch one webhook request.

Malformed JSON and oversized bodies are rejected. Authentication failures return the same 404 used by route failures so callers cannot distinguish a secret oracle from an absent endpoint.

async shutdown() → None[source]

Wait for all background update tasks before server shutdown.

class peyk.webhook.WebhookRegistrar(*args, **kwargs)[source]

Bases: Protocol

Register and remove a webhook for one platform.

async install(bot: Bot[object], url: str, secret: str, options: WebhookOptions) → None[source]

Provides the install operation for the peyk integration.

Parameters:
  • bot – Value used by this operation.

  • url – Value used by this operation.

  • secret – Value used by this operation.

  • options – Value used by this operation.

async uninstall(bot: Bot[object]) → None[source]

Provides the uninstall operation for the peyk integration.

Parameters:

bot – Value used by this operation.

class peyk.webhook.WebhookResult(status: int, body: bytes | None = None)[source]

Bases: object

HTTP result returned by a webhook processor.

body: bytes | None = None
status: int
peyk.webhook.constant_time_compare(a: str | None, b: str | None) → bool[source]

Compare two non-empty strings without ordinary early-exit timing.

peyk.webhook.registrar_for(bot: Bot[object]) → WebhookRegistrar[source]

Return the audited registrar for bot.platform.

peyk.webhook.verify_telegram_secret_header(headers: Mapping[str, str], secret_token: str) → bool[source]

Validate Telegram’s documented webhook secret header.