我试图通过file_id下载一个使用Pyrogram的文档,这个程序以一个错误结束:
pyrogram.errors.exceptions.bad_request_400.FileReferenceExpired: [400 FILE_REFERENCE_EXPIRED]: The file id contains an expired file reference, you must obtain a valid one by fetching the message from the origin context (caused by "upload.GetFile")。
代码:
import aiogram.utils.markdown as md
from aiogram import Bot, Dispatcher, types
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters import Text
from aiogram.types import ParseMode
from aiogram.utils import executor
bot = Bot(token=BotToken)
storage = MemoryStorage()
dp = Dispatcher(bot, storage=storage)
@dp.message_handler(content_types='document')
async def DownalodDocument(message: types.Message):
await DownloadFile(message.document.file_id)
if __name__ == '__main__':
executor.start_polling(dp, skip_updates=False)from pyrogram import Client
async def DownloadFile(file_id):
async with Client("my_account", api_id, api_hash) as app:
await app.download_media(file_id)发布于 2022-02-17 17:09:17
FileReferenceExpired意味着对原始文件(file_id)的引用现在无效,并且已经过期。您需要在收到消息后再次获取file_id。
作为最佳实践,在使用file_id时,您应该始终处理以下错误:
FileReferenceEmpty
FileReferenceInvalid
FileReferenceExpired
FileIdInvalid 捕获它们并在异常情况下再次获取file_id。
https://stackoverflow.com/questions/71099145
复制相似问题