I did a big project on Python3, a Telegram bot with many functions, due to the scarcity of luggage knowledge and (as I understood it necessary) the wallet on the servers used bot.polling () (I use pyTelegramBotAPI) and literally in half an hour the bot drops. I wrote the usual bot for the response test through time, thinking that it overloaded the project, but no, in fact, the code falls:

import telebot import extras bot = telebot.TeleBot(extras.token) @bot.message_handler(content_types="text") def handler_text(message): bot.send_message(message.from_user.id, 'Все ещё в деле') bot.polling(none_stop=True) 

Console Error:

 During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\MainWindowsPC\AppData\Local\Programs\Python\Python36-32\lib\sit e-packages\telebot\util.py", line 58, in run task(*args, **kwargs) File "D:/Python/test.py", line 10, in handler_text bot.send_message(message.from_user.id, 'Все ещё в деле') File "C:\Users\MainWindowsPC\AppData\Local\Programs\Python\Python36-32\lib\sit e-packages\telebot\__init__.py", line 439, in send_message reply_markup, parse_mode, disable_notification)) File "C:\Users\MainWindowsPC\AppData\Local\Programs\Python\Python36-32\lib\sit e-packages\telebot\apihelper.py", line 135, in send_message return _make_request(token, method_url, params=payload, method='post') File "C:\Users\MainWindowsPC\AppData\Local\Programs\Python\Python36-32\lib\sit e-packages\telebot\apihelper.py", line 54, in _make_request timeout=(connect_timeout, read_timeout), proxies=proxy) File "C:\Users\MainWindowsPC\AppData\Local\Programs\Python\Python36-32\lib\sit e-packages\requests\sessions.py", line 508, in request resp = self.send(prep, **send_kwargs) File "C:\Users\MainWindowsPC\AppData\Local\Programs\Python\Python36-32\lib\sit e-packages\requests\sessions.py", line 618, in send r = adapter.send(request, **kwargs) File "C:\Users\MainWindowsPC\AppData\Local\Programs\Python\Python36-32\lib\sit e-packages\requests\adapters.py", line 490, in send raise ConnectionError(err, request=request) requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected( 'Remote end closed connection without response',)) 

Something like that. What to do? I don’t want to switch to the webhuk system, I don’t know how, maybe there is a flooring repair?

    3 answers 3

    The telebot developers themselves suggest not polishing and stupidly polling up an eternal loop and catching a connection error:

     while True: try: bot.polling(none_stop=True) except Exception as e: logger.error(e) # или просто print(e) если у вас логгера нет, # или import traceback; traceback.print_exc() для печати полной инфы time.sleep(15) 

    UPD: with multithreading (which is the default) problems were detected (when restarting, polling fell into a can't start thread ), you can bypass them by switching to the single-threaded version (it will work in simple cases):

     bot = telebot.TeleBot(extras.token, threaded=False) 
    • catching mistakes does not help, everything falls early - Pudberg
    • @Pudberg exactly how falls? - andreymal
    • if you start on the line it just turns off, through the console it gives errors - Pudberg
    • @Pudberg means you have incorrectly rewrote the code for yourself, this code is guaranteed to work, if only because the developer himself offered it to telebot :) - andreymal
    • Well, I copied it: from - Pudberg

    There is another option:

    instead of bot.polling(none_stop=True) write bot.infinity_polling(True) .

      You have a problem with Roskomnadzor. Use the proxy to connect the chat bot to the Telegram server, if it is running locally in the RF.

       from telebot import apihelper apihelper.proxy = { 'https': 'socks5h://127.127.127.127:12345' } 
      • And when did Roskomnadzor start blocking telegrams? - 0xdb 6:12 pm