1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00

redesigned the antiflood method for guaranteed message delivery

This commit is contained in:
Alexey Isaev 2023-03-18 14:43:23 +03:00
parent 5d9a76b0dd
commit 14294d1aa3

View File

@ -588,7 +588,7 @@ def webhook_google_functions(bot, request):
return 'Bot ON' return 'Bot ON'
def antiflood(function: Callable, *args, **kwargs): def antiflood(function: Callable, *args, number_retries=5, **kwargs):
""" """
Use this function inside loops in order to avoid getting TooManyRequests error. Use this function inside loops in order to avoid getting TooManyRequests error.
Example: Example:
@ -602,6 +602,9 @@ def antiflood(function: Callable, *args, **kwargs):
:param function: The function to call :param function: The function to call
:type function: :obj:`Callable` :type function: :obj:`Callable`
:param number_retries: Number of retries to send
:type function: :obj:int
:param args: The arguments to pass to the function :param args: The arguments to pass to the function
:type args: :obj:`tuple` :type args: :obj:`tuple`
@ -613,14 +616,16 @@ def antiflood(function: Callable, *args, **kwargs):
from telebot.apihelper import ApiTelegramException from telebot.apihelper import ApiTelegramException
from time import sleep from time import sleep
for _ in range(number_retries - 1):
try: try:
return function(*args, **kwargs) return function(*args, **kwargs)
except ApiTelegramException as ex: except ApiTelegramException as ex:
if ex.error_code == 429: if ex.error_code == 429:
sleep(ex.result_json['parameters']['retry_after']) sleep(ex.result_json['parameters']['retry_after'])
return function(*args, **kwargs)
else: else:
raise raise
else:
return function(*args, **kwargs)
def parse_web_app_data(token: str, raw_init_data: str): def parse_web_app_data(token: str, raw_init_data: str):