Skip to content

Commit

Permalink
Update __init__.py
Browse files Browse the repository at this point in the history
  • Loading branch information
coder2020official committed Aug 16, 2021
1 parent 24ef644 commit f553960
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions telebot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,13 +549,14 @@ def __notify_update(self, new_messages):
for listener in self.update_listener:
self._exec_task(listener, new_messages)

def infinity_polling(self, timeout=20, long_polling_timeout=20, logger_level=logging.ERROR,
def infinity_polling(self, timeout=20, skip_pending=False, long_polling_timeout=20, logger_level=logging.ERROR,
allowed_updates=None, *args, **kwargs):
"""
Wrap polling with infinite loop and exception handling to avoid bot stops polling.
:param timeout: Request connection timeout
:param long_polling_timeout: Timeout in seconds for long polling (see API docs)
:param skip_pending: skip old updates
:param logger_level: Custom logging level for infinity_polling logging.
Use logger levels from logging as a value. None/NOTSET = no error logging
:param allowed_updates: A list of the update types you want your bot to receive.
Expand All @@ -567,6 +568,9 @@ def infinity_polling(self, timeout=20, long_polling_timeout=20, logger_level=log
Please note that this parameter doesn't affect updates created before the call to the get_updates,
so unwanted updates may be received for a short period of time.
"""
if skip_pending:
self.__skip_updates()

while not self.__stop_polling.is_set():
try:
self.polling(none_stop=True, timeout=timeout, long_polling_timeout=long_polling_timeout,
Expand All @@ -583,7 +587,7 @@ def infinity_polling(self, timeout=20, long_polling_timeout=20, logger_level=log
if logger_level and logger_level >= logging.INFO:
logger.error("Break infinity polling")

def polling(self, none_stop: bool=False, interval: int=0, timeout: int=20,
def polling(self, none_stop: bool=False, skip_pending=False, interval: int=0, timeout: int=20,
long_polling_timeout: int=20, allowed_updates: Optional[List[str]]=None):
"""
This function creates a new Thread that calls an internal __retrieve_updates function.
Expand All @@ -595,6 +599,7 @@ def polling(self, none_stop: bool=False, interval: int=0, timeout: int=20,
:param interval: Delay between two update retrivals
:param none_stop: Do not stop polling when an ApiException occurs.
:param timeout: Request connection timeout
:param skip_pending: skip old updates
:param long_polling_timeout: Timeout in seconds for long polling (see API docs)
:param allowed_updates: A list of the update types you want your bot to receive.
For example, specify [“message”, “edited_channel_post”, “callback_query”] to only receive updates of these types.
Expand All @@ -606,6 +611,9 @@ def polling(self, none_stop: bool=False, interval: int=0, timeout: int=20,
so unwanted updates may be received for a short period of time.
:return:
"""
if skip_pending:
self.__skip_updates()

if self.threaded:
self.__threaded_polling(none_stop, interval, timeout, long_polling_timeout, allowed_updates)
else:
Expand Down

0 comments on commit f553960

Please sign in to comment.