mautrix / telegram

A Matrix-Telegram hybrid puppeting/relaybot bridge
https://matrix.to/#/#telegram:maunium.net
GNU Affero General Public License v3.0
1.32k stars 174 forks source link

AttributeError: 'PhotoSizeProgressive' object has no attribute 'bytes' #586

Closed showermat closed 3 years ago

showermat commented 3 years ago

Since sometime yesterday, without me having touched my homeserver setup, I stopped receiving images from Telegram and seeing the following backtrace whenever someone tried to send to me one:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/mautrix_telegram/abstract_user.py", line 195, in _update_catch
    await self._update(update)
  File "/usr/local/lib/python3.8/dist-packages/mautrix_telegram/abstract_user.py", line 242, in _update
    await self.update_message(update)
  File "/usr/local/lib/python3.8/dist-packages/mautrix_telegram/abstract_user.py", line 476, in update_message
    return await portal.handle_telegram_message(self, sender, update)
  File "/usr/local/lib/python3.8/dist-packages/mautrix_telegram/portal/telegram.py", line 596, in handle_telegram_message
    event_id = await {
  File "/usr/local/lib/python3.8/dist-packages/mautrix_telegram/portal/telegram.py", line 92, in handle_telegram_photo
    loc, largest_size = self._get_largest_photo_size(media.photo)
  File "/usr/local/lib/python3.8/dist-packages/mautrix_telegram/portal/base.py", line 225, in _get_largest_photo_size
    largest = max(photo.thumbs if isinstance(photo, Document) else photo.sizes,
  File "/usr/local/lib/python3.8/dist-packages/mautrix_telegram/portal/base.py", line 226, in <lambda>
    key=(lambda photo2: (len(photo2.bytes)
AttributeError: 'PhotoSizeProgressive' object has no attribute 'bytes'

I didn't dig into it much, but the following diff seems to fix the problem for me:

--- /usr/local/lib/python3.8/dist-packages/mautrix_telegram/portal/base.py  2021-02-14 19:07:59.375990416 +0000
+++ base.py 2021-02-14 19:05:55.399157039 +0000
@@ -24,7 +24,7 @@
                                InputPeerChannel, InputPeerChat, InputPeerUser, InputUser,
                                PeerChannel, PeerChat, PeerUser, TypeChat, TypeInputPeer, TypePeer,
                                TypeUser, TypeUserFull, User, UserFull, TypeInputChannel, Photo,
-                               Document, TypePhotoSize, PhotoSize, InputPhotoFileLocation,
+                               Document, TypePhotoSize, PhotoSize, PhotoSizeProgressive, InputPhotoFileLocation,
                                TypeChatParticipant, TypeChannelParticipant, PhotoEmpty, ChatPhoto,
                                ChatPhotoEmpty)

@@ -223,9 +223,9 @@
             return None, None

         largest = max(photo.thumbs if isinstance(photo, Document) else photo.sizes,
-                      key=(lambda photo2: (len(photo2.bytes)
-                                           if not isinstance(photo2, PhotoSize)
-                                           else photo2.size)))
+                      key=(lambda photo2: (photo2.size if isinstance(photo2, PhotoSize)
+                                           else max(photo2.sizes) if isinstance(photo2, PhotoSizeProgressive)
+                                           else len(photo2.bytes))))
         return InputPhotoFileLocation(
             id=photo.id,
             access_hash=photo.access_hash,

I have zero familiarity with these libraries, so I have no idea whether this is the right fix or a larger change is needed, but it seems to patch things up for now.

disambiguator commented 3 years ago

Thanks for posting this. I also just started seeing the same error yesterday. It's seemingly inconsistent, though. I've had trouble duplicating it.