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

Merge pull request #1320 from Badiboy/master

polling should leave our world. :)
This commit is contained in:
Badiboy 2021-09-28 19:18:32 +03:00 committed by GitHub
commit d1d5b9effb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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:
```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 = '<token_string>'
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

View File

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

View File

@ -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)
bot.infinity_polling(allowed_updates=util.update_types)

View File

@ -9,4 +9,4 @@ def answer_for_admin(message):
# Register filter
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(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):
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()

View File

@ -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)
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.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")
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.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.polling()
bot.infinity_polling()

View File

@ -25,4 +25,4 @@ def echo_message(message):
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():
bot.polling(True)
bot.infinity_polling()
while 1:
time.sleep(3)

View File

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

View File

@ -50,4 +50,4 @@ def start(message):
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.polling()
bot.infinity_polling()

View File

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

View File

@ -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()
bot.infinity_polling()

View File

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

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!
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.polling()
bot.infinity_polling()

View File

@ -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)
if error_interval * 2 < 60:
error_interval *= 2
else:
error_interval = 60
else:
# polling_thread.clear_exceptions()
# self.worker_pool.clear_exceptions()