skip to Main Content

I’m unable to connect to Telegram using Telethon.

from telethon import TelegramClient
import logging

api_id = 1111111
api_hash = 'd0111111111111111111111111111111'

client = TelegramClient('session_name', api_id, api_hash)

logging.basicConfig(level=logging.DEBUG)

client.start()

Judging by the log, it seems it will connect and immediately disconnect. Telegram is not blocked in my country (Czech Republic), I’m using desktop client normally without any sort of VPN/proxy. I tried Python 3.7 and Python 3.9.

See the log bellow.

INFO:telethon.network.mtprotosender:Connecting to 149.154.167.51:443/TcpFull...
DEBUG:telethon.network.mtprotosender:Connection attempt 1...
DEBUG:telethon.network.mtprotosender:Connection success!
DEBUG:telethon.network.mtprotosender:New auth_key attempt 1...
INFO:telethon.network.connection.connection:The server closed the connection
WARNING:telethon.network.mtprotosender:Connection error 1 during auth_key gen: ConnectionError: Not connected
DEBUG:telethon.network.mtprotosender:Connection attempt 2...
DEBUG:telethon.network.mtprotosender:Connection success!
DEBUG:telethon.network.mtprotosender:New auth_key attempt 2...
INFO:telethon.network.connection.connection:The server closed the connection
WARNING:telethon.network.mtprotosender:Connection error 2 during auth_key gen: ConnectionError: Not connected
DEBUG:telethon.network.mtprotosender:Connection attempt 3...
DEBUG:telethon.network.mtprotosender:Connection success!
DEBUG:telethon.network.mtprotosender:New auth_key attempt 3...
INFO:telethon.network.connection.connection:The server closed the connection
WARNING:telethon.network.mtprotosender:Connection error 3 during auth_key gen: ConnectionError: Not connected
DEBUG:telethon.network.mtprotosender:Connection attempt 4...
DEBUG:telethon.network.mtprotosender:Connection success!
DEBUG:telethon.network.mtprotosender:New auth_key attempt 4...
INFO:telethon.network.connection.connection:The server closed the connection
WARNING:telethon.network.mtprotosender:Connection error 4 during auth_key gen: ConnectionError: Not connected
DEBUG:telethon.network.mtprotosender:Connection attempt 5...
DEBUG:telethon.network.mtprotosender:Connection success!
DEBUG:telethon.network.mtprotosender:New auth_key attempt 5...
INFO:telethon.network.connection.connection:The server closed the connection
WARNING:telethon.network.mtprotosender:Connection error 5 during auth_key gen: ConnectionError: Not connected
DEBUG:telethon.network.mtprotosender:Connection attempt 6...
DEBUG:telethon.network.mtprotosender:Connection success!
DEBUG:telethon.network.mtprotosender:New auth_key attempt 6...
INFO:telethon.network.connection.connection:The server closed the connection
WARNING:telethon.network.mtprotosender:Connection error 6 during auth_key gen: ConnectionError: Not connected
Traceback (most recent call last):
  File "c:UsersusernameDocumentsPythontelegram_read_messages.py", line 11, in <module>
    client.start()
  File "C:UsersusernameAppDataLocalProgramsPythonPython39libsite-packagestelethonclientauth.py", line 133, in start
    else self.loop.run_until_complete(coro)
  File "C:UsersusernameAppDataLocalProgramsPythonPython39libasynciobase_events.py", line 642, in run_until_complete
    return future.result()
  File "C:UsersusernameAppDataLocalProgramsPythonPython39libsite-packagestelethonclientauth.py", line 140, in _start
    await self.connect()
  File "C:UsersusernameAppDataLocalProgramsPythonPython39libsite-packagestelethonclienttelegrambaseclient.py", line 513, in connect
    if not await self._sender.connect(self._connection(
  File "C:UsersusernameAppDataLocalProgramsPythonPython39libsite-packagestelethonnetworkmtprotosender.py", line 127, in connect
    await self._connect()
  File "C:UsersusernameAppDataLocalProgramsPythonPython39libsite-packagestelethonnetworkmtprotosender.py", line 253, in _connect
    raise ConnectionError('Connection to Telegram failed {} time(s)'.format(self._retries))
ConnectionError: Connection to Telegram failed 5 time(s)

My end goal is to be able to simply connect and read messages from groups I’m part of.

Any tips appreciated.

2

Answers


  1. Chosen as BEST ANSWER

    Thank you for all the suggestions. In the end, my original code was perfectly fine.

    The issue was network related.


  2. Try following this steps:
    https://docs.telethon.dev/en/latest/

    Something like this:

    from telethon.sync import TelegramClient, events
    
    with TelegramClient('name', api_id, api_hash) as client:
       client.send_message('me', 'Hello, myself!')
       print(client.download_profile_photo('me'))
    
       @client.on(events.NewMessage(pattern='(?i).*Hello'))
       async def handler(event):
          await event.reply('Hey!')
    
       client.run_until_disconnected()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search