Exemplo n.º 1
0
 def _make_media_content(
         attachment: Attachment) -> MediaMessageEventContent:
     if attachment.content_type.startswith("image/"):
         msgtype = MessageType.IMAGE
         info = ImageInfo(mimetype=attachment.content_type,
                          width=attachment.width,
                          height=attachment.height)
     elif attachment.content_type.startswith("video/"):
         msgtype = MessageType.VIDEO
         info = VideoInfo(mimetype=attachment.content_type,
                          width=attachment.width,
                          height=attachment.height)
     elif attachment.voice_note or attachment.content_type.startswith(
             "audio/"):
         msgtype = MessageType.AUDIO
         info = AudioInfo(mimetype=attachment.content_type)
     else:
         msgtype = MessageType.FILE
         info = FileInfo(mimetype=attachment.content_type)
     if not attachment.custom_filename:
         ext = mimetypes.guess_extension(attachment.content_type) or ""
         attachment.custom_filename = attachment.id + ext
     return MediaMessageEventContent(msgtype=msgtype,
                                     info=info,
                                     body=attachment.custom_filename)
Exemplo n.º 2
0
 async def _handle_facebook_attachment(self, intent: IntentAPI, attachment: AttachmentClass
                                       ) -> Optional[EventID]:
     if isinstance(attachment, AudioAttachment):
         mxc, mime, size = await self._reupload_fb_photo(attachment.url, intent,
                                                         attachment.filename)
         event_id = await intent.send_file(self.mxid, mxc, file_type=MessageType.AUDIO,
                                           info=AudioInfo(size=size, mimetype=mime,
                                                          duration=attachment.duration),
                                           file_name=attachment.filename, )
     # elif isinstance(attachment, VideoAttachment):
     # TODO
     elif isinstance(attachment, FileAttachment):
         mxc, mime, size = await self._reupload_fb_photo(attachment.url, intent, attachment.name)
         event_id = await intent.send_file(self.mxid, mxc,
                                           info=FileInfo(size=size, mimetype=mime),
                                           file_name=attachment.name)
     elif isinstance(attachment, ImageAttachment):
         mxc, mime, size = await self._reupload_fb_photo(attachment.large_preview_url, intent)
         event_id = await intent.send_image(self.mxid, mxc,
                                            file_name=f"image.{attachment.original_extension}",
                                            info=ImageInfo(size=size, mimetype=mime,
                                                           width=attachment.large_preview_width,
                                                           height=attachment.large_preview_height))
     elif isinstance(attachment, LocationAttachment):
         content = await self._convert_facebook_location(intent, attachment)
         event_id = await intent.send_message(self.mxid, content)
     else:
         self.log.warn(f"Unsupported attachment type: {attachment}")
         return None
     self._last_bridged_mxid = event_id
     return event_id
Exemplo n.º 3
0
Arquivo: bot.py Projeto: maubot/karma
 async def export_own_karma(self, evt: MessageEvent) -> None:
     karma_list = [
         karma.to_dict() for karma in self.karma_t.export(evt.sender)
     ]
     data = json.dumps(karma_list).encode("utf-8")
     url = await self.client.upload_media(data,
                                          mime_type="application/json")
     await evt.reply(
         MediaMessageEventContent(msgtype=MessageType.FILE,
                                  body=f"karma-{evt.sender}.json",
                                  url=url,
                                  info=FileInfo(
                                      mimetype="application/json",
                                      size=len(data),
                                  )))
