Please answer these questions before submitting your issue. Thanks!
What version of pyTelegramBotAPI are you using?
4.17.0
What OS are you using?
Alpine linux on Docker container
What version of python are you using?
3.12.2
I'm trying to catch an exception telebot.apihelper.ApiTelegramException: A request to the Telegram API was unsuccessful. Error code: 502. Description: Bad Gateway:
def start_polling(self):
"""Start bot polling"""
while True:
try:
self.sleep_time += 5 # The time in seconds that we sleep for after each cycle.
bot_logger.info('Start polling')
self.bot.polling(timeout=60, long_polling_timeout=60, none_stop=True, skip_pending=True)
except (ReadTimeout, HTTPError, ConnectionError) as e:
self.bot.stop_polling()
bot_logger.debug(f"Connection error: {e}. Retry after {self.sleep_time} seconds")
bot_logger.error(f'Connection error. Retry after {self.sleep_time} seconds')
sleep(self.sleep_time)
continue
except telebot.apihelper.ApiTelegramException as e:
self.bot.stop_polling()
bot_logger.debug(f'Telegram API error: {e}. Connection attempt after {self.sleep_time} seconds.')
bot_logger.error(f'Telegram API error. Connection attempt after {self.sleep_time} seconds.')
sleep(self.sleep_time)
continue
except Exception as e:
self.bot.stop_polling()
bot_logger.debug(f"Unexpected exception: {e}. Unable to perform an automatic restart. Shutdown bot ")
bot_logger.error("Unexpected exception. Unable to perform an automatic restart. Shutdown bot")
break
For some reason that I don't fully understand, I am unable to complete the task. Every time an error 502 occurs, a full traceback is generated in the logs:
2024-05-14 01:10:40,769 (__init__.py:1212 MainThread) ERROR - TeleBot: "Threaded polling exception: A request to the Telegram API was unsuccessful. Error code: 502. Description: Bad Gateway"
2024-05-14 01:10:40,770 (__init__.py:1214 MainThread) ERROR - TeleBot: "Exception traceback:
Traceback (most recent call last):
File "/venv/lib/python3.12/site-packages/telebot/__init__.py", line 1205, in __threaded_polling
polling_thread.raise_exceptions()
File "/venv/lib/python3.12/site-packages/telebot/util.py", line 111, in raise_exceptions
raise self.exception_info
File "/venv/lib/python3.12/site-packages/telebot/util.py", line 93, in run
task(*args, **kwargs)
File "/venv/lib/python3.12/site-packages/telebot/__init__.py", line 675, in __retrieve_updates
updates = self.get_updates(offset=(self.last_update_id + 1),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/venv/lib/python3.12/site-packages/telebot/__init__.py", line 647, in get_updates
json_updates = apihelper.get_updates(
^^^^^^^^^^^^^^^^^^^^^^
File "/venv/lib/python3.12/site-packages/telebot/apihelper.py", line 327, in get_updates
return _make_request(token, method_url, params=payload)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/venv/lib/python3.12/site-packages/telebot/apihelper.py", line 167, in _make_request
json_result = _check_result(method_name, result)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/venv/lib/python3.12/site-packages/telebot/apihelper.py", line 194, in _check_result
raise ApiTelegramException(method_name, result, result_json)
telebot.apihelper.ApiTelegramException: A request to the Telegram API was unsuccessful. Error code: 502. Description: Bad Gateway
"
2024-05-14 01:10:41,108 (__init__.py:1212 MainThread) ERROR - TeleBot: "Threaded polling exception: A request to the Telegram API was unsuccessful. Error code: 502. Description: Bad Gateway"
2024-05-14 01:10:41,109 (__init__.py:1214 MainThread) ERROR - TeleBot: "Exception traceback:
Traceback (most recent call last):
File "/venv/lib/python3.12/site-packages/telebot/__init__.py", line 1205, in __threaded_polling
polling_thread.raise_exceptions()
File "/venv/lib/python3.12/site-packages/telebot/util.py", line 111, in raise_exceptions
raise self.exception_info
File "/venv/lib/python3.12/site-packages/telebot/util.py", line 93, in run
task(*args, **kwargs)
File "/venv/lib/python3.12/site-packages/telebot/__init__.py", line 675, in __retrieve_updates
updates = self.get_updates(offset=(self.last_update_id + 1),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/venv/lib/python3.12/site-packages/telebot/__init__.py", line 647, in get_updates
json_updates = apihelper.get_updates(
^^^^^^^^^^^^^^^^^^^^^^
File "/venv/lib/python3.12/site-packages/telebot/apihelper.py", line 327, in get_updates
return _make_request(token, method_url, params=payload)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/venv/lib/python3.12/site-packages/telebot/apihelper.py", line 167, in _make_request
json_result = _check_result(method_name, result)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/venv/lib/python3.12/site-packages/telebot/apihelper.py", line 194, in _check_result
raise ApiTelegramException(method_name, result, result_json)
telebot.apihelper.ApiTelegramException: A request to the Telegram API was unsuccessful. Error code: 502. Description: Bad Gateway
"
2024-05-14 01:10:41,697 (__init__.py:1212 MainThread) ERROR - TeleBot: "Threaded polling exception: A request to the Telegram API was unsuccessful. Error code: 502. Description: Bad Gateway"
2024-05-14 01:10:41,698 (__init__.py:1214 MainThread) ERROR - TeleBot: "Exception traceback:
Traceback (most recent call last):
File "/venv/lib/python3.12/site-packages/telebot/__init__.py", line 1205, in __threaded_polling
polling_thread.raise_exceptions()
File "/venv/lib/python3.12/site-packages/telebot/util.py", line 111, in raise_exceptions
raise self.exception_info
File "/venv/lib/python3.12/site-packages/telebot/util.py", line 93, in run
task(*args, **kwargs)
File "/venv/lib/python3.12/site-packages/telebot/__init__.py", line 675, in __retrieve_updates
updates = self.get_updates(offset=(self.last_update_id + 1),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/venv/lib/python3.12/site-packages/telebot/__init__.py", line 647, in get_updates
json_updates = apihelper.get_updates(
^^^^^^^^^^^^^^^^^^^^^^
File "/venv/lib/python3.12/site-packages/telebot/apihelper.py", line 327, in get_updates
return _make_request(token, method_url, params=payload)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/venv/lib/python3.12/site-packages/telebot/apihelper.py", line 167, in _make_request
json_result = _check_result(method_name, result)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/venv/lib/python3.12/site-packages/telebot/apihelper.py", line 194, in _check_result
raise ApiTelegramException(method_name, result, result_json)
telebot.apihelper.ApiTelegramException: A request to the Telegram API was unsuccessful. Error code: 502. Description: Bad Gateway
"
2024-05-14 01:11:47 - pyTMbot - ERROR - Connection error. Retry after 10 seconds [main.py | start_polling:44]
2024-05-14 01:11:57 - pyTMbot - INFO - Start polling [main.py | start_polling:39]
Please answer these questions before submitting your issue. Thanks!
What version of pyTelegramBotAPI are you using? 4.17.0
What OS are you using? Alpine linux on Docker container
What version of python are you using? 3.12.2
I'm trying to catch an exception
telebot.apihelper.ApiTelegramException: A request to the Telegram API was unsuccessful. Error code: 502. Description: Bad Gateway
:For some reason that I don't fully understand, I am unable to complete the task. Every time an error 502 occurs, a full traceback is generated in the logs:
Please let me know the correct method.
P.S.: The full source code is here: https://github.com/orenlab/pytmbot/tree/master/app