2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-26 03:47:24 +00:00

Fix FILE_REFERENCE_* errors for uploads

This commit is contained in:
Dan 2019-09-21 22:13:02 +02:00
parent 1cd94520bf
commit 92c1b48132
31 changed files with 163 additions and 69 deletions

View File

@ -1869,7 +1869,7 @@ class Client(Methods, BaseClient):
peer_access_hash: int, peer_access_hash: int,
volume_id: int, volume_id: int,
local_id: int, local_id: int,
file_ref: bytes, file_ref: str,
file_size: int, file_size: int,
is_big: bool, is_big: bool,
progress: callable, progress: callable,
@ -1910,6 +1910,8 @@ class Client(Methods, BaseClient):
self.media_sessions[dc_id] = session self.media_sessions[dc_id] = session
file_ref = utils.decode_file_ref(file_ref)
if media_type == 1: if media_type == 1:
location = types.InputPeerPhotoFileLocation( location = types.InputPeerPhotoFileLocation(
peer=types.InputPeerUser( peer=types.InputPeerUser(

View File

@ -22,7 +22,7 @@ class FileData:
self, *, media_type: int = None, dc_id: int = None, document_id: int = None, access_hash: int = None, 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, 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, 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.media_type = media_type
self.dc_id = dc_id self.dc_id = dc_id

View File

@ -70,6 +70,17 @@ def encode(s: bytes) -> str:
return base64.urlsafe_b64encode(r).decode().rstrip("=") 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): def get_offset_date(dialogs):
for m in reversed(dialogs.messages): for m in reversed(dialogs.messages):
if isinstance(m, types.MessageEmpty): if isinstance(m, types.MessageEmpty):
@ -82,6 +93,7 @@ def get_offset_date(dialogs):
def get_input_media_from_file_id( def get_input_media_from_file_id(
file_id_str: str, file_id_str: str,
file_ref: str = None,
expected_media_type: int = None expected_media_type: int = None
) -> Union[types.InputMediaPhoto, types.InputMediaDocument]: ) -> Union[types.InputMediaPhoto, types.InputMediaDocument]:
try: try:
@ -111,7 +123,7 @@ def get_input_media_from_file_id(
id=types.InputPhoto( id=types.InputPhoto(
id=file_id, id=file_id,
access_hash=access_hash, 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=types.InputDocument(
id=file_id, id=file_id,
access_hash=access_hash, access_hash=access_hash,
file_reference=b"" file_reference=decode_file_ref(file_ref)
) )
) )

View File

@ -35,7 +35,7 @@ class DownloadMedia(BaseClient):
def download_media( def download_media(
self, self,
message: Union["pyrogram.Message", str], message: Union["pyrogram.Message", str],
file_ref: bytes = None, file_ref: str = None,
file_name: str = DEFAULT_DOWNLOAD_DIR, file_name: str = DEFAULT_DOWNLOAD_DIR,
block: bool = True, block: bool = True,
progress: callable = None, progress: callable = None,
@ -48,8 +48,9 @@ class DownloadMedia(BaseClient):
Pass a Message containing the media, the media itself (message.audio, message.video, ...) or Pass a Message containing the media, the media itself (message.audio, message.video, ...) or
the file id as string. the file id as string.
file_ref (``bytes``, *optional*): file_ref (``str``, *optional*):
A valid file reference obtained by a recently fetched media message. 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*): file_name (``str``, *optional*):
A custom *file_name* to be used instead of the one provided by Telegram. 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, file_size=file_size,
mime_type=mime_type, mime_type=mime_type,
date=date, date=date,
file_ref=file_ref or b"" file_ref=file_ref
) )
def get_existing_attributes() -> dict: def get_existing_attributes() -> dict:

View File

@ -77,35 +77,35 @@ class EditInlineMedia(BaseClient):
url=media.media url=media.media
) )
else: 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): elif isinstance(media, InputMediaVideo):
if media.media.startswith("http"): if media.media.startswith("http"):
media = types.InputMediaDocumentExternal( media = types.InputMediaDocumentExternal(
url=media.media url=media.media
) )
else: 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): elif isinstance(media, InputMediaAudio):
if media.media.startswith("http"): if media.media.startswith("http"):
media = types.InputMediaDocumentExternal( media = types.InputMediaDocumentExternal(
url=media.media url=media.media
) )
else: 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): elif isinstance(media, InputMediaAnimation):
if media.media.startswith("http"): if media.media.startswith("http"):
media = types.InputMediaDocumentExternal( media = types.InputMediaDocumentExternal(
url=media.media url=media.media
) )
else: 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): elif isinstance(media, InputMediaDocument):
if media.media.startswith("http"): if media.media.startswith("http"):
media = types.InputMediaDocumentExternal( media = types.InputMediaDocumentExternal(
url=media.media url=media.media
) )
else: 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( return self.send(
functions.messages.EditInlineBotMessage( functions.messages.EditInlineBotMessage(

View File

@ -100,7 +100,7 @@ class EditMessageMedia(BaseClient):
url=media.media url=media.media
) )
else: 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): elif isinstance(media, InputMediaVideo):
if os.path.exists(media.media): if os.path.exists(media.media):
media = self.send( media = self.send(
@ -137,7 +137,7 @@ class EditMessageMedia(BaseClient):
url=media.media url=media.media
) )
else: 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): elif isinstance(media, InputMediaAudio):
if os.path.exists(media.media): if os.path.exists(media.media):
media = self.send( media = self.send(
@ -173,7 +173,7 @@ class EditMessageMedia(BaseClient):
url=media.media url=media.media
) )
else: 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): elif isinstance(media, InputMediaAnimation):
if os.path.exists(media.media): if os.path.exists(media.media):
media = self.send( media = self.send(
@ -211,7 +211,7 @@ class EditMessageMedia(BaseClient):
url=media.media url=media.media
) )
else: 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): elif isinstance(media, InputMediaDocument):
if os.path.exists(media.media): if os.path.exists(media.media):
media = self.send( media = self.send(
@ -242,7 +242,7 @@ class EditMessageMedia(BaseClient):
url=media.media url=media.media
) )
else: 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( r = self.send(
functions.messages.EditMessage( functions.messages.EditMessage(

View File

@ -30,6 +30,7 @@ class SendAnimation(BaseClient):
self, self,
chat_id: Union[int, str], chat_id: Union[int, str],
animation: str, animation: str,
file_ref: str = None,
caption: str = "", caption: str = "",
unsave: bool = False, unsave: bool = False,
parse_mode: Union[str, None] = object, 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 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. 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*): caption (``str``, *optional*):
Animation caption, 0-1024 characters. Animation caption, 0-1024 characters.
@ -176,7 +181,7 @@ class SendAnimation(BaseClient):
url=animation url=animation
) )
else: 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: while True:
try: try:
@ -209,7 +214,7 @@ class SendAnimation(BaseClient):
if unsave: if unsave:
document = message.animation or message.document 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( self.send(
functions.messages.SaveGif( functions.messages.SaveGif(

View File

@ -30,6 +30,7 @@ class SendAudio(BaseClient):
self, self,
chat_id: Union[int, str], chat_id: Union[int, str],
audio: str, audio: str,
file_ref: str = None,
caption: str = "", caption: str = "",
parse_mode: Union[str, None] = object, parse_mode: Union[str, None] = object,
duration: int = 0, 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 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. 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*): caption (``str``, *optional*):
Audio caption, 0-1024 characters. Audio caption, 0-1024 characters.
@ -174,7 +179,7 @@ class SendAudio(BaseClient):
url=audio url=audio
) )
else: 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: while True:
try: try:

View File

@ -28,6 +28,7 @@ class SendCachedMedia(BaseClient):
self, self,
chat_id: Union[int, str], chat_id: Union[int, str],
file_id: str, file_id: str,
file_ref: str = None,
caption: str = "", caption: str = "",
parse_mode: Union[str, None] = object, parse_mode: Union[str, None] = object,
disable_notification: bool = None, disable_notification: bool = None,
@ -56,6 +57,10 @@ class SendCachedMedia(BaseClient):
Media to send. Media to send.
Pass a file_id as string to send a media that exists on the Telegram servers. 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*): caption (``bool``, *optional*):
Media caption, 0-1024 characters. Media caption, 0-1024 characters.
@ -92,7 +97,7 @@ class SendCachedMedia(BaseClient):
r = self.send( r = self.send(
functions.messages.SendMedia( functions.messages.SendMedia(
peer=self.resolve_peer(chat_id), 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, silent=disable_notification or None,
reply_to_msg_id=reply_to_message_id, reply_to_msg_id=reply_to_message_id,
random_id=self.rnd_id(), random_id=self.rnd_id(),

View File

@ -30,6 +30,7 @@ class SendDocument(BaseClient):
self, self,
chat_id: Union[int, str], chat_id: Union[int, str],
document: str, document: str,
file_ref: str = None,
thumb: str = None, thumb: str = None,
caption: str = "", caption: str = "",
parse_mode: Union[str, None] = object, 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 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. 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*): thumb (``str``, *optional*):
Thumbnail of the file sent. Thumbnail of the file sent.
The thumbnail should be in JPEG format and less than 200 KB in size. The thumbnail should be in JPEG format and less than 200 KB in size.
@ -149,7 +154,7 @@ class SendDocument(BaseClient):
url=document url=document
) )
else: 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: while True:
try: try:

View File

@ -119,7 +119,7 @@ class SendMediaGroup(BaseClient):
) )
) )
else: 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): elif isinstance(i, pyrogram.InputMediaVideo):
if os.path.exists(i.media): if os.path.exists(i.media):
while True: while True:
@ -174,7 +174,7 @@ class SendMediaGroup(BaseClient):
) )
) )
else: 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( multi_media.append(
types.InputSingleMedia( types.InputSingleMedia(

View File

@ -30,6 +30,7 @@ class SendPhoto(BaseClient):
self, self,
chat_id: Union[int, str], chat_id: Union[int, str],
photo: str, photo: str,
file_ref: str = None,
caption: str = "", caption: str = "",
parse_mode: Union[str, None] = object, parse_mode: Union[str, None] = object,
ttl_seconds: int = None, 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 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. 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*): caption (``bool``, *optional*):
Photo caption, 0-1024 characters. Photo caption, 0-1024 characters.
@ -144,7 +149,7 @@ class SendPhoto(BaseClient):
ttl_seconds=ttl_seconds ttl_seconds=ttl_seconds
) )
else: 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: while True:
try: try:

View File

@ -30,6 +30,7 @@ class SendSticker(BaseClient):
self, self,
chat_id: Union[int, str], chat_id: Union[int, str],
sticker: str, sticker: str,
file_ref: str = None,
disable_notification: bool = None, disable_notification: bool = None,
reply_to_message_id: int = None, reply_to_message_id: int = None,
schedule_date: 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 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. 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*): disable_notification (``bool``, *optional*):
Sends the message silently. Sends the message silently.
Users will receive a notification with no sound. Users will receive a notification with no sound.
@ -122,7 +127,7 @@ class SendSticker(BaseClient):
url=sticker url=sticker
) )
else: 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: while True:
try: try:

