python telethon ConnectionError: Connection to Telegram failed 5 time(s)

Viewed 2557

I'm trying to get every new message sent in a channel with python and then use the data to do some further processing. This is my code:

api_id = *******
api_hash = '**********************'

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

)
client.start()
print(client.get_me().stringify())
client.send_message('someone', 'Hello! Talking to you from Telethon')

And this is the error that I get:


C:\Users\erfan\AppData\Local\Programs\Python\Python39\python.exe "C:/Users/erfan/Desktop/Boors py/main.py"
Traceback (most recent call last):
  File "C:\Users\erfan\Desktop\Boors py\main.py", line 14, in <module>
    client.start()
  File "C:\Users\erfan\AppData\Local\Programs\Python\Python39\lib\site-packages\telethon\client\auth.py", line 133, in start
    else self.loop.run_until_complete(coro)
  File "C:\Users\erfan\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 642, in run_until_complete
    return future.result()
  File "C:\Users\erfan\AppData\Local\Programs\Python\Python39\lib\site-packages\telethon\client\auth.py", line 140, in _start
    await self.connect()
  File "C:\Users\erfan\AppData\Local\Programs\Python\Python39\lib\site-packages\telethon\client\telegrambaseclient.py", line 516, in connect
    if not await self._sender.connect(self._connection(
  File "C:\Users\erfan\AppData\Local\Programs\Python\Python39\lib\site-packages\telethon\network\mtprotosender.py", line 123, in connect
    await self._connect()
  File "C:\Users\erfan\AppData\Local\Programs\Python\Python39\lib\site-packages\telethon\network\mtprotosender.py", line 249, in _connect
    raise ConnectionError('Connection to Telegram failed {} time(s)'.format(self._retries))
ConnectionError: Connection to Telegram failed 5 time(s)

Process finished with exit code 1

Telegram is blocked by my country. So I use Lantern to have access to it and I can access web.telegram.com. I also tried using a mtproxy but got the same error.

1 Answers

I don't know which part of my code was wrong, but as I saw many people had the same problem, I decided to post my solution. I could get telethon work very well with help of this article. And as I said telegram is blocked in my country so I could use MTProxy with help of telethon documentation at this part: 2.2.5 Using MTProto Proxies.

Related