You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Telethon上传文件至Telegram后,能否生成InputDocument类型?

环境
  • Telethon 1.36.0
  • Python 3.12.4
upload = await client.upload_file(request, file_name="gif.mp4")
print(type(upload))
# 输出:<class 'telethon.tl.custom.inputsizedfile.InputSizedFile'>
问题

我能否通过构造器从已上传的文件生成InputDocument类型?我需要获取该类型的以下内容:

  • id
  • access_hash
  • file_reference

请勿提供将该文档发送至聊天的解决方案。

解答

不行,直接通过InputSizedFile构造InputDocument做不到——因为InputDocument所需的id、access_hash、file_reference是Telegram服务器为已持久化保存的文档分配的专属标识,而upload_file返回的InputSizedFile只是临时上传会话对象,不包含这些服务器端生成的信息。

你可以调用client.save_file()方法,把已上传的文件保存到自己的账号存储中,这个方法会返回完整的文档对象,从中就能提取所需的三个字段:

document = await client.save_file(upload)
input_doc = document.to_input_document()
# 提取目标字段
doc_id = input_doc.id
access_hash = input_doc.access_hash
file_reference = input_doc.file_reference

这样得到的input_doc就是标准的InputDocument类型,且无需将文件发送到任何聊天。

内容的提问来源于stack exchange,提问作者root

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 14:20:57