async def delete_profile_photos(self, photo_ids: Union[str,
                                                           List[str]]) -> bool:
        """Delete your own profile photos.

        Parameters:
            photo_ids (``str`` | List of ``str``):
                A single :obj:`Photo` id as string or multiple ids as list of strings for deleting
                more than one photos at once.

        Returns:
            ``bool``: True on success.

        Raises:
            RPCError: In case of a Telegram RPC error.
        """
        photo_ids = photo_ids if isinstance(photo_ids, list) else [photo_ids]
        input_photos = []

        for photo_id in photo_ids:
            unpacked = unpack("<iiqqc", utils.decode(photo_id))

            input_photos.append(
                types.InputPhoto(id=unpacked[2],
                                 access_hash=unpacked[3],
                                 file_reference=b""))

        return bool(await
                    self.send(functions.photos.DeletePhotos(id=input_photos)))
    def write(self):
        try:
            decoded = utils.decode(self.audio_file_id)
            fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq"
            unpacked = struct.unpack(fmt, decoded)
        except (AssertionError, binascii.Error, struct.error):
            raise FileIdInvalid from None
        else:
            if unpacked[0] != 9:
                media_type = BaseClient.MEDIA_TYPE_ID.get(unpacked[0], None)

                if media_type:
                    raise FileIdInvalid(
                        "The file_id belongs to a {}".format(media_type))
                else:
                    raise FileIdInvalid("Unknown media type: {}".format(
                        unpacked[0]))

        audio = types.InputDocument(id=unpacked[2], access_hash=unpacked[3])

        return types.InputBotInlineResultDocument(
            id=self.id,
            type="audio",
            document=audio,
            send_message=types.InputBotInlineMessageMediaAuto(
                reply_markup=self.reply_markup.write()
                if self.reply_markup else None,
                **self.style.parse(self.caption)))
Example #3
0
    async def download_media(
        self,
        message: Union["pyrogram.Message", str],
        file_name: str = DEFAULT_DOWNLOAD_DIR,
        block: bool = True,
        progress: callable = None,
        progress_args: tuple = ()) -> Union[str, None]:
        """Download the media from a message.

        Parameters:
            message (:obj:`Message` | ``str``):
                Pass a Message containing the media, the media itself (message.audio, message.video, ...) or
                the file id as string.

            file_name (``str``, *optional*):
                A custom *file_name* to be used instead of the one provided by Telegram.
                By default, all files are downloaded in the *downloads* folder in your working directory.
                You can also specify a path for downloading files in a custom location: paths that end with "/"
                are considered directories. All non-existent folders will be created automatically.

            block (``bool``, *optional*):
                Blocks the code execution until the file has been downloaded.
                Defaults to True.

            progress (``callable``):
                Pass a callback function to view the download progress.
                The function must take *(client, current, total, \*args)* as positional arguments (look at the section
                below for a detailed description).

            progress_args (``tuple``):
                Extra custom arguments for the progress callback function. Useful, for example, if you want to pass
                a chat_id and a message_id in order to edit a message with the updated progress.

        Other Parameters:
            client (:obj:`Client`):
                The Client itself, useful when you want to call other API methods inside the callback function.

            current (``int``):
                The amount of bytes downloaded so far.

            total (``int``):
                The size of the file.

            *args (``tuple``, *optional*):
                Extra custom arguments as defined in the *progress_args* parameter.
                You can either keep *\*args* or add every single extra argument in your function signature.

        Returns:
            ``str`` | ``None``: On success, the absolute path of the downloaded file is returned, otherwise, in case
            the download failed or was deliberately stopped with :meth:`~Client.stop_transmission`, None is returned.

        Raises:
            RPCError: In case of a Telegram RPC error.
            ``ValueError`` if the message doesn't contain any downloadable media
        """
        error_message = "This message doesn't contain any downloadable media"
        available_media = ("audio", "document", "photo", "sticker",
                           "animation", "video", "voice", "video_note")

        media_file_name = None
        file_size = None
        mime_type = None
        date = None

        if isinstance(message, pyrogram.Message):
            for kind in available_media:
                media = getattr(message, kind, None)

                if media is not None:
                    break
            else:
                raise ValueError(error_message)
        else:
            media = message

        if isinstance(media, str):
            file_id_str = media
        else:
            file_id_str = media.file_id
            media_file_name = getattr(media, "file_name", "")
            file_size = getattr(media, "file_size", None)
            mime_type = getattr(media, "mime_type", None)
            date = getattr(media, "date", None)

        data = FileData(file_name=media_file_name,
                        file_size=file_size,
                        mime_type=mime_type,
                        date=date)

        def get_existing_attributes() -> dict:
            return dict(
                filter(lambda x: x[1] is not None, data.__dict__.items()))

        try:
            decoded = utils.decode(file_id_str)
            media_type = decoded[0]

            if media_type == 1:
                unpacked = struct.unpack("<iiqqib", decoded)
                dc_id, peer_id, volume_id, local_id, is_big = unpacked[1:]

                data = FileData(**get_existing_attributes(),
                                media_type=media_type,
                                dc_id=dc_id,
                                peer_id=peer_id,
                                volume_id=volume_id,
                                local_id=local_id,
                                is_big=bool(is_big))
            elif media_type in (0, 2, 14):
                unpacked = struct.unpack("<iiqqc", decoded)
                dc_id, document_id, access_hash, thumb_size = unpacked[1:]

                data = FileData(**get_existing_attributes(),
                                media_type=media_type,
                                dc_id=dc_id,
                                document_id=document_id,
                                access_hash=access_hash,
                                thumb_size=thumb_size.decode())
            elif media_type in (3, 4, 5, 8, 9, 10, 13):
                unpacked = struct.unpack("<iiqq", decoded)
                dc_id, document_id, access_hash = unpacked[1:]

                data = FileData(**get_existing_attributes(),
                                media_type=media_type,
                                dc_id=dc_id,
                                document_id=document_id,
                                access_hash=access_hash)
            else:
                raise ValueError("Unknown media type: {}".format(file_id_str))
        except (AssertionError, binascii.Error, struct.error):
            raise FileIdInvalid from None

        done = asyncio.Event()
        path = [None]

        directory, file_name = os.path.split(file_name)
        file_name = file_name or data.file_name or ""

        if not os.path.isabs(file_name):
            directory = self.PARENT_DIR / (directory or DEFAULT_DOWNLOAD_DIR)

        media_type_str = self.MEDIA_TYPE_ID[data.media_type]

        if not file_name:
            guessed_extension = self.guess_extension(data.mime_type)

            if data.media_type in (0, 1, 2, 14):
                extension = ".jpg"
            elif data.media_type == 3:
                extension = guessed_extension or ".ogg"
            elif data.media_type in (4, 10, 13):
                extension = guessed_extension or ".mp4"
            elif data.media_type == 5:
                extension = guessed_extension or ".zip"
            elif data.media_type == 8:
                extension = guessed_extension or ".webp"
            elif data.media_type == 9:
                extension = guessed_extension or ".mp3"
            else:
                extension = ".unknown"

            file_name = "{}_{}_{}{}".format(
                media_type_str,
                datetime.fromtimestamp(
                    data.date or time.time()).strftime("%Y-%m-%d_%H-%M-%S"),
                self.rnd_id(), extension)

        self.download_queue.put_nowait(
            (data, directory, file_name, done, progress, progress_args, path))

        if block:
            await done.wait()

        return path[0]
Example #4
0
    def send_voice(
        self,
        chat_id: Union[int, str],
        voice: str,
        caption: str = "",
        parse_mode: str = "",
        duration: int = 0,
        disable_notification: bool = None,
        reply_to_message_id: int = None,
        reply_markup: Union["pyrogram.InlineKeyboardMarkup",
                            "pyrogram.ReplyKeyboardMarkup",
                            "pyrogram.ReplyKeyboardRemove",
                            "pyrogram.ForceReply"] = None,
        progress: callable = None,
        progress_args: tuple = ()
    ) -> Union["pyrogram.Message", None]:
        """Use this method to send audio files.

        Args:
            chat_id (``int`` | ``str``):
                Unique identifier (int) or username (str) of the target chat.
                For your personal cloud (Saved Messages) you can simply use "me" or "self".
                For a contact that exists in your Telegram address book you can use his phone number (str).

            voice (``str``):
                Audio file to send.
                Pass a file_id as string to send an audio that exists on the Telegram servers,
                pass an HTTP URL as a string for Telegram to get an audio from the Internet, or
                pass a file path as string to upload a new audio that exists on your local machine.

            caption (``str``, *optional*):
                Voice message caption, 0-1024 characters.

            parse_mode (``str``, *optional*):
                Use :obj:`MARKDOWN <pyrogram.ParseMode.MARKDOWN>` or :obj:`HTML <pyrogram.ParseMode.HTML>`
                if you want Telegram apps to show bold, italic, fixed-width text or inline URLs in your caption.
                Defaults to Markdown.

            duration (``int``, *optional*):
                Duration of the voice message in seconds.

            disable_notification (``bool``, *optional*):
                Sends the message silently.
                Users will receive a notification with no sound.

            reply_to_message_id (``int``, *optional*):
                If the message is a reply, ID of the original message

            reply_markup (:obj:`InlineKeyboardMarkup` | :obj:`ReplyKeyboardMarkup` | :obj:`ReplyKeyboardRemove` | :obj:`ForceReply`, *optional*):
                Additional interface options. An object for an inline keyboard, custom reply keyboard,
                instructions to remove reply keyboard or to force a reply from the user.

            progress (``callable``, *optional*):
                Pass a callback function to view the upload progress.
                The function must take *(client, current, total, \*args)* as positional arguments (look at the section
                below for a detailed description).

            progress_args (``tuple``, *optional*):
                Extra custom arguments for the progress callback function. Useful, for example, if you want to pass
                a chat_id and a message_id in order to edit a message with the updated progress.

        Other Parameters:
            client (:obj:`Client <pyrogram.Client>`):
                The Client itself, useful when you want to call other API methods inside the callback function.

            current (``int``):
                The amount of bytes uploaded so far.

            total (``int``):
                The size of the file.

            *args (``tuple``, *optional*):
                Extra custom arguments as defined in the *progress_args* parameter.
                You can either keep *\*args* or add every single extra argument in your function signature.

        Returns:
            On success, the sent :obj:`Message <pyrogram.Message>` is returned.
            In case the upload is deliberately stopped with :meth:`stop_transmission`, None is returned instead.

        Raises:
            :class:`Error <pyrogram.Error>` in case of a Telegram RPC error.
        """
        file = None
        style = self.html if parse_mode.lower() == "html" else self.markdown

        try:
            if os.path.exists(voice):
                file = self.save_file(voice,
                                      progress=progress,
                                      progress_args=progress_args)
                media = types.InputMediaUploadedDocument(
                    mime_type=mimetypes.types_map.get(
                        "." + voice.split(".")[-1], "audio/mpeg"),
                    file=file,
                    attributes=[
                        types.DocumentAttributeAudio(voice=True,
                                                     duration=duration)
                    ])
            elif voice.startswith("http"):
                media = types.InputMediaDocumentExternal(url=voice)
            else:
                try:
                    decoded = utils.decode(voice)
                    fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq"
                    unpacked = struct.unpack(fmt, decoded)
                except (AssertionError, binascii.Error, struct.error):
                    raise FileIdInvalid from None
                else:
                    if unpacked[0] != 3:
                        media_type = BaseClient.MEDIA_TYPE_ID.get(
                            unpacked[0], None)

                        if media_type:
                            raise FileIdInvalid(
                                "The file_id belongs to a {}".format(
                                    media_type))
                        else:
                            raise FileIdInvalid(
                                "Unknown media type: {}".format(unpacked[0]))

                    media = types.InputMediaDocument(
                        id=types.InputDocument(id=unpacked[2],
                                               access_hash=unpacked[3],
                                               file_reference=b""))

            while True:
                try:
                    r = self.send(
                        functions.messages.SendMedia(
                            peer=self.resolve_peer(chat_id),
                            media=media,
                            silent=disable_notification or None,
                            reply_to_msg_id=reply_to_message_id,
                            random_id=self.rnd_id(),
                            reply_markup=reply_markup.write()
                            if reply_markup else None,
                            **style.parse(caption)))
                except FilePartMissing as e:
                    self.save_file(voice, file_id=file.id, file_part=e.x)
                else:
                    for i in r.updates:
                        if isinstance(i, (types.UpdateNewMessage,
                                          types.UpdateNewChannelMessage)):
                            return pyrogram.Message._parse(
                                self, i.message, {i.id: i
                                                  for i in r.users},
                                {i.id: i
                                 for i in r.chats})
        except BaseClient.StopTransmission:
            return None
    def send_media_group(self,
                         chat_id: int or str,
                         media: list,
                         disable_notification: bool = None,
                         reply_to_message_id: int = None):
        """Use this method to send a group of photos or videos as an album.
        On success, an Update containing the sent Messages is returned.

        Args:
            chat_id (``int`` | ``str``):
                Unique identifier (int) or username (str) of the target chat.
                For your personal cloud (Saved Messages) you can simply use "me" or "self".
                For a contact that exists in your Telegram address book you can use his phone number (str).
                For a private channel/supergroup you can use its *t.me/joinchat/* link.

            media (``list``):
                A list containing either :obj:`InputMediaPhoto <pyrogram.InputMediaPhoto>` or
                :obj:`InputMediaVideo <pyrogram.InputMediaVideo>` objects
                describing photos and videos to be sent, must include 2–10 items.

            disable_notification (``bool``, *optional*):
                Sends the message silently.
                Users will receive a notification with no sound.

            reply_to_message_id (``int``, *optional*):
                If the message is a reply, ID of the original message.
        """
        multi_media = []

        for i in media:
            style = self.html if i.parse_mode.lower() == "html" else self.markdown

            if isinstance(i, pyrogram_types.InputMediaPhoto):
                if os.path.exists(i.media):
                    media = self.send(
                        functions.messages.UploadMedia(
                            peer=self.resolve_peer(chat_id),
                            media=types.InputMediaUploadedPhoto(
                                file=self.save_file(i.media)
                            )
                        )
                    )

                    media = types.InputMediaPhoto(
                        id=types.InputPhoto(
                            id=media.photo.id,
                            access_hash=media.photo.access_hash
                        )
                    )
                else:
                    try:
                        decoded = utils.decode(i.media)
                        fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq"
                        unpacked = struct.unpack(fmt, decoded)
                    except (AssertionError, binascii.Error, struct.error):
                        raise FileIdInvalid from None
                    else:
                        if unpacked[0] != 2:
                            media_type = BaseClient.MEDIA_TYPE_ID.get(unpacked[0], None)

                            if media_type:
                                raise FileIdInvalid("The file_id belongs to a {}".format(media_type))
                            else:
                                raise FileIdInvalid("Unknown media type: {}".format(unpacked[0]))

                        media = types.InputMediaPhoto(
                            id=types.InputPhoto(
                                id=unpacked[2],
                                access_hash=unpacked[3]
                            )
                        )
            elif isinstance(i, pyrogram_types.InputMediaVideo):
                if os.path.exists(i.media):
                    media = self.send(
                        functions.messages.UploadMedia(
                            peer=self.resolve_peer(chat_id),
                            media=types.InputMediaUploadedDocument(
                                file=self.save_file(i.media),
                                mime_type=mimetypes.types_map[".mp4"],
                                attributes=[
                                    types.DocumentAttributeVideo(
                                        supports_streaming=i.supports_streaming or None,
                                        duration=i.duration,
                                        w=i.width,
                                        h=i.height
                                    ),
                                    types.DocumentAttributeFilename(os.path.basename(i.media))
                                ]
                            )
                        )
                    )

                    media = types.InputMediaDocument(
                        id=types.InputDocument(
                            id=media.document.id,
                            access_hash=media.document.access_hash
                        )
                    )
                else:
                    try:
                        decoded = utils.decode(i.media)
                        fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq"
                        unpacked = struct.unpack(fmt, decoded)
                    except (AssertionError, binascii.Error, struct.error):
                        raise FileIdInvalid from None
                    else:
                        if unpacked[0] != 4:
                            media_type = BaseClient.MEDIA_TYPE_ID.get(unpacked[0], None)

                            if media_type:
                                raise FileIdInvalid("The file_id belongs to a {}".format(media_type))
                            else:
                                raise FileIdInvalid("Unknown media type: {}".format(unpacked[0]))

                        media = types.InputMediaDocument(
                            id=types.InputDocument(
                                id=unpacked[2],
                                access_hash=unpacked[3]
                            )
                        )

            multi_media.append(
                types.InputSingleMedia(
                    media=media,
                    random_id=self.rnd_id(),
                    **style.parse(i.caption)
                )
            )

        return self.send(
            functions.messages.SendMultiMedia(
                peer=self.resolve_peer(chat_id),
                multi_media=multi_media,
                silent=disable_notification or None,
                reply_to_msg_id=reply_to_message_id
            )
        )
Example #6
0
    def send_sticker(self,
                     chat_id: int or str,
                     sticker: str,
                     disable_notification: bool = None,
                     reply_to_message_id: int = None,
                     reply_markup=None,
                     progress: callable = None,
                     progress_args: tuple = ()):
        """Use this method to send .webp stickers.

        Args:
            chat_id (``int`` | ``str``):
                Unique identifier (int) or username (str) of the target chat.
                For your personal cloud (Saved Messages) you can simply use "me" or "self".
                For a contact that exists in your Telegram address book you can use his phone number (str).

            sticker (``str``):
                Sticker to send.
                Pass a file_id as string to send a sticker that exists on the Telegram servers,
                pass an HTTP URL as a string for Telegram to get a .webp sticker file from the Internet, or
                pass a file path as string to upload a new sticker that exists on your local machine.

            disable_notification (``bool``, *optional*):
                Sends the message silently.
                Users will receive a notification with no sound.

            reply_to_message_id (``int``, *optional*):
                If the message is a reply, ID of the original message.

            reply_markup (:obj:`InlineKeyboardMarkup` | :obj:`ReplyKeyboardMarkup` | :obj:`ReplyKeyboardRemove` | :obj:`ForceReply`, *optional*):
                Additional interface options. An object for an inline keyboard, custom reply keyboard,
                instructions to remove reply keyboard or to force a reply from the user.

            progress (``callable``, *optional*):
                Pass a callback function to view the upload progress.
                The function must take *(client, current, total, \*args)* as positional arguments (look at the section
                below for a detailed description).

            progress_args (``tuple``, *optional*):
                Extra custom arguments for the progress callback function. Useful, for example, if you want to pass
                a chat_id and a message_id in order to edit a message with the updated progress.

        Other Parameters:
            client (:obj:`Client <pyrogram.Client>`):
                The Client itself, useful when you want to call other API methods inside the callback function.

            current (``int``):
                The amount of bytes uploaded so far.

            total (``int``):
                The size of the file.

            *args (``tuple``, *optional*):
                Extra custom arguments as defined in the *progress_args* parameter.
                You can either keep *\*args* or add every single extra argument in your function signature.

        Returns:
            On success, the sent :obj:`Message <pyrogram.Message>` is returned.

        Raises:
            :class:`Error <pyrogram.Error>` in case of a Telegram RPC error.
        """
        file = None

        if os.path.exists(sticker):
            file = self.save_file(sticker,
                                  progress=progress,
                                  progress_args=progress_args)
            media = types.InputMediaUploadedDocument(
                mime_type="image/webp",
                file=file,
                attributes=[
                    types.DocumentAttributeFilename(os.path.basename(sticker))
                ])
        elif sticker.startswith("http"):
            media = types.InputMediaDocumentExternal(url=sticker)
        else:
            try:
                decoded = utils.decode(sticker)
                fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq"
                unpacked = struct.unpack(fmt, decoded)
            except (AssertionError, binascii.Error, struct.error):
                raise FileIdInvalid from None
            else:
                if unpacked[0] != 8:
                    media_type = BaseClient.MEDIA_TYPE_ID.get(
                        unpacked[0], None)

                    if media_type:
                        raise FileIdInvalid(
                            "The file_id belongs to a {}".format(media_type))
                    else:
                        raise FileIdInvalid("Unknown media type: {}".format(
                            unpacked[0]))

                media = types.InputMediaDocument(id=types.InputDocument(
                    id=unpacked[2], access_hash=unpacked[3]))

        while True:
            try:
                r = self.send(
                    functions.messages.SendMedia(
                        peer=self.resolve_peer(chat_id),
                        media=media,
                        silent=disable_notification or None,
                        reply_to_msg_id=reply_to_message_id,
                        random_id=self.rnd_id(),
                        reply_markup=reply_markup.write()
                        if reply_markup else None,
                        message=""))
            except FilePartMissing as e:
                self.save_file(sticker, file_id=file.id, file_part=e.x)
            else:
                for i in r.updates:
                    if isinstance(i, (types.UpdateNewMessage,
                                      types.UpdateNewChannelMessage)):
                        return utils.parse_messages(self, i.message,
                                                    {i.id: i
                                                     for i in r.users},
                                                    {i.id: i
                                                     for i in r.chats})
Example #7
0
    def send_gif(self,
                 chat_id: int or str,
                 gif: str,
                 caption: str = "",
                 parse_mode: str = "",
                 duration: int = 0,
                 width: int = 0,
                 height: int = 0,
                 thumb: str = None,
                 disable_notification: bool = None,
                 reply_to_message_id: int = None,
                 reply_markup=None,
                 progress: callable = None,
                 progress_args: tuple = ()):
        """Use this method to send GIF files.

        Args:
            chat_id (``int`` | ``str``):
                Unique identifier (int) or username (str) of the target chat.
                For your personal cloud (Saved Messages) you can simply use "me" or "self".
                For a contact that exists in your Telegram address book you can use his phone number (str).
                For a private channel/supergroup you can use its *t.me/joinchat/* link.

            gif (``str``):
                GIF to send.
                Pass a file_id as string to send a GIF that exists on the Telegram servers,
                pass an HTTP URL as a string for Telegram to get a GIF from the Internet, or
                pass a file path as string to upload a new GIF that exists on your local machine.

            caption (``str``, *optional*):
                GIF caption, 0-200 characters.

            parse_mode (``str``, *optional*):
                Use :obj:`MARKDOWN <pyrogram.ParseMode.MARKDOWN>` or :obj:`HTML <pyrogram.ParseMode.HTML>`
                if you want Telegram apps to show bold, italic, fixed-width text or inline URLs in your caption.
                Defaults to Markdown.

            duration (``int``, *optional*):
                Duration of sent GIF in seconds.

            width (``int``, *optional*):
                GIF width.

            height (``int``, *optional*):
                GIF height.

            thumb (``str``, *optional*):
                GIF thumbnail.
                Pass a file path as string to send an image that exists on your local machine.
                Thumbnail should have 90 or less pixels of width and 90 or less pixels of height.

            disable_notification (``bool``, *optional*):
                Sends the message silently.
                Users will receive a notification with no sound.

            reply_to_message_id (``int``, *optional*):
                If the message is a reply, ID of the original message.

            reply_markup (:obj:`InlineKeyboardMarkup` | :obj:`ReplyKeyboardMarkup` | :obj:`ReplyKeyboardRemove` | :obj:`ForceReply`, *optional*):
                Additional interface options. An object for an inline keyboard, custom reply keyboard,
                instructions to remove reply keyboard or to force a reply from the user.

            progress (``callable``, *optional*):
                Pass a callback function to view the upload progress.
                The function must take *(client, current, total, \*args)* as positional arguments (look at the section
                below for a detailed description).

            progress_args (``tuple``, *optional*):
                Extra custom arguments for the progress callback function. Useful, for example, if you want to pass
                a chat_id and a message_id in order to edit a message with the updated progress.

        Other Parameters:
            client (:obj:`Client <pyrogram.Client>`):
                The Client itself, useful when you want to call other API methods inside the callback function.

            current (``int``):
                The amount of bytes uploaded so far.

            total (``int``):
                The size of the file.

            *args (``tuple``, *optional*):
                Extra custom arguments as defined in the *progress_args* parameter.
                You can either keep *\*args* or add every single extra argument in your function signature.

        Returns:
            On success, the sent :obj:`Message <pyrogram.Message>` is returned.

        Raises:
            :class:`Error <pyrogram.Error>`
        """
        file = None
        style = self.html if parse_mode.lower() == "html" else self.markdown

        if os.path.exists(gif):
            thumb = None if thumb is None else self.save_file(thumb)
            file = self.save_file(gif,
                                  progress=progress,
                                  progress_args=progress_args)
            media = types.InputMediaUploadedDocument(
                mime_type=mimetypes.types_map[".mp4"],
                file=file,
                thumb=thumb,
                attributes=[
                    types.DocumentAttributeVideo(supports_streaming=True,
                                                 duration=duration,
                                                 w=width,
                                                 h=height),
                    types.DocumentAttributeFilename(os.path.basename(gif)),
                    types.DocumentAttributeAnimated()
                ])
        elif gif.startswith("http"):
            media = types.InputMediaDocumentExternal(url=gif)
        else:
            try:
                decoded = utils.decode(gif)
                fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq"
                unpacked = struct.unpack(fmt, decoded)
            except (AssertionError, binascii.Error, struct.error):
                raise FileIdInvalid from None
            else:
                if unpacked[0] != 10:
                    media_type = BaseClient.MEDIA_TYPE_ID.get(
                        unpacked[0], None)

                    if media_type:
                        raise FileIdInvalid(
                            "The file_id belongs to a {}".format(media_type))
                    else:
                        raise FileIdInvalid("Unknown media type: {}".format(
                            unpacked[0]))

                media = types.InputMediaDocument(id=types.InputDocument(
                    id=unpacked[2], access_hash=unpacked[3]))

        while True:
            try:
                r = self.send(
                    functions.messages.SendMedia(
                        peer=self.resolve_peer(chat_id),
                        media=media,
                        silent=disable_notification or None,
                        reply_to_msg_id=reply_to_message_id,
                        random_id=self.rnd_id(),
                        reply_markup=reply_markup.write()
                        if reply_markup else None,
                        **style.parse(caption)))
            except FilePartMissing as e:
                self.save_file(gif, file_id=file.id, file_part=e.x)
            else:
                for i in r.updates:
                    if isinstance(i, (types.UpdateNewMessage,
                                      types.UpdateNewChannelMessage)):
                        return utils.parse_messages(self, i.message,
                                                    {i.id: i
                                                     for i in r.users},
                                                    {i.id: i
                                                     for i in r.chats})
