Skip to content

Commit a4566de

Browse files
committed
Add support for the new Bot API fields: file_id, file_unique_id
Remove file_ref from Pyrogram's API
1 parent e0c3578 commit a4566de

34 files changed

+400
-669
lines changed

pyrogram/client.py

Lines changed: 32 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
from pyrogram.types import User, TermsOfService
4848
from pyrogram.utils import ainput
4949
from .dispatcher import Dispatcher
50+
from .file_id import FileId, FileType, ThumbnailSource
5051
from .scaffold import Scaffold
5152

5253
log = logging.getLogger(__name__)
@@ -493,22 +494,11 @@ async def handle_download(self, packet):
493494
final_file_path = ""
494495

495496
try:
496-
data, directory, file_name, progress, progress_args = packet
497+
file_id, directory, file_name, file_size, progress, progress_args = packet
497498

498499
temp_file_path = await self.get_file(
499-
media_type=data.media_type,
500-
dc_id=data.dc_id,
501-
document_id=data.document_id,
502-
access_hash=data.access_hash,
503-
thumb_size=data.thumb_size,
504-
peer_id=data.peer_id,
505-
peer_type=data.peer_type,
506-
peer_access_hash=data.peer_access_hash,
507-
volume_id=data.volume_id,
508-
local_id=data.local_id,
509-
file_ref=data.file_ref,
510-
file_size=data.file_size,
511-
is_big=data.is_big,
500+
file_id=file_id,
501+
file_size=file_size,
512502
progress=progress,
513503
progress_args=progress_args
514504
)
@@ -817,22 +807,13 @@ def load_plugins(self):
817807

818808
async def get_file(
819809
self,
820-
media_type: int,
821-
dc_id: int,
822-
document_id: int,
823-
access_hash: int,
824-
thumb_size: str,
825-
peer_id: int,
826-
peer_type: str,
827-
peer_access_hash: int,
828-
volume_id: int,
829-
local_id: int,
830-
file_ref: str,
810+
file_id: FileId,
831811
file_size: int,
832-
is_big: bool,
833812
progress: callable,
834813
progress_args: tuple = ()
835814
) -> str:
815+
dc_id = file_id.dc_id
816+
836817
async with self.media_sessions_lock:
837818
session = self.media_sessions.get(dc_id, None)
838819

