Pluton
Pluton

Reputation: 11

Failed connect to telegram with telethon

My code:

from telethon import TelegramClient, connection
import logging

logging.basicConfig(format='[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s',
                    level=logging.DEBUG)

api_id = 1234567
api_hash = '1234567890abcdef1234567890abcdef'

client = TelegramClient('anon', api_id, api_hash)
client.start()

I'm trying to connect to telegram via a telethon, but I always get this error:

[DEBUG/2020-06-05 11:40:42,860] asyncio: Using selector: SelectSelector [ INFO/2020-06-05 11:40:43,006] telethon.network.mtprotosender: Connecting to 1.1.1.1:111/TcpFull... [DEBUG/2020-06-05 11:40:43,009] telethon.network.mtprotosender: Connection attempt 1... [WARNING/2020-06-05 11:40:53,011] telethon.network.mtprotosender: Attempt 1 at connecting failed: TimeoutError: [DEBUG/2020-06-05 11:40:54,024] telethon.network.mtprotosender: Connection attempt 2... [WARNING/2020-06-05 11:41:04,026] telethon.network.mtprotosender: Attempt 2 at connecting failed: TimeoutError: [DEBUG/2020-06-05 11:41:05,059] telethon.network.mtprotosender: Connection attempt 3... [WARNING/2020-06-05 11:41:15,061] telethon.network.mtprotosender: Attempt 3 at connecting failed: TimeoutError: [DEBUG/2020-06-05 11:41:16,084] telethon.network.mtprotosender: Connection attempt 4... [WARNING/2020-06-05 11:41:26,086] telethon.network.mtprotosender: Attempt 4 at connecting failed: TimeoutError: [DEBUG/2020-06-05 11:41:27,088] telethon.network.mtprotosender: Connection attempt 5... [WARNING/2020-06-05 11:41:37,076] telethon.network.mtprotosender: Attempt 5 at connecting failed: TimeoutError: [DEBUG/2020-06-05 11:41:38,079] telethon.network.mtprotosender: Connection attempt 6... [WARNING/2020-06-05 11:41:48,093] telethon.network.mtprotosender: Attempt 6 at connecting failed: TimeoutError: Traceback (most recent call last): File "C:\Users\xxx\AppData\Local\Programs\Python\Python37-32\cfc.py", line 11, in client.start() File "C:\Users\xxx\AppData\Roaming\Python\Python37\site-packages\telethon\client\auth.py", line 132, in start else self.loop.run_until_complete(coro) File "C:\Users\xxx\AppData\Local\Programs\Python\Python37-32\lib\asyncio\base_events.py", line 579, in run_until_complete return future.result() File "C:\Users\xxx\AppData\Roaming\Python\Python37\site-packages\telethon\client\auth.py", line 139, in _start await self.connect() File "C:\Users\xxx\AppData\Roaming\Python\Python37\site-packages\telethon\client\telegrambaseclient.py", line 478, in connect proxy=self._proxy File "C:\Users\xxx\AppData\Roaming\Python\Python37\site-packages\telethon\network\mtprotosender.py", line 125, in connect await self._connect() File "C:\Users\xxx\AppData\Roaming\Python\Python37\site-packages\telethon\network\mtprotosender.py", line 250, in _connect raise ConnectionError('Connection to Telegram failed {} time(s)'.format(self._retries)) ConnectionError: Connection to Telegram failed 5 time(s)

Upvotes: 1

Views: 4556

Answers (1)

Tohid Savari
Tohid Savari

Reputation: 156

to whom may concern! you need to add a proxy parameter to your TelegramClient

import socks
proxy = (socks.SOCKS5, 'your socks proxy IP', 'your socks proxy port')
TelegramClient(proxy=proxy, 'add other required arguments')

Upvotes: 2

Related Questions