Exemple #1
0
    async def handle_matrix_avatar(self, sender: 'u.User', url: ContentURI) -> None:
        if self.peer_type not in ("chat", "channel"):
            # Invalid peer type
            return

        file = await self.main_intent.download_media(url)
        mime = magic.from_buffer(file, mime=True)
        ext = sane_mimetypes.guess_extension(mime)
        uploaded = await sender.client.upload_file(file, file_name=f"avatar{ext}")
        photo = InputChatUploadedPhoto(file=uploaded)

        if self.peer_type == "chat":
            response = await sender.client(EditChatPhotoRequest(chat_id=self.tgid, photo=photo))
        else:
            channel = await self.get_input_entity(sender)
            response = await sender.client(EditPhotoRequest(channel=channel, photo=photo))
        self.dedup.register_outgoing_actions(response)
        for update in response.updates:
            is_photo_update = (isinstance(update, UpdateNewMessage)
                               and isinstance(update.message, MessageService)
                               and isinstance(update.message.action, MessageActionChatEditPhoto))
            if is_photo_update:
                loc, size = self._get_largest_photo_size(update.message.action.photo)
                self.photo_id = f"{size.location.volume_id}-{size.location.local_id}"
                self.save()
                break
Exemple #2
0
async def update_profile_pic(event):
    if event.reply:
        message = await event.get_reply_message()
        chat = await event.get_chat()
        if not chat.admin_rights or not chat.creator:
            await client.update_message(
                event, "`Chat admin privileges are required to do that`")
            return
        photo = None
        if message.media:
            if isinstance(message.media, MessageMediaPhoto):
                await client.update_message(event, "`DOWNLOADING`")
                photo = await client.download_media(message.photo)

            elif isinstance(message.media, MessageMediaDocument):
                mime_type = message.media.document.mime_type.split("/")
                media_type = mime_type[0]
                media_ext = mime_type[1]

                if media_type == "image":
                    await client.update_message(event, "`DOWNLOADING`")
                    photo = await client.download_file(message.media.document)
                    photo = io.BytesIO(photo)
                    photo.name = "image." + media_ext

            else:
                await client.update_message(
                    event, "`The type of this media entity is invalid.`")

        if photo:
            await client.update_message(event, "`UPLOADING`")
            file = await client.upload_file(photo)
            try:
                await client(EditChatPhotoRequest(chat.id, file))
                await client.update_message(event, "`Channel picture changed`")

            except Exception as exc:
                if isinstance(exc, errors.PhotoInvalidError):
                    await client.update_message(
                        event, "`The selected photo is invalid`")

            if isinstance(photo, str):
                os.remove(photo)