E
E
Edward D2020-06-13 23:14:44
Python
Edward D, 2020-06-13 23:14:44

Why doesn't the python-telegram-bot from the tutorial work?

I tried to make a bot from the tutorial , but no luck. Please tell me what the problem is and how to fix it.

from telegram.ext import Updater
import logging
from telegram.ext import CommandHandler

def start(update, context):
    context.bot.send_message(chat_id=update.effective_chat.id,
                             text="I'm a bot, please talk to me!")



def main():
    TOKEN = "***********************************************"
    updater = Updater(token=TOKEN, use_context=True)

    dispatcher = updater.dispatcher
    logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                        level=logging.INFO)

    start_handler = CommandHandler('start', start)
    dispatcher.add_handler(start_handler)


    updater.start_polling(poll_interval=1)


if __name__ == "__main__":
    main()


Logs:
2020-06-13 19:53:28,594 - telegram.vendor.ptb_urllib3.urllib3.connectionpool - WARNING - Retrying (Retry(total=2, connect=None, read=None, redirect=None)) after connection broken by 'ConnectTimeoutError(<telegram.vendor.ptb_urllib3.urllib3.connection.VerifiedHTTPSConnection object at 0x0000025E016A2F10>, 'Connection to api.telegram.org timed out. (connect timeout=5.0)')': /bot{MY_TOKEN}/getMe
2020-06-13 19:53:33,597 - telegram.vendor.ptb_urllib3.urllib3.connectionpool - WARNING - Retrying (Retry(total=1, connect=None, read=None, redirect=None)) after connection broken by 'ConnectTimeoutError(<telegram.vendor.ptb_urllib3.urllib3.connection.VerifiedHTTPSConnection object at 0x0000025E016AE070>, 'Connection to api.telegram.org timed out. (connect timeout=5.0)')': /bot{MY_TOKEN}/getMe
2020-06-13 19:53:38,598 - telegram.vendor.ptb_urllib3.urllib3.connectionpool - WARNING - Retrying (Retry(total=0, connect=None, read=None, redirect=None)) after connection broken by 'ConnectTimeoutError(<telegram.vendor.ptb_urllib3.urllib3.connection.VerifiedHTTPSConnection object at 0x0000025E016AE190>, 'Connection to api.telegram.org timed out. (connect timeout=5.0)')': /bot1226016878:{MY_TOKEN}/getMe
Traceback (most recent call last):
  File "C:\Users\ДНС\AppData\Local\Programs\Python\Python38\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connection.py", line 140, in _new_conn
    conn = connection.create_connection(
  File "C:\Users\ДНС\AppData\Local\Programs\Python\Python38\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\util\connection.py", line 83, in create_connection
    raise err
  File "C:\Users\ДНС\AppData\Local\Programs\Python\Python38\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\util\connection.py", line 73, in create_connection
    sock.connect(sa)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\ДНС\AppData\Local\Programs\Python\Python38\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\ДНС\AppData\Local\Programs\Python\Python38\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connectionpool.py", line 360, in _make_request
    self._validate_conn(conn)
  File "C:\Users\ДНС\AppData\Local\Programs\Python\Python38\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connectionpool.py", line 857, in _validate_conn
    super(HTTPSConnectionPool, self)._validate_conn(conn)
  File "C:\Users\ДНС\AppData\Local\Programs\Python\Python38\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connectionpool.py", line 289, in _validate_conn
    conn.connect()
  File "C:\Users\ДНС\AppData\Local\Programs\Python\Python38\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connection.py", line 284, in connect
    conn = self._new_conn()
  File "C:\Users\ДНС\AppData\Local\Programs\Python\Python38\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connection.py", line 144, in _new_conn
    raise ConnectTimeoutError(
telegram.vendor.ptb_urllib3.urllib3.exceptions.ConnectTimeoutError: (<telegram.vendor.ptb_urllib3.urllib3.connection.VerifiedHTTPSConnection object at 0x0000025E016AE2B0>, 'Connection to api.telegram.org timed out. (connect timeout=5.0)')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\ДНС\AppData\Local\Programs\Python\Python38\lib\site-packages\telegram\utils\request.py", line 225, in _request_wrapper
    resp = self._con_pool.request(*args, **kwargs)
  File "C:\Users\ДНС\AppData\Local\Programs\Python\Python38\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\request.py", line 64, in request
    return self.request_encode_url(method, url, fields=fields,
  File "C:\Users\ДНС\AppData\Local\Programs\Python\Python38\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\request.py", line 87, in request_encode_url
    return self.urlopen(method, url, **extra_kw)
  File "C:\Users\ДНС\AppData\Local\Programs\Python\Python38\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\ДНС\AppData\Local\Programs\Python\Python38\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connectionpool.py", line 691, in urlopen
    return self.urlopen(method, url, body, headers, retries,
  File "C:\Users\ДНС\AppData\Local\Programs\Python\Python38\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connectionpool.py", line 691, in urlopen
    return self.urlopen(method, url, body, headers, retries,
  File "C:\Users\ДНС\AppData\Local\Programs\Python\Python38\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connectionpool.py", line 691, in urlopen
    return self.urlopen(method, url, body, headers, retries,
  File "C:\Users\ДНС\AppData\Local\Programs\Python\Python38\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\ДНС\AppData\Local\Programs\Python\Python38\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\util\retry.py", line 376, in increment
    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: /bot1226016878:AAFuppi9tsY0nsvog6WdovI2heHxqR4CASU/getMe (Caused by ConnectTimeoutError(<telegram.vendor.ptb_urllib3.urllib3.connection.VerifiedHTTPSConnection object at 0x0000025E016AE2B0>, 'Connection to api.telegram.org timed out. (connect timeout=5.0)'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/pycharm_projects/WebDevelopment Python Course/TelegramBOT/main.py", line 31, in <module>
    main()
  File "C:/pycharm_projects/WebDevelopment Python Course/TelegramBOT/main.py", line 27, in main
    updater.start_polling(poll_interval=1)
  File "C:\Users\ДНС\AppData\Local\Programs\Python\Python38\lib\site-packages\telegram\ext\updater.py", line 260, in start_polling
    self.job_queue.start()
  File "C:\Users\ДНС\AppData\Local\Programs\Python\Python38\lib\site-packages\telegram\ext\jobqueue.py", line 446, in start
    name="Bot:{}:job_queue".format(self._dispatcher.bot.id))
  File "C:\Users\ДНС\AppData\Local\Programs\Python\Python38\lib\site-packages\telegram\bot.py", line 54, in decorator
    self.get_me()
  File "<decorator-gen-1>", line 2, in get_me
  File "C:\Users\ДНС\AppData\Local\Programs\Python\Python38\lib\site-packages\telegram\bot.py", line 70, in decorator
    result = func(*args, **kwargs)
  File "C:\Users\ДНС\AppData\Local\Programs\Python\Python38\lib\site-packages\telegram\bot.py", line 292, in get_me
    result = self._request.get(url, timeout=timeout)
  File "C:\Users\ДНС\AppData\Local\Programs\Python\Python38\lib\site-packages\telegram\utils\request.py", line 277, in get
    result = self._request_wrapper('GET', url, **urlopen_kwargs)
  File "C:\Users\ДНС\AppData\Local\Programs\Python\Python38\lib\site-packages\telegram\utils\request.py", line 231, in _request_wrapper
    raise NetworkError('urllib3 HTTPError {0}'.format(error))
telegram.error.NetworkError: urllib3 HTTPError HTTPSConnectionPool(host='api.telegram.org', port=443): Max retries exceeded with url: /bot1226016878:{MY_TOKEN}/getMe (Caused by ConnectTimeoutError(<telegram.vendor.ptb_urllib3.urllib3.connection.VerifiedHTTPSConnection object at 0x0000025E016AE2B0>, 'Connection to api.telegram.org timed out. (connect timeout=5.0)'))

Process finished with exit code 1

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Karbivnichy, 2020-06-13
@jfaFan

Use VPN or proxy as telegram is most likely blocked.

E
Edward D, 2020-07-07
@jfaFan

For Telegram, a proxy is no longer required. Roskomnadzor removed the blocking. Thanks everyone for the replies.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question