@@ -874,49 +855,43 @@ async def get_file(
874855

875856
self.media_sessions[dc_id] = session
876857

877-
file_ref = utils.decode_file_ref(file_ref)
858+
file_type = file_id.file_type
878859

879-
if media_type == 1:
880-
if peer_type == "user":
860+
if file_type == FileType.CHAT_PHOTO:
861+
if file_id.chat_id > 0:
881862
peer = raw.types.InputPeerUser(
882-
user_id=peer_id,
883-
access_hash=peer_access_hash
884-
)
885-
elif peer_type == "chat":
886-
peer = raw.types.InputPeerChat(
887-
chat_id=peer_id
863+
user_id=file_id.chat_id,
864+
access_hash=file_id.chat_access_hash
888865
)
889866
else:
890-
peer = raw.types.InputPeerChannel(
891-
channel_id=peer_id,
892-
access_hash=peer_access_hash
893-
)
867+
if file_id.chat_access_hash == 0:
868+
peer = raw.types.InputPeerChat(
869+
chat_id=file_id.chat_id
870+
)
871+
else:
872+
peer = raw.types.InputPeerChannel(
873+
channel_id=file_id.chat_id,
874+
access_hash=file_id.chat_access_hash
875+
)
894876

895877
location = raw.types.InputPeerPhotoFileLocation(
896878
peer=peer,
897-
volume_id=volume_id,
898-
local_id=local_id,
899-
big=is_big or None
879+
volume_id=file_id.volume_id,
880+
local_id=file_id.local_id,
881+
big=file_id.thumbnail_source == ThumbnailSource.CHAT_PHOTO_BIG
900882
)
901-
elif media_type in (0, 2):
883+
elif file_type in (FileType.THUMBNAIL, FileType.PHOTO):
902884
location = raw.types.InputPhotoFileLocation(
903-
id=document_id,
904-
access_hash=access_hash,
905-
file_reference=file_ref,
906-
thumb_size=thumb_size
907-
)
908-
elif media_type == 14:
909-
location = raw.types.InputDocumentFileLocation(
910-
id=document_id,
911-
access_hash=access_hash,
912-
file_reference=file_ref,
913-
thumb_size=thumb_size
885+
id=file_id.media_id,
886+
access_hash=file_id.access_hash,
887+
file_reference=file_id.file_reference,
888+
thumb_size=file_id.thumbnail_size
914889
)
915890
else:
916891
location = raw.types.InputDocumentFileLocation(
917-
id=document_id,
918-
access_hash=access_hash,
919-
file_reference=file_ref,
892+
id=file_id.media_id,
893+
access_hash=file_id.access_hash,
894+
file_reference=file_id.file_reference,
920895
thumb_size=""
921896
)
922897

pyrogram/methods/chats/set_chat_photo.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from pyrogram import raw
2323
from pyrogram import utils
2424
from pyrogram.scaffold import Scaffold
25+
from pyrogram.file_id import FileType
2526

2627

2728
class SetChatPhoto(Scaffold):
@@ -30,8 +31,7 @@ async def set_chat_photo(
3031
chat_id: Union[int, str],
3132
*,
3233
photo: Union[str, BinaryIO] = None,
33-
video: Union[str, BinaryIO] = None,
34-
file_ref: str = None
34+
video: Union[str, BinaryIO] = None
3535
) -> bool:
3636
"""Set a new chat photo or video (H.264/MPEG-4 AVC video, max 5 seconds).
3737
@@ -45,19 +45,15 @@ async def set_chat_photo(
4545
Unique identifier (int) or username (str) of the target chat.
4646
4747
photo (``str`` | ``BinaryIO``, *optional*):
48-
New chat photo. You can pass a :obj:`~pyrogram.types.Photo` file_id (in pair with a valid file_ref), a
49-
file path to upload a new photo from your local machine or a binary file-like object with its attribute
48+
New chat photo. You can pass a :obj:`~pyrogram.types.Photo` file_id, a file path to upload a new photo
49+
from your local machine or a binary file-like object with its attribute
5050
".name" set for in-memory uploads.
5151
5252
video (``str`` | ``BinaryIO``, *optional*):
53-
New chat video. You can pass a :obj:`~pyrogram.types.Video` file_id (in pair with a valid file_ref), a
54-
file path to upload a new video from your local machine or a binary file-like object with its attribute
53+
New chat video. You can pass a :obj:`~pyrogram.types.Video` file_id, a file path to upload a new video
54+
from your local machine or a binary file-like object with its attribute
5555
".name" set for in-memory uploads.
5656
57-
file_ref (``str``, *optional*):
58-
A valid file reference obtained by a recently fetched media message.
59-
To be used in combination with a file_id in case a file reference is needed.
60-
6157
Returns:
6258
``bool``: True on success.
6359
@@ -71,14 +67,14 @@ async def set_chat_photo(
7167
app.set_chat_photo(chat_id, photo="photo.jpg")
7268
7369
# Set chat photo using an exiting Photo file_id
74-
app.set_chat_photo(chat_id, photo=photo.file_id, file_ref=photo.file_ref)
70+
app.set_chat_photo(chat_id, photo=photo.file_id)
7571
7672
7773
# Set chat video using a local file
7874
app.set_chat_photo(chat_id, video="video.mp4")
7975
8076
# Set chat photo using an exiting Video file_id
81-
app.set_chat_photo(chat_id, video=video.file_id, file_ref=video.file_ref)
77+
app.set_chat_photo(chat_id, video=video.file_id)
8278
"""
8379
peer = await self.resolve_peer(chat_id)
8480

@@ -89,7 +85,7 @@ async def set_chat_photo(
8985
video=await self.save_file(video)
9086
)
9187
else:
92-
photo = utils.get_input_media_from_file_id(photo, file_ref, 2)
88+
photo = utils.get_input_media_from_file_id(photo, FileType.PHOTO)
9389
photo = raw.types.InputChatPhoto(id=photo.id)
9490
else:
9591
photo = raw.types.InputChatUploadedPhoto(

0 commit comments

Comments
 (0)