From 92c1b48132783c31b2cdbdd33b576b6da7700f7c Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sat, 21 Sep 2019 22:13:02 +0200 Subject: [PATCH] Fix FILE_REFERENCE_* errors for uploads --- pyrogram/client/client.py | 4 +++- pyrogram/client/ext/file_data.py | 2 +- pyrogram/client/ext/utils.py | 16 ++++++++++++++-- .../client/methods/messages/download_media.py | 7 ++++--- .../client/methods/messages/edit_inline_media.py | 10 +++++----- .../methods/messages/edit_message_media.py | 10 +++++----- .../client/methods/messages/send_animation.py | 9 +++++++-- pyrogram/client/methods/messages/send_audio.py | 7 ++++++- .../client/methods/messages/send_cached_media.py | 7 ++++++- .../client/methods/messages/send_document.py | 7 ++++++- .../client/methods/messages/send_media_group.py | 4 ++-- pyrogram/client/methods/messages/send_photo.py | 7 ++++++- pyrogram/client/methods/messages/send_sticker.py | 7 ++++++- pyrogram/client/methods/messages/send_video.py | 7 ++++++- .../client/methods/messages/send_video_note.py | 7 ++++++- pyrogram/client/methods/messages/send_voice.py | 7 ++++++- pyrogram/client/types/input_media/input_media.py | 3 ++- .../types/input_media/input_media_animation.py | 7 ++++++- .../types/input_media/input_media_audio.py | 7 ++++++- .../types/input_media/input_media_document.py | 7 ++++++- .../types/input_media/input_media_photo.py | 7 ++++++- .../types/input_media/input_media_video.py | 7 ++++++- .../client/types/messages_and_media/animation.py | 8 ++++---- .../client/types/messages_and_media/audio.py | 8 ++++---- .../client/types/messages_and_media/document.py | 8 ++++---- .../client/types/messages_and_media/message.py | 12 ++++++++++-- .../client/types/messages_and_media/photo.py | 8 ++++---- .../client/types/messages_and_media/sticker.py | 8 ++++---- .../client/types/messages_and_media/video.py | 8 ++++---- .../types/messages_and_media/video_note.py | 8 ++++---- .../client/types/messages_and_media/voice.py | 8 ++++---- 31 files changed, 163 insertions(+), 69 deletions(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index 5e8d15c1..880d3c49 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -1869,7 +1869,7 @@ class Client(Methods, BaseClient): peer_access_hash: int, volume_id: int, local_id: int, - file_ref: bytes, + file_ref: str, file_size: int, is_big: bool, progress: callable, @@ -1910,6 +1910,8 @@ class Client(Methods, BaseClient): self.media_sessions[dc_id] = session + file_ref = utils.decode_file_ref(file_ref) + if media_type == 1: location = types.InputPeerPhotoFileLocation( peer=types.InputPeerUser( diff --git a/pyrogram/client/ext/file_data.py b/pyrogram/client/ext/file_data.py index 1f13a4cb..ae023d18 100644 --- a/pyrogram/client/ext/file_data.py +++ b/pyrogram/client/ext/file_data.py @@ -22,7 +22,7 @@ class FileData: self, *, media_type: int = None, dc_id: int = None, document_id: int = None, access_hash: int = None, thumb_size: str = None, peer_id: int = None, peer_access_hash: int = None, volume_id: int = None, local_id: int = None, is_big: bool = None, file_size: int = None, mime_type: str = None, file_name: str = None, - date: int = None, file_ref: bytes = None + date: int = None, file_ref: str = None ): self.media_type = media_type self.dc_id = dc_id diff --git a/pyrogram/client/ext/utils.py b/pyrogram/client/ext/utils.py index 663d67ff..bb1f104a 100644 --- a/pyrogram/client/ext/utils.py +++ b/pyrogram/client/ext/utils.py @@ -70,6 +70,17 @@ def encode(s: bytes) -> str: return base64.urlsafe_b64encode(r).decode().rstrip("=") +def encode_file_ref(file_ref: bytes) -> str: + return base64.urlsafe_b64encode(file_ref).decode().rstrip("=") + + +def decode_file_ref(file_ref: str) -> bytes: + if file_ref is None: + return b"" + + return base64.urlsafe_b64decode(file_ref + "=" * (-len(file_ref) % 4)) + + def get_offset_date(dialogs): for m in reversed(dialogs.messages): if isinstance(m, types.MessageEmpty): @@ -82,6 +93,7 @@ def get_offset_date(dialogs): def get_input_media_from_file_id( file_id_str: str, + file_ref: str = None, expected_media_type: int = None ) -> Union[types.InputMediaPhoto, types.InputMediaDocument]: try: @@ -111,7 +123,7 @@ def get_input_media_from_file_id( id=types.InputPhoto( id=file_id, access_hash=access_hash, - file_reference=b"" + file_reference=decode_file_ref(file_ref) ) ) @@ -123,7 +135,7 @@ def get_input_media_from_file_id( id=types.InputDocument( id=file_id, access_hash=access_hash, - file_reference=b"" + file_reference=decode_file_ref(file_ref) ) ) diff --git a/pyrogram/client/methods/messages/download_media.py b/pyrogram/client/methods/messages/download_media.py index c15e918a..40e150b4 100644 --- a/pyrogram/client/methods/messages/download_media.py +++ b/pyrogram/client/methods/messages/download_media.py @@ -35,7 +35,7 @@ class DownloadMedia(BaseClient): def download_media( self, message: Union["pyrogram.Message", str], - file_ref: bytes = None, + file_ref: str = None, file_name: str = DEFAULT_DOWNLOAD_DIR, block: bool = True, progress: callable = None, @@ -48,8 +48,9 @@ class DownloadMedia(BaseClient): Pass a Message containing the media, the media itself (message.audio, message.video, ...) or the file id as string. - file_ref (``bytes``, *optional*): + file_ref (``str``, *optional*): A valid file reference obtained by a recently fetched media message. + To be used in combination with a file id in case a file reference is needed. file_name (``str``, *optional*): A custom *file_name* to be used instead of the one provided by Telegram. @@ -133,7 +134,7 @@ class DownloadMedia(BaseClient): file_size=file_size, mime_type=mime_type, date=date, - file_ref=file_ref or b"" + file_ref=file_ref ) def get_existing_attributes() -> dict: diff --git a/pyrogram/client/methods/messages/edit_inline_media.py b/pyrogram/client/methods/messages/edit_inline_media.py index 7a82f3a8..0768ba8f 100644 --- a/pyrogram/client/methods/messages/edit_inline_media.py +++ b/pyrogram/client/methods/messages/edit_inline_media.py @@ -77,35 +77,35 @@ class EditInlineMedia(BaseClient): url=media.media ) else: - media = utils.get_input_media_from_file_id(media.media, 2) + media = utils.get_input_media_from_file_id(media.media, media.file_ref, 2) elif isinstance(media, InputMediaVideo): if media.media.startswith("http"): media = types.InputMediaDocumentExternal( url=media.media ) else: - media = utils.get_input_media_from_file_id(media.media, 4) + media = utils.get_input_media_from_file_id(media.media, media.file_ref, 4) elif isinstance(media, InputMediaAudio): if media.media.startswith("http"): media = types.InputMediaDocumentExternal( url=media.media ) else: - media = utils.get_input_media_from_file_id(media.media, 9) + media = utils.get_input_media_from_file_id(media.media, media.file_ref, 9) elif isinstance(media, InputMediaAnimation): if media.media.startswith("http"): media = types.InputMediaDocumentExternal( url=media.media ) else: - media = utils.get_input_media_from_file_id(media.media, 10) + media = utils.get_input_media_from_file_id(media.media, media.file_ref, 10) elif isinstance(media, InputMediaDocument): if media.media.startswith("http"): media = types.InputMediaDocumentExternal( url=media.media ) else: - media = utils.get_input_media_from_file_id(media.media, 5) + media = utils.get_input_media_from_file_id(media.media, media.file_ref, 5) return self.send( functions.messages.EditInlineBotMessage( diff --git a/pyrogram/client/methods/messages/edit_message_media.py b/pyrogram/client/methods/messages/edit_message_media.py index f543af2b..6050a9c5 100644 --- a/pyrogram/client/methods/messages/edit_message_media.py +++ b/pyrogram/client/methods/messages/edit_message_media.py @@ -100,7 +100,7 @@ class EditMessageMedia(BaseClient): url=media.media ) else: - media = utils.get_input_media_from_file_id(media.media, 2) + media = utils.get_input_media_from_file_id(media.media, media.file_ref, 2) elif isinstance(media, InputMediaVideo): if os.path.exists(media.media): media = self.send( @@ -137,7 +137,7 @@ class EditMessageMedia(BaseClient): url=media.media ) else: - media = utils.get_input_media_from_file_id(media.media, 4) + media = utils.get_input_media_from_file_id(media.media, media.file_ref, 4) elif isinstance(media, InputMediaAudio): if os.path.exists(media.media): media = self.send( @@ -173,7 +173,7 @@ class EditMessageMedia(BaseClient): url=media.media ) else: - media = utils.get_input_media_from_file_id(media.media, 9) + media = utils.get_input_media_from_file_id(media.media, media.file_ref, 9) elif isinstance(media, InputMediaAnimation): if os.path.exists(media.media): media = self.send( @@ -211,7 +211,7 @@ class EditMessageMedia(BaseClient): url=media.media ) else: - media = utils.get_input_media_from_file_id(media.media, 10) + media = utils.get_input_media_from_file_id(media.media, media.file_ref, 10) elif isinstance(media, InputMediaDocument): if os.path.exists(media.media): media = self.send( @@ -242,7 +242,7 @@ class EditMessageMedia(BaseClient): url=media.media ) else: - media = utils.get_input_media_from_file_id(media.media, 5) + media = utils.get_input_media_from_file_id(media.media, media.file_ref, 5) r = self.send( functions.messages.EditMessage( diff --git a/pyrogram/client/methods/messages/send_animation.py b/pyrogram/client/methods/messages/send_animation.py index b4ca4e43..1351e540 100644 --- a/pyrogram/client/methods/messages/send_animation.py +++ b/pyrogram/client/methods/messages/send_animation.py @@ -30,6 +30,7 @@ class SendAnimation(BaseClient): self, chat_id: Union[int, str], animation: str, + file_ref: str = None, caption: str = "", unsave: bool = False, parse_mode: Union[str, None] = object, @@ -63,6 +64,10 @@ class SendAnimation(BaseClient): pass an HTTP URL as a string for Telegram to get an animation from the Internet, or pass a file path as string to upload a new animation that exists on your local machine. + file_ref (``str``, *optional*): + A valid file reference obtained by a recently fetched media message. + To be used in combination with a file id in case a file reference is needed. + caption (``str``, *optional*): Animation caption, 0-1024 characters. @@ -176,7 +181,7 @@ class SendAnimation(BaseClient): url=animation ) else: - media = utils.get_input_media_from_file_id(animation, 10) + media = utils.get_input_media_from_file_id(animation, file_ref, 10) while True: try: @@ -209,7 +214,7 @@ class SendAnimation(BaseClient): if unsave: document = message.animation or message.document - document_id = utils.get_input_media_from_file_id(document.file_id).id + document_id = utils.get_input_media_from_file_id(document.file_id, document.file_ref).id self.send( functions.messages.SaveGif( diff --git a/pyrogram/client/methods/messages/send_audio.py b/pyrogram/client/methods/messages/send_audio.py index 63478c63..8d49fe8e 100644 --- a/pyrogram/client/methods/messages/send_audio.py +++ b/pyrogram/client/methods/messages/send_audio.py @@ -30,6 +30,7 @@ class SendAudio(BaseClient): self, chat_id: Union[int, str], audio: str, + file_ref: str = None, caption: str = "", parse_mode: Union[str, None] = object, duration: int = 0, @@ -64,6 +65,10 @@ class SendAudio(BaseClient): pass an HTTP URL as a string for Telegram to get an audio file from the Internet, or pass a file path as string to upload a new audio file that exists on your local machine. + file_ref (``str``, *optional*): + A valid file reference obtained by a recently fetched media message. + To be used in combination with a file id in case a file reference is needed. + caption (``str``, *optional*): Audio caption, 0-1024 characters. @@ -174,7 +179,7 @@ class SendAudio(BaseClient): url=audio ) else: - media = utils.get_input_media_from_file_id(audio, 9) + media = utils.get_input_media_from_file_id(audio, file_ref, 9) while True: try: diff --git a/pyrogram/client/methods/messages/send_cached_media.py b/pyrogram/client/methods/messages/send_cached_media.py index 874b0507..d706d85e 100644 --- a/pyrogram/client/methods/messages/send_cached_media.py +++ b/pyrogram/client/methods/messages/send_cached_media.py @@ -28,6 +28,7 @@ class SendCachedMedia(BaseClient): self, chat_id: Union[int, str], file_id: str, + file_ref: str = None, caption: str = "", parse_mode: Union[str, None] = object, disable_notification: bool = None, @@ -56,6 +57,10 @@ class SendCachedMedia(BaseClient): Media to send. Pass a file_id as string to send a media that exists on the Telegram servers. + file_ref (``str``, *optional*): + A valid file reference obtained by a recently fetched media message. + To be used in combination with a file id in case a file reference is needed. + caption (``bool``, *optional*): Media caption, 0-1024 characters. @@ -92,7 +97,7 @@ class SendCachedMedia(BaseClient): r = self.send( functions.messages.SendMedia( peer=self.resolve_peer(chat_id), - media=utils.get_input_media_from_file_id(file_id), + media=utils.get_input_media_from_file_id(file_id, file_ref), silent=disable_notification or None, reply_to_msg_id=reply_to_message_id, random_id=self.rnd_id(), diff --git a/pyrogram/client/methods/messages/send_document.py b/pyrogram/client/methods/messages/send_document.py index 4064fc2a..c8013aed 100644 --- a/pyrogram/client/methods/messages/send_document.py +++ b/pyrogram/client/methods/messages/send_document.py @@ -30,6 +30,7 @@ class SendDocument(BaseClient): self, chat_id: Union[int, str], document: str, + file_ref: str = None, thumb: str = None, caption: str = "", parse_mode: Union[str, None] = object, @@ -59,6 +60,10 @@ class SendDocument(BaseClient): pass an HTTP URL as a string for Telegram to get a file from the Internet, or pass a file path as string to upload a new file that exists on your local machine. + file_ref (``str``, *optional*): + A valid file reference obtained by a recently fetched media message. + To be used in combination with a file id in case a file reference is needed. + thumb (``str``, *optional*): Thumbnail of the file sent. The thumbnail should be in JPEG format and less than 200 KB in size. @@ -149,7 +154,7 @@ class SendDocument(BaseClient): url=document ) else: - media = utils.get_input_media_from_file_id(document, 5) + media = utils.get_input_media_from_file_id(document, file_ref, 5) while True: try: diff --git a/pyrogram/client/methods/messages/send_media_group.py b/pyrogram/client/methods/messages/send_media_group.py index ac38c0d6..6a7d9f28 100644 --- a/pyrogram/client/methods/messages/send_media_group.py +++ b/pyrogram/client/methods/messages/send_media_group.py @@ -119,7 +119,7 @@ class SendMediaGroup(BaseClient): ) ) else: - media = utils.get_input_media_from_file_id(i.media, 2) + media = utils.get_input_media_from_file_id(i.media, i.file_ref, 2) elif isinstance(i, pyrogram.InputMediaVideo): if os.path.exists(i.media): while True: @@ -174,7 +174,7 @@ class SendMediaGroup(BaseClient): ) ) else: - media = utils.get_input_media_from_file_id(i.media, 4) + media = utils.get_input_media_from_file_id(i.media, i.file_ref, 4) multi_media.append( types.InputSingleMedia( diff --git a/pyrogram/client/methods/messages/send_photo.py b/pyrogram/client/methods/messages/send_photo.py index 95fdeef8..4e8ab03f 100644 --- a/pyrogram/client/methods/messages/send_photo.py +++ b/pyrogram/client/methods/messages/send_photo.py @@ -30,6 +30,7 @@ class SendPhoto(BaseClient): self, chat_id: Union[int, str], photo: str, + file_ref: str = None, caption: str = "", parse_mode: Union[str, None] = object, ttl_seconds: int = None, @@ -59,6 +60,10 @@ class SendPhoto(BaseClient): 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. + file_ref (``str``, *optional*): + A valid file reference obtained by a recently fetched media message. + To be used in combination with a file id in case a file reference is needed. + caption (``bool``, *optional*): Photo caption, 0-1024 characters. @@ -144,7 +149,7 @@ class SendPhoto(BaseClient): ttl_seconds=ttl_seconds ) else: - media = utils.get_input_media_from_file_id(photo, 2) + media = utils.get_input_media_from_file_id(photo, file_ref, 2) while True: try: diff --git a/pyrogram/client/methods/messages/send_sticker.py b/pyrogram/client/methods/messages/send_sticker.py index 9c6e042c..00763f2b 100644 --- a/pyrogram/client/methods/messages/send_sticker.py +++ b/pyrogram/client/methods/messages/send_sticker.py @@ -30,6 +30,7 @@ class SendSticker(BaseClient): self, chat_id: Union[int, str], sticker: str, + file_ref: str = None, disable_notification: bool = None, reply_to_message_id: int = None, schedule_date: int = None, @@ -56,6 +57,10 @@ class SendSticker(BaseClient): 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. + file_ref (``str``, *optional*): + A valid file reference obtained by a recently fetched media message. + To be used in combination with a file id in case a file reference is needed. + disable_notification (``bool``, *optional*): Sends the message silently. Users will receive a notification with no sound. @@ -122,7 +127,7 @@ class SendSticker(BaseClient): url=sticker ) else: - media = utils.get_input_media_from_file_id(sticker, 8) + media = utils.get_input_media_from_file_id(sticker, file_ref, 8) while True: try: diff --git a/pyrogram/client/methods/messages/send_video.py b/pyrogram/client/methods/messages/send_video.py index 26a70ac1..6eb8fc29 100644 --- a/pyrogram/client/methods/messages/send_video.py +++ b/pyrogram/client/methods/messages/send_video.py @@ -30,6 +30,7 @@ class SendVideo(BaseClient): self, chat_id: Union[int, str], video: str, + file_ref: str = None, caption: str = "", parse_mode: Union[str, None] = object, duration: int = 0, @@ -63,6 +64,10 @@ class SendVideo(BaseClient): pass an HTTP URL as a string for Telegram to get a video from the Internet, or pass a file path as string to upload a new video that exists on your local machine. + file_ref (``str``, *optional*): + A valid file reference obtained by a recently fetched media message. + To be used in combination with a file id in case a file reference is needed. + caption (``str``, *optional*): Video caption, 0-1024 characters. @@ -172,7 +177,7 @@ class SendVideo(BaseClient): url=video ) else: - media = utils.get_input_media_from_file_id(video, 4) + media = utils.get_input_media_from_file_id(video, file_ref, 4) while True: try: diff --git a/pyrogram/client/methods/messages/send_video_note.py b/pyrogram/client/methods/messages/send_video_note.py index 3f309fa3..615120bb 100644 --- a/pyrogram/client/methods/messages/send_video_note.py +++ b/pyrogram/client/methods/messages/send_video_note.py @@ -30,6 +30,7 @@ class SendVideoNote(BaseClient): self, chat_id: Union[int, str], video_note: str, + file_ref: str = None, duration: int = 0, length: int = 1, thumb: str = None, @@ -59,6 +60,10 @@ class SendVideoNote(BaseClient): 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. + file_ref (``str``, *optional*): + A valid file reference obtained by a recently fetched media message. + To be used in combination with a file id in case a file reference is needed. + duration (``int``, *optional*): Duration of sent video in seconds. @@ -140,7 +145,7 @@ class SendVideoNote(BaseClient): ] ) else: - media = utils.get_input_media_from_file_id(video_note, 13) + media = utils.get_input_media_from_file_id(video_note, file_ref, 13) while True: try: diff --git a/pyrogram/client/methods/messages/send_voice.py b/pyrogram/client/methods/messages/send_voice.py index 5db42cd2..df5070c4 100644 --- a/pyrogram/client/methods/messages/send_voice.py +++ b/pyrogram/client/methods/messages/send_voice.py @@ -30,6 +30,7 @@ class SendVoice(BaseClient): self, chat_id: Union[int, str], voice: str, + file_ref=None, caption: str = "", parse_mode: Union[str, None] = object, duration: int = 0, @@ -59,6 +60,10 @@ class SendVoice(BaseClient): 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. + file_ref (``str``, *optional*): + A valid file reference obtained by a recently fetched media message. + To be used in combination with a file id in case a file reference is needed. + caption (``str``, *optional*): Voice message caption, 0-1024 characters. @@ -144,7 +149,7 @@ class SendVoice(BaseClient): url=voice ) else: - media = utils.get_input_media_from_file_id(voice, 3) + media = utils.get_input_media_from_file_id(voice, file_ref, 3) while True: try: diff --git a/pyrogram/client/types/input_media/input_media.py b/pyrogram/client/types/input_media/input_media.py index 9b89fe12..f244620b 100644 --- a/pyrogram/client/types/input_media/input_media.py +++ b/pyrogram/client/types/input_media/input_media.py @@ -31,9 +31,10 @@ class InputMedia(Object): - :obj:`InputMediaVideo` """ - def __init__(self, media: str, caption: str, parse_mode: str): + def __init__(self, media: str, file_ref: str, caption: str, parse_mode: str): super().__init__() self.media = media + self.file_ref = file_ref self.caption = caption self.parse_mode = parse_mode diff --git a/pyrogram/client/types/input_media/input_media_animation.py b/pyrogram/client/types/input_media/input_media_animation.py index dc70cbec..14cc9774 100644 --- a/pyrogram/client/types/input_media/input_media_animation.py +++ b/pyrogram/client/types/input_media/input_media_animation.py @@ -30,6 +30,10 @@ class InputMediaAnimation(InputMedia): Pass a file_id as string to send a file that exists on the Telegram servers or pass a file path as string to upload a new file that exists on your local machine. + file_ref (``str``, *optional*): + A valid file reference obtained by a recently fetched media message. + To be used in combination with a file id in case a file reference is needed. + thumb (``str``, *optional*): Thumbnail of the animation file sent. The thumbnail should be in JPEG format and less than 200 KB in size. @@ -59,6 +63,7 @@ class InputMediaAnimation(InputMedia): def __init__( self, media: str, + file_ref: str = None, thumb: str = None, caption: str = "", parse_mode: Union[str, None] = object, @@ -66,7 +71,7 @@ class InputMediaAnimation(InputMedia): height: int = 0, duration: int = 0 ): - super().__init__(media, caption, parse_mode) + super().__init__(media, file_ref, caption, parse_mode) self.thumb = thumb self.width = width diff --git a/pyrogram/client/types/input_media/input_media_audio.py b/pyrogram/client/types/input_media/input_media_audio.py index 5ed670a6..c1932436 100644 --- a/pyrogram/client/types/input_media/input_media_audio.py +++ b/pyrogram/client/types/input_media/input_media_audio.py @@ -32,6 +32,10 @@ class InputMediaAudio(InputMedia): Pass a file_id as string to send an audio that exists on the Telegram servers or pass a file path as string to upload a new audio that exists on your local machine. + file_ref (``str``, *optional*): + A valid file reference obtained by a recently fetched media message. + To be used in combination with a file id in case a file reference is needed. + thumb (``str``, *optional*): Thumbnail of the music file album cover. The thumbnail should be in JPEG format and less than 200 KB in size. @@ -61,6 +65,7 @@ class InputMediaAudio(InputMedia): def __init__( self, media: str, + file_ref: str = None, thumb: str = None, caption: str = "", parse_mode: Union[str, None] = object, @@ -68,7 +73,7 @@ class InputMediaAudio(InputMedia): performer: int = "", title: str = "" ): - super().__init__(media, caption, parse_mode) + super().__init__(media, file_ref, caption, parse_mode) self.thumb = thumb self.duration = duration diff --git a/pyrogram/client/types/input_media/input_media_document.py b/pyrogram/client/types/input_media/input_media_document.py index 14756e02..9244b8f5 100644 --- a/pyrogram/client/types/input_media/input_media_document.py +++ b/pyrogram/client/types/input_media/input_media_document.py @@ -30,6 +30,10 @@ class InputMediaDocument(InputMedia): Pass a file_id as string to send a file that exists on the Telegram servers or pass a file path as string to upload a new file that exists on your local machine. + file_ref (``str``, *optional*): + A valid file reference obtained by a recently fetched media message. + To be used in combination with a file id in case a file reference is needed. + thumb (``str``): Thumbnail of the file sent. The thumbnail should be in JPEG format and less than 200 KB in size. @@ -50,10 +54,11 @@ class InputMediaDocument(InputMedia): def __init__( self, media: str, + file_ref: str = None, thumb: str = None, caption: str = "", parse_mode: Union[str, None] = object ): - super().__init__(media, caption, parse_mode) + super().__init__(media, file_ref, caption, parse_mode) self.thumb = thumb diff --git a/pyrogram/client/types/input_media/input_media_photo.py b/pyrogram/client/types/input_media/input_media_photo.py index 5e18cdd6..b351f03c 100644 --- a/pyrogram/client/types/input_media/input_media_photo.py +++ b/pyrogram/client/types/input_media/input_media_photo.py @@ -32,6 +32,10 @@ class InputMediaPhoto(InputMedia): pass a file path as string to upload a new photo that exists on your local machine. Sending photo by a URL is currently unsupported. + file_ref (``str``, *optional*): + A valid file reference obtained by a recently fetched media message. + To be used in combination with a file id in case a file reference is needed. + caption (``str``, *optional*): Caption of the photo to be sent, 0-1024 characters @@ -46,7 +50,8 @@ class InputMediaPhoto(InputMedia): def __init__( self, media: str, + file_ref: str = None, caption: str = "", parse_mode: Union[str, None] = object ): - super().__init__(media, caption, parse_mode) + super().__init__(media, file_ref, caption, parse_mode) diff --git a/pyrogram/client/types/input_media/input_media_video.py b/pyrogram/client/types/input_media/input_media_video.py index 6b64caa8..a33ae93a 100644 --- a/pyrogram/client/types/input_media/input_media_video.py +++ b/pyrogram/client/types/input_media/input_media_video.py @@ -32,6 +32,10 @@ class InputMediaVideo(InputMedia): pass a file path as string to upload a new video that exists on your local machine. Sending video by a URL is currently unsupported. + file_ref (``str``, *optional*): + A valid file reference obtained by a recently fetched media message. + To be used in combination with a file id in case a file reference is needed. + thumb (``str``): Thumbnail of the video sent. The thumbnail should be in JPEG format and less than 200 KB in size. @@ -64,6 +68,7 @@ class InputMediaVideo(InputMedia): def __init__( self, media: str, + file_ref: str = None, thumb: str = None, caption: str = "", parse_mode: Union[str, None] = object, @@ -72,7 +77,7 @@ class InputMediaVideo(InputMedia): duration: int = 0, supports_streaming: bool = True ): - super().__init__(media, caption, parse_mode) + super().__init__(media, file_ref, caption, parse_mode) self.thumb = thumb self.width = width diff --git a/pyrogram/client/types/messages_and_media/animation.py b/pyrogram/client/types/messages_and_media/animation.py index 2b30bc8c..6331cf13 100644 --- a/pyrogram/client/types/messages_and_media/animation.py +++ b/pyrogram/client/types/messages_and_media/animation.py @@ -23,7 +23,7 @@ import pyrogram from pyrogram.api import types from .thumbnail import Thumbnail from ..object import Object -from ...ext.utils import encode +from ...ext.utils import encode, encode_file_ref class Animation(Object): @@ -33,7 +33,7 @@ class Animation(Object): file_id (``str``): Unique identifier for this file. - file_ref (``bytes``): + file_ref (``str``): Up to date file reference. width (``int``): @@ -66,7 +66,7 @@ class Animation(Object): *, client: "pyrogram.BaseClient" = None, file_id: str, - file_ref: bytes, + file_ref: str, width: int, height: int, duration: int, @@ -106,7 +106,7 @@ class Animation(Object): animation.access_hash ) ), - file_ref=animation.file_reference, + file_ref=encode_file_ref(animation.file_reference), width=getattr(video_attributes, "w", 0), height=getattr(video_attributes, "h", 0), duration=getattr(video_attributes, "duration", 0), diff --git a/pyrogram/client/types/messages_and_media/audio.py b/pyrogram/client/types/messages_and_media/audio.py index 7cf0d29c..e9b545e7 100644 --- a/pyrogram/client/types/messages_and_media/audio.py +++ b/pyrogram/client/types/messages_and_media/audio.py @@ -23,7 +23,7 @@ import pyrogram from pyrogram.api import types from .thumbnail import Thumbnail from ..object import Object -from ...ext.utils import encode +from ...ext.utils import encode, encode_file_ref class Audio(Object): @@ -33,7 +33,7 @@ class Audio(Object): file_id (``str``): Unique identifier for this file. - file_ref (``bytes``): + file_ref (``str``): Up to date file reference. duration (``int``): @@ -66,7 +66,7 @@ class Audio(Object): *, client: "pyrogram.BaseClient" = None, file_id: str, - file_ref: bytes, + file_ref: str, duration: int, file_name: str = None, mime_type: str = None, @@ -106,7 +106,7 @@ class Audio(Object): audio.access_hash ) ), - file_ref=audio.file_reference, + file_ref=encode_file_ref(audio.file_reference), duration=audio_attributes.duration, performer=audio_attributes.performer, title=audio_attributes.title, diff --git a/pyrogram/client/types/messages_and_media/document.py b/pyrogram/client/types/messages_and_media/document.py index 30c4b8d2..b77431a2 100644 --- a/pyrogram/client/types/messages_and_media/document.py +++ b/pyrogram/client/types/messages_and_media/document.py @@ -23,7 +23,7 @@ import pyrogram from pyrogram.api import types from .thumbnail import Thumbnail from ..object import Object -from ...ext.utils import encode +from ...ext.utils import encode, encode_file_ref class Document(Object): @@ -33,7 +33,7 @@ class Document(Object): file_id (``str``): Unique file identifier. - file_ref (``bytes``): + file_ref (``str``): Up to date file reference. file_name (``str``, *optional*): @@ -57,7 +57,7 @@ class Document(Object): *, client: "pyrogram.BaseClient" = None, file_id: str, - file_ref: bytes, + file_ref: str, file_name: str = None, mime_type: str = None, file_size: int = None, @@ -86,7 +86,7 @@ class Document(Object): document.access_hash ) ), - file_ref=document.file_reference, + file_ref=encode_file_ref(document.file_reference), file_name=file_name, mime_type=document.mime_type, file_size=document.size, diff --git a/pyrogram/client/types/messages_and_media/message.py b/pyrogram/client/types/messages_and_media/message.py index 54aabdfc..a4092cb6 100644 --- a/pyrogram/client/types/messages_and_media/message.py +++ b/pyrogram/client/types/messages_and_media/message.py @@ -2642,20 +2642,28 @@ class Message(Object, Update): if self.photo: file_id = self.photo.file_id + file_ref = self.photo.file_ref elif self.audio: file_id = self.audio.file_id + file_ref = self.audio.file_ref elif self.document: file_id = self.document.file_id + file_ref = self.document.file_ref elif self.video: file_id = self.video.file_id + file_ref = self.video.file_ref elif self.animation: file_id = self.animation.file_id + file_ref = self.animation.file_ref elif self.voice: file_id = self.voice.file_id + file_ref = self.voice.file_ref elif self.sticker: file_id = self.sticker.file_id + file_ref = self.sticker.file_ref elif self.video_note: file_id = self.video_note.file_id + file_ref = self.video_note.file_ref elif self.contact: return self._client.send_contact( chat_id, @@ -2700,9 +2708,9 @@ class Message(Object, Update): raise ValueError("Unknown media type") if self.sticker or self.video_note: # Sticker and VideoNote should have no caption - return send_media(file_id=file_id) + return send_media(file_id=file_id, file_ref=file_ref) else: - return send_media(file_id=file_id, caption=caption, parse_mode="html") + return send_media(file_id=file_id, file_ref=file_ref, caption=caption, parse_mode="html") else: raise ValueError("Can't copy this message") else: diff --git a/pyrogram/client/types/messages_and_media/photo.py b/pyrogram/client/types/messages_and_media/photo.py index 72d4c25c..81fd9d78 100644 --- a/pyrogram/client/types/messages_and_media/photo.py +++ b/pyrogram/client/types/messages_and_media/photo.py @@ -23,7 +23,7 @@ import pyrogram from pyrogram.api import types from .thumbnail import Thumbnail from ..object import Object -from ...ext.utils import encode +from ...ext.utils import encode, encode_file_ref class Photo(Object): @@ -33,7 +33,7 @@ class Photo(Object): file_id (``str``): Unique identifier for this photo. - file_ref (``bytes``): + file_ref (``str``): Up to date file reference. width (``int``): @@ -57,7 +57,7 @@ class Photo(Object): *, client: "pyrogram.BaseClient" = None, file_id: str, - file_ref: bytes, + file_ref: str, width: int, height: int, file_size: int, @@ -88,7 +88,7 @@ class Photo(Object): big.location.local_id ) ), - file_ref=photo.file_reference, + file_ref=encode_file_ref(photo.file_reference), width=big.w, height=big.h, file_size=big.size, diff --git a/pyrogram/client/types/messages_and_media/sticker.py b/pyrogram/client/types/messages_and_media/sticker.py index 6442c24d..086d34af 100644 --- a/pyrogram/client/types/messages_and_media/sticker.py +++ b/pyrogram/client/types/messages_and_media/sticker.py @@ -25,7 +25,7 @@ from pyrogram.api import types, functions from pyrogram.errors import StickersetInvalid from .thumbnail import Thumbnail from ..object import Object -from ...ext.utils import encode +from ...ext.utils import encode, encode_file_ref class Sticker(Object): @@ -35,7 +35,7 @@ class Sticker(Object): file_id (``str``): Unique identifier for this file. - file_ref (``bytes``): + file_ref (``str``): Up to date file reference. width (``int``): @@ -76,7 +76,7 @@ class Sticker(Object): *, client: "pyrogram.BaseClient" = None, file_id: str, - file_ref: bytes, + file_ref: str, width: int, height: int, is_animated: bool, @@ -140,7 +140,7 @@ class Sticker(Object): sticker.access_hash ) ), - file_ref=sticker.file_reference, + file_ref=encode_file_ref(sticker.file_reference), width=image_size_attributes.w if image_size_attributes else 512, height=image_size_attributes.h if image_size_attributes else 512, is_animated=sticker.mime_type == "application/x-tgsticker", diff --git a/pyrogram/client/types/messages_and_media/video.py b/pyrogram/client/types/messages_and_media/video.py index c61e8a6e..5ebba363 100644 --- a/pyrogram/client/types/messages_and_media/video.py +++ b/pyrogram/client/types/messages_and_media/video.py @@ -23,7 +23,7 @@ import pyrogram from pyrogram.api import types from .thumbnail import Thumbnail from ..object import Object -from ...ext.utils import encode +from ...ext.utils import encode, encode_file_ref class Video(Object): @@ -33,7 +33,7 @@ class Video(Object): file_id (``str``): Unique identifier for this file. - file_ref (``bytes``): + file_ref (``str``): Up to date file reference. width (``int``): @@ -69,7 +69,7 @@ class Video(Object): *, client: "pyrogram.BaseClient" = None, file_id: str, - file_ref: bytes, + file_ref: str, width: int, height: int, duration: int, @@ -111,7 +111,7 @@ class Video(Object): video.access_hash ) ), - file_ref=video.file_reference, + file_ref=encode_file_ref(video.file_reference), width=video_attributes.w, height=video_attributes.h, duration=video_attributes.duration, diff --git a/pyrogram/client/types/messages_and_media/video_note.py b/pyrogram/client/types/messages_and_media/video_note.py index a1d95798..9194bc9a 100644 --- a/pyrogram/client/types/messages_and_media/video_note.py +++ b/pyrogram/client/types/messages_and_media/video_note.py @@ -23,7 +23,7 @@ import pyrogram from pyrogram.api import types from .thumbnail import Thumbnail from ..object import Object -from ...ext.utils import encode +from ...ext.utils import encode, encode_file_ref class VideoNote(Object): @@ -33,7 +33,7 @@ class VideoNote(Object): file_id (``str``): Unique identifier for this file. - file_ref (``bytes``): + file_ref (``str``): Up to date file reference. length (``int``): @@ -60,7 +60,7 @@ class VideoNote(Object): *, client: "pyrogram.BaseClient" = None, file_id: str, - file_ref: bytes, + file_ref: str, length: int, duration: int, thumbs: List[Thumbnail] = None, @@ -91,7 +91,7 @@ class VideoNote(Object): video_note.access_hash ) ), - file_ref=video_note.file_reference, + file_ref=encode_file_ref(video_note.file_reference), length=video_attributes.w, duration=video_attributes.duration, file_size=video_note.size, diff --git a/pyrogram/client/types/messages_and_media/voice.py b/pyrogram/client/types/messages_and_media/voice.py index 558f7475..d614eba6 100644 --- a/pyrogram/client/types/messages_and_media/voice.py +++ b/pyrogram/client/types/messages_and_media/voice.py @@ -21,7 +21,7 @@ from struct import pack import pyrogram from pyrogram.api import types from ..object import Object -from ...ext.utils import encode +from ...ext.utils import encode, encode_file_ref class Voice(Object): @@ -31,7 +31,7 @@ class Voice(Object): file_id (``str``): Unique identifier for this file. - file_ref (``bytes``): + file_ref (``str``): Up to date file reference. duration (``int``): @@ -55,7 +55,7 @@ class Voice(Object): *, client: "pyrogram.BaseClient" = None, file_id: str, - file_ref: bytes, + file_ref: str, duration: int, waveform: bytes = None, mime_type: str = None, @@ -84,7 +84,7 @@ class Voice(Object): voice.access_hash ) ), - file_ref=voice.file_reference, + file_ref=encode_file_ref(voice.file_reference), duration=attributes.duration, mime_type=voice.mime_type, file_size=voice.size,