From ebf6201b42d59a8a1ccca5825429d1df3bad08e1 Mon Sep 17 00:00:00 2001 From: Flo Date: Wed, 1 Apr 2026 09:13:08 +0300 Subject: [PATCH] fix(telegram): handle RetryAfter delay internally in channel (#2552) --- nanobot/channels/telegram.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/nanobot/channels/telegram.py b/nanobot/channels/telegram.py index 439d1c4d9..8cb85844c 100644 --- a/nanobot/channels/telegram.py +++ b/nanobot/channels/telegram.py @@ -432,7 +432,9 @@ class TelegramChannel(BaseChannel): await self._send_text(chat_id, chunk, reply_params, thread_kwargs) async def _call_with_retry(self, fn, *args, **kwargs): - """Call an async Telegram API function with retry on pool/network timeout.""" + """Call an async Telegram API function with retry on pool/network timeout and RetryAfter.""" + from telegram.error import RetryAfter + for attempt in range(1, _SEND_MAX_RETRIES + 1): try: return await fn(*args, **kwargs) @@ -445,6 +447,15 @@ class TelegramChannel(BaseChannel): attempt, _SEND_MAX_RETRIES, delay, ) await asyncio.sleep(delay) + except RetryAfter as e: + if attempt == _SEND_MAX_RETRIES: + raise + delay = float(e.retry_after) + logger.warning( + "Telegram Flood Control (attempt {}/{}), retrying in {:.1f}s", + attempt, _SEND_MAX_RETRIES, delay, + ) + await asyncio.sleep(delay) async def _send_text( self,