diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index 0e11175a7..558304aff 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -391,6 +391,7 @@ def get_title_list(s: str) -> list: Dialog Restriction EmojiStatus + ChatBackground """, "messages_media": """ Messages & Media diff --git a/hydrogram/types/user_and_chats/__init__.py b/hydrogram/types/user_and_chats/__init__.py index 791bfa03f..72d65abfe 100644 --- a/hydrogram/types/user_and_chats/__init__.py +++ b/hydrogram/types/user_and_chats/__init__.py @@ -19,6 +19,7 @@ from .chat import Chat from .chat_admin_with_invite_links import ChatAdminWithInviteLinks +from .chat_background import ChatBackground from .chat_event import ChatEvent from .chat_event_filter import ChatEventFilter from .chat_invite_link import ChatInviteLink @@ -54,6 +55,7 @@ __all__ = [ "Chat", "ChatAdminWithInviteLinks", + "ChatBackground", "ChatEvent", "ChatEventFilter", "ChatInviteLink", diff --git a/hydrogram/types/user_and_chats/chat.py b/hydrogram/types/user_and_chats/chat.py index 54de552b2..e8d291b7f 100644 --- a/hydrogram/types/user_and_chats/chat.py +++ b/hydrogram/types/user_and_chats/chat.py @@ -115,6 +115,9 @@ class Chat(Object): Pinned message, for groups, supergroups channels and own chat. Returned only in :meth:`~hydrogram.Client.get_chat`. + background (:obj:`~hydrogram.types.ChatBackground`, *optional*): + A chat background. + sticker_set_name (``str``, *optional*): For supergroups, name of group sticker set. Returned only in :meth:`~hydrogram.Client.get_chat`. @@ -177,7 +180,8 @@ def __init__( dc_id: int | None = None, has_protected_content: bool | None = None, invite_link: str | None = None, - pinned_message=None, + pinned_message: types.Message | None = None, + background: types.ChatBackground | None = None, sticker_set_name: str | None = None, can_set_sticker_set: bool | None = None, members_count: int | None = None, @@ -213,6 +217,7 @@ def __init__( self.has_protected_content = has_protected_content self.invite_link = invite_link self.pinned_message = pinned_message + self.background = background self.sticker_set_name = sticker_set_name self.can_set_sticker_set = can_set_sticker_set self.members_count = members_count @@ -346,6 +351,9 @@ async def _parse_full( parsed_chat.pinned_message = await client.get_messages( parsed_chat.id, message_ids=full_user.pinned_msg_id ) + + if getattr(full_user, "wallpaper", None): + parsed_chat.background = types.ChatBackground._parse(client, full_user.wallpaper) else: full_chat = chat_full.full_chat chat_raw = chats[full_chat.id] @@ -375,6 +383,9 @@ async def _parse_full( parsed_chat.send_as_chat = Chat._parse_chat(client, send_as_raw) + if getattr(full_chat, "wallpaper", None): + parsed_chat.background = types.ChatBackground._parse(client, full_chat.wallpaper) + parsed_chat.description = full_chat.about or None if full_chat.pinned_msg_id: diff --git a/hydrogram/types/user_and_chats/chat_background.py b/hydrogram/types/user_and_chats/chat_background.py new file mode 100644 index 000000000..10be00ac7 --- /dev/null +++ b/hydrogram/types/user_and_chats/chat_background.py @@ -0,0 +1,116 @@ +# Hydrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2023-present Hydrogram +# +# This file is part of Hydrogram. +# +# Hydrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Hydrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Hydrogram. If not, see . + +from __future__ import annotations + +from typing import TYPE_CHECKING + +import hydrogram +from hydrogram import raw, types, utils +from hydrogram.file_id import ( + FileId, + FileType, + FileUniqueId, + FileUniqueType, + ThumbnailSource, +) +from hydrogram.types.object import Object + +if TYPE_CHECKING: + from datetime import datetime + + +class ChatBackground(Object): + """Describes a background set for a specific chat. + + Parameters: + file_id (``str``): + Identifier for this file, which can be used to download the file. + + file_unique_id (``str``): + Unique identifier for this file, which is supposed to be the same over time and for different accounts. + Can't be used to download or reuse the file. + + file_size (``int``): + File size. + + date (:py:obj:`~datetime.datetime`): + Date the background was setted. + + slug (``str``): + Identifier of the background code. + You can combine it with `https://t.me/bg/{slug}` + to get link for this background. + + thumbs (List of :obj:`~pyrogram.types.Thumbnail`, *optional*): + Available thumbnails of this background. + + link (``str``, *property*): + Generate a link to this background code. + """ + + def __init__( + self, + *, + client: hydrogram.Client = None, + file_id: str, + file_unique_id: str, + file_size: int, + date: datetime, + slug: str, + thumbs: list[types.Thumbnail] | None = None, + ): + super().__init__(client) + + self.file_id = file_id + self.file_unique_id = file_unique_id + self.file_size = file_size + self.date = date + self.slug = slug + self.thumbs = thumbs + + @property + def link(self) -> str: + return f"https://t.me/bg/{self.slug}" + + @staticmethod + def _parse( + client, + wallpaper: raw.types.Wallpaper, + ) -> ChatBackground: + return ChatBackground( + file_id=FileId( + dc_id=wallpaper.document.dc_id, + file_reference=wallpaper.document.file_reference, + access_hash=wallpaper.document.access_hash, + file_type=FileType.BACKGROUND, + media_id=wallpaper.document.id, + volume_id=0, + local_id=0, + thumbnail_source=ThumbnailSource.THUMBNAIL, + thumbnail_file_type=FileType.BACKGROUND, + ).encode(), + file_unique_id=FileUniqueId( + file_unique_type=FileUniqueType.DOCUMENT, media_id=wallpaper.document.id + ).encode(), + file_size=wallpaper.document.size, + slug=wallpaper.slug, + date=utils.timestamp_to_datetime(wallpaper.document.date), + thumbs=types.Thumbnail._parse(client, wallpaper.document), + client=client, + )