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

polling should leave our world. :)

This commit is contained in:
Badiboy 2021-09-28 19:17:09 +03:00
parent b35f17124f
commit 062fababf2
22 changed files with 34 additions and 44 deletions

View File

@ -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: 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 ```python
bot.polling() bot.infinity_polling()
``` ```
Alright, that's it! Our source file now looks like this: Alright, that's it! Our source file now looks like this:
```python ```python
import telebot import telebot
bot = telebot.TeleBot("TOKEN") bot = telebot.TeleBot("YOUR_BOT_TOKEN")
@bot.message_handler(commands=['start', 'help']) @bot.message_handler(commands=['start', 'help'])
def send_welcome(message): def send_welcome(message):
@ -137,7 +137,7 @@ def send_welcome(message):
def echo_all(message): def echo_all(message):
bot.reply_to(message, message.text) 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. 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 = '<token_string>'
tb = telebot.TeleBot(TOKEN) #create a new Telegram Bot object tb = telebot.TeleBot(TOKEN) #create a new Telegram Bot object
# Upon calling this function, TeleBot starts polling the Telegram servers for new messages. # 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: int (default 0) - The interval between polling requests
# - interval: True/False (default False) - The interval between polling requests
# Note: Editing this parameter harms the bot's response time
# - timeout: integer (default 20) - Timeout in seconds for long polling. # - timeout: integer (default 20) - Timeout in seconds for long polling.
# - allowed_updates: List of Strings (default None) - List of update types to request # - 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 # getMe
user = tb.get_me() user = tb.get_me()
@ -398,6 +396,7 @@ tb.remove_webhook()
# getUpdates # getUpdates
updates = tb.get_updates() updates = tb.get_updates()
# or
updates = tb.get_updates(1234,100,20) #get_Updates(offset, limit, timeout): updates = tb.get_updates(1234,100,20) #get_Updates(offset, limit, timeout):
# sendMessage # sendMessage
@ -614,7 +613,7 @@ def handle_messages(messages):
bot.reply_to(message, 'Hi') bot.reply_to(message, 'Hi')
bot.set_update_listener(handle_messages) bot.set_update_listener(handle_messages)
bot.polling() bot.infinity_polling()
``` ```
### Using web hooks ### Using web hooks

View File

@ -114,13 +114,4 @@ def chatting(message: types.Message):
else: else:
bot.send_message(message.chat.id, 'No one can hear you...') bot.send_message(message.chat.id, 'No one can hear you...')
# Start retrieving updates bot.infinity_polling(skip_pending=True)
# 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()

View File

@ -30,4 +30,4 @@ def my_chat_m(message: types.ChatMemberUpdated):
@bot.message_handler(content_types=util.content_type_service) @bot.message_handler(content_types=util.content_type_service)
def delall(message: types.Message): def delall(message: types.Message):
bot.delete_message(message.chat.id,message.message_id) bot.delete_message(message.chat.id,message.message_id)
bot.polling(allowed_updates=util.update_types) bot.infinity_polling(allowed_updates=util.update_types)

View File

@ -9,4 +9,4 @@ def answer_for_admin(message):
# Register filter # Register filter
bot.add_custom_filter(custom_filters.IsAdminFilter(bot)) bot.add_custom_filter(custom_filters.IsAdminFilter(bot))
bot.polling() bot.infinity_polling()

View File

@ -39,4 +39,4 @@ def bye_user(message):
bot.add_custom_filter(MainFilter()) bot.add_custom_filter(MainFilter())
bot.add_custom_filter(IsAdmin()) bot.add_custom_filter(IsAdmin())
bot.polling(skip_pending=True,non_stop=True) # Skip old updates bot.infinity_polling(skip_pending=True) # Skip old updates

View File

@ -13,10 +13,7 @@ def admin_rep(message):
def not_admin(message): def not_admin(message):
bot.send_message(message.chat.id, "You are not allowed to use this command") bot.send_message(message.chat.id, "You are not allowed to use this command")
# Do not forget to register # Do not forget to register
bot.add_custom_filter(custom_filters.ChatFilter()) bot.add_custom_filter(custom_filters.ChatFilter())
bot.infinity_polling()
bot.polling(non_stop=True)

View File

@ -18,4 +18,4 @@ def text_filter(message):
bot.add_custom_filter(custom_filters.IsReplyFilter()) bot.add_custom_filter(custom_filters.IsReplyFilter())
bot.add_custom_filter(custom_filters.ForwardFilter()) bot.add_custom_filter(custom_filters.ForwardFilter())
bot.polling(non_stop=True) bot.infinity_polling()

View File

@ -18,4 +18,4 @@ def text_filter(message):
bot.add_custom_filter(custom_filters.TextMatchFilter()) bot.add_custom_filter(custom_filters.TextMatchFilter())
bot.add_custom_filter(custom_filters.TextStartsFilter()) bot.add_custom_filter(custom_filters.TextStartsFilter())
bot.polling(non_stop=True) bot.infinity_polling()

View File

@ -31,4 +31,4 @@ def ready_for_answer(message, state: State):
bot.send_message(message.chat.id, "Ready, take a look:\n<b>Name: {name}\nSurname: {surname}\nAge: {age}</b>".format(name=data['name'], surname=data['surname'], age=message.text), parse_mode="html") bot.send_message(message.chat.id, "Ready, take a look:\n<b>Name: {name}\nSurname: {surname}\nAge: {age}</b>".format(name=data['name'], surname=data['surname'], age=message.text), parse_mode="html")
state.finish(message.chat.id) state.finish(message.chat.id)
bot.polling() bot.infinity_polling()

View File

@ -74,4 +74,4 @@ def send_welcome(message):
bot.reply_to(message, reply) bot.reply_to(message, reply)
bot.polling() bot.infinity_polling()

View File

@ -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.send_message(m.chat.id, "I don't understand \"" + m.text + "\"\nMaybe try the help page at /help")
bot.polling() bot.infinity_polling()

View File

@ -25,4 +25,4 @@ def echo_message(message):
bot.reply_to(message, message.text) bot.reply_to(message, message.text)
bot.polling() bot.infinity_polling()

View File

@ -61,7 +61,7 @@ def default_query(inline_query):
def main_loop(): def main_loop():
bot.polling(True) bot.infinity_polling()
while 1: while 1:
time.sleep(3) time.sleep(3)

View File

@ -24,4 +24,4 @@ def callback_query(call):
def message_handler(message): def message_handler(message):
bot.send_message(message.chat.id, "Yes/no?", reply_markup=gen_markup()) bot.send_message(message.chat.id, "Yes/no?", reply_markup=gen_markup())
bot.polling(none_stop=True) bot.infinity_polling()

View File

@ -50,4 +50,4 @@ def start(message):
bot.send_message(message.chat.id, _('hello')) bot.send_message(message.chat.id, _('hello'))
bot.polling() bot.infinity_polling()

View File

@ -58,4 +58,4 @@ def start(message):
bot.send_message(message.chat.id, bot.session['state']) bot.send_message(message.chat.id, bot.session['state'])
bot.polling() bot.infinity_polling()

View File

@ -78,5 +78,4 @@ def got_payment(message):
parse_mode='Markdown') parse_mode='Markdown')
bot.skip_pending = True bot.infinity_polling(skip_pending = True)
bot.polling(none_stop=True, interval=0)

View File

@ -18,4 +18,4 @@ bot.register_message_handler(start_executor, commands=['start']) # Start command
# bot.register_edited_message_handler(*args, **kwargs) # bot.register_edited_message_handler(*args, **kwargs)
# And other functions.. # And other functions..
bot.polling() bot.infinity_polling()

View File

@ -10,4 +10,4 @@ def send_welcome(message):
def echo_all(message): def echo_all(message):
bot.reply_to(message, message.text) 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

View File

@ -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! # WARNING It will work only if enable_save_next_step_handlers was called!
bot.load_next_step_handlers() bot.load_next_step_handlers()
bot.polling() bot.infinity_polling()

View File

@ -81,4 +81,4 @@ def listener(messages):
bot.set_update_listener(listener) bot.set_update_listener(listener)
bot.polling() bot.infinity_polling()

View File

@ -563,8 +563,9 @@ 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, 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. Wrap polling with infinite loop and exception handling to avoid bot stops polling.
@ -673,7 +674,10 @@ class TeleBot:
# self.worker_pool.clear_exceptions() # self.worker_pool.clear_exceptions()
logger.info("Waiting for {0} seconds until retry".format(error_interval)) logger.info("Waiting for {0} seconds until retry".format(error_interval))
time.sleep(error_interval) time.sleep(error_interval)
error_interval *= 2 if error_interval * 2 < 60:
error_interval *= 2
else:
error_interval = 60
else: else:
# polling_thread.clear_exceptions() # polling_thread.clear_exceptions()
# self.worker_pool.clear_exceptions() # self.worker_pool.clear_exceptions()