mirror of
https://github.com/eternnoir/pyTelegramBotAPI.git
synced 2023-08-10 21:12:57 +03:00
Merge pull request #1264 from coder2020official/master
Added skip_pending parameter to polling and infinity_polling in addition to skip_pending in Telebot.Init. Allows skip pending updates for already created Bot instances.
This commit is contained in:
commit
958ca34e9c
13
examples/skip_updates_example.py
Normal file
13
examples/skip_updates_example.py
Normal file
@ -0,0 +1,13 @@
|
||||
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_pending=True)# Skip pending skips old updates
|
@ -364,17 +364,9 @@ class TeleBot:
|
||||
def __skip_updates(self):
|
||||
"""
|
||||
Get and discard all pending updates before first poll of the bot
|
||||
:return: total updates skipped
|
||||
:return:
|
||||
"""
|
||||
total = 0
|
||||
updates = self.get_updates(offset=self.last_update_id, long_polling_timeout=1)
|
||||
while updates:
|
||||
total += len(updates)
|
||||
for update in updates:
|
||||
if update.update_id > self.last_update_id:
|
||||
self.last_update_id = update.update_id
|
||||
updates = self.get_updates(offset=self.last_update_id + 1, long_polling_timeout=1)
|
||||
return total
|
||||
self.get_updates(offset=-1)
|
||||
|
||||
def __retrieve_updates(self, timeout=20, long_polling_timeout=20, allowed_updates=None):
|
||||
"""
|
||||
@ -383,7 +375,8 @@ class TeleBot:
|
||||
:raises ApiException when a call has failed.
|
||||
"""
|
||||
if self.skip_pending:
|
||||
logger.debug('Skipped {0} pending messages'.format(self.__skip_updates()))
|
||||
self.__skip_updates()
|
||||
logger.debug('Skipped all pending messages')
|
||||
self.skip_pending = False
|
||||
updates = self.get_updates(offset=(self.last_update_id + 1),
|
||||
allowed_updates=allowed_updates,
|
||||
@ -556,13 +549,14 @@ 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_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.
|
||||
@ -574,6 +568,9 @@ 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_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,
|
||||
@ -590,7 +587,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_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.
|
||||
@ -602,6 +599,7 @@ class TeleBot:
|
||||
: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.
|
||||
@ -613,6 +611,9 @@ class TeleBot:
|
||||
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:
|
||||
|
Loading…
x
Reference in New Issue
Block a user