Example #8
0
    def send_photo(self,
                   chat_id: int or str,
                   photo: str,
                   caption: str = "",
                   parse_mode: str = "",
                   ttl_seconds: int = None,
                   disable_notification: bool = None,
                   reply_to_message_id: int = None,
                   reply_markup=None,
                   progress: callable = None,
                   progress_args: tuple = ()):
        """Use this method to send photos.

        Args:
            chat_id (``int`` | ``str``):
                Unique identifier (int) or username (str) of the target chat.
                For your personal cloud (Saved Messages) you can simply use "me" or "self".
                For a contact that exists in your Telegram address book you can use his phone number (str).

            photo (``str``):
                Photo to send.
                Pass a file_id as string to send a photo that exists on the Telegram servers,
                pass an HTTP URL as a string for Telegram to get a photo from the Internet, or
                pass a file path as string to upload a new photo that exists on your local machine.

            caption (``bool``, *optional*):
                Photo caption, 0-200 characters.

            parse_mode (``str``, *optional*):
                Use :obj:`MARKDOWN <pyrogram.ParseMode.MARKDOWN>` or :obj:`HTML <pyrogram.ParseMode.HTML>`
                if you want Telegram apps to show bold, italic, fixed-width text or inline URLs in your caption.
                Defaults to Markdown.

            ttl_seconds (``int``, *optional*):
                Self-Destruct Timer.
                If you set a timer, the photo will self-destruct in *ttl_seconds*
                seconds after it was viewed.

            disable_notification (``bool``, *optional*):
                Sends the message silently.
                Users will receive a notification with no sound.

            reply_to_message_id (``int``, *optional*):
                If the message is a reply, ID of the original message.

            reply_markup (:obj:`InlineKeyboardMarkup` | :obj:`ReplyKeyboardMarkup` | :obj:`ReplyKeyboardRemove` | :obj:`ForceReply`, *optional*):
                Additional interface options. An object for an inline keyboard, custom reply keyboard,
                instructions to remove reply keyboard or to force a reply from the user.

            progress (``callable``, *optional*):
                Pass a callback function to view the upload progress.
                The function must take *(client, current, total, \*args)* as positional arguments (look at the section
                below for a detailed description).

            progress_args (``tuple``, *optional*):
                Extra custom arguments for the progress callback function. Useful, for example, if you want to pass
                a chat_id and a message_id in order to edit a message with the updated progress.

        Other Parameters:
            client (:obj:`Client <pyrogram.Client>`):
                The Client itself, useful when you want to call other API methods inside the callback function.

            current (``int``):
                The amount of bytes uploaded so far.

            total (``int``):
                The size of the file.

            *args (``tuple``, *optional*):
                Extra custom arguments as defined in the *progress_args* parameter.
                You can either keep *\*args* or add every single extra argument in your function signature.

        Returns:
            On success, the sent :obj:`Message <pyrogram.Message>` is returned.

        Raises:
            :class:`Error <pyrogram.Error>`
        """
        file = None
        style = self.html if parse_mode.lower() == "html" else self.markdown

        if os.path.exists(photo):
            file = self.save_file(photo,
                                  progress=progress,
                                  progress_args=progress_args)
            media = types.InputMediaUploadedPhoto(file=file,
                                                  ttl_seconds=ttl_seconds)
        elif photo.startswith("http"):
            media = types.InputMediaPhotoExternal(url=photo,
                                                  ttl_seconds=ttl_seconds)
        else:
            try:
                decoded = utils.decode(photo)
                fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq"
                unpacked = struct.unpack(fmt, decoded)
            except (AssertionError, binascii.Error, struct.error):
                raise FileIdInvalid from None
            else:
                if unpacked[0] != 2:
                    media_type = BaseClient.MEDIA_TYPE_ID.get(
                        unpacked[0], None)

                    if media_type:
                        raise FileIdInvalid(
                            "The file_id belongs to a {}".format(media_type))
                    else:
                        raise FileIdInvalid("Unknown media type: {}".format(
                            unpacked[0]))

                media = types.InputMediaPhoto(id=types.InputPhoto(
                    id=unpacked[2], access_hash=unpacked[3]),
                                              ttl_seconds=ttl_seconds)

        while True:
            try:
                r = self.send(
                    functions.messages.SendMedia(
                        peer=self.resolve_peer(chat_id),
                        media=media,
                        silent=disable_notification or None,
                        reply_to_msg_id=reply_to_message_id,
                        random_id=self.rnd_id(),
                        reply_markup=reply_markup.write()
                        if reply_markup else None,
                        **style.parse(caption)))
            except FilePartMissing as e:
                self.save_file(photo, file_id=file.id, file_part=e.x)
            else:
                for i in r.updates:
                    if isinstance(i, (types.UpdateNewMessage,
                                      types.UpdateNewChannelMessage)):
                        return utils.parse_messages(self, i.message,
                                                    {i.id: i
                                                     for i in r.users},
                                                    {i.id: i
                                                     for i in r.chats})
