Telethon: resolve_bot_file_id returns None for valid file_id

Checklist

  • The error is in the library’s code, and not in my own.
  • I have searched for this issue before posting it and there isn’t a duplicate.
  • I ran pip install -U https://github.com/LonamiWebs/Telethon/archive/master.zip and triggered the bug in the latest version.

Code that causes the issue

import asyncio

from telegram.ext import Updater, MessageHandler, Filters
from telethon.utils import resolve_bot_file_id, get_input_location
from telethon import TelegramClient

import logging

logging.basicConfig()

api_id = 123
api_hash = 'my_api_hash'
token = 'my_bot_token'


def file_handler(update, context):
    file = update.effective_message.document
    file_size = file.file_size
    file_id = file.file_id
    file_name = file.file_name
    if file_size > 20000000:
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        get_file_telethon(file_id, file_name, loop)
    else:
        update.effective_message.document.get_file().download()


def get_file_telethon(file_id, file_name, loop):
    bot = TelegramClient('bot', api_id, api_hash, loop=loop).start(bot_token=token)
    with bot:
        document = resolve_bot_file_id(file_id)
        location = get_input_location(document)
        loop.run_until_complete(bot.download_file(location[1], file_name))


updater = Updater(token, use_context=True)
dp = updater.dispatcher
dp.add_handler(MessageHandler(Filters.document, file_handler))
updater.start_polling()
updater.idle()

...

Traceback

ERROR:telegram.ext.dispatcher:No error handlers are registered, logging exception.
Traceback (most recent call last):
  File "**/venv/lib/python3.6/site-packages/telethon/utils.py", line 768, in _get_file_info
    if location.SUBCLASS_OF_ID == 0x1523d462:
AttributeError: 'NoneType' object has no attribute 'SUBCLASS_OF_ID'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "**/venv/lib/python3.6/site-packages/telegram/ext/dispatcher.py", line 425, in process_update
    handler.handle_update(update, self, check, context)
  File "**/venv/lib/python3.6/site-packages/telegram/ext/handler.py", line 145, in handle_update
    return self.callback(update, context)
  File **/bot.py", line 24, in file_handler
    get_file_telethon(file_id, file_name, loop)
  File "**/bot.py", line 33, in get_file_telethon
    location = get_input_location(document)
  File "**/venv/lib/python3.6/site-packages/telethon/utils.py", line 762, in get_input_location
    info = _get_file_info(location)
  File "**/venv/lib/python3.6/site-packages/telethon/utils.py", line 771, in _get_file_info
    _raise_cast_fail(location, 'InputFileLocation')
  File "**/venv/lib/python3.6/site-packages/telethon/utils.py", line 139, in _raise_cast_fail
    type(entity).__name__, target))
TypeError: Cannot cast NoneType to any kind of InputFileLocation.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 18 (8 by maintainers)

Commits related to this issue

Most upvoted comments

The thing is, this bot file ID should be equivalent to the one given by Telegram’s HTTP bot API. However, they may’ve changed the format over time and Telethon has not been keeping up.