View File

@ -30,6 +30,7 @@ class SendVideo(BaseClient):
self, self,
chat_id: Union[int, str], chat_id: Union[int, str],
video: str, video: str,
file_ref: str = None,
caption: str = "", caption: str = "",
parse_mode: Union[str, None] = object, parse_mode: Union[str, None] = object,
duration: int = 0, 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 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. 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*): caption (``str``, *optional*):
Video caption, 0-1024 characters. Video caption, 0-1024 characters.
@ -172,7 +177,7 @@ class SendVideo(BaseClient):
url=video url=video
) )
else: 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: while True:
try: try:

View File

@ -30,6 +30,7 @@ class SendVideoNote(BaseClient):
self, self,
chat_id: Union[int, str], chat_id: Union[int, str],
video_note: str, video_note: str,
file_ref: str = None,
duration: int = 0, duration: int = 0,
length: int = 1, length: int = 1,
thumb: str = None, 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. 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. 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 (``int``, *optional*):
Duration of sent video in seconds. Duration of sent video in seconds.
@ -140,7 +145,7 @@ class SendVideoNote(BaseClient):
] ]
) )
else: 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: while True:
try: try:

View File

@ -30,6 +30,7 @@ class SendVoice(BaseClient):
self, self,
chat_id: Union[int, str], chat_id: Union[int, str],
voice: str, voice: str,
file_ref=None,
caption: str = "", caption: str = "",
parse_mode: Union[str, None] = object, parse_mode: Union[str, None] = object,
duration: int = 0, 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 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. 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*): caption (``str``, *optional*):
Voice message caption, 0-1024 characters. Voice message caption, 0-1024 characters.
@ -144,7 +149,7 @@ class SendVoice(BaseClient):
url=voice url=voice
) )
else: 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: while True:
try: try:

