Visitar URL original
Allow uploading chat photos using BytesIO objects · EVAyo/pyrogram@8a14f58 · GitHub
Skip to content

Commit 8a14f58

Browse files
committed
Allow uploading chat photos using BytesIO objects
1 parent 2034a78 commit 8a14f58

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

‎pyrogram/client/methods/chats/set_chat_photo.py‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

1919
import os
20-
from typing import Union
20+
from typing import Union, BinaryIO
2121

2222
from pyrogram.api import functions, types
2323
from ...ext import BaseClient, utils
@@ -27,7 +27,7 @@ class SetChatPhoto(BaseClient):
2727
def set_chat_photo(
2828
self,
2929
chat_id: Union[int, str],
30-
photo: str,
30+
photo: Union[str, BinaryIO],
3131
file_ref: str = None
3232
) -> bool:
3333
"""Set a new profile photo for the chat.
@@ -39,12 +39,13 @@ def set_chat_photo(
3939
Unique identifier (int) or username (str) of the target chat.
4040
4141
photo (``str``):
42-
New chat photo. You can pass a :obj:`Photo` file_id or a file path to upload a new photo from your local
43-
machine.
42+
New chat photo. You can pass a :obj:`Photo` file_id (in pair with a valid file_ref), a file path to
43+
upload a new photo from your local machine or a binary file-like object with its attribute ".name"
44+
set for in-memory uploads.
4445
4546
file_ref (``str``, *optional*):
4647
A valid file reference obtained by a recently fetched media message.
47-
To be used in combination with a file id in case a file reference is needed.
48+
To be used in combination with a file_id in case a file reference is needed.
4849
4950
Returns:
5051
``bool``: True on success.
@@ -63,11 +64,14 @@ def set_chat_photo(
6364
"""
6465
peer = self.resolve_peer(chat_id)
6566

66-
if os.path.isfile(photo):
67-
photo = types.InputChatUploadedPhoto(file=self.save_file(photo))
67+
if isinstance(photo, str):
68+
if os.path.isfile(photo):
69+
photo = types.InputChatUploadedPhoto(file=self.save_file(photo))
70+
else:
71+
photo = utils.get_input_media_from_file_id(photo, file_ref, 2)
72+
photo = types.InputChatPhoto(id=photo.id)
6873
else:
69-
photo = utils.get_input_media_from_file_id(photo, file_ref, 2)
70-
photo = types.InputChatPhoto(id=photo.id)
74+
photo = types.InputChatUploadedPhoto(file=self.save_file(photo))
7175

7276
if isinstance(peer, types.InputPeerChat):
7377
self.send(

0 commit comments

Comments
 (0)