-2

Loosing internet connection, the output console is filled with errors. I am trying to catch those exceptions. I added the try and except blocks but to no use:

try:
    bot = telebot.TeleBot(API_KEY)
    bot.infinity_polling(long_polling_timeout=30)

except ConnectionError:
    print("Connection Error...")
except urlexceptions.TimeoutError:
    print("Timeout Error...")
except telebot.apihelper.ApiException as e:
    print("API Error: ", e)

infinity_polling kept generating errors:

2023-09-02 22:04:55,168 (__init__.py:960 MainThread) ERROR - TeleBot: "Infinity polling exception: HTTPSConnectionPool(host='api.telegram.org', port=443): Max retries exceeded with url: /bot6148432373:AAEqpEvBD4oD6dyT8Vc00PY2cf8uPbEzsK0/getMe (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x000001469D4D4A90>: Failed to resolve 'api.telegram.org' ([Errno 11001] getaddrinfo failed)"))"
2023-09-02 22:04:55,174 (__init__.py:962 MainThread) ERROR - TeleBot: "Exception traceback:
Traceback (most recent call last):
  File "C:\Users\Harsha\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connection.py", line 203, in _new_conn
    sock = connection.create_connection(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Harsha\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\util\connection.py", line 60, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Harsha\AppData\Local\Programs\Python\Python311\Lib\socket.py", line 962, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
socket.gaierror: [Errno 11001] getaddrinfo failed

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Harsha\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 790, in urlopen
    response = self._make_request(
               ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Harsha\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 491, in _make_request
    raise new_e
  File "C:\Users\Harsha\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 467, in _make_request
    self._validate_conn(conn)
  File "C:\Users\Harsha\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 1092, in _validate_conn
    conn.connect()
  File "C:\Users\Harsha\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connection.py", line 611, in connect
    self.sock = sock = self._new_conn()
                       ^^^^^^^^^^^^^^^^
  File "C:\Users\Harsha\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connection.py", line 210, in _new_conn
    raise NameResolutionError(self.host, self, e) from e
urllib3.exceptions.NameResolutionError: <urllib3.connection.HTTPSConnection object at 0x000001469D4D4A90>: Failed to resolve 'api.telegram.org' ([Errno 11001] getaddrinfo failed)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Harsha\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\adapters.py", line 486, in send
    resp = conn.urlopen(
           ^^^^^^^^^^^^^
  File "C:\Users\Harsha\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 844, in urlopen
    retries = retries.increment(
              ^^^^^^^^^^^^^^^^^^
  File "C:\Users\Harsha\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\util\retry.py", line 515, in increment
    raise MaxRetryError(_pool, url, reason) from reason  # type: ignore[arg-type]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.telegram.org', port=443): Max retries exceeded with url: /bot6148432373:AAEqpEvBD4oD6dyT8Vc00PY2cf8uPbEzsK0/getMe (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x000001469D4D4A90>: Failed to resolve 'api.telegram.org' ([Errno 11001] getaddrinfo failed)"))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Learning\Codes\Telegram\venv\Lib\site-packages\telebot\__init__.py", line 955, in infinity_polling
    self.polling(non_stop=True, timeout=timeout, long_polling_timeout=long_polling_timeout,
  File "D:\Learning\Codes\Telegram\venv\Lib\site-packages\telebot\__init__.py", line 1040, in polling
    logger.info('Starting your bot with username: [@%s]', self.user.username)
                                                          ^^^^^^^^^
  File "D:\Learning\Codes\Telegram\venv\Lib\site-packages\telebot\__init__.py", line 273, in user
    self._user = self.get_me()
                 ^^^^^^^^^^^^^
  File "D:\Learning\Codes\Telegram\venv\Lib\site-packages\telebot\__init__.py", line 1236, in get_me
    result = apihelper.get_me(self.token)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Learning\Codes\Telegram\venv\Lib\site-packages\telebot\apihelper.py", line 196, in get_me
    return _make_request(token, method_url)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Learning\Codes\Telegram\venv\Lib\site-packages\telebot\apihelper.py", line 156, in _make_request
    result = _get_req_session().request(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Harsha\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Harsha\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Harsha\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\adapters.py", line 519, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.telegram.org', port=443): Max retries exceeded with url: /bot6148432373:AAEqpEvBD4oD6dyT8Vc00PY2cf8uPbEzsK0/getMe (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x000001469D4D4A90>: Failed to resolve 'api.telegram.org' ([Errno 11001] getaddrinfo failed)"))
"
Traceback (most recent call last):
  File "C:\Users\Harsha\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connection.py", line 203, in _new_conn
    sock = connection.create_connection(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Harsha\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\util\connection.py", line 60, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Harsha\AppData\Local\Programs\Python\Python311\Lib\socket.py", line 962, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
socket.gaierror: [Errno 11001] getaddrinfo failed

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Harsha\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 790, in urlopen
    response = self._make_request(
               ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Harsha\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 491, in _make_request
    raise new_e
  File "C:\Users\Harsha\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 467, in _make_request
    self._validate_conn(conn)
  File "C:\Users\Harsha\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 1092, in _validate_conn
    conn.connect()
  File "C:\Users\Harsha\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connection.py", line 611, in connect
    self.sock = sock = self._new_conn()
                       ^^^^^^^^^^^^^^^^
  File "C:\Users\Harsha\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connection.py", line 210, in _new_conn
    raise NameResolutionError(self.host, self, e) from e
urllib3.exceptions.NameResolutionError: <urllib3.connection.HTTPSConnection object at 0x000001469D4D4A90>: Failed to resolve 'api.telegram.org' ([Errno 11001] getaddrinfo failed)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Harsha\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\adapters.py", line 486, in send
    resp = conn.urlopen(
           ^^^^^^^^^^^^^
  File "C:\Users\Harsha\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 844, in urlopen
    retries = retries.increment(
              ^^^^^^^^^^^^^^^^^^
  File "C:\Users\Harsha\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\util\retry.py", line 515, in increment
    raise MaxRetryError(_pool, url, reason) from reason  # type: ignore[arg-type]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.telegram.org', port=443): Max retries exceeded with url: /bot6148432373:AAEqpEvBD4oD6dyT8Vc00PY2cf8uPbEzsK0/getMe (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x000001469D4D4A90>: Failed to resolve 'api.telegram.org' ([Errno 11001] getaddrinfo failed)"))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Learning\Codes\Telegram\venv\Lib\site-packages\telebot\__init__.py", line 955, in infinity_polling
    self.polling(non_stop=True, timeout=timeout, long_polling_timeout=long_polling_timeout,
  File "D:\Learning\Codes\Telegram\venv\Lib\site-packages\telebot\__init__.py", line 1040, in polling
    logger.info('Starting your bot with username: [@%s]', self.user.username)
                                                          ^^^^^^^^^
  File "D:\Learning\Codes\Telegram\venv\Lib\site-packages\telebot\__init__.py", line 273, in user
    self._user = self.get_me()
                 ^^^^^^^^^^^^^
  File "D:\Learning\Codes\Telegram\venv\Lib\site-packages\telebot\__init__.py", line 1236, in get_me
    result = apihelper.get_me(self.token)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Learning\Codes\Telegram\venv\Lib\site-packages\telebot\apihelper.py", line 196, in get_me
    return _make_request(token, method_url)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Learning\Codes\Telegram\venv\Lib\site-packages\telebot\apihelper.py", line 156, in _make_request
    result = _get_req_session().request(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Harsha\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Harsha\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Harsha\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\adapters.py", line 519, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.telegram.org', port=443): Max retries exceeded with url: /bot6148432373:AAEqpEvBD4oD6dyT8Vc00PY2cf8uPbEzsK0/getMe (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x000001469D4D4A90>: Failed to resolve 'api.telegram.org' ([Errno 11001] getaddrinfo failed)"))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Learning\Codes\Telegram\Google-Telegram.py", line 31, in <module>
    bot.infinity_polling(long_polling_timeout=30)
  File "D:\Learning\Codes\Telegram\venv\Lib\site-packages\telebot\__init__.py", line 963, in infinity_polling
    time.sleep(3)
KeyboardInterrupt
user4157124
  • 2,809
  • 13
  • 27
  • 42

0 Answers0