From ddfffaf6a20eab97459413f6bbaea4054465c5a7 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sat, 1 Jun 2019 22:09:05 +0300 Subject: [PATCH] Handle some image send errors by resending as document. Fixes #324 --- mautrix_telegram/portal.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/mautrix_telegram/portal.py b/mautrix_telegram/portal.py index f95de355..a86d6eee 100644 --- a/mautrix_telegram/portal.py +++ b/mautrix_telegram/portal.py @@ -43,7 +43,8 @@ from telethon.tl.functions.channels import ( UpdateUsernameRequest) from telethon.tl.functions.messages import ReadHistoryRequest as ReadMessageHistoryRequest from telethon.tl.functions.channels import ReadHistoryRequest as ReadChannelHistoryRequest -from telethon.errors import ChatAdminRequiredError, ChatNotModifiedError +from telethon.errors import (ChatAdminRequiredError, ChatNotModifiedError, PhotoExtInvalidError, + PhotoInvalidDimensionsError, PhotoSaveFileInvalidError) from telethon.tl.patched import Message, MessageService from telethon.tl.types import ( Channel, ChatAdminRights, ChatBannedRights, ChannelFull, ChannelParticipantAdmin, @@ -61,7 +62,8 @@ from telethon.tl.types import ( SendMessageTypingAction, TypeChannelParticipant, TypeChat, TypeChatParticipant, TypeDocumentAttribute, TypeInputPeer, TypeMessageAction, TypeMessageEntity, TypePeer, TypePhotoSize, TypeUpdates, TypeUser, PhotoSize, TypeUserFull, UpdateChatUserTyping, - UpdateNewChannelMessage, UpdateNewMessage, UpdateUserTyping, User, UserFull, MessageEntityPre) + UpdateNewChannelMessage, UpdateNewMessage, UpdateUserTyping, User, UserFull, MessageEntityPre, + InputMediaUploadedDocument) from mautrix_appservice import MatrixRequestError, IntentError, AppService, IntentAPI from .types import MatrixEventID, MatrixRoomID, MatrixUserID, TelegramID @@ -1016,8 +1018,14 @@ class Portal: caption, file=media) self._add_telegram_message_to_db(event_id, space, -1, response) return - response = await client.send_media(self.peer, media, reply_to=reply_to, - caption=caption) + try: + response = await client.send_media(self.peer, media, reply_to=reply_to, + caption=caption) + except (PhotoInvalidDimensionsError, PhotoSaveFileInvalidError, PhotoExtInvalidError): + media = InputMediaUploadedDocument(file=media.file, mime_type=mime, + attributes=attributes) + response = await client.send_media(self.peer, media, reply_to=reply_to, + caption=caption) self._add_telegram_message_to_db(event_id, space, 0, response) async def _handle_matrix_location(self, sender_id: TelegramID, event_id: MatrixEventID,