View File

@ -31,9 +31,10 @@ class InputMedia(Object):
- :obj:`InputMediaVideo` - :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__() super().__init__()
self.media = media self.media = media
self.file_ref = file_ref
self.caption = caption self.caption = caption
self.parse_mode = parse_mode self.parse_mode = parse_mode

View File

@ -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_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. 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*): thumb (``str``, *optional*):
Thumbnail of the animation file sent. Thumbnail of the animation file sent.
The thumbnail should be in JPEG format and less than 200 KB in size. The thumbnail should be in JPEG format and less than 200 KB in size.
@ -59,6 +63,7 @@ class InputMediaAnimation(InputMedia):
def __init__( def __init__(
self, self,
media: str, media: str,
file_ref: str = None,
thumb: str = None, thumb: str = None,
caption: str = "", caption: str = "",
parse_mode: Union[str, None] = object, parse_mode: Union[str, None] = object,
@ -66,7 +71,7 @@ class InputMediaAnimation(InputMedia):
height: int = 0, height: int = 0,
duration: int = 0 duration: int = 0
): ):
super().__init__(media, caption, parse_mode) super().__init__(media, file_ref, caption, parse_mode)
self.thumb = thumb self.thumb = thumb
self.width = width self.width = width

View File

@ -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_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. 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*): thumb (``str``, *optional*):
Thumbnail of the music file album cover. Thumbnail of the music file album cover.
The thumbnail should be in JPEG format and less than 200 KB in size. The thumbnail should be in JPEG format and less than 200 KB in size.
@ -61,6 +65,7 @@ class InputMediaAudio(InputMedia):
def __init__( def __init__(
self, self,
media: str, media: str,
file_ref: str = None,
thumb: str = None, thumb: str = None,
caption: str = "", caption: str = "",
parse_mode: Union[str, None] = object, parse_mode: Union[str, None] = object,
@ -68,7 +73,7 @@ class InputMediaAudio(InputMedia):
performer: int = "", performer: int = "",
title: str = "" title: str = ""
): ):
super().__init__(media, caption, parse_mode) super().__init__(media, file_ref, caption, parse_mode)
self.thumb = thumb self.thumb = thumb
self.duration = duration self.duration = duration

View File

@ -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_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. 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``): thumb (``str``):
Thumbnail of the file sent. Thumbnail of the file sent.
The thumbnail should be in JPEG format and less than 200 KB in size. The thumbnail should be in JPEG format and less than 200 KB in size.
@ -50,10 +54,11 @@ class InputMediaDocument(InputMedia):
def __init__( def __init__(
self, self,
media: str, media: str,
file_ref: str = None,
thumb: str = None, thumb: str = None,
caption: str = "", caption: str = "",
parse_mode: Union[str, None] = object parse_mode: Union[str, None] = object
): ):
super().__init__(media, caption, parse_mode) super().__init__(media, file_ref, caption, parse_mode)
self.thumb = thumb self.thumb = thumb

View File

@ -32,6 +32,10 @@ class InputMediaPhoto(InputMedia):
pass a file path as string to upload a new photo that exists on your local machine. 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. 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 (``str``, *optional*):
Caption of the photo to be sent, 0-1024 characters Caption of the photo to be sent, 0-1024 characters
@ -46,7 +50,8 @@ class InputMediaPhoto(InputMedia):
def __init__( def __init__(
self, self,
media: str, media: str,
file_ref: str = None,
caption: str = "", caption: str = "",
parse_mode: Union[str, None] = object parse_mode: Union[str, None] = object
): ):
super().__init__(media, caption, parse_mode) super().__init__(media, file_ref, caption, parse_mode)

View File

@ -32,6 +32,10 @@ class InputMediaVideo(InputMedia):
pass a file path as string to upload a new video that exists on your local machine. 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. 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``): thumb (``str``):
Thumbnail of the video sent. Thumbnail of the video sent.
The thumbnail should be in JPEG format and less than 200 KB in size. The thumbnail should be in JPEG format and less than 200 KB in size.
@ -64,6 +68,7 @@ class InputMediaVideo(InputMedia):
def __init__( def __init__(
self, self,
media: str, media: str,
file_ref: str = None,
thumb: str = None, thumb: str = None,
caption: str = "", caption: str = "",
parse_mode: Union[str, None] = object, parse_mode: Union[str, None] = object,
@ -72,7 +77,7 @@ class InputMediaVideo(InputMedia):
duration: int = 0, duration: int = 0,
supports_streaming: bool = True supports_streaming: bool = True
): ):
super().__init__(media, caption, parse_mode) super().__init__(media, file_ref, caption, parse_mode)
self.thumb = thumb self.thumb = thumb
self.width = width self.width = width

View File

@ -23,7 +23,7 @@ import pyrogram
from pyrogram.api import types from pyrogram.api import types
from .thumbnail import Thumbnail from .thumbnail import Thumbnail
from ..object import Object from ..object import Object
from ...ext.utils import encode from ...ext.utils import encode, encode_file_ref
class Animation(Object): class Animation(Object):
@ -33,7 +33,7 @@ class Animation(Object):
file_id (``str``): file_id (``str``):
Unique identifier for this file. Unique identifier for this file.
file_ref (``bytes``): file_ref (``str``):
Up to date file reference. Up to date file reference.
width (``int``): width (``int``):
@ -66,7 +66,7 @@ class Animation(Object):
*, *,
client: "pyrogram.BaseClient" = None, client: "pyrogram.BaseClient" = None,
file_id: str, file_id: str,
file_ref: bytes, file_ref: str,
width: int, width: int,
height: int, height: int,
duration: int, duration: int,
@ -106,7 +106,7 @@ class Animation(Object):
animation.access_hash animation.access_hash
) )
), ),
file_ref=animation.file_reference, file_ref=encode_file_ref(animation.file_reference),
width=getattr(video_attributes, "w", 0), width=getattr(video_attributes, "w", 0),
height=getattr(video_attributes, "h", 0), height=getattr(video_attributes, "h", 0),
duration=getattr(video_attributes, "duration", 0), duration=getattr(video_attributes, "duration", 0),

View File

@ -23,7 +23,7 @@ import pyrogram
from pyrogram.api import types from pyrogram.api import types
from .thumbnail import Thumbnail from .thumbnail import Thumbnail
from ..object import Object from ..object import Object
from ...ext.utils import encode from ...ext.utils import encode, encode_file_ref
class Audio(Object): class Audio(Object):
@ -33,7 +33,7 @@ class Audio(Object):
file_id (``str``): file_id (``str``):
Unique identifier for this file. Unique identifier for this file.
file_ref (``bytes``): file_ref (``str``):
Up to date file reference. Up to date file reference.
duration (``int``): duration (``int``):
@ -66,7 +66,7 @@ class Audio(Object):
*, *,
client: "pyrogram.BaseClient" = None, client: "pyrogram.BaseClient" = None,
file_id: str, file_id: str,
file_ref: bytes, file_ref: str,
duration: int, duration: int,
file_name: str = None, file_name: str = None,
mime_type: str = None, mime_type: str = None,
@ -106,7 +106,7 @@ class Audio(Object):
audio.access_hash audio.access_hash
) )
), ),
file_ref=audio.file_reference, file_ref=encode_file_ref(audio.file_reference),
duration=audio_attributes.duration, duration=audio_attributes.duration,
performer=audio_attributes.performer, performer=audio_attributes.performer,
title=audio_attributes.title, title=audio_attributes.title,

View File

@ -23,7 +23,7 @@ import pyrogram
from pyrogram.api import types from pyrogram.api import types
from .thumbnail import Thumbnail from .thumbnail import Thumbnail
from ..object import Object from ..object import Object
from ...ext.utils import encode from ...ext.utils import encode, encode_file_ref
class Document(Object): class Document(Object):
@ -33,7 +33,7 @@ class Document(Object):
file_id (``str``): file_id (``str``):
Unique file identifier. Unique file identifier.
file_ref (``bytes``): file_ref (``str``):
Up to date file reference. Up to date file reference.
file_name (``str``, *optional*): file_name (``str``, *optional*):
@ -57,7 +57,7 @@ class Document(Object):
*, *,
client: "pyrogram.BaseClient" = None, client: "pyrogram.BaseClient" = None,
file_id: str, file_id: str,
file_ref: bytes, file_ref: str,
file_name: str = None, file_name: str = None,
mime_type: str = None, mime_type: str = None,
file_size: int = None, file_size: int = None,
@ -86,7 +86,7 @@ class Document(Object):
document.access_hash document.access_hash
) )
), ),
file_ref=document.file_reference, file_ref=encode_file_ref(document.file_reference),
file_name=file_name, file_name=file_name,
mime_type=document.mime_type, mime_type=document.mime_type,
file_size=document.size, file_size=document.size,

View File

@ -2642,20 +2642,28 @@ class Message(Object, Update):
if self.photo: if self.photo:
file_id = self.photo.file_id file_id = self.photo.file_id
file_ref = self.photo.file_ref
elif self.audio: elif self.audio:
file_id = self.audio.file_id file_id = self.audio.file_id
file_ref = self.audio.file_ref
elif self.document: elif self.document:
file_id = self.document.file_id file_id = self.document.file_id
file_ref = self.document.file_ref
elif self.video: elif self.video:
file_id = self.video.file_id file_id = self.video.file_id
file_ref = self.video.file_ref
elif self.animation: elif self.animation:
file_id = self.animation.file_id file_id = self.animation.file_id
file_ref = self.animation.file_ref
elif self.voice: elif self.voice:
file_id = self.voice.file_id file_id = self.voice.file_id
file_ref = self.voice.file_ref
elif self.sticker: elif self.sticker:
file_id = self.sticker.file_id file_id = self.sticker.file_id
file_ref = self.sticker.file_ref
elif self.video_note: elif self.video_note:
file_id = self.video_note.file_id file_id = self.video_note.file_id
file_ref = self.video_note.file_ref
elif self.contact: elif self.contact:
return self._client.send_contact( return self._client.send_contact(
chat_id, chat_id,
@ -2700,9 +2708,9 @@ class Message(Object, Update):
raise ValueError("Unknown media type") raise ValueError("Unknown media type")
if self.sticker or self.video_note: # Sticker and VideoNote should have no caption 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: 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: else:
raise ValueError("Can't copy this message") raise ValueError("Can't copy this message")
else: else:

View File

@ -23,7 +23,7 @@ import pyrogram
from pyrogram.api import types from pyrogram.api import types
from .thumbnail import Thumbnail from .thumbnail import Thumbnail
from ..object import Object from ..object import Object
from ...ext.utils import encode from ...ext.utils import encode, encode_file_ref
class Photo(Object): class Photo(Object):
@ -33,7 +33,7 @@ class Photo(Object):
file_id (``str``): file_id (``str``):
Unique identifier for this photo. Unique identifier for this photo.
file_ref (``bytes``): file_ref (``str``):
Up to date file reference. Up to date file reference.
width (``int``): width (``int``):
@ -57,7 +57,7 @@ class Photo(Object):
*, *,
client: "pyrogram.BaseClient" = None, client: "pyrogram.BaseClient" = None,
file_id: str, file_id: str,
file_ref: bytes, file_ref: str,
width: int, width: int,
height: int, height: int,
file_size: int, file_size: int,
@ -88,7 +88,7 @@ class Photo(Object):
big.location.local_id big.location.local_id
) )
), ),
file_ref=photo.file_reference, file_ref=encode_file_ref(photo.file_reference),
width=big.w, width=big.w,
height=big.h, height=big.h,
file_size=big.size, file_size=big.size,

View File

@ -25,7 +25,7 @@ from pyrogram.api import types, functions
from pyrogram.errors import StickersetInvalid from pyrogram.errors import StickersetInvalid
from .thumbnail import Thumbnail from .thumbnail import Thumbnail
from ..object import Object from ..object import Object
from ...ext.utils import encode from ...ext.utils import encode, encode_file_ref
class Sticker(Object): class Sticker(Object):
@ -35,7 +35,7 @@ class Sticker(Object):
file_id (``str``): file_id (``str``):
Unique identifier for this file. Unique identifier for this file.
file_ref (``bytes``): file_ref (``str``):
Up to date file reference. Up to date file reference.
width (``int``): width (``int``):
@ -76,7 +76,7 @@ class Sticker(Object):
*, *,
client: "pyrogram.BaseClient" = None, client: "pyrogram.BaseClient" = None,
file_id: str, file_id: str,
file_ref: bytes, file_ref: str,
width: int, width: int,
height: int, height: int,
is_animated: bool, is_animated: bool,
@ -140,7 +140,7 @@ class Sticker(Object):
sticker.access_hash 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, width=image_size_attributes.w if image_size_attributes else 512,
height=image_size_attributes.h 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", is_animated=sticker.mime_type == "application/x-tgsticker",