Example #9
0
    async def send_video_note(
        self,
        chat_id: Union[int, str],
        video_note: str,
        duration: int = 0,
        length: int = 1,
        thumb: str = None,
        disable_notification: bool = None,
        reply_to_message_id: int = None,
        reply_markup: Union["pyrogram.InlineKeyboardMarkup",
                            "pyrogram.ReplyKeyboardMarkup",
                            "pyrogram.ReplyKeyboardRemove",
                            "pyrogram.ForceReply"] = None,
        progress: callable = None,
        progress_args: tuple = ()
    ) -> Union["pyrogram.Message", None]:
        """Use this method to send video messages.

        Args:
            chat_id (``int`` | ``str``):
                Unique identifier (int) or username (str) of the target chat.
                For your personal cloud (Saved Messages) you can simply use "me" or "self".
                For a contact that exists in your Telegram address book you can use his phone number (str).

            video_note (``str``):
                Video note to send.
                Pass a file_id as string to send a video note that exists on the Telegram servers, or
                pass a file path as string to upload a new video note that exists on your local machine.
                Sending video notes by a URL is currently unsupported.

            duration (``int``, *optional*):
                Duration of sent video in seconds.

            length (``int``, *optional*):
                Video width and height.

            thumb (``str``, *optional*):
                Thumbnail of the video sent.
                The thumbnail should be in JPEG format and less than 200 KB in size.
                A thumbnail's width and height should not exceed 90 pixels.
                Thumbnails can't be reused and can be only uploaded as a new file.

            disable_notification (``bool``, *optional*):
                Sends the message silently.
                Users will receive a notification with no sound.

            reply_to_message_id (``int``, *optional*):
                If the message is a reply, ID of the original message

            reply_markup (:obj:`InlineKeyboardMarkup` | :obj:`ReplyKeyboardMarkup` | :obj:`ReplyKeyboardRemove` | :obj:`ForceReply`, *optional*):
                Additional interface options. An object for an inline keyboard, custom reply keyboard,
                instructions to remove reply keyboard or to force a reply from the user.

            progress (``callable``, *optional*):
                Pass a callback function to view the upload progress.
                The function must take *(client, current, total, \*args)* as positional arguments (look at the section
                below for a detailed description).

            progress_args (``tuple``, *optional*):
                Extra custom arguments for the progress callback function. Useful, for example, if you want to pass
                a chat_id and a message_id in order to edit a message with the updated progress.

        Other Parameters:
            client (:obj:`Client <pyrogram.Client>`):
                The Client itself, useful when you want to call other API methods inside the callback function.

            current (``int``):
                The amount of bytes uploaded so far.

            total (``int``):
                The size of the file.

            *args (``tuple``, *optional*):
                Extra custom arguments as defined in the *progress_args* parameter.
                You can either keep *\*args* or add every single extra argument in your function signature.

        Returns:
            On success, the sent :obj:`Message <pyrogram.Message>` is returned.
            In case the upload is deliberately stopped with :meth:`stop_transmission`, None is returned instead.

        Raises:
            :class:`RPCError <pyrogram.RPCError>` in case of a Telegram RPC error.
        """
        file = None

        try:
            if os.path.exists(video_note):
                thumb = None if thumb is None else await self.save_file(thumb)
                file = await self.save_file(video_note,
                                            progress=progress,
                                            progress_args=progress_args)
                media = types.InputMediaUploadedDocument(
                    mime_type="video/mp4",
                    file=file,
                    thumb=thumb,
                    attributes=[
                        types.DocumentAttributeVideo(round_message=True,
                                                     duration=duration,
                                                     w=length,
                                                     h=length)
                    ])
            else:
                try:
                    decoded = utils.decode(video_note)
                    fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq"
                    unpacked = struct.unpack(fmt, decoded)
                except (AssertionError, binascii.Error, struct.error):
                    raise FileIdInvalid from None
                else:
                    if unpacked[0] != 13:
                        media_type = BaseClient.MEDIA_TYPE_ID.get(
                            unpacked[0], None)

                        if media_type:
                            raise FileIdInvalid(
                                "The file_id belongs to a {}".format(
                                    media_type))
                        else:
                            raise FileIdInvalid(
                                "Unknown media type: {}".format(unpacked[0]))

                    media = types.InputMediaDocument(
                        id=types.InputDocument(id=unpacked[2],
                                               access_hash=unpacked[3],
                                               file_reference=b""))

            while True:
                try:
                    r = await self.send(
                        functions.messages.SendMedia(
                            peer=await self.resolve_peer(chat_id),
                            media=media,
                            silent=disable_notification or None,
                            reply_to_msg_id=reply_to_message_id,
                            random_id=self.rnd_id(),
                            reply_markup=reply_markup.write()
                            if reply_markup else None,
                            message=""))
                except FilePartMissing as e:
                    await self.save_file(video_note,
                                         file_id=file.id,
                                         file_part=e.x)
                else:
                    for i in r.updates:
                        if isinstance(i, (types.UpdateNewMessage,
                                          types.UpdateNewChannelMessage)):
                            return await pyrogram.Message._parse(
                                self, i.message, {i.id: i
                                                  for i in r.users},
                                {i.id: i
                                 for i in r.chats})
        except BaseClient.StopTransmission:
            return None
Example #10
0
    def send_media_group(self,
                         chat_id: Union[int, str],
                         media: List[Union["pyrogram.InputMediaPhoto",
                                           "pyrogram.InputMediaVideo"]],
                         disable_notification: bool = None,
                         reply_to_message_id: int = None):
        """Use this method to send a group of photos or videos as an album.

        Args:
            chat_id (``int`` | ``str``):
                Unique identifier (int) or username (str) of the target chat.
                For your personal cloud (Saved Messages) you can simply use "me" or "self".
                For a contact that exists in your Telegram address book you can use his phone number (str).

            media (List of :obj:`InputMediaPhoto` and :obj:`InputMediaVideo`):
                A list describing photos and videos to be sent, must include 2–10 items.

            disable_notification (``bool``, *optional*):
                Sends the message silently.
                Users will receive a notification with no sound.

            reply_to_message_id (``int``, *optional*):
                If the message is a reply, ID of the original message.

        Returns:
            On success, a :obj:`Messages <pyrogram.Messages>` object is returned containing all the
            single messages sent.

        Raises:
            :class:`RPCError <pyrogram.RPCError>` in case of a Telegram RPC error.
        """
        multi_media = []

        for i in media:
            style = self.html if i.parse_mode.lower(
            ) == "html" else self.markdown

            if isinstance(i, pyrogram.InputMediaPhoto):
                if os.path.exists(i.media):
                    while True:
                        try:
                            media = self.send(
                                functions.messages.UploadMedia(
                                    peer=self.resolve_peer(chat_id),
                                    media=types.InputMediaUploadedPhoto(
                                        file=self.save_file(i.media))))
                        except FloodWait as e:
                            log.warning("Sleeping for {}s".format(e.x))
                            time.sleep(e.x)
                        else:
                            break

                    media = types.InputMediaPhoto(id=types.InputPhoto(
                        id=media.photo.id,
                        access_hash=media.photo.access_hash,
                        file_reference=b""))
                else:
                    try:
                        decoded = utils.decode(i.media)
                        fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq"
                        unpacked = struct.unpack(fmt, decoded)
                    except (AssertionError, binascii.Error, struct.error):
                        raise FileIdInvalid from None
                    else:
                        if unpacked[0] != 2:
                            media_type = BaseClient.MEDIA_TYPE_ID.get(
                                unpacked[0], None)

                            if media_type:
                                raise FileIdInvalid(
                                    "The file_id belongs to a {}".format(
                                        media_type))
                            else:
                                raise FileIdInvalid(
                                    "Unknown media type: {}".format(
                                        unpacked[0]))

                        media = types.InputMediaPhoto(
                            id=types.InputPhoto(id=unpacked[2],
                                                access_hash=unpacked[3],
                                                file_reference=b""))
            elif isinstance(i, pyrogram.InputMediaVideo):
                if os.path.exists(i.media):
                    while True:
                        try:
                            media = self.send(
                                functions.messages.UploadMedia(
                                    peer=self.resolve_peer(chat_id),
                                    media=types.InputMediaUploadedDocument(
                                        file=self.save_file(i.media),
                                        thumb=None if i.thumb is None else
                                        self.save_file(i.thumb),
                                        mime_type="video/mp4",
                                        attributes=[
                                            types.DocumentAttributeVideo(
                                                supports_streaming=i.
                                                supports_streaming or None,
                                                duration=i.duration,
                                                w=i.width,
                                                h=i.height),
                                            types.DocumentAttributeFilename(
                                                file_name=os.path.basename(
                                                    i.media))
                                        ])))
                        except FloodWait as e:
                            log.warning("Sleeping for {}s".format(e.x))
                            time.sleep(e.x)
                        else:
                            break

                    media = types.InputMediaDocument(id=types.InputDocument(
                        id=media.document.id,
                        access_hash=media.document.access_hash,
                        file_reference=b""))
                else:
                    try:
                        decoded = utils.decode(i.media)
                        fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq"
                        unpacked = struct.unpack(fmt, decoded)
                    except (AssertionError, binascii.Error, struct.error):
                        raise FileIdInvalid from None
                    else:
                        if unpacked[0] != 4:
                            media_type = BaseClient.MEDIA_TYPE_ID.get(
                                unpacked[0], None)

                            if media_type:
                                raise FileIdInvalid(
                                    "The file_id belongs to a {}".format(
                                        media_type))
                            else:
                                raise FileIdInvalid(
                                    "Unknown media type: {}".format(
                                        unpacked[0]))

                        media = types.InputMediaDocument(
                            id=types.InputDocument(id=unpacked[2],
                                                   access_hash=unpacked[3],
                                                   file_reference=b""))

            multi_media.append(
                types.InputSingleMedia(media=media,
                                       random_id=self.rnd_id(),
                                       **style.parse(i.caption)))

        while True:
            try:
                r = self.send(
                    functions.messages.SendMultiMedia(
                        peer=self.resolve_peer(chat_id),
                        multi_media=multi_media,
                        silent=disable_notification or None,
                        reply_to_msg_id=reply_to_message_id))
            except FloodWait as e:
                log.warning("Sleeping for {}s".format(e.x))
                time.sleep(e.x)
            else:
                break

        return pyrogram.Messages._parse(
            self,
            types.messages.Messages(messages=[
                m.message for m in filter(
                    lambda u: isinstance(u, (types.UpdateNewMessage, types.
                                             UpdateNewChannelMessage)),
                    r.updates)
            ],
                                    users=r.users,
                                    chats=r.chats))
Example #11
0
    async def send_cached_media(
        self,
        chat_id: Union[int, str],
        file_id: str,
        caption: str = "",
        parse_mode: str = "",
        disable_notification: bool = None,
        reply_to_message_id: int = None,
        reply_markup: Union["pyrogram.InlineKeyboardMarkup",
                            "pyrogram.ReplyKeyboardMarkup",
                            "pyrogram.ReplyKeyboardRemove",
                            "pyrogram.ForceReply"] = None
    ) -> Union["pyrogram.Message", None]:
        """Use this method to send any media stored on the Telegram servers using a file_id.

        This convenience method works with any valid file_id only.
        It does the same as calling the relevant method for sending media using a file_id, thus saving you from the
        hassle of using the correct method for the media the file_id is pointing to.

        Args:
            chat_id (``int`` | ``str``):
                Unique identifier (int) or username (str) of the target chat.
                For your personal cloud (Saved Messages) you can simply use "me" or "self".
                For a contact that exists in your Telegram address book you can use his phone number (str).

            file_id (``str``):
                Media to send.
                Pass a file_id as string to send a media that exists on the Telegram servers.

            caption (``bool``, *optional*):
                Media caption, 0-1024 characters.

            parse_mode (``str``, *optional*):
                Use :obj:`MARKDOWN <pyrogram.ParseMode.MARKDOWN>` or :obj:`HTML <pyrogram.ParseMode.HTML>`
                if you want Telegram apps to show bold, italic, fixed-width text or inline URLs in your caption.
                Defaults to Markdown.

            disable_notification (``bool``, *optional*):
                Sends the message silently.
                Users will receive a notification with no sound.

            reply_to_message_id (``int``, *optional*):
                If the message is a reply, ID of the original message.

            reply_markup (:obj:`InlineKeyboardMarkup` | :obj:`ReplyKeyboardMarkup` | :obj:`ReplyKeyboardRemove` | :obj:`ForceReply`, *optional*):
                Additional interface options. An object for an inline keyboard, custom reply keyboard,
                instructions to remove reply keyboard or to force a reply from the user.

        Returns:
            On success, the sent :obj:`Message <pyrogram.Message>` is returned.

        Raises:
            :class:`RPCError <pyrogram.RPCError>` in case of a Telegram RPC error.
        """
        style = self.html if parse_mode.lower() == "html" else self.markdown

        try:
            decoded = utils.decode(file_id)
            fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq"
            unpacked = struct.unpack(fmt, decoded)
        except (AssertionError, binascii.Error, struct.error):
            raise FileIdInvalid from None
        else:
            media_type = BaseClient.MEDIA_TYPE_ID.get(unpacked[0], None)

            if not media_type:
                raise FileIdInvalid("Unknown media type: {}".format(
                    unpacked[0]))

            if media_type == "photo":
                media = types.InputMediaPhoto(
                    id=types.InputPhoto(id=unpacked[2],
                                        access_hash=unpacked[3],
                                        file_reference=b""))
            else:
                media = types.InputMediaDocument(
                    id=types.InputDocument(id=unpacked[2],
                                           access_hash=unpacked[3],
                                           file_reference=b""))

        r = await self.send(
            functions.messages.SendMedia(
                peer=await self.resolve_peer(chat_id),
                media=media,
                silent=disable_notification or None,
                reply_to_msg_id=reply_to_message_id,
                random_id=self.rnd_id(),
                reply_markup=reply_markup.write() if reply_markup else None,
                **await style.parse(caption)))

        for i in r.updates:
            if isinstance(
                    i,
                (types.UpdateNewMessage, types.UpdateNewChannelMessage)):
                return await pyrogram.Message._parse(
                    self, i.message, {i.id: i
                                      for i in r.users},
                    {i.id: i
                     for i in r.chats})
Example #12
0
    async def edit_message_media(
        self,
        chat_id: Union[int, str],
        message_id: int,
        media: InputMedia,
        reply_markup: "pyrogram.InlineKeyboardMarkup" = None
    ) -> "pyrogram.Message":
        """Use this method to edit audio, document, photo, or video messages.

        If a message is a part of a message album, then it can be edited only to a photo or a video. Otherwise,
        message type can be changed arbitrarily. When inline message is edited, new file can't be uploaded.
        Use previously uploaded file via its file_id or specify a URL. On success, if the edited message was sent
        by the bot, the edited Message is returned, otherwise True is returned.

        Args:
            chat_id (``int`` | ``str``):
                Unique identifier (int) or username (str) of the target chat.
                For your personal cloud (Saved Messages) you can simply use "me" or "self".
                For a contact that exists in your Telegram address book you can use his phone number (str).

            message_id (``int``):
                Message identifier in the chat specified in chat_id.

            media (:obj:`InputMedia`)
                One of the InputMedia objects describing an animation, audio, document, photo or video.

            reply_markup (:obj:`InlineKeyboardMarkup`, *optional*):
                An InlineKeyboardMarkup object.

        Returns:
            On success, the edited :obj:`Message <pyrogram.Message>` is returned.

        Raises:
            :class:`RPCError <pyrogram.RPCError>` in case of a Telegram RPC error.
        """
        style = self.html if media.parse_mode.lower(
        ) == "html" else self.markdown
        caption = media.caption

        if isinstance(media, InputMediaPhoto):
            if os.path.exists(media.media):
                media = await self.send(
                    functions.messages.UploadMedia(
                        peer=await self.resolve_peer(chat_id),
                        media=types.InputMediaUploadedPhoto(
                            file=await self.save_file(media.media))))

                media = types.InputMediaPhoto(
                    id=types.InputPhoto(id=media.photo.id,
                                        access_hash=media.photo.access_hash,
                                        file_reference=b""))
            elif media.media.startswith("http"):
                media = types.InputMediaPhotoExternal(url=media.media)
            else:
                try:
                    decoded = utils.decode(media.media)
                    fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq"
                    unpacked = struct.unpack(fmt, decoded)
                except (AssertionError, binascii.Error, struct.error):
                    raise FileIdInvalid from None
                else:
                    if unpacked[0] != 2:
                        media_type = BaseClient.MEDIA_TYPE_ID.get(
                            unpacked[0], None)

                        if media_type:
                            raise FileIdInvalid(
                                "The file_id belongs to a {}".format(
                                    media_type))
                        else:
                            raise FileIdInvalid(
                                "Unknown media type: {}".format(unpacked[0]))

                    media = types.InputMediaPhoto(
                        id=types.InputPhoto(id=unpacked[2],
                                            access_hash=unpacked[3],
                                            file_reference=b""))

        if isinstance(media, InputMediaVideo):
            if os.path.exists(media.media):
                media = await self.send(
                    functions.messages.UploadMedia(
                        peer=await self.resolve_peer(chat_id),
                        media=types.InputMediaUploadedDocument(
                            mime_type="video/mp4",
                            thumb=None if media.thumb is None else
                            self.save_file(media.thumb),
                            file=await self.save_file(media.media),
                            attributes=[
                                types.DocumentAttributeVideo(
                                    supports_streaming=media.supports_streaming
                                    or None,
                                    duration=media.duration,
                                    w=media.width,
                                    h=media.height),
                                types.DocumentAttributeFilename(
                                    file_name=os.path.basename(media.media))
                            ])))

                media = types.InputMediaDocument(id=types.InputDocument(
                    id=media.document.id,
                    access_hash=media.document.access_hash,
                    file_reference=b""))
            elif media.media.startswith("http"):
                media = types.InputMediaDocumentExternal(url=media.media)
            else:
                try:
                    decoded = utils.decode(media.media)
                    fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq"
                    unpacked = struct.unpack(fmt, decoded)
                except (AssertionError, binascii.Error, struct.error):
                    raise FileIdInvalid from None
                else:
                    if unpacked[0] != 4:
                        media_type = BaseClient.MEDIA_TYPE_ID.get(
                            unpacked[0], None)

                        if media_type:
                            raise FileIdInvalid(
                                "The file_id belongs to a {}".format(
                                    media_type))
                        else:
                            raise FileIdInvalid(
                                "Unknown media type: {}".format(unpacked[0]))

                    media = types.InputMediaDocument(
                        id=types.InputDocument(id=unpacked[2],
                                               access_hash=unpacked[3],
                                               file_reference=b""))

        if isinstance(media, InputMediaAudio):
            if os.path.exists(media.media):
                media = await self.send(
                    functions.messages.UploadMedia(
                        peer=await self.resolve_peer(chat_id),
                        media=types.InputMediaUploadedDocument(
                            mime_type="audio/mpeg",
                            thumb=None if media.thumb is None else
                            self.save_file(media.thumb),
                            file=await self.save_file(media.media),
                            attributes=[
                                types.DocumentAttributeAudio(
                                    duration=media.duration,
                                    performer=media.performer,
                                    title=media.title),
                                types.DocumentAttributeFilename(
                                    file_name=os.path.basename(media.media))
                            ])))

                media = types.InputMediaDocument(id=types.InputDocument(
                    id=media.document.id,
                    access_hash=media.document.access_hash,
                    file_reference=b""))
            elif media.media.startswith("http"):
                media = types.InputMediaDocumentExternal(url=media.media)
            else:
                try:
                    decoded = utils.decode(media.media)
                    fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq"
                    unpacked = struct.unpack(fmt, decoded)
                except (AssertionError, binascii.Error, struct.error):
                    raise FileIdInvalid from None
                else:
                    if unpacked[0] != 9:
                        media_type = BaseClient.MEDIA_TYPE_ID.get(
                            unpacked[0], None)

                        if media_type:
                            raise FileIdInvalid(
                                "The file_id belongs to a {}".format(
                                    media_type))
                        else:
                            raise FileIdInvalid(
                                "Unknown media type: {}".format(unpacked[0]))

                    media = types.InputMediaDocument(
                        id=types.InputDocument(id=unpacked[2],
                                               access_hash=unpacked[3],
                                               file_reference=b""))

        if isinstance(media, InputMediaAnimation):
            if os.path.exists(media.media):
                media = await self.send(
                    functions.messages.UploadMedia(
                        peer=await self.resolve_peer(chat_id),
                        media=types.InputMediaUploadedDocument(
                            mime_type="video/mp4",
                            thumb=None if media.thumb is None else
                            self.save_file(media.thumb),
                            file=await self.save_file(media.media),
                            attributes=[
                                types.DocumentAttributeVideo(
                                    supports_streaming=True,
                                    duration=media.duration,
                                    w=media.width,
                                    h=media.height),
                                types.DocumentAttributeFilename(
                                    file_name=os.path.basename(media.media)),
                                types.DocumentAttributeAnimated()
                            ])))

                media = types.InputMediaDocument(id=types.InputDocument(
                    id=media.document.id,
                    access_hash=media.document.access_hash,
                    file_reference=b""))
            elif media.media.startswith("http"):
                media = types.InputMediaDocumentExternal(url=media.media)
            else:
                try:
                    decoded = utils.decode(media.media)
                    fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq"
                    unpacked = struct.unpack(fmt, decoded)
                except (AssertionError, binascii.Error, struct.error):
                    raise FileIdInvalid from None
                else:
                    if unpacked[0] != 10:
                        media_type = BaseClient.MEDIA_TYPE_ID.get(
                            unpacked[0], None)

                        if media_type:
                            raise FileIdInvalid(
                                "The file_id belongs to a {}".format(
                                    media_type))
                        else:
                            raise FileIdInvalid(
                                "Unknown media type: {}".format(unpacked[0]))

                    media = types.InputMediaDocument(
                        id=types.InputDocument(id=unpacked[2],
                                               access_hash=unpacked[3],
                                               file_reference=b""))

        if isinstance(media, InputMediaDocument):
            if os.path.exists(media.media):
                media = await self.send(
                    functions.messages.UploadMedia(
                        peer=await self.resolve_peer(chat_id),
                        media=types.InputMediaUploadedDocument(
                            mime_type="application/zip",
                            thumb=None if media.thumb is None else
                            self.save_file(media.thumb),
                            file=await self.save_file(media.media),
                            attributes=[
                                types.DocumentAttributeFilename(
                                    file_name=os.path.basename(media.media))
                            ])))

                media = types.InputMediaDocument(id=types.InputDocument(
                    id=media.document.id,
                    access_hash=media.document.access_hash,
                    file_reference=b""))
            elif media.media.startswith("http"):
                media = types.InputMediaDocumentExternal(url=media.media)
            else:
                try:
                    decoded = utils.decode(media.media)
                    fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq"
                    unpacked = struct.unpack(fmt, decoded)
                except (AssertionError, binascii.Error, struct.error):
                    raise FileIdInvalid from None
                else:
                    if unpacked[0] not in (5, 10):
                        media_type = BaseClient.MEDIA_TYPE_ID.get(
                            unpacked[0], None)

                        if media_type:
                            raise FileIdInvalid(
                                "The file_id belongs to a {}".format(
                                    media_type))
                        else:
                            raise FileIdInvalid(
                                "Unknown media type: {}".format(unpacked[0]))

                    media = types.InputMediaDocument(
                        id=types.InputDocument(id=unpacked[2],
                                               access_hash=unpacked[3],
                                               file_reference=b""))

        r = await self.send(
            functions.messages.EditMessage(
                peer=await self.resolve_peer(chat_id),
                id=message_id,
                reply_markup=reply_markup.write() if reply_markup else None,
                media=media,
                **await style.parse(caption)))

        for i in r.updates:
            if isinstance(
                    i,
                (types.UpdateEditMessage, types.UpdateEditChannelMessage)):
                return await pyrogram.Message._parse(
                    self, i.message, {i.id: i
                                      for i in r.users},
                    {i.id: i
                     for i in r.chats})
