From 3a7a8065b662cf9fff02e73dd7b7796de32153c1 Mon Sep 17 00:00:00 2001 From: Gaung Ramadhan Date: Sun, 5 Nov 2023 10:42:34 +0700 Subject: [PATCH] fix(forward): handle telegram rate limit (#90) --- forwarder/modules/forward.py | 7 ++++++- pyproject.toml | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/forwarder/modules/forward.py b/forwarder/modules/forward.py index 06c304f..955118e 100644 --- a/forwarder/modules/forward.py +++ b/forwarder/modules/forward.py @@ -1,7 +1,8 @@ +import asyncio from typing import Union, Optional from telegram import Update, Message, MessageId -from telegram.error import ChatMigrated +from telegram.error import ChatMigrated, RetryAfter from telegram.ext import MessageHandler, filters, ContextTypes from forwarder import bot, REMOVE_TAG, LOGGER @@ -26,6 +27,10 @@ async def forwarder(update: Update, _: ContextTypes.DEFAULT_TYPE) -> None: for chat in get_destenation(message.chat_id, message.message_thread_id): try: await send_message(message, chat["chat_id"], thread_id=chat["thread_id"]) + except RetryAfter as err: + LOGGER.warning(f"Rate limited, retrying in {err.retry_after} seconds") + await asyncio.sleep(err.retry_after + 0.2) + await send_message(message, chat["chat_id"], thread_id=chat["thread_id"]) except ChatMigrated as err: await send_message(message, err.new_chat_id) LOGGER.warning( diff --git a/pyproject.toml b/pyproject.toml index 35a1b65..c81831b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "telegram-forwarder" -version = "2.2.0" +version = "2.2.1" description = "" authors = ["mrmissx "] license = "GNU General Public License v3.0"