0

我正在尝试通过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)
4

1 回答 1

0

FileReferenceExpired表示对原始文件 ( file_id) 的引用现在无效并且已过期。您需要file_id在收到消息后再次获取。

作为最佳实践,在使用 时file_id,您应该始终处理以下错误:

FileReferenceEmpty
FileReferenceInvalid
FileReferenceExpired
FileIdInvalid 

抓住它们并file_id再次获取异常。

于 2022-02-17T17:09:17.617 回答