Ejemplo n.º 1
0
 async def handle_telegram_photo(
         self,
         source: 'AbstractUser',
         intent: IntentAPI,
         evt: Message,
         relates_to: Dict = None) -> Optional[EventID]:
     loc, largest_size = self._get_largest_photo_size(evt.media.photo)
     file = await util.transfer_file_to_matrix(source.client,
                                               intent,
                                               loc,
                                               encrypt=self.encrypted)
     if not file:
         return None
     if self.get_config("inline_images") and (evt.message or evt.fwd_from
                                              or evt.reply_to_msg_id):
         content = await formatter.telegram_to_matrix(
             evt,
             source,
             self.main_intent,
             prefix_html=
             f"<img src='{file.mxc}' alt='Inline Telegram photo'/><br/>",
             prefix_text="Inline image: ")
         content.external_url = self._get_external_url(evt)
         await intent.set_typing(self.mxid, is_typing=False)
         return await self._send_message(intent,
                                         content,
                                         timestamp=evt.date)
     info = ImageInfo(height=largest_size.h,
                      width=largest_size.w,
                      orientation=0,
                      mimetype=file.mime_type,
                      size=(len(largest_size.bytes) if
                            (isinstance(largest_size, PhotoCachedSize)) else
                            largest_size.size))
     name = f"image{sane_mimetypes.guess_extension(file.mime_type)}"
     await intent.set_typing(self.mxid, is_typing=False)
     content = MediaMessageEventContent(
         msgtype=MessageType.IMAGE,
         info=info,
         body=name,
         relates_to=relates_to,
         external_url=self._get_external_url(evt))
     if file.decryption_info:
         content.file = file.decryption_info
     else:
         content.url = file.mxc
     result = await self._send_message(intent, content, timestamp=evt.date)
     if evt.message:
         caption_content = await formatter.telegram_to_matrix(
             evt, source, self.main_intent, no_reply_fallback=True)
         caption_content.external_url = content.external_url
         result = await self._send_message(intent,
                                           caption_content,
                                           timestamp=evt.date)
     return result
Ejemplo n.º 2
0
    async def handle_telegram_document(self, source: 'AbstractUser', intent: IntentAPI,
                                       evt: Message, relates_to: RelatesTo = None
                                       ) -> Optional[EventID]:
        document = evt.media.document

        attrs = self._parse_telegram_document_attributes(document.attributes)

        if document.size > config["bridge.max_document_size"] * 1000 ** 2:
            name = attrs.name or ""
            caption = f"\n{evt.message}" if evt.message else ""
            # TODO encrypt
            return await intent.send_notice(self.mxid, f"Too large file {name}{caption}")

        thumb_loc, thumb_size = self._get_largest_photo_size(document)
        if thumb_size and not isinstance(thumb_size, (PhotoSize, PhotoCachedSize)):
            self.log.debug(f"Unsupported thumbnail type {type(thumb_size)}")
            thumb_loc = None
            thumb_size = None
        parallel_id = source.tgid if config["bridge.parallel_file_transfer"] else None
        file = await util.transfer_file_to_matrix(source.client, intent, document, thumb_loc,
                                                  is_sticker=attrs.is_sticker,
                                                  tgs_convert=config["bridge.animated_sticker"],
                                                  filename=attrs.name, parallel_id=parallel_id,
                                                  encrypt=self.encrypted)
        if not file:
            return None

        info, name = self._parse_telegram_document_meta(evt, file, attrs, thumb_size)

        await intent.set_typing(self.mxid, is_typing=False)

        event_type = EventType.ROOM_MESSAGE
        # Riot only supports images as stickers, so send animated webm stickers as m.video
        if attrs.is_sticker and file.mime_type.startswith("image/"):
            event_type = EventType.STICKER
        content = MediaMessageEventContent(
            body=name or "unnamed file", info=info, relates_to=relates_to,
            external_url=self._get_external_url(evt),
            msgtype={
                "video/": MessageType.VIDEO,
                "audio/": MessageType.AUDIO,
                "image/": MessageType.IMAGE,
            }.get(info.mimetype[:6], MessageType.FILE))
        if file.decryption_info:
            content.file = file.decryption_info
        else:
            content.url = file.mxc
        res = await self._send_message(intent, content, event_type=event_type, timestamp=evt.date)
        if evt.message:
            caption_content = await formatter.telegram_to_matrix(evt, source, self.main_intent,
                                                                 no_reply_fallback=True)
            caption_content.external_url = content.external_url
            res = await self._send_message(intent, caption_content, timestamp=evt.date)
        return res