Skip to content

Add async_antiflood helper for AsyncTeleBot#2589

Open
Dramex wants to merge 2 commits intoeternnoir:masterfrom
Dramex:feat/async-antiflood
Open

Add async_antiflood helper for AsyncTeleBot#2589
Dramex wants to merge 2 commits intoeternnoir:masterfrom
Dramex:feat/async-antiflood

Conversation

@Dramex
Copy link
Copy Markdown
Contributor

@Dramex Dramex commented Apr 20, 2026

telebot.util.antiflood() wraps a sync call in a retry loop that honours Telegram's retry_after parameter on 429 responses. AsyncTeleBot users have no equivalent, so every async bot re-implements the same loop around asyncio.sleep and ApiTelegramException.

This adds async_antiflood() next to the sync one in telebot.util. Same signature, same retry budget semantics, but awaits function(*args, **kwargs) and sleeps via asyncio.sleep. Drop-in for an async bot:

from telebot.util import async_antiflood

for chat_id in chat_ids:
    await async_antiflood(bot.send_message, chat_id, text)

Behaviour

Mirrors the sync version exactly:

  • Non-429 ApiTelegramException propagates immediately, no retry.
  • 429 responses sleep for the server-supplied retry_after seconds, then retry, up to number_retries - 1 times.
  • The final attempt runs without the 429 guard, so an exhausted budget surfaces the last exception to the caller.

Tests

Four new tests in tests/test_async_telebot.py:

  • returns on first success without sleeping
  • single 429 retries exactly once after sleeping retry_after seconds
  • non-429 error propagates without retrying
  • after number_retries attempts the last 429 surfaces

All five tests in the file pass (the earlier strong-ref test from #2588 plus the four new ones).

Notes

  • async_antiflood imports ApiTelegramException and asyncio lazily at call time, same style the sync helper uses, so telebot.util keeps its import graph clean.
  • Exported from telebot.util.__all__.

The sync antiflood() helper in telebot.util wraps a call in a retry
loop that honours Telegram's retry_after parameter on 429 responses.
There is no equivalent for AsyncTeleBot users, so every async bot
ends up re-implementing the same loop around asyncio.sleep and
ApiTelegramException.

This adds async_antiflood(), mirroring the sync signature and
behaviour but awaiting function() and using asyncio.sleep().

Also adds four tests covering:
- the happy path returns on the first call without sleeping
- a single 429 triggers exactly one retry after sleeping retry_after
  seconds
- non-429 ApiTelegramException propagates immediately with no retry
- after number_retries attempts the final 429 surfaces to the caller
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an async equivalent of telebot.util.antiflood() for AsyncTeleBot, enabling retry-on-429 behavior that honors Telegram’s retry_after parameter without requiring each async bot to re-implement the loop.

Changes:

  • Introduce telebot.util.async_antiflood() mirroring antiflood() semantics for async callables.
  • Export async_antiflood via telebot.util.__all__.
  • Add unit tests covering success, 429 retry/sleep behavior, non-429 propagation, and retry budget exhaustion.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
telebot/util.py Adds async_antiflood() and exports it in __all__.
tests/test_async_telebot.py Adds test cases validating async_antiflood() behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/test_async_telebot.py Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@Badiboy
Copy link
Copy Markdown
Collaborator

Badiboy commented Apr 23, 2026

LGTM. @coder2020official ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants