Esempio n. 1
0
 async def _handle_matrix_text(self, sender_id: TelegramID,
                               event_id: EventID, space: TelegramID,
                               client: 'MautrixTelegramClient',
                               content: TextMessageEventContent,
                               reply_to: TelegramID) -> None:
     message, entities = await formatter.matrix_to_telegram(
         client, text=content.body, html=content.formatted(Format.HTML))
     async with self.send_lock(sender_id):
         lp = self.get_config("telegram_link_preview")
         if content.get_edit():
             orig_msg = DBMessage.get_by_mxid(content.get_edit(), self.mxid,
                                              space)
             if orig_msg:
                 response = await client.edit_message(
                     self.peer,
                     orig_msg.tgid,
                     message,
                     formatting_entities=entities,
                     link_preview=lp)
                 self._add_telegram_message_to_db(event_id, space, -1,
                                                  response)
                 return
         response = await client.send_message(self.peer,
                                              message,
                                              reply_to=reply_to,
                                              formatting_entities=entities,
                                              link_preview=lp)
         self._add_telegram_message_to_db(event_id, space, 0, response)
     await self._send_delivery_receipt(event_id)
Esempio n. 2
0
    async def _handle_matrix_file(
            self,
            sender_id: TelegramID,
            event_id: EventID,
            space: TelegramID,
            client: 'MautrixTelegramClient',
            content: MediaMessageEventContent,
            reply_to: TelegramID,
            caption: TextMessageEventContent = None) -> None:
        mime = content.info.mimetype
        if isinstance(content.info, (ImageInfo, VideoInfo)):
            w, h = content.info.width, content.info.height
        else:
            w = h = None
        file_name = content["net.maunium.telegram.internal.filename"]
        max_image_size = config["bridge.image_as_file_size"] * 1000**2

        if config["bridge.parallel_file_transfer"] and content.url:
            file_handle, file_size = await parallel_transfer_to_telegram(
                client, self.main_intent, content.url, sender_id)
        else:
            if content.file:
                if not decrypt_attachment:
                    self.log.warning(
                        f"Can't bridge encrypted media event {event_id}:"
                        " matrix-nio not installed")
                    return
                file = await self.main_intent.download_media(content.file.url)
                file = decrypt_attachment(file, content.file.key.key,
                                          content.file.hashes.get("sha256"),
                                          content.file.iv)
            else:
                file = await self.main_intent.download_media(content.url)

            if content.msgtype == MessageType.STICKER:
                if mime != "image/gif":
                    mime, file, w, h = util.convert_image(file,
                                                          source_mime=mime,
                                                          target_type="webp")
                else:
                    # Remove sticker description
                    file_name = "sticker.gif"

            file_handle = await client.upload_file(file)
            file_size = len(file)

        file_handle.name = file_name

        attributes = [DocumentAttributeFilename(file_name=file_name)]
        if w and h:
            attributes.append(DocumentAttributeImageSize(w, h))

        if (mime == "image/png"
                or mime == "image/jpeg") and file_size < max_image_size:
            media = InputMediaUploadedPhoto(file_handle)
        else:
            media = InputMediaUploadedDocument(file=file_handle,
                                               attributes=attributes,
                                               mime_type=mime
                                               or "application/octet-stream")

        capt, entities = (await formatter.matrix_to_telegram(
            client, text=caption.body, html=caption.formatted(Format.HTML))
                          if caption else (None, None))

        async with self.send_lock(sender_id):
            if await self._matrix_document_edit(client, content, space, capt,
                                                media, event_id):
                return
            try:
                response = await client.send_media(self.peer,
                                                   media,
                                                   reply_to=reply_to,
                                                   caption=capt,
                                                   entities=entities)
            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=capt,
                                                   entities=entities)
            self._add_telegram_message_to_db(event_id, space, 0, response)
        await self._send_delivery_receipt(event_id)