'Tell me why my telegram bot stops working after some time and gives an error:

The bot works for 10-30 minutes, then with any command in the telegram bot, the following error appears in the code terminal:

Traceback (most recent call last): File "C:\Users\79602\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connectionpool.py", line 402, in _make_request

six.raise_from(e, None)
      File "<string>", line 2, in raise_from
      File "C:\Users\79602\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connectionpool.py", line 398, in _make_request
        httplib_response = conn.getresponse()
      File "C:\Users\79602\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1377, in getresponse
        response.begin()
      File "C:\Users\79602\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 320, in begin
        version, status, reason = self._read_status()
      File "C:\Users\79602\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 281, in _read_status
        line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
      File "C:\Users\79602\AppData\Local\Programs\Python\Python39\lib\socket.py", line 704, in readinto
        return self._sock.recv_into(b)
      File "C:\Users\79602\AppData\Local\Programs\Python\Python39\lib\ssl.py", line 1241, in recv_into
        return self.read(nbytes, buffer)
      File "C:\Users\79602\AppData\Local\Programs\Python\Python39\lib\ssl.py", line 1099, in read
        return self._sslobj.read(len, buffer)
    socket.timeout: The read operation timed out
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "C:\Users\79602\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\utils\request.py", line 224, in _request_wrapper
        resp = self._con_pool.request(*args, **kwargs)
      File "C:\Users\79602\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\request.py", line 68, in request
        return self.request_encode_body(method, url, fields=fields,
      File "C:\Users\79602\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\request.py", line 148, in request_encode_body
        return self.urlopen(method, url, **extra_kw)
      File "C:\Users\79602\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\poolmanager.py", line 244, in urlopen
        response = conn.urlopen(method, u.request_uri, **kw)
      File "C:\Users\79602\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connectionpool.py", line 665, in urlopen
        retries = retries.increment(method, url, error=e, _pool=self,
      File "C:\Users\79602\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\util\retry.py", line 347, in increment
        raise six.reraise(type(error), error, _stacktrace)
      File "C:\Users\79602\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\packages\six.py", line 686, in reraise
        raise value
      File "C:\Users\79602\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connectionpool.py", line 614, in urlopen
        httplib_response = self._make_request(conn, method, url,
      File "C:\Users\79602\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connectionpool.py", line 404, in _make_request
        self._raise_timeout(err=e, url=url, timeout_value=read_timeout,
      File "C:\Users\79602\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connectionpool.py", line 321, in _raise_timeout
        raise exc_cls(*args)
    telegram.vendor.ptb_urllib3.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='api.telegram.org', port=443): Read timed out. (read timeout=5.0)
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "C:\Users\79602\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\utils\promise.py", line 56, in run
        self._result = self.pooled_function(*self.args, **self.kwargs)
      File "C:\Users\79602\Desktop\sss\CryptoTrackerBot-master\cryptotrackerbot\utils.py", line 26, in send_autodestruction_message
        update.message.reply_text(text, parse_mode=parse_mode, disable_web_page_preview=disable_web_page_preview)
      File "C:\Users\79602\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\message.py", line 515, in reply_text
        return self.bot.send_message(self.chat_id, *args, **kwargs)
      File "C:\Users\79602\AppData\Local\Programs\Python\Python39\lib\site-packages\decorator.py", line 232, in fun
        return caller(func, *(extras + args), **kw)
      File "C:\Users\79602\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\bot.py", line 67, in decorator
        result = func(*args, **kwargs)
      File "C:\Users\79602\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\bot.py", line 346, in send_message
        return self._message(url, data, disable_notification=disable_notification,
      File "C:\Users\79602\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\bot.py", line 175, in _message
        result = self._request.post(url, data, timeout=timeout)
      File "C:\Users\79602\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\utils\request.py", line 330, in post
        result = self._request_wrapper('POST', url,
      File "C:\Users\79602\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\utils\request.py", line 226, in _request_wrapper
        raise TimedOut()
    telegram.error.TimedOut: Timed out

Code:

import logging
from telegram.ext import (
    Updater,
    CommandHandler)

import commands


logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                    level=logging.INFO)
logger = logging.getLogger(__name__)


def error(bot, update, error):
    logger.warning('Update "%s" caused error "%s"' % (update, error))


def main():

    updater = Updater(token='TOKEN', workers=10)


    dp = updater.dispatcher

    # commands
    dp.add_handler(CommandHandler(('price', 'p'), commands.price_command, pass_args=True, pass_job_queue=True))
    dp.add_handler(CommandHandler(('start', 'help'), commands.help, pass_job_queue=True))
    dp.add_handler(CommandHandler(('graph', 'g'), commands.graph_command, pass_args=True, pass_job_queue=True))
    dp.add_handler(CommandHandler(('stats', 's'), commands.stats, pass_job_queue=True))


    # handle errors
    dp.add_error_handler(error)

    updater.start_polling()
    updater.idle()

if __name__ == '__main__':
    main()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source