Skip to content

Commit

Permalink
Merge pull request #1947 from alex75311/edit_antiflood_method
Browse files Browse the repository at this point in the history
redesigned the antiflood method for guaranteed message delivery
  • Loading branch information
Badiboy authored May 8, 2023
2 parents ea3c159 + 14294d1 commit 48377ac
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions telebot/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ def webhook_google_functions(bot, request):
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.
Example:
Expand All @@ -604,6 +604,9 @@ def antiflood(function: Callable, *args, **kwargs):
:param function: The function to call
: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
:type args: :obj:`tuple`
Expand All @@ -615,14 +618,16 @@ def antiflood(function: Callable, *args, **kwargs):
from telebot.apihelper import ApiTelegramException
from time import sleep

try:
return function(*args, **kwargs)
except ApiTelegramException as ex:
if ex.error_code == 429:
sleep(ex.result_json['parameters']['retry_after'])
for _ in range(number_retries - 1):
try:
return function(*args, **kwargs)
else:
raise
except ApiTelegramException as ex:
if ex.error_code == 429:
sleep(ex.result_json['parameters']['retry_after'])
else:
raise
else:
return function(*args, **kwargs)


def parse_web_app_data(token: str, raw_init_data: str):
Expand Down

0 comments on commit 48377ac

Please sign in to comment.