如何使用Telethon在Telegram发送超过2GB的文件?
解决Telethon发送超过2GB文件的限制问题
问题根源
Telethon默认的send_file()方法依赖旧版上传接口,仅支持2GB以内的文件;而Telegram官方应用采用的是MTProto v2大文件上传接口,最高支持4GB文件传输。
无需拆分文件的解决方法
通过手动调用upload_file()方法启用大文件上传接口,再将上传后的文件对象传入send_file()即可绕过限制,修改后的代码如下:
from telethon import TelegramClient from telethon.tl.types import InputMediaDocument api_id = 123456 api_hash = 'api_hash' session_string = 'session_string' client = TelegramClient(session_string, api_id, api_hash) async def send_large_file(): # 手动上传大文件,指定大文件分块参数 uploaded_file = await client.upload_file( 'large_video.mp4', part_size_kb=512 * 1024 # 设置512MB分块,适配Telegram大文件接口 ) # 用上传后的文件对象发送,强制使用文档媒体类型 await client.send_file( 'me', file=InputMediaDocument(uploaded_file), force_document=True ) with client: client.loop.run_until_complete(send_large_file())
关键注意事项
- 确保Telethon版本在1.20.0及以上,旧版本不支持大文件上传参数。
part_size_kb设置为512MB是Telegram大文件接口支持的最大分块,能最大化上传效率。- 使用
InputMediaDocument包装上传结果是触发大文件接口的核心,不能省略。
内容的提问来源于stack exchange,提问作者Harshit Kamboj
相关产品推荐
相关产品推荐