Exemplo n.º 4
0
    async def _handle_facebook_attachment(
            self, intent: IntentAPI, attachment: AttachmentClass,
            reply_to: str, message_text: str) -> Optional[EventID]:
        if isinstance(attachment, AudioAttachment):
            mxc, mime, size = await self._reupload_fb_photo(
                attachment.url, intent, attachment.filename)
            event_id = await intent.send_file(
                self.mxid,
                mxc,
                file_type=MessageType.AUDIO,
                info=AudioInfo(size=size,
                               mimetype=mime,
                               duration=attachment.duration),
                file_name=attachment.filename,
                relates_to=self._get_facebook_reply(reply_to))
        # elif isinstance(attachment, VideoAttachment):
        # TODO
        elif isinstance(attachment, FileAttachment):
            mxc, mime, size = await self._reupload_fb_photo(
                attachment.url, intent, attachment.name)
            event_id = await intent.send_file(
                self.mxid,
                mxc,
                info=FileInfo(size=size, mimetype=mime),
                file_name=attachment.name,
                relates_to=self._get_facebook_reply(reply_to))
        elif isinstance(attachment, ImageAttachment):
            mxc, mime, size = await self._reupload_fb_photo(
                attachment.large_preview_url or attachment.preview_url, intent)
            info = ImageInfo(size=size,
                             mimetype=mime,
                             width=attachment.large_preview_width,
                             height=attachment.large_preview_height)
            event_id = await intent.send_image(
                self.mxid,
                mxc,
                info=info,
                file_name=f"image.{attachment.original_extension}",
                relates_to=self._get_facebook_reply(reply_to))
        elif isinstance(attachment, LocationAttachment):
            content = await self._convert_facebook_location(intent, attachment)
            content.relates_to = self._get_facebook_reply(reply_to)
            event_id = await intent.send_message(self.mxid, content)
        elif isinstance(attachment, ShareAttachment):
            # remove trailing slash for url searching
            url = attachment.original_url
            if url[-1] == "/":
                url = url[0:-1]

            title = attachment.title
            if title == "":
                title = url
            # Prevent sending urls that are already in the original message text
            if message_text is not None and url not in message_text:
                content = TextMessageEventContent(
                    msgtype=MessageType.TEXT,
                    body=f"{title}: {attachment.original_url}")
                content.format = Format.HTML
                content.formatted_body = f"<a href='{attachment.original_url}'>{title}</a>"
                event_id = await intent.send_message(self.mxid, content)
            elif message_text is None:
                content = TextMessageEventContent(
                    msgtype=MessageType.TEXT,
                    body=f"{title}: {attachment.original_url}")
                content.format = Format.HTML
                content.formatted_body = f"<a href='{attachment.original_url}'>{title}</a>"
                event_id = await intent.send_message(self.mxid, content)
            else:
                return None
        else:
            self.log.warning(f"Unsupported attachment type: {attachment}")
            return None
        self._last_bridged_mxid = event_id
        return event_id
Exemplo n.º 5
0
 async def _handle_facebook_attachment(self, intent: IntentAPI,
                                       attachment: AttachmentClass,
                                       reply_to: str) -> Optional[EventID]:
     if isinstance(attachment, AudioAttachment):
         mxc, mime, size, decryption_info = await self._reupload_fb_file(
             attachment.url,
             intent,
             attachment.filename,
             encrypt=self.encrypted)
         event_id = await self._send_message(
             intent,
             MediaMessageEventContent(
                 url=mxc,
                 file=decryption_info,
                 msgtype=MessageType.AUDIO,
                 body=attachment.filename,
                 info=AudioInfo(size=size,
                                mimetype=mime,
                                duration=attachment.duration),
                 relates_to=self._get_facebook_reply(reply_to)))
     # elif isinstance(attachment, VideoAttachment):
     # TODO
     elif isinstance(attachment, FileAttachment):
         mxc, mime, size, decryption_info = await self._reupload_fb_file(
             attachment.url,
             intent,
             attachment.name,
             encrypt=self.encrypted)
         event_id = await self._send_message(
             intent,
             MediaMessageEventContent(
                 url=mxc,
                 file=decryption_info,
                 msgtype=MessageType.FILE,
                 body=attachment.name,
                 info=FileInfo(size=size, mimetype=mime),
                 relates_to=self._get_facebook_reply(reply_to)))
     elif isinstance(attachment, ImageAttachment):
         mxc, mime, size, decryption_info = await self._reupload_fb_file(
             attachment.large_preview_url or attachment.preview_url,
             intent,
             encrypt=self.encrypted)
         event_id = await self._send_message(
             intent,
             MediaMessageEventContent(
                 url=mxc,
                 file=decryption_info,
                 msgtype=MessageType.IMAGE,
                 body=f"image.{attachment.original_extension}",
                 info=ImageInfo(size=size,
                                mimetype=mime,
                                width=attachment.large_preview_width,
                                height=attachment.large_preview_height),
                 relates_to=self._get_facebook_reply(reply_to)))
     elif isinstance(attachment, LocationAttachment):
         content = await self._convert_facebook_location(intent, attachment)
         content.relates_to = self._get_facebook_reply(reply_to)
         event_id = await self._send_message(intent, content)
     elif isinstance(attachment, ShareAttachment):
         # These are handled in the text formatter
         return None
     else:
         self.log.warning(f"Unsupported attachment type: {attachment}")
         return None
     self._last_bridged_mxid = event_id
     return event_id