Search code examples
pythontelegram-botpython-telegram-bot

“SSL: CERTIFICATE_VERIFY_FAILED” error in my python telegram bot


My Telegram bot code was working fine for weeks and I didn't changed anything today suddenly I got [SSL: CERTIFICATE_VERIFY_FAILED] error and my bot code no longer working in my PC. I use Ubuntu 16.04 and I'm usng python telegram bot library.

What is wrong and how to fix it?

My error logs :

0|main_fil |   File "/home/angel/.local/lib/python3.6/site-packages/telegram/ext/jobqueue.py", line 314, in start
0|main_fil |     name="Bot:{}:job_queue".format(self._dispatcher.bot.id))
0|main_fil |   File "/home/angel/.local/lib/python3.6/site-packages/telegram/bot.py", line 54, in decorator
0|main_fil |     self.get_me()
0|main_fil |   File "<decorator-gen-1>", line 2, in get_me
0|main_fil |   File "/home/angel/.local/lib/python3.6/site-packages/telegram/bot.py", line 70, in decorator
0|main_fil |     result = func(*args, **kwargs)
0|main_fil |   File "/home/angel/.local/lib/python3.6/site-packages/telegram/bot.py", line 292, in get_me
0|main_fil |     result = self._request.get(url, timeout=timeout)
0|main_fil |   File "/home/angel/.local/lib/python3.6/site-packages/telegram/utils/request.py", line 277, in get
0|main_fil |     result = self._request_wrapper('GET', url, **urlopen_kwargs)
0|main_fil |   File "/home/angel/.local/lib/python3.6/site-packages/telegram/utils/request.py", line 231, in _request_wrapper
0|main_fil |     raise NetworkError('urllib3 HTTPError {0}'.format(error))
0|main_fil | telegram.error.NetworkError: urllib3 HTTPError [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)

I also tried curl :

 curl -X POST -H 'Content-Type: application/json'  -d '{"chat_id": "******", "text": "Test", "disable_notification": true}'  https://api.telegram.org/bot*****************/sendMessage

It returns this error

curl: (60) SSL certificate problem: self signed certificate in certificate chain
More details here: https://curl.haxx.se/docs/sslcerts.html

curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.

In some post they said to downgrade urllib3 to 1.24.1 , i dit it but still same error. I ran the script with same bot token in local and its running normally

Also this

angel@***:/$ python3 -m telegram
python-telegram-bot 12.6.1
certifi 2020.04.05.1
future 0.18.2
Python 3.6.8 (default, Oct  7 2019, 12:59:55)  [GCC 8.3.0]

Solution

  • Is not an answer, but it can start your bot.

    In this file:

    AppData\Local\Programs\Python\Python39\Lib\site-packages\telegram\utils\request.py

    On line 140-150 you can find something like this

        kwargs = dict(
            maxsize=con_pool_size,
            cert_reqs='CERT_REQUIRED',
            ca_certs=certifi.where(),
            socket_options=sockopts,
            timeout=urllib3.Timeout(connect=self._connect_timeout, read=read_timeout, total=None),
        )
    

    Do this and its will be worked:

        urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
        kwargs = dict(
            maxsize=con_pool_size,
            cert_reqs='CERT_NONE',
            ca_certs=certifi.where(),
            socket_options=sockopts,
            timeout=urllib3.Timeout(connect=self._connect_timeout, read=read_timeout, total=None),
        )
    

    With warnings, but worked =)