pyrofork: Add support to set emoji as Chat Photo

Signed-off-by: wulan17 <wulan17@nusantararom.org>
This commit is contained in:
wulan17 2024-09-07 16:04:43 +07:00
parent 11836faab7
commit e5327d2216
No known key found for this signature in database
GPG key ID: 318CD6CD3A6AC0A5

View file

@ -21,8 +21,7 @@ import os
from typing import Union, BinaryIO
import pyrogram
from pyrogram import raw
from pyrogram import utils
from pyrogram import raw, types, utils
from pyrogram.file_id import FileType
@ -32,9 +31,11 @@ class SetChatPhoto:
chat_id: Union[int, str],
*,
photo: Union[str, BinaryIO] = None,
emoji: int = None,
emoji_background: Union[int, list[int]] = None,
video: Union[str, BinaryIO] = None,
video_start_ts: float = None,
) -> bool:
) -> Union["types.Message", bool]:
"""Set a new chat photo or video (H.264/MPEG-4 AVC video, max 5 seconds).
The ``photo`` and ``video`` arguments are mutually exclusive.
@ -54,8 +55,14 @@ class SetChatPhoto:
from your local machine or a binary file-like object with its attribute
".name" set for in-memory uploads.
emoji (``int``, *optional*):
Unique identifier (int) of the emoji to be used as the chat photo.
emoji_background (``int`` | List of ``int``, *optional*):
hexadecimal colors or List of hexadecimal colors to be used as the chat photo background.
video (``str`` | ``BinaryIO``, *optional*):
New chat video. You can pass a :obj:`~pyrogram.types.Video` file_id, a file path to upload a new video
New chat video. You can pass a file path to upload a new video
from your local machine or a binary file-like object with its attribute
".name" set for in-memory uploads.
@ -63,7 +70,8 @@ class SetChatPhoto:
The timestamp in seconds of the video frame to use as photo profile preview.
Returns:
``bool``: True on success.
:obj:`~pyrogram.types.Message` | ``bool``: On success, a service message will be returned (when applicable),
otherwise, in case a message object couldn't be returned, True is returned.
Raises:
ValueError: if a chat_id belongs to user.
@ -78,40 +86,68 @@ class SetChatPhoto:
await app.set_chat_photo(chat_id, photo=photo.file_id)
# Set chat video using a local file
await app.set_chat_photo(chat_id, video="video.mp4")
# Set chat photo using an emoji
await app.set_chat_photo(chat_id, emoji=5366316836101038579)
# Set chat photo using an existing Video file_id
await app.set_chat_photo(chat_id, video=video.file_id)
# Set chat photo using an emoji and background colors
await app.set_chat_photo(chat_id, emoji=5366316836101038579, emoji_background=[0xFFFFFF, 0x000000])
# Set chat video
await app.set_chat_photo(chat_id, video="video.mp4")
"""
peer = await self.resolve_peer(chat_id)
if isinstance(photo, str):
if os.path.isfile(photo):
if photo is not None:
if isinstance(photo, str):
if os.path.isfile(photo):
photo = raw.types.InputChatUploadedPhoto(
file=await self.save_file(photo),
video_start_ts=video_start_ts,
)
else:
photo = utils.get_input_media_from_file_id(photo, FileType.PHOTO)
photo = raw.types.InputChatPhoto(id=photo.id)
else:
photo = raw.types.InputChatUploadedPhoto(
file=await self.save_file(photo),
video=await self.save_file(video),
video_start_ts=video_start_ts,
)
elif video is not None:
if isinstance(video, str):
if os.path.isfile(video):
photo = raw.types.InputChatUploadedPhoto(
video=await self.save_file(video),
video_start_ts=video_start_ts,
)
else:
raise ValueError("You must provide a valid file path for the video")
else:
photo = utils.get_input_media_from_file_id(photo, FileType.PHOTO)
photo = raw.types.InputChatPhoto(id=photo.id)
else:
photo = raw.types.InputChatUploadedPhoto(
video=await self.save_file(video),
video_start_ts=video_start_ts
)
elif emoji is not None:
background_colors = emoji_background if emoji_background is not None else [0xFFFFFF]
if isinstance(background_colors, int):
background_colors = [background_colors]
photo = raw.types.InputChatUploadedPhoto(
file=await self.save_file(photo),
video=await self.save_file(video),
video_start_ts=video_start_ts,
video_emoji_markup=raw.types.VideoSizeEmojiMarkup(
emoji_id=emoji,
background_colors=background_colors
)
)
else:
raise ValueError("You must provide either a photo, a video or an emoji")
if isinstance(peer, raw.types.InputPeerChat):
await self.invoke(
r = await self.invoke(
raw.functions.messages.EditChatPhoto(
chat_id=peer.chat_id,
photo=photo,
)
)
elif isinstance(peer, raw.types.InputPeerChannel):
await self.invoke(
r = await self.invoke(
raw.functions.channels.EditPhoto(
channel=peer,
photo=photo
@ -120,4 +156,13 @@ class SetChatPhoto:
else:
raise ValueError(f'The chat_id "{chat_id}" belongs to a user')
return True
for i in r.updates:
if isinstance(i, (raw.types.UpdateNewMessage, raw.types.UpdateNewChannelMessage)):
return await types.Message._parse(
self,
i.message,
{i.id: i for i in r.users},
{i.id: i for i in r.chats}
)
else:
return True