Skip to content
Jingzhe

Long polling

Bot::new(token)?.command(start).run_polling().await?;

run_polling() listens for Ctrl-C. For a custom shutdown signal:

bot.run_polling_until(shutdown_future).await?;

Repository example:

Terminal
cargo run -p jingzhe --example polling

PollingConfig supports:

  • Wait time
  • Batch size
  • Non-negative recovery offset
  • allowed_updates

The library limits wait time to 0–50 seconds and batch size to 1–100. The HTTP timeout for long polling is at least the wait time plus 10 seconds so a normal wait is not cut off by the default request timeout.

Updates are processed in response order with a concurrency of 1; the runtime does not create an unbounded task set.

The runner stops when a handler fails, and the failed event’s offset does not advance. Successful events advance the in-memory offset first and are confirmed to Telegram on the next getUpdates request. A shutdown signal stops fetching and finishes the current batch. Updates that were not yet confirmed may be delivered again after restart.

  • Only idempotent long-polling rate-limit errors wait according to retry_after, and that wait can be cancelled by shutdown.
  • Other failures propagate to the caller; they are not retried automatically.
  • Send and edit requests are never retried implicitly.

The library checks webhook state before polling. If a webhook is configured, polling refuses to start and does not delete the webhook automatically.

Call DeleteWebhook::new() explicitly before switching. Pending updates are preserved by default; only drop_pending_updates(true) discards them.