2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-29 13:27:47 +00:00

Merge develop -> asyncio

This commit is contained in:
Dan 2019-09-21 22:21:20 +02:00
commit a541bb45db
35 changed files with 217 additions and 65 deletions

View File

@ -127,4 +127,6 @@ BUTTON_URL_INVALID The button url is invalid
AUTH_BYTES_INVALID The authorization bytes are invalid AUTH_BYTES_INVALID The authorization bytes are invalid
CHANNELS_TOO_MUCH You have joined too many channels or supergroups CHANNELS_TOO_MUCH You have joined too many channels or supergroups
ADMIN_RANK_INVALID The custom administrator title is invalid or is longer than 16 characters ADMIN_RANK_INVALID The custom administrator title is invalid or is longer than 16 characters
ADMIN_RANK_EMOJI_NOT_ALLOWED Emojis are not allowed in custom administrator titles ADMIN_RANK_EMOJI_NOT_ALLOWED Emojis are not allowed in custom administrator titles
FILE_REFERENCE_EMPTY The file reference is empty
FILE_REFERENCE_INVALID The file reference is invalid
1 id message
127 AUTH_BYTES_INVALID The authorization bytes are invalid
128 CHANNELS_TOO_MUCH You have joined too many channels or supergroups
129 ADMIN_RANK_INVALID The custom administrator title is invalid or is longer than 16 characters
130 ADMIN_RANK_EMOJI_NOT_ALLOWED Emojis are not allowed in custom administrator titles
131 FILE_REFERENCE_EMPTY The file reference is empty
132 FILE_REFERENCE_INVALID The file reference is invalid

View File

@ -1235,6 +1235,7 @@ class Client(Methods, BaseClient):
peer_access_hash=data.peer_access_hash, peer_access_hash=data.peer_access_hash,
volume_id=data.volume_id, volume_id=data.volume_id,
local_id=data.local_id, local_id=data.local_id,
file_ref=data.file_ref,
file_size=data.file_size, file_size=data.file_size,
is_big=data.is_big, is_big=data.is_big,
progress=progress, progress=progress,
@ -1888,7 +1889,7 @@ class Client(Methods, BaseClient):
peer_id: int, peer_id: int,
peer_access_hash: int, volume_id: int, peer_access_hash: int, volume_id: int,
local_id: int, local_id: int,
file_size: int, file_ref: str,file_size: int,
is_big: bool, is_big: bool,
progress: callable, progress: callable,
@ -1928,6 +1929,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(
@ -1942,21 +1945,21 @@ class Client(Methods, BaseClient):
location = types.InputPhotoFileLocation( location = types.InputPhotoFileLocation(
id=document_id, id=document_id,
access_hash=access_hash, access_hash=access_hash,
file_reference=b"", file_reference=file_ref,
thumb_size=thumb_size thumb_size=thumb_size
) )
elif media_type == 14: elif media_type == 14:
location = types.InputDocumentFileLocation( location = types.InputDocumentFileLocation(
id=document_id, id=document_id,
access_hash=access_hash, access_hash=access_hash,
file_reference=b"", file_reference=file_ref,
thumb_size=thumb_size thumb_size=thumb_size
) )
else: else:
location = types.InputDocumentFileLocation( location = types.InputDocumentFileLocation(
id=document_id, id=document_id,
access_hash=access_hash, access_hash=access_hash,
file_reference=b"", file_reference=file_ref,
thumb_size="" thumb_size=""
) )

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 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
@ -38,3 +38,4 @@ class FileData:
self.mime_type = mime_type self.mime_type = mime_type
self.file_name = file_name self.file_name = file_name
self.date = date self.date = date
self.file_ref = file_ref

View File

@ -30,7 +30,7 @@ from . import BaseClient
from ...api import types from ...api import types
def decode(s: str) -> bytes: def decode_file_id(s: str) -> bytes:
s = base64.urlsafe_b64decode(s + "=" * (-len(s) % 4)) s = base64.urlsafe_b64decode(s + "=" * (-len(s) % 4))
r = b"" r = b""
@ -56,7 +56,7 @@ def decode(s: str) -> bytes:
return r return r
def encode(s: bytes) -> str: def encode_file_id(s: bytes) -> str:
r = b"" r = b""
n = 0 n = 0
@ -73,6 +73,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))
async def ainput(prompt: str = ""): async def ainput(prompt: str = ""):
print(prompt, end="", flush=True) print(prompt, end="", flush=True)
@ -94,10 +105,11 @@ 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:
decoded = decode(file_id_str) decoded = decode_file_id(file_id_str)
except Exception: except Exception:
raise ValueError("Failed to decode file_id: {}".format(file_id_str)) raise ValueError("Failed to decode file_id: {}".format(file_id_str))
else: else:
@ -123,7 +135,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)
) )
) )
@ -135,7 +147,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

