基于Telethon开发Telegram Bot遇TimeoutError连接失败求助
问题描述
我用Telethon开发了一个简单的Telegram Bot,代码如下:
from telethon import TelegramClient, events from config import * client = TelegramClient('bot_session',api_id= api_id, api_hash=api_hash) @client.on(events.NewMessage(pattern= r'/start')) async def start(event): await client.send_message(entity=event.chat_id,message='Hi') client.start() client.run_until_disconnected()
运行后出现连接超时错误,尝试过VPS、VPN及代理仍无法解决,错误信息如下:
Attempt 1 at connecting failed: TimeoutError: Attempt 2 at connecting failed: TimeoutError: Attempt 3 at connecting failed: TimeoutError: Attempt 4 at connecting failed: TimeoutError: Attempt 5 at connecting failed: TimeoutError: Attempt 6 at connecting failed: TimeoutError: Traceback (most recent call last): File "e:\MyPython\TelegramBot\TelegramBot-telethon.py", line 11, in <module> client.start() File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\telethon\client\auth.py", line 128, in start else self.loop.run_until_complete(coro) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 653, in run_until_complete return future.result() ^^^^^^^^^^^^^^^ File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\telethon\client\auth.py", line 135, in _start await self.connect() File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\telethon\client\telegrambaseclient.py", line 547, in connect if not await self._sender.connect(self._connection( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\telethon\network\mtprotosender.py", line 134, in connect await self._connect() File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\telethon\network\mtprotosender.py", line 260, in _connect raise ConnectionError('Connection to Telegram failed {} time(s)'.format(self._retries)) ConnectionError: Connection to Telegram failed 5 time(s)
解决建议
明确配置代理参数:如果使用代理,必须在Telethon客户端初始化时指定完整代理信息,比如socks5代理示例:
from telethon import TelegramClient, events from config import * # 替换为你的代理地址、端口、用户名和密码(如有) proxy = ('socks5', 'proxy_host', proxy_port, True, 'proxy_user', 'proxy_pass') client = TelegramClient('bot_session', api_id=api_id, api_hash=api_hash, proxy=proxy)若用VPN,需确认系统全局代理已接管Python进程的网络请求,可检查
HTTP_PROXY和HTTPS_PROXY环境变量是否正确设置。切换Telegram备用服务器:默认服务器可能被屏蔽,尝试使用官方公开的备用IP,通过
connection_details指定:from telethon import TelegramClient, events from telethon.network.connection.tcpabridged import ConnectionTcpAbridged from config import * # 示例备用IP,可替换为其他官方公开节点 client = TelegramClient( 'bot_session', api_id=api_id, api_hash=api_hash, connection=ConnectionTcpAbridged, connection_details=('149.154.167.50', 443) )验证API信息有效性:确认从Telegram开发者平台获取的
api_id和api_hash正确,且对应账号未被限制,可尝试用同一API信息登录官方Telegram客户端测试连接。延长连接超时时间:初始化客户端时增加超时参数,给连接更多等待时间:
client = TelegramClient( 'bot_session', api_id=api_id, api_hash=api_hash, timeout=30 # 设置为30秒 )检查VPS网络限制:若使用VPS,通过命令测试Telegram服务器端口是否可通:
telnet 149.154.167.50 443连接失败则联系VPS提供商解除端口限制,或更换其他地区节点。
更新Telethon版本:旧版本可能存在兼容问题,执行命令升级:
pip install --upgrade telethon
内容的提问来源于stack exchange,提问作者Mohsen Rahmdeli
相关产品推荐
相关产品推荐

