From beeb60aab8171b9a426f9d239f9b3a3da41ca3fa Mon Sep 17 00:00:00 2001 From: _run Date: Sun, 15 Aug 2021 11:40:13 +0400 Subject: [PATCH] skip_updates --- examples/skip_updates_example.py | 15 +++++++++++++++ telebot/__init__.py | 10 ++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 examples/skip_updates_example.py diff --git a/examples/skip_updates_example.py b/examples/skip_updates_example.py new file mode 100644 index 0000000..8474e16 --- /dev/null +++ b/examples/skip_updates_example.py @@ -0,0 +1,15 @@ +import telebot + +bot = telebot.TeleBot("TOKEN") + +@bot.message_handler(commands=['start', 'help']) +def send_welcome(message): + bot.reply_to(message, "Howdy, how are you doing?") + +@bot.message_handler(func=lambda message: True) +def echo_all(message): + bot.reply_to(message, message.text) + +bot.polling(skip_updates=True) # Will skip old messages when skip_updates is set + +# Also, you can use skip_updates in infinity_polling() \ No newline at end of file diff --git a/telebot/__init__.py b/telebot/__init__.py index 9fd86a2..26354b0 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -556,7 +556,7 @@ class TeleBot: 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_updates=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. @@ -565,6 +565,7 @@ class TeleBot: :param long_polling_timeout: Timeout in seconds for long polling (see API docs) :param logger_level: Custom logging level for infinity_polling logging. Use logger levels from logging as a value. None/NOTSET = no error logging + :param skip_updates: Skip previous updates :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. See util.update_types for a complete list of available update types. @@ -574,6 +575,8 @@ class TeleBot: 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_updates: + apihelper.get_updates(self.token, -1) while not self.__stop_polling.is_set(): try: self.polling(none_stop=True, timeout=timeout, long_polling_timeout=long_polling_timeout, @@ -590,7 +593,7 @@ class TeleBot: 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_updates=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. @@ -601,6 +604,7 @@ class TeleBot: Always get updates. :param interval: Delay between two update retrivals :param none_stop: Do not stop polling when an ApiException occurs. + :param skip_updates: Skip previous updates :param timeout: Request connection timeout :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. @@ -613,6 +617,8 @@ class TeleBot: so unwanted updates may be received for a short period of time. :return: """ + if skip_updates: + apihelper.get_updates(self.token, -1) if self.threaded: self.__threaded_polling(none_stop, interval, timeout, long_polling_timeout, allowed_updates) else: