Telethon icon indicating copy to clipboard operation
Telethon copied to clipboard

'PhotoSize' object has no attribute 'location'

Open Blakil opened this issue 7 months ago • 3 comments

Code that causes the issue

@client.on(events.NewMessage(func=lambda e: e.photo or e.video or (e.document and getattr(e.document, 'mime_type', '').startswith('video/gif')))) async def handle_media(event): sender = await event.get_sender() if event.photo: logger.info(f"Received photo from {sender_id}") logger.info(f"event.message.photo: {event.message.photo}") logger.info(f"event.message.media: {event.message.media}") media_type = 'photo' bot_file_id = pack_bot_file_id(event.message.photo)

Expected behavior

wanted to get file_id to save it in the database and then use later

Actual behavior

got 'PhotoSize' object has no attribute 'location' on last code line, in pack_bot_file_id method

Traceback

No response

Telethon version

1.4.0

Python version

3.11

Operating system (including distribution name and version)

Docker

Other details

This error was written about a long time ago, several years ago, but there is very little information there and apparently the problem has not been fixed.

Checklist

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

Blakil avatar May 10 '25 00:05 Blakil

I've no plans to continue to maintain pack_bot_file_id. So someone else would need to make a fix if they want to use it.

Lonami avatar May 10 '25 07:05 Lonami

I basically found a solution that works with workaround

if event.photo:
    media_type = 'photo'
    photo = event.message.photo
    bot_file_id = {
        "type": "photo",
        "id": photo.id,
        "access_hash": photo.access_hash,
        "file_reference": b64encode(photo.file_reference).decode(),
        "dc_id": photo.dc_id
    }

elif event.video:
    media_type = 'video'
    doc = event.message.document
    bot_file_id = {
        "type": "document",
        "id": doc.id,
        "access_hash": doc.access_hash,
        "file_reference": b64encode(doc.file_reference).decode(),
        "dc_id": doc.dc_id
    }

elif event.document and getattr(event.document, 'mime_type', '').startswith('video'):
    media_type = 'animation'
    doc = event.message.document
    bot_file_id = {
        "type": "document",
        "id": doc.id,
        "access_hash": doc.access_hash,
        "file_reference": b64encode(doc.file_reference).decode(),
        "dc_id": doc.dc_id
    }

usage later:

if file_id.get("type") == "photo":
    media = Photo(
        id=file_id["id"],
        access_hash=file_id["access_hash"],
        file_reference=b64decode(file_id["file_reference"]),
        date=None,
        sizes=[],
        dc_id=file_id["dc_id"],
        has_stickers=False
    )
elif file_id.get("type") == "document":
    media = Document(
        id=file_id["id"],
        access_hash=file_id["access_hash"],
        file_reference=b64decode(file_id["file_reference"]),
        date=None,
        mime_type='',
        size=0,
        thumbs=None,
        dc_id=file_id["dc_id"],
        attributes=[],
    )

I can remake the algorithm of pack_bot_file_id and resolve_bot_file_id according to a similar principle (after all, because it works if you reassemble the object, including in other contexts/dialogues), or I can make deserializer/serializer and create separate functions, then commit

Blakil avatar May 10 '25 19:05 Blakil

If we are to fix pack_bot_file_id, it should behave like the HTTP Bot API does, with the same format. Inventing a new one is unfortunately not viable.

Lonami avatar May 10 '25 20:05 Lonami