From 92095f69e7410bb010a305d8f826e1220d43f857 Mon Sep 17 00:00:00 2001 From: Zaid _ Date: Fri, 28 Jun 2024 21:23:27 +0300 Subject: [PATCH] add retry_after parameter --- tgram/tgbot.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tgram/tgbot.py b/tgram/tgbot.py index 7419b52..acce287 100644 --- a/tgram/tgbot.py +++ b/tgram/tgbot.py @@ -85,6 +85,7 @@ def __init__( parse_mode: Literal["Markdown", "MarkdownV2", "HTML"] = None, protect_content: bool = None, workers: int = None, + retry_after: int = 0, ) -> None: self.bot_token = bot_token self.api_url = api_url @@ -93,6 +94,7 @@ def __init__( self.parse_mode = parse_mode self.protect_content = protect_content self.workers = workers or min(32, (os.cpu_count() or 0) + 4) + self.retry_after = retry_after self.executor = ThreadPoolExecutor(self.workers, thread_name_prefix="Handlers") self.loop = asyncio.get_event_loop() @@ -172,6 +174,16 @@ async def _send_request(self, method: str, **kwargs) -> Any: response_json = await response.json() if not response_json["ok"]: + if response_json["error_code"] is 429 and self.retry_after: + s = response_json["parameters"]["retry_after"] + retry_after = s if s < self.retry_after else self.retry_after + logger.warning( + "You got floodwait for %s seconds, I will retry after %s", + s, + retry_after, + ) + await asyncio.sleep(retry_after) + return await self._send_request(method, **kwargs) del response_json["ok"] raise APIException(json.dumps(response_json))