1616# You should have received a copy of the GNU Lesser General Public License
1717# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818
19- from typing import Union , BinaryIO , Optional
19+ import logging
20+ from typing import BinaryIO , Optional , Union
2021
2122import pyrogram
22- from pyrogram import raw
23+ from pyrogram import raw , types
2324
25+ log = logging .getLogger (__name__ )
2426
2527class SetProfilePhoto :
2628 async def set_profile_photo (
2729 self : "pyrogram.Client" ,
30+ photo : Optional ["types.InputChatPhoto" ] = None ,
31+ is_public : Optional [bool ] = None ,
2832 * ,
29- photo : Optional [Union [str , BinaryIO ]] = None ,
30- video : Optional [Union [str , BinaryIO ]] = None ,
31- is_public : Optional [bool ] = None
33+ video : Optional [Union [str , BinaryIO ]] = None
3234 ) -> bool :
33- """Set a new profile photo or video (H.264/MPEG-4 AVC video, max 5 seconds).
34-
35- The ``photo`` and ``video`` arguments are mutually exclusive.
36- Pass either one as named argument (see examples below).
37-
38- .. note::
39-
40- This method only works for Users.
41- Bots profile photos must be set using BotFather.
35+ """Changes a profile photo for the current user.
4236
4337 .. include:: /_includes/usable-by/users.rst
4438
4539 Parameters:
46- photo (``str`` | ``BinaryIO` `, *optional*):
40+ photo (:obj:`~pyrogram.types.InputChatPhoto `, *optional*):
4741 Profile photo to set.
48- Pass a file path as string to upload a new photo that exists on your local machine or
49- pass a binary file-like object with its attribute ".name" set for in-memory uploads.
50-
51- video (``str`` | ``BinaryIO``, *optional*):
52- Profile video to set.
53- Pass a file path as string to upload a new video that exists on your local machine or
54- pass a binary file-like object with its attribute ".name" set for in-memory uploads.
5542
5643 is_public (``bool``, *optional*):
57- If set to True, the chosen profile photo will be shown to users that can't display
58- your main profile photo due to your privacy settings.
44+ Pass True to set the public photo, which will be visible even if the main photo is hidden by privacy settings.
5945
6046 Returns:
6147 ``bool``: True on success.
@@ -64,21 +50,104 @@ async def set_profile_photo(
6450 .. code-block:: python
6551
6652 # Set a new profile photo
67- await app.set_profile_photo(photo="new_photo.jpg")
53+ await app.set_profile_photo(photo=types.InputChatPhotoStatic( "new_photo.jpg") )
6854
6955 # Set a new profile video
70- await app.set_profile_photo(video="new_video.mp4")
56+ await app.set_profile_photo(photo=types.InputChatPhotoAnimation("new_video.mp4"))
57+
58+ # Set a previous profile photo
59+ await app.set_profile_photo(photo=types.InputChatPhotoPrevious(file_id))
7160
7261 # Set/update your account's public profile photo
73- await app.set_profile_photo(photo="new_photo.jpg", is_public=True)
62+ await app.set_profile_photo(photo=types.InputChatPhotoStatic( "new_photo.jpg") , is_public=True)
7463 """
64+ if video is not None :
65+ log .warning (
66+ "`video` is deprecated and will be removed in future updates. Use `photo` instead."
67+ )
68+
69+ photo = types .InputChatPhotoAnimation (animation = video )
70+
71+ if photo is not None and not isinstance (photo , types .InputChatPhoto ):
72+ log .warning (
73+ "You must pass `photo` as `types.InputChatPhoto`. Passing `photo` as a string "
74+ "or binary object is deprecated and will be removed in future updates."
75+ )
76+
77+ photo = types .InputChatPhotoStatic (photo = photo )
78+
79+ if isinstance (photo , types .InputChatPhotoPrevious ):
80+ return bool (
81+ await self .invoke (
82+ raw .functions .photos .UpdateProfilePhoto (
83+ fallback = is_public ,
84+ id = await photo .write (self ),
85+ )
86+ )
87+ )
88+ else :
89+ return bool (
90+ await self .invoke (
91+ raw .functions .photos .UploadProfilePhoto (
92+ fallback = is_public ,
93+ file = await photo .write (self ) if isinstance (photo , types .InputChatPhotoStatic ) else None ,
94+ video = await photo .write (self ) if isinstance (photo , types .InputChatPhotoAnimation ) else None ,
95+ video_start_ts = getattr (photo , "main_frame_timestamp" , None ),
96+ )
97+ )
98+ )
99+
100+
101+ class SetBotProfilePhoto :
102+ async def set_bot_profile_photo (
103+ self : "pyrogram.Client" ,
104+ bot_user_id : Union [int , str ],
105+ photo : Optional ["types.InputChatPhoto" ] = None ,
106+ ) -> bool :
107+ """Changes a profile photo for a bot.
108+
109+ .. include:: /_includes/usable-by/users.rst
110+
111+ Parameters:
112+ bot_user_id (``int`` | ``str``):
113+ Unique identifier (int) or username (str) of the target bot.
114+
115+ photo (:obj:`~pyrogram.types.InputChatPhoto`, *optional*):
116+ Profile photo to set.
117+ Pass None to remove the profile photo.
118+
119+ Returns:
120+ ``bool``: True on success.
121+
122+ Example:
123+ .. code-block:: python
124+
125+ # Set a new bot profile photo
126+ await app.set_bot_profile_photo(bot_user_id="@KurigramBot", photo=types.InputChatPhotoStatic("new_photo.jpg"))
75127
76- return bool (
77- await self .invoke (
78- raw .functions .photos .UploadProfilePhoto (
79- fallback = is_public ,
80- file = await self .save_file (photo ),
81- video = await self .save_file (video )
128+ # Set a new bot profile video
129+ await app.set_bot_profile_photo(bot_user_id="@KurigramBot", photo=types.InputChatPhotoAnimation("new_video.mp4"))
130+
131+ # Remove bot profile photo
132+ await app.set_bot_profile_photo(bot_user_id="@KurigramBot")
133+ """
134+ if isinstance (photo , types .InputChatPhotoPrevious ):
135+ return bool (
136+ await self .invoke (
137+ raw .functions .photos .UpdateProfilePhoto (
138+ id = await photo .write (self ) if photo else raw .types .InputPhotoEmpty (),
139+ bot = await self .resolve_peer (bot_user_id ),
140+ )
141+ )
142+ )
143+ else :
144+ return bool (
145+ await self .invoke (
146+ raw .functions .photos .UploadProfilePhoto (
147+ bot = await self .resolve_peer (bot_user_id ),
148+ file = await photo .write (self ) if isinstance (photo , types .InputChatPhotoStatic ) else None ,
149+ video = await photo .write (self ) if isinstance (photo , types .InputChatPhotoAnimation ) else None ,
150+ video_start_ts = getattr (photo , "main_frame_timestamp" , None ),
151+ )
82152 )
83153 )
84- )
0 commit comments