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

Telegram Bot获取照片file_id时报错‘list对象无file_id属性’求助

问题排查与解决方案

错误根因

Telegram Bot API 返回的 update.message.photo 是列表类型,并非单个文件对象。该列表按分辨率从小到大存储了同一张照片的多个尺寸版本,每个列表元素才是携带 file_id 属性的照片对象。
你原有代码直接对列表调用 file_id 属性,自然会触发 'list' object has no attribute 'file_id' 报错。

修复代码

通常我们需要最高清的照片版本,直接取列表最后一个元素即可,修改后的处理函数如下:

def photo(update, context):
    try:
        print(update)
        # 取分辨率最高的最后一个照片元素的file_id
        obj = context.bot.getFile(file_id=update.message.photo[-1].file_id)
        print(obj)
        #obj.download()
        update.message.reply_text("File has been downloaded")
    except Exception as e:
        print(str(e))

dp.add_handler(MessageHandler(Filters.photo, photo))

补充说明

如果需要其他分辨率的照片版本,可按需选择列表对应索引的元素:索引0对应分辨率最小的版本,索引值越大对应分辨率越高。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 15:57:04