Skip to content

Commit

Permalink
Handle some image send errors by resending as document. Fixes #324
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Jun 1, 2019
1 parent baffe1b commit ddfffaf
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions mautrix_telegram/portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
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,
Expand All @@ -61,7 +62,8 @@
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
Expand Down Expand Up @@ -1016,8 +1018,14 @@ async def _handle_matrix_file(self, msgtype: str, sender_id: TelegramID,
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,
Expand Down

0 comments on commit ddfffaf

Please sign in to comment.