0

I have a list of proxy addresses for which I want to connect the telegrams-bot. Proxies can be blocked over time or simply not work, this can be seen by the MaxRetryError error. But I can't catch an error. I have an error in logs. I want to catch an error and switch to another proxy server.

from telegram.ext import Updater

REQUEST_KWARGS = {
    'proxy_url': proxy_url,
    'urllib3_proxy_kwargs': {
        'retries': 0
    }
}

updater = Updater(token=TELEGRAM_TOKEN, request_kwargs=REQUEST_KWARGS)
queue = updater.start_polling(bootstrap_retries=0)

.....
raise MaxRetryError(_pool, url, error or ResponseError(cause))
telegram.vendor.ptb_urllib3.urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.telegram.org', port=443): Max retries exceeded with url: /bot877422445:AAEGBD0D9stJKMF6PvCClChx8MNMGX-vLEY/deleteWebhook (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<telegram.vendor.ptb_urllib3.urllib3.connection.VerifiedHTTPSConnection object at 0x11572df98>: Failed to establish a new connection: [Errno 61] Connection refused')))
......
telegram.error.NetworkError: urllib3 HTTPError HTTPSConnectionPool(host='api.telegram.org', port=443): Max retries exceeded with url: /bot877422445:AAEGBD0D9stJKMF6PvCClChx8MNMGX-vLEY/deleteWebhook (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<telegram.vendor.ptb_urllib3.urllib3.connection.VerifiedHTTPSConnection object at 0x11572df98>: Failed to establish a new connection: [Errno 61] Connection refused')))

2019-07-26 16:07:38,553 - telegram.ext.dispatcher - CRITICAL - stopping due to exception in another thread

1 Answers1

0

This way ?

try:
  updater = Updater(token=TELEGRAM_TOKEN, request_kwargs=REQUEST_KWARGS)
  queue = updater.start_polling(bootstrap_retries=0)
except MaxRetryError:
  #doSomething
IQbrod
  • 2,060
  • 1
  • 6
  • 28
  • thread dont 'die', it's frosen with this in logs "telegram.ext.dispatcher - CRITICAL - stopping due to exception in another thread" – Alexandr Dmitrievich Jul 26 '19 at 13:25
  • You have to catch the thread exception ```telegram.error.NetworkError```. It's also a duplication of this [post](https://stackoverflow.com/questions/42958390/max-retry-exceeded-with-url-in-telegram-bot) – IQbrod Jul 26 '19 at 13:32
  • thx, but it did not work, try-except any 'Error' doesn't work. Maybe there is some queue that has error inside and doesn't give it "up" – Alexandr Dmitrievich Jul 26 '19 at 13:42