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

skip_updates

This commit is contained in:
_run 2021-08-15 11:40:13 +04:00
parent 5b942a5b31
commit beeb60aab8
2 changed files with 23 additions and 2 deletions

View File

@ -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()

View File

@ -556,7 +556,7 @@ class TeleBot:
for listener in self.update_listener: for listener in self.update_listener:
self._exec_task(listener, new_messages) 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): allowed_updates=None, *args, **kwargs):
""" """
Wrap polling with infinite loop and exception handling to avoid bot stops polling. 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 long_polling_timeout: Timeout in seconds for long polling (see API docs)
:param logger_level: Custom logging level for infinity_polling logging. :param logger_level: Custom logging level for infinity_polling logging.
Use logger levels from logging as a value. None/NOTSET = no error 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. :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. 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. 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, 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. 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(): while not self.__stop_polling.is_set():
try: try:
self.polling(none_stop=True, timeout=timeout, long_polling_timeout=long_polling_timeout, 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: if logger_level and logger_level >= logging.INFO:
logger.error("Break infinity polling") 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): long_polling_timeout: int=20, allowed_updates: Optional[List[str]]=None):
""" """
This function creates a new Thread that calls an internal __retrieve_updates function. This function creates a new Thread that calls an internal __retrieve_updates function.
@ -601,6 +604,7 @@ class TeleBot:
Always get updates. Always get updates.
:param interval: Delay between two update retrivals :param interval: Delay between two update retrivals
:param none_stop: Do not stop polling when an ApiException occurs. :param none_stop: Do not stop polling when an ApiException occurs.
:param skip_updates: Skip previous updates
:param timeout: Request connection timeout :param timeout: Request connection timeout
:param long_polling_timeout: Timeout in seconds for long polling (see API docs) :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. :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. so unwanted updates may be received for a short period of time.
:return: :return:
""" """
if skip_updates:
apihelper.get_updates(self.token, -1)
if self.threaded: if self.threaded:
self.__threaded_polling(none_stop, interval, timeout, long_polling_timeout, allowed_updates) self.__threaded_polling(none_stop, interval, timeout, long_polling_timeout, allowed_updates)
else: else: