From 062fababf2d3e7b7728355abf891ba5cba01cf6e Mon Sep 17 00:00:00 2001 From: Badiboy Date: Tue, 28 Sep 2021 19:17:09 +0300 Subject: [PATCH] polling should leave our world. :) --- README.md | 15 +++++++-------- examples/anonymous_bot.py | 11 +---------- examples/chat_member_example.py | 2 +- examples/custom_filters/admin_filter_example.py | 2 +- examples/custom_filters/general_custom_filters.py | 2 +- examples/custom_filters/id_filter_example.py | 5 +---- examples/custom_filters/is_filter_example.py | 2 +- examples/custom_filters/text_filter_example.py | 2 +- examples/custom_states.py | 2 +- examples/deep_linking.py | 2 +- examples/detailed_example/detailed_example.py | 2 +- examples/echo_bot.py | 2 +- examples/inline_example.py | 2 +- examples/inline_keyboard_example.py | 2 +- examples/middleware/i18n.py | 2 +- examples/middleware/session.py | 2 +- examples/payments_example.py | 3 +-- examples/register_handler.py | 2 +- examples/skip_updates_example.py | 2 +- examples/step_example.py | 2 +- examples/telebot_bot/telebot_bot.py | 2 +- telebot/__init__.py | 10 +++++++--- 22 files changed, 34 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 34d955b..01f1bbd 100644 --- a/README.md +++ b/README.md @@ -121,13 +121,13 @@ This one echoes all incoming text messages back to the sender. It uses a lambda We now have a basic bot which replies a static message to "/start" and "/help" commands and which echoes the rest of the sent messages. To start the bot, add the following to our source file: ```python -bot.polling() +bot.infinity_polling() ``` Alright, that's it! Our source file now looks like this: ```python import telebot -bot = telebot.TeleBot("TOKEN") +bot = telebot.TeleBot("YOUR_BOT_TOKEN") @bot.message_handler(commands=['start', 'help']) def send_welcome(message): @@ -137,7 +137,7 @@ def send_welcome(message): def echo_all(message): bot.reply_to(message, message.text) -bot.polling() +bot.infinity_polling() ``` To start the bot, simply open up a terminal and enter `python echo_bot.py` to run the bot! Test it by sending commands ('/start' and '/help') and arbitrary text messages. @@ -381,12 +381,10 @@ TOKEN = '' tb = telebot.TeleBot(TOKEN) #create a new Telegram Bot object # Upon calling this function, TeleBot starts polling the Telegram servers for new messages. -# - none_stop: True/False (default False) - Don't stop polling when receiving an error from the Telegram servers -# - interval: True/False (default False) - The interval between polling requests -# Note: Editing this parameter harms the bot's response time +# - interval: int (default 0) - The interval between polling requests # - timeout: integer (default 20) - Timeout in seconds for long polling. # - allowed_updates: List of Strings (default None) - List of update types to request -tb.polling(none_stop=False, interval=0, timeout=20) +tb.infinity_polling(interval=0, timeout=20) # getMe user = tb.get_me() @@ -398,6 +396,7 @@ tb.remove_webhook() # getUpdates updates = tb.get_updates() +# or updates = tb.get_updates(1234,100,20) #get_Updates(offset, limit, timeout): # sendMessage @@ -614,7 +613,7 @@ def handle_messages(messages): bot.reply_to(message, 'Hi') bot.set_update_listener(handle_messages) -bot.polling() +bot.infinity_polling() ``` ### Using web hooks diff --git a/examples/anonymous_bot.py b/examples/anonymous_bot.py index da0c00e..db7eaf7 100644 --- a/examples/anonymous_bot.py +++ b/examples/anonymous_bot.py @@ -114,13 +114,4 @@ def chatting(message: types.Message): else: bot.send_message(message.chat.id, 'No one can hear you...') -# Start retrieving updates -# Questions: -# 1. Is there any way not to process messages sent earlier? -# -# For example: -# If the bot is turned off, and i tried to type `/find` nothing will happen, but... -# When i start the bot, `/find` command will processed, and i will be added to search -# -# I tried `skip_pending=True`, but thats was not helpful -bot.polling() +bot.infinity_polling(skip_pending=True) diff --git a/examples/chat_member_example.py b/examples/chat_member_example.py index b6fca73..36dbfb2 100644 --- a/examples/chat_member_example.py +++ b/examples/chat_member_example.py @@ -30,4 +30,4 @@ def my_chat_m(message: types.ChatMemberUpdated): @bot.message_handler(content_types=util.content_type_service) def delall(message: types.Message): bot.delete_message(message.chat.id,message.message_id) -bot.polling(allowed_updates=util.update_types) \ No newline at end of file +bot.infinity_polling(allowed_updates=util.update_types) diff --git a/examples/custom_filters/admin_filter_example.py b/examples/custom_filters/admin_filter_example.py index 98993e1..2aa683e 100644 --- a/examples/custom_filters/admin_filter_example.py +++ b/examples/custom_filters/admin_filter_example.py @@ -9,4 +9,4 @@ def answer_for_admin(message): # Register filter bot.add_custom_filter(custom_filters.IsAdminFilter(bot)) -bot.polling() \ No newline at end of file +bot.infinity_polling() diff --git a/examples/custom_filters/general_custom_filters.py b/examples/custom_filters/general_custom_filters.py index 382b68c..5eab569 100644 --- a/examples/custom_filters/general_custom_filters.py +++ b/examples/custom_filters/general_custom_filters.py @@ -39,4 +39,4 @@ def bye_user(message): bot.add_custom_filter(MainFilter()) bot.add_custom_filter(IsAdmin()) -bot.polling(skip_pending=True,non_stop=True) # Skip old updates \ No newline at end of file +bot.infinity_polling(skip_pending=True) # Skip old updates diff --git a/examples/custom_filters/id_filter_example.py b/examples/custom_filters/id_filter_example.py index 06a6ce3..4a5b883 100644 --- a/examples/custom_filters/id_filter_example.py +++ b/examples/custom_filters/id_filter_example.py @@ -13,10 +13,7 @@ def admin_rep(message): def not_admin(message): bot.send_message(message.chat.id, "You are not allowed to use this command") - # Do not forget to register bot.add_custom_filter(custom_filters.ChatFilter()) - -bot.polling(non_stop=True) - +bot.infinity_polling() diff --git a/examples/custom_filters/is_filter_example.py b/examples/custom_filters/is_filter_example.py index 46d769c..414ac0d 100644 --- a/examples/custom_filters/is_filter_example.py +++ b/examples/custom_filters/is_filter_example.py @@ -18,4 +18,4 @@ def text_filter(message): bot.add_custom_filter(custom_filters.IsReplyFilter()) bot.add_custom_filter(custom_filters.ForwardFilter()) -bot.polling(non_stop=True) \ No newline at end of file +bot.infinity_polling() diff --git a/examples/custom_filters/text_filter_example.py b/examples/custom_filters/text_filter_example.py index 2476097..73b6682 100644 --- a/examples/custom_filters/text_filter_example.py +++ b/examples/custom_filters/text_filter_example.py @@ -18,4 +18,4 @@ def text_filter(message): bot.add_custom_filter(custom_filters.TextMatchFilter()) bot.add_custom_filter(custom_filters.TextStartsFilter()) -bot.polling(non_stop=True) \ No newline at end of file +bot.infinity_polling() diff --git a/examples/custom_states.py b/examples/custom_states.py index 9ad9b1c..309fa6c 100644 --- a/examples/custom_states.py +++ b/examples/custom_states.py @@ -31,4 +31,4 @@ def ready_for_answer(message, state: State): bot.send_message(message.chat.id, "Ready, take a look:\nName: {name}\nSurname: {surname}\nAge: {age}".format(name=data['name'], surname=data['surname'], age=message.text), parse_mode="html") state.finish(message.chat.id) -bot.polling() \ No newline at end of file +bot.infinity_polling() diff --git a/examples/deep_linking.py b/examples/deep_linking.py index f5ea506..039e3a7 100644 --- a/examples/deep_linking.py +++ b/examples/deep_linking.py @@ -74,4 +74,4 @@ def send_welcome(message): bot.reply_to(message, reply) -bot.polling() +bot.infinity_polling() diff --git a/examples/detailed_example/detailed_example.py b/examples/detailed_example/detailed_example.py index 8f2878a..76359cf 100644 --- a/examples/detailed_example/detailed_example.py +++ b/examples/detailed_example/detailed_example.py @@ -130,4 +130,4 @@ def command_default(m): bot.send_message(m.chat.id, "I don't understand \"" + m.text + "\"\nMaybe try the help page at /help") -bot.polling() +bot.infinity_polling() diff --git a/examples/echo_bot.py b/examples/echo_bot.py index b66eb34..c55a004 100644 --- a/examples/echo_bot.py +++ b/examples/echo_bot.py @@ -25,4 +25,4 @@ def echo_message(message): bot.reply_to(message, message.text) -bot.polling() +bot.infinity_polling() diff --git a/examples/inline_example.py b/examples/inline_example.py index 21f05eb..b0dc106 100644 --- a/examples/inline_example.py +++ b/examples/inline_example.py @@ -61,7 +61,7 @@ def default_query(inline_query): def main_loop(): - bot.polling(True) + bot.infinity_polling() while 1: time.sleep(3) diff --git a/examples/inline_keyboard_example.py b/examples/inline_keyboard_example.py index f2b3fce..41f088a 100644 --- a/examples/inline_keyboard_example.py +++ b/examples/inline_keyboard_example.py @@ -24,4 +24,4 @@ def callback_query(call): def message_handler(message): bot.send_message(message.chat.id, "Yes/no?", reply_markup=gen_markup()) -bot.polling(none_stop=True) +bot.infinity_polling() diff --git a/examples/middleware/i18n.py b/examples/middleware/i18n.py index 3cea875..aafbdd0 100644 --- a/examples/middleware/i18n.py +++ b/examples/middleware/i18n.py @@ -50,4 +50,4 @@ def start(message): bot.send_message(message.chat.id, _('hello')) -bot.polling() +bot.infinity_polling() diff --git a/examples/middleware/session.py b/examples/middleware/session.py index a1a30e5..ccda6fc 100644 --- a/examples/middleware/session.py +++ b/examples/middleware/session.py @@ -58,4 +58,4 @@ def start(message): bot.send_message(message.chat.id, bot.session['state']) -bot.polling() +bot.infinity_polling() diff --git a/examples/payments_example.py b/examples/payments_example.py index d0f52d4..c8dbfc5 100644 --- a/examples/payments_example.py +++ b/examples/payments_example.py @@ -78,5 +78,4 @@ def got_payment(message): parse_mode='Markdown') -bot.skip_pending = True -bot.polling(none_stop=True, interval=0) +bot.infinity_polling(skip_pending = True) diff --git a/examples/register_handler.py b/examples/register_handler.py index 2ca423a..9ee074e 100644 --- a/examples/register_handler.py +++ b/examples/register_handler.py @@ -18,4 +18,4 @@ bot.register_message_handler(start_executor, commands=['start']) # Start command # bot.register_edited_message_handler(*args, **kwargs) # And other functions.. -bot.polling() \ No newline at end of file +bot.infinity_polling() diff --git a/examples/skip_updates_example.py b/examples/skip_updates_example.py index 0bd631b..dee5dd2 100644 --- a/examples/skip_updates_example.py +++ b/examples/skip_updates_example.py @@ -10,4 +10,4 @@ def send_welcome(message): def echo_all(message): bot.reply_to(message, message.text) -bot.polling(skip_pending=True)# Skip pending skips old updates +bot.infinity_polling(skip_pending=True)# Skip pending skips old updates diff --git a/examples/step_example.py b/examples/step_example.py index 0291c66..7c35bad 100644 --- a/examples/step_example.py +++ b/examples/step_example.py @@ -83,4 +83,4 @@ bot.enable_save_next_step_handlers(delay=2) # WARNING It will work only if enable_save_next_step_handlers was called! bot.load_next_step_handlers() -bot.polling() +bot.infinity_polling() diff --git a/examples/telebot_bot/telebot_bot.py b/examples/telebot_bot/telebot_bot.py index ac6b63c..1120707 100644 --- a/examples/telebot_bot/telebot_bot.py +++ b/examples/telebot_bot/telebot_bot.py @@ -81,4 +81,4 @@ def listener(messages): bot.set_update_listener(listener) -bot.polling() +bot.infinity_polling() diff --git a/telebot/__init__.py b/telebot/__init__.py index a9ce65c..5f96ee8 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -563,8 +563,9 @@ class TeleBot: for listener in self.update_listener: self._exec_task(listener, new_messages) - def infinity_polling(self, timeout=20, skip_pending=False, long_polling_timeout=20, logger_level=logging.ERROR, - allowed_updates=None, *args, **kwargs): + + def infinity_polling(self, timeout: int=20, skip_pending: bool=False, long_polling_timeout: int=20, logger_level=logging.ERROR, + allowed_updates: Optional[List[str]]=None, *args, **kwargs): """ Wrap polling with infinite loop and exception handling to avoid bot stops polling. @@ -673,7 +674,10 @@ class TeleBot: # self.worker_pool.clear_exceptions() logger.info("Waiting for {0} seconds until retry".format(error_interval)) time.sleep(error_interval) - error_interval *= 2 + if error_interval * 2 < 60: + error_interval *= 2 + else: + error_interval = 60 else: # polling_thread.clear_exceptions() # self.worker_pool.clear_exceptions()