@ -36,6 +36,7 @@ class DownloadMedia(BaseClient):
async def download_media( async def download_media(
self, self,
message: Union["pyrogram.Message", str], message: Union["pyrogram.Message", str],
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,6 +49,10 @@ 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 (``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*): 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.
By default, all files are downloaded in the *downloads* folder in your working directory. By default, all files are downloaded in the *downloads* folder in your working directory.
@ -123,19 +128,21 @@ class DownloadMedia(BaseClient):
file_size = getattr(media, "file_size", None) file_size = getattr(media, "file_size", None)
mime_type = getattr(media, "mime_type", None) mime_type = getattr(media, "mime_type", None)
date = getattr(media, "date", None) date = getattr(media, "date", None)
file_ref = getattr(media, "file_ref", None)
data = FileData( data = FileData(
file_name=media_file_name, file_name=media_file_name,
file_size=file_size, file_size=file_size,
mime_type=mime_type, mime_type=mime_type,
date=date date=date,
file_ref=file_ref
) )
def get_existing_attributes() -> dict: def get_existing_attributes() -> dict:
return dict(filter(lambda x: x[1] is not None, data.__dict__.items())) return dict(filter(lambda x: x[1] is not None, data.__dict__.items()))
try: try:
decoded = utils.decode(file_id_str) decoded = utils.decode_file_id(file_id_str)
media_type = decoded[0] media_type = decoded[0]
if media_type == 1: if media_type == 1:

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 await self.send( return await 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 = await self.send( media = await 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 = await self.send( media = await 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 = await self.send( media = await 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 = await self.send( media = await 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 = await self.send( r = await 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
await self.send( await 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,
@ -63,6 +64,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.
@ -173,7 +178,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 = await self.send( r = await self.send(
functions.messages.SendMedia( functions.messages.SendMedia(
peer=await self.resolve_peer(chat_id), peer=await 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

@ -55,7 +55,7 @@ class DeleteProfilePhotos(BaseClient):
input_photos = [] input_photos = []
for photo_id in photo_ids: for photo_id in photo_ids:
unpacked = unpack("<iiqqc", utils.decode(photo_id)) unpacked = unpack("<iiqqc", utils.decode_file_id(photo_id))
input_photos.append( input_photos.append(
types.InputPhoto( types.InputPhoto(

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_file_id, encode_file_ref
class Animation(Object): class Animation(Object):
@ -33,6 +33,9 @@ class Animation(Object):
file_id (``str``): file_id (``str``):
Unique identifier for this file. Unique identifier for this file.
file_ref (``str``):
Up to date file reference.
width (``int``): width (``int``):
Animation width as defined by sender. Animation width as defined by sender.
@ -63,6 +66,7 @@ class Animation(Object):
*, *,
client: "pyrogram.BaseClient" = None, client: "pyrogram.BaseClient" = None,
file_id: str, file_id: str,
file_ref: str,
width: int, width: int,
height: int, height: int,
duration: int, duration: int,
@ -75,6 +79,7 @@ class Animation(Object):
super().__init__(client) super().__init__(client)
self.file_id = file_id self.file_id = file_id
self.file_ref = file_ref
self.file_name = file_name self.file_name = file_name
self.mime_type = mime_type self.mime_type = mime_type
self.file_size = file_size self.file_size = file_size
@ -92,7 +97,7 @@ class Animation(Object):
file_name: str file_name: str
) -> "Animation": ) -> "Animation":
return Animation( return Animation(
file_id=encode( file_id=encode_file_id(
pack( pack(
"<iiqq", "<iiqq",
10, 10,
@ -101,6 +106,7 @@ class Animation(Object):
animation.access_hash animation.access_hash
) )
), ),
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_file_id, encode_file_ref
class Audio(Object): class Audio(Object):
@ -33,6 +33,9 @@ class Audio(Object):
file_id (``str``): file_id (``str``):
Unique identifier for this file. Unique identifier for this file.
file_ref (``str``):
Up to date file reference.
duration (``int``): duration (``int``):
Duration of the audio in seconds as defined by sender. Duration of the audio in seconds as defined by sender.
@ -63,6 +66,7 @@ class Audio(Object):
*, *,
client: "pyrogram.BaseClient" = None, client: "pyrogram.BaseClient" = None,
file_id: str, file_id: str,
file_ref: str,
duration: int, duration: int,
file_name: str = None, file_name: str = None,
mime_type: str = None, mime_type: str = None,
@ -75,6 +79,7 @@ class Audio(Object):
super().__init__(client) super().__init__(client)
self.file_id = file_id self.file_id = file_id
self.file_ref = file_ref
self.file_name = file_name self.file_name = file_name
self.mime_type = mime_type self.mime_type = mime_type
self.file_size = file_size self.file_size = file_size
@ -92,7 +97,7 @@ class Audio(Object):
file_name: str file_name: str
) -> "Audio": ) -> "Audio":
return Audio( return Audio(
file_id=encode( file_id=encode_file_id(
pack( pack(
"<iiqq", "<iiqq",
9, 9,
@ -101,6 +106,7 @@ class Audio(Object):
audio.access_hash audio.access_hash
) )
), ),
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_file_id, encode_file_ref
class Document(Object): class Document(Object):
@ -33,6 +33,9 @@ class Document(Object):
file_id (``str``): file_id (``str``):
Unique file identifier. Unique file identifier.
file_ref (``str``):
Up to date file reference.
file_name (``str``, *optional*): file_name (``str``, *optional*):
Original filename as defined by sender. Original filename as defined by sender.
@ -54,6 +57,7 @@ class Document(Object):
*, *,
client: "pyrogram.BaseClient" = None, client: "pyrogram.BaseClient" = None,
file_id: str, file_id: str,
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,
@ -63,6 +67,7 @@ class Document(Object):
super().__init__(client) super().__init__(client)
self.file_id = file_id self.file_id = file_id
self.file_ref = file_ref
self.file_name = file_name self.file_name = file_name
self.mime_type = mime_type self.mime_type = mime_type
self.file_size = file_size self.file_size = file_size
@ -72,7 +77,7 @@ class Document(Object):
@staticmethod @staticmethod
def _parse(client, document: types.Document, file_name: str) -> "Document": def _parse(client, document: types.Document, file_name: str) -> "Document":
return Document( return Document(
file_id=encode( file_id=encode_file_id(
pack( pack(
"<iiqq", "<iiqq",
5, 5,
@ -81,6 +86,7 @@ class Document(Object):
document.access_hash document.access_hash
) )
), ),
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 await self._client.send_contact( return await 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 await send_media(file_id=file_id) return await send_media(file_id=file_id, file_ref=file_ref)
else: else:
return await send_media(file_id=file_id, caption=caption, parse_mode="html") return await 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_file_id, encode_file_ref
class Photo(Object): class Photo(Object):
@ -33,6 +33,9 @@ class Photo(Object):
file_id (``str``): file_id (``str``):
Unique identifier for this photo. Unique identifier for this photo.
file_ref (``str``):
Up to date file reference.
width (``int``): width (``int``):
Photo width. Photo width.
@ -54,6 +57,7 @@ class Photo(Object):
*, *,
client: "pyrogram.BaseClient" = None, client: "pyrogram.BaseClient" = None,
file_id: str, file_id: str,
file_ref: str,
width: int, width: int,
height: int, height: int,
file_size: int, file_size: int,
@ -63,6 +67,7 @@ class Photo(Object):
super().__init__(client) super().__init__(client)
self.file_id = file_id self.file_id = file_id
self.file_ref = file_ref
self.width = width self.width = width
self.height = height self.height = height
self.file_size = file_size self.file_size = file_size
@ -75,7 +80,7 @@ class Photo(Object):
big = photo.sizes[-1] big = photo.sizes[-1]
return Photo( return Photo(
file_id=encode( file_id=encode_file_id(
pack( pack(
"<iiqqqiiii", "<iiqqqiiii",
2, photo.dc_id, photo.id, photo.access_hash, 2, photo.dc_id, photo.id, photo.access_hash,
@ -83,6 +88,7 @@ class Photo(Object):
big.location.local_id big.location.local_id
) )
), ),
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

@ -26,7 +26,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_file_id, encode_file_ref
class Sticker(Object): class Sticker(Object):
@ -36,6 +36,9 @@ class Sticker(Object):
file_id (``str``): file_id (``str``):
Unique identifier for this file. Unique identifier for this file.
file_ref (``str``):
Up to date file reference.
width (``int``): width (``int``):
Sticker width. Sticker width.
@ -74,6 +77,7 @@ class Sticker(Object):
*, *,
client: "pyrogram.BaseClient" = None, client: "pyrogram.BaseClient" = None,
file_id: str, file_id: str,
file_ref: str,
width: int, width: int,
height: int, height: int,
is_animated: bool, is_animated: bool,
@ -88,6 +92,7 @@ class Sticker(Object):
super().__init__(client) super().__init__(client)
self.file_id = file_id self.file_id = file_id
self.file_ref = file_ref
self.file_name = file_name self.file_name = file_name
self.mime_type = mime_type self.mime_type = mime_type
self.file_size = file_size self.file_size = file_size
@ -127,7 +132,7 @@ class Sticker(Object):
set_name = None set_name = None
return Sticker( return Sticker(
file_id=encode( file_id=encode_file_id(
pack( pack(
"<iiqq", "<iiqq",
8, 8,
@ -136,6 +141,7 @@ class Sticker(Object):
sticker.access_hash sticker.access_hash
) )
), ),
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

@ -21,7 +21,7 @@ from typing import Union, List
import pyrogram import pyrogram
from pyrogram.api import types from pyrogram.api import types
from pyrogram.client.ext.utils import encode from pyrogram.client.ext.utils import encode_file_id
from .stripped_thumbnail import StrippedThumbnail from .stripped_thumbnail import StrippedThumbnail
from ..object import Object from ..object import Object
@ -85,7 +85,7 @@ class Thumbnail(Object):
if isinstance(thumbnail, types.PhotoSize): if isinstance(thumbnail, types.PhotoSize):
thumbnails.append( thumbnails.append(
Thumbnail( Thumbnail(
file_id=encode( file_id=encode_file_id(
pack( pack(
"<iiqqqiiii", "<iiqqqiiii",
media_type, media.dc_id, media.id, media.access_hash, media_type, media.dc_id, media.id, media.access_hash,

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_file_id, encode_file_ref
class Video(Object): class Video(Object):
@ -33,6 +33,9 @@ class Video(Object):
file_id (``str``): file_id (``str``):
Unique identifier for this file. Unique identifier for this file.
file_ref (``str``):
Up to date file reference.
width (``int``): width (``int``):
Video width as defined by sender. Video width as defined by sender.
@ -66,6 +69,7 @@ class Video(Object):
*, *,
client: "pyrogram.BaseClient" = None, client: "pyrogram.BaseClient" = None,
file_id: str, file_id: str,
file_ref: str,
width: int, width: int,
height: int, height: int,
duration: int, duration: int,
@ -79,6 +83,7 @@ class Video(Object):
super().__init__(client) super().__init__(client)
self.file_id = file_id self.file_id = file_id
self.file_ref = file_ref
self.width = width self.width = width
self.height = height self.height = height
self.duration = duration self.duration = duration
@ -97,7 +102,7 @@ class Video(Object):
file_name: str file_name: str
) -> "Video": ) -> "Video":
return Video( return Video(
file_id=encode( file_id=encode_file_id(
pack( pack(
"<iiqq", "<iiqq",
4, 4,
@ -106,6 +111,7 @@ class Video(Object):
video.access_hash video.access_hash
) )
), ),
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_file_id, encode_file_ref
class VideoNote(Object): class VideoNote(Object):
@ -33,6 +33,9 @@ class VideoNote(Object):
file_id (``str``): file_id (``str``):
Unique identifier for this file. Unique identifier for this file.
file_ref (``str``):
Up to date file reference.
length (``int``): length (``int``):
Video width and height as defined by sender. Video width and height as defined by sender.
@ -57,6 +60,7 @@ class VideoNote(Object):
*, *,
client: "pyrogram.BaseClient" = None, client: "pyrogram.BaseClient" = None,
file_id: str, file_id: str,
file_ref: str,
length: int, length: int,
duration: int, duration: int,
thumbs: List[Thumbnail] = None, thumbs: List[Thumbnail] = None,
@ -67,6 +71,7 @@ class VideoNote(Object):
super().__init__(client) super().__init__(client)
self.file_id = file_id self.file_id = file_id
self.file_ref = file_ref
self.mime_type = mime_type self.mime_type = mime_type
self.file_size = file_size self.file_size = file_size
self.date = date self.date = date
@ -77,7 +82,7 @@ class VideoNote(Object):
@staticmethod @staticmethod
def _parse(client, video_note: types.Document, video_attributes: types.DocumentAttributeVideo) -> "VideoNote": def _parse(client, video_note: types.Document, video_attributes: types.DocumentAttributeVideo) -> "VideoNote":
return VideoNote( return VideoNote(
file_id=encode( file_id=encode_file_id(
pack( pack(
"<iiqq", "<iiqq",
13, 13,
@ -86,6 +91,7 @@ class VideoNote(Object):
video_note.access_hash video_note.access_hash
) )
), ),
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_file_id, encode_file_ref
class Voice(Object): class Voice(Object):
@ -31,6 +31,9 @@ class Voice(Object):
file_id (``str``): file_id (``str``):
Unique identifier for this file. Unique identifier for this file.
file_ref (``str``):
Up to date file reference.
duration (``int``): duration (``int``):
Duration of the audio in seconds as defined by sender. Duration of the audio in seconds as defined by sender.
@ -52,6 +55,7 @@ class Voice(Object):
*, *,
client: "pyrogram.BaseClient" = None, client: "pyrogram.BaseClient" = None,
file_id: str, file_id: str,
file_ref: str,
duration: int, duration: int,
waveform: bytes = None, waveform: bytes = None,
mime_type: str = None, mime_type: str = None,
@ -61,6 +65,7 @@ class Voice(Object):
super().__init__(client) super().__init__(client)
self.file_id = file_id self.file_id = file_id
self.file_ref = file_ref
self.duration = duration self.duration = duration
self.waveform = waveform self.waveform = waveform
self.mime_type = mime_type self.mime_type = mime_type
@ -70,7 +75,7 @@ class Voice(Object):
@staticmethod @staticmethod
def _parse(client, voice: types.Document, attributes: types.DocumentAttributeAudio) -> "Voice": def _parse(client, voice: types.Document, attributes: types.DocumentAttributeAudio) -> "Voice":
return Voice( return Voice(
file_id=encode( file_id=encode_file_id(
pack( pack(
"<iiqq", "<iiqq",
3, 3,
@ -79,6 +84,7 @@ class Voice(Object):
voice.access_hash voice.access_hash
) )
), ),
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,

View File

@ -22,7 +22,7 @@ import pyrogram
from pyrogram.api import types from pyrogram.api import types
from pyrogram.client.ext import utils from pyrogram.client.ext import utils
from ..object import Object from ..object import Object
from ...ext.utils import encode from ...ext.utils import encode_file_id
class ChatPhoto(Object): class ChatPhoto(Object):
@ -73,7 +73,7 @@ class ChatPhoto(Object):
x = -234 x = -234
return ChatPhoto( return ChatPhoto(
small_file_id=encode( small_file_id=encode_file_id(
pack( pack(
"<iiqqqiiiqi", "<iiqqqiiiqi",
1, chat_photo.dc_id, photo_id, 1, chat_photo.dc_id, photo_id,
@ -81,7 +81,7 @@ class ChatPhoto(Object):
2, peer_id, x, peer_access_hash, loc_small.local_id 2, peer_id, x, peer_access_hash, loc_small.local_id
) )
), ),
big_file_id=encode( big_file_id=encode_file_id(
pack( pack(
"<iiqqqiiiqi", "<iiqqqiiiqi",
1, chat_photo.dc_id, photo_id, 1, chat_photo.dc_id, photo_id,