Example #13
0
    def edit_message_media(self,
                           chat_id: int or str,
                           message_id: int,
                           media,
                           reply_markup=None):
        style = self.html if media.parse_mode.lower(
        ) == "html" else self.markdown
        caption = media.caption

        if isinstance(media, InputMediaPhoto):
            if os.path.exists(media.media):
                media = self.send(
                    functions.messages.UploadMedia(
                        peer=self.resolve_peer(chat_id),
                        media=types.InputMediaUploadedPhoto(
                            file=self.save_file(media.media))))

                media = types.InputMediaPhoto(id=types.InputPhoto(
                    id=media.photo.id, access_hash=media.photo.access_hash))
            elif media.media.startswith("http"):
                media = types.InputMediaPhotoExternal(url=media.media)
            else:
                try:
                    decoded = utils.decode(media.media)
                    fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq"
                    unpacked = struct.unpack(fmt, decoded)
                except (AssertionError, binascii.Error, struct.error):
                    raise FileIdInvalid from None
                else:
                    if unpacked[0] != 2:
                        media_type = BaseClient.MEDIA_TYPE_ID.get(
                            unpacked[0], None)

                        if media_type:
                            raise FileIdInvalid(
                                "The file_id belongs to a {}".format(
                                    media_type))
                        else:
                            raise FileIdInvalid(
                                "Unknown media type: {}".format(unpacked[0]))

                    media = types.InputMediaPhoto(id=types.InputPhoto(
                        id=unpacked[2], access_hash=unpacked[3]))

        if isinstance(media, InputMediaVideo):
            if os.path.exists(media.media):
                media = self.send(
                    functions.messages.UploadMedia(
                        peer=self.resolve_peer(chat_id),
                        media=types.InputMediaUploadedDocument(
                            mime_type=mimetypes.types_map[".mp4"],
                            file=self.save_file(media.media),
                            attributes=[
                                types.DocumentAttributeVideo(
                                    supports_streaming=media.supports_streaming
                                    or None,
                                    duration=media.duration,
                                    w=media.width,
                                    h=media.height),
                                types.DocumentAttributeFilename(
                                    os.path.basename(media.media))
                            ])))

                media = types.InputMediaDocument(id=types.InputDocument(
                    id=media.document.id,
                    access_hash=media.document.access_hash))
            elif media.media.startswith("http"):
                media = types.InputMediaDocumentExternal(url=media.media)
            else:
                try:
                    decoded = utils.decode(media.media)
                    fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq"
                    unpacked = struct.unpack(fmt, decoded)
                except (AssertionError, binascii.Error, struct.error):
                    raise FileIdInvalid from None
                else:
                    if unpacked[0] != 4:
                        media_type = BaseClient.MEDIA_TYPE_ID.get(
                            unpacked[0], None)

                        if media_type:
                            raise FileIdInvalid(
                                "The file_id belongs to a {}".format(
                                    media_type))
                        else:
                            raise FileIdInvalid(
                                "Unknown media type: {}".format(unpacked[0]))

                    media = types.InputMediaDocument(id=types.InputDocument(
                        id=unpacked[2], access_hash=unpacked[3]))

        if isinstance(media, InputMediaAudio):
            if os.path.exists(media.media):
                media = self.send(
                    functions.messages.UploadMedia(
                        peer=self.resolve_peer(chat_id),
                        media=types.InputMediaUploadedDocument(
                            mime_type=mimetypes.types_map.get(
                                "." + media.media.split(".")[-1],
                                "audio/mpeg"),
                            file=self.save_file(media.media),
                            attributes=[
                                types.DocumentAttributeAudio(
                                    duration=media.duration,
                                    performer=media.performer,
                                    title=media.title),
                                types.DocumentAttributeFilename(
                                    os.path.basename(media.media))
                            ])))

                media = types.InputMediaDocument(id=types.InputDocument(
                    id=media.document.id,
                    access_hash=media.document.access_hash))
            elif media.media.startswith("http"):
                media = types.InputMediaDocumentExternal(url=media.media)
            else:
                try:
                    decoded = utils.decode(media.media)
                    fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq"
                    unpacked = struct.unpack(fmt, decoded)
                except (AssertionError, binascii.Error, struct.error):
                    raise FileIdInvalid from None
                else:
                    if unpacked[0] != 9:
                        media_type = BaseClient.MEDIA_TYPE_ID.get(
                            unpacked[0], None)

                        if media_type:
                            raise FileIdInvalid(
                                "The file_id belongs to a {}".format(
                                    media_type))
                        else:
                            raise FileIdInvalid(
                                "Unknown media type: {}".format(unpacked[0]))

                    media = types.InputMediaDocument(id=types.InputDocument(
                        id=unpacked[2], access_hash=unpacked[3]))

        if isinstance(media, InputMediaAnimation):
            if os.path.exists(media.media):
                media = self.send(
                    functions.messages.UploadMedia(
                        peer=self.resolve_peer(chat_id),
                        media=types.InputMediaUploadedDocument(
                            mime_type=mimetypes.types_map[".mp4"],
                            file=self.save_file(media.media),
                            attributes=[
                                types.DocumentAttributeVideo(
                                    supports_streaming=True,
                                    duration=media.duration,
                                    w=media.width,
                                    h=media.height),
                                types.DocumentAttributeFilename(
                                    os.path.basename(media.media)),
                                types.DocumentAttributeAnimated()
                            ])))

                media = types.InputMediaDocument(id=types.InputDocument(
                    id=media.document.id,
                    access_hash=media.document.access_hash))
            elif media.media.startswith("http"):
                media = types.InputMediaDocumentExternal(url=media.media)
            else:
                try:
                    decoded = utils.decode(media.media)
                    fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq"
                    unpacked = struct.unpack(fmt, decoded)
                except (AssertionError, binascii.Error, struct.error):
                    raise FileIdInvalid from None
                else:
                    if unpacked[0] != 10:
                        media_type = BaseClient.MEDIA_TYPE_ID.get(
                            unpacked[0], None)

                        if media_type:
                            raise FileIdInvalid(
                                "The file_id belongs to a {}".format(
                                    media_type))
                        else:
                            raise FileIdInvalid(
                                "Unknown media type: {}".format(unpacked[0]))

                    media = types.InputMediaDocument(id=types.InputDocument(
                        id=unpacked[2], access_hash=unpacked[3]))

        if isinstance(media, InputMediaDocument):
            if os.path.exists(media.media):
                media = self.send(
                    functions.messages.UploadMedia(
                        peer=self.resolve_peer(chat_id),
                        media=types.InputMediaUploadedDocument(
                            mime_type=mimetypes.types_map.get(
                                "." + media.media.split(".")[-1],
                                "text/plain"),
                            file=self.save_file(media.media),
                            attributes=[
                                types.DocumentAttributeFilename(
                                    os.path.basename(media.media))
                            ])))

                media = types.InputMediaDocument(id=types.InputDocument(
                    id=media.document.id,
                    access_hash=media.document.access_hash))
            elif media.media.startswith("http"):
                media = types.InputMediaDocumentExternal(url=media.media)
            else:
                try:
                    decoded = utils.decode(media.media)
                    fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq"
                    unpacked = struct.unpack(fmt, decoded)
                except (AssertionError, binascii.Error, struct.error):
                    raise FileIdInvalid from None
                else:
                    if unpacked[0] not in (5, 10):
                        media_type = BaseClient.MEDIA_TYPE_ID.get(
                            unpacked[0], None)

                        if media_type:
                            raise FileIdInvalid(
                                "The file_id belongs to a {}".format(
                                    media_type))
                        else:
                            raise FileIdInvalid(
                                "Unknown media type: {}".format(unpacked[0]))

                    media = types.InputMediaDocument(id=types.InputDocument(
                        id=unpacked[2], access_hash=unpacked[3]))

        r = self.send(
            functions.messages.EditMessage(
                peer=self.resolve_peer(chat_id),
                id=message_id,
                reply_markup=reply_markup.write() if reply_markup else None,
                media=media,
                **style.parse(caption)))

        for i in r.updates:
            if isinstance(
                    i,
                (types.UpdateEditMessage, types.UpdateEditChannelMessage)):
                return utils.parse_messages(self, i.message,
                                            {i.id: i
                                             for i in r.users},
                                            {i.id: i
                                             for i in r.chats})