View File

@ -23,7 +23,7 @@ import pyrogram
from pyrogram.api import types from pyrogram.api import types
from .thumbnail import Thumbnail from .thumbnail import Thumbnail
from ..object import Object from ..object import Object
from ...ext.utils import encode from ...ext.utils import encode, encode_file_ref
class Video(Object): class Video(Object):
@ -33,7 +33,7 @@ class Video(Object):
file_id (``str``): file_id (``str``):
Unique identifier for this file. Unique identifier for this file.
file_ref (``bytes``): file_ref (``str``):
Up to date file reference. Up to date file reference.
width (``int``): width (``int``):
@ -69,7 +69,7 @@ class Video(Object):
*, *,
client: "pyrogram.BaseClient" = None, client: "pyrogram.BaseClient" = None,
file_id: str, file_id: str,
file_ref: bytes, file_ref: str,
width: int, width: int,
height: int, height: int,
duration: int, duration: int,
@ -111,7 +111,7 @@ class Video(Object):
video.access_hash video.access_hash
) )
), ),
file_ref=video.file_reference, file_ref=encode_file_ref(video.file_reference),
width=video_attributes.w, width=video_attributes.w,
height=video_attributes.h, height=video_attributes.h,
duration=video_attributes.duration, duration=video_attributes.duration,

View File

@ -23,7 +23,7 @@ import pyrogram
from pyrogram.api import types from pyrogram.api import types
from .thumbnail import Thumbnail from .thumbnail import Thumbnail
from ..object import Object from ..object import Object
from ...ext.utils import encode from ...ext.utils import encode, encode_file_ref
class VideoNote(Object): class VideoNote(Object):
@ -33,7 +33,7 @@ class VideoNote(Object):
file_id (``str``): file_id (``str``):
Unique identifier for this file. Unique identifier for this file.
file_ref (``bytes``): file_ref (``str``):
Up to date file reference. Up to date file reference.
length (``int``): length (``int``):
@ -60,7 +60,7 @@ class VideoNote(Object):
*, *,
client: "pyrogram.BaseClient" = None, client: "pyrogram.BaseClient" = None,
file_id: str, file_id: str,
file_ref: bytes, file_ref: str,
length: int, length: int,
duration: int, duration: int,
thumbs: List[Thumbnail] = None, thumbs: List[Thumbnail] = None,
@ -91,7 +91,7 @@ class VideoNote(Object):
video_note.access_hash video_note.access_hash
) )
), ),
file_ref=video_note.file_reference, file_ref=encode_file_ref(video_note.file_reference),
length=video_attributes.w, length=video_attributes.w,
duration=video_attributes.duration, duration=video_attributes.duration,
file_size=video_note.size, file_size=video_note.size,

View File

@ -21,7 +21,7 @@ from struct import pack
import pyrogram import pyrogram
from pyrogram.api import types from pyrogram.api import types
from ..object import Object from ..object import Object
from ...ext.utils import encode from ...ext.utils import encode, encode_file_ref
class Voice(Object): class Voice(Object):
@ -31,7 +31,7 @@ class Voice(Object):
file_id (``str``): file_id (``str``):
Unique identifier for this file. Unique identifier for this file.
file_ref (``bytes``): file_ref (``str``):
Up to date file reference. Up to date file reference.
duration (``int``): duration (``int``):
@ -55,7 +55,7 @@ class Voice(Object):
*, *,
client: "pyrogram.BaseClient" = None, client: "pyrogram.BaseClient" = None,
file_id: str, file_id: str,
file_ref: bytes, file_ref: str,
duration: int, duration: int,
waveform: bytes = None, waveform: bytes = None,
mime_type: str = None, mime_type: str = None,
@ -84,7 +84,7 @@ class Voice(Object):
voice.access_hash voice.access_hash
) )
), ),
file_ref=voice.file_reference, file_ref=encode_file_ref(voice.file_reference),
duration=attributes.duration, duration=attributes.duration,
mime_type=voice.mime_type, mime_type=voice.mime_type,
file_size=voice.size, file_size=voice.size,