Skip to content

Commit 34d0c02

Browse files
z44dalissonlauffer
andauthored
feat: add ChatBackground type (#33)
Also fixes Message.pinned_message typing hint. * add ChatBackground * add .background field * fix type hint * update docs * reformat * update copyright and import * chore: enable `annotations` future import and lint --------- Co-authored-by: Alisson Lauffer <alissonvitortc@gmail.com>
1 parent ed4e775 commit 34d0c02

File tree

4 files changed

+131
-1
lines changed

4 files changed

+131
-1
lines changed

compiler/docs/compiler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ def get_title_list(s: str) -> list[str]:
389389
Dialog
390390
Restriction
391391
EmojiStatus
392+
ChatBackground
392393
""",
393394
"messages_media": """
394395
Messages & Media

hydrogram/types/user_and_chats/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
from .chat import Chat
2121
from .chat_admin_with_invite_links import ChatAdminWithInviteLinks
22+
from .chat_background import ChatBackground
2223
from .chat_event import ChatEvent
2324
from .chat_event_filter import ChatEventFilter
2425
from .chat_invite_link import ChatInviteLink
@@ -54,6 +55,7 @@
5455
__all__ = [
5556
"Chat",
5657
"ChatAdminWithInviteLinks",
58+
"ChatBackground",
5759
"ChatEvent",
5860
"ChatEventFilter",
5961
"ChatInviteLink",

hydrogram/types/user_and_chats/chat.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ class Chat(Object):
115115
Pinned message, for groups, supergroups channels and own chat.
116116
Returned only in :meth:`~hydrogram.Client.get_chat`.
117117
118+
background (:obj:`~hydrogram.types.ChatBackground`, *optional*):
119+
A chat background.
120+
118121
sticker_set_name (``str``, *optional*):
119122
For supergroups, name of group sticker set.
120123
Returned only in :meth:`~hydrogram.Client.get_chat`.
@@ -177,7 +180,8 @@ def __init__(
177180
dc_id: int | None = None,
178181
has_protected_content: bool | None = None,
179182
invite_link: str | None = None,
180-
pinned_message=None,
183+
pinned_message: types.Message | None = None,
184+
background: types.ChatBackground | None = None,
181185
sticker_set_name: str | None = None,
182186
can_set_sticker_set: bool | None = None,
183187
members_count: int | None = None,
@@ -213,6 +217,7 @@ def __init__(
213217
self.has_protected_content = has_protected_content
214218
self.invite_link = invite_link
215219
self.pinned_message = pinned_message
220+
self.background = background
216221
self.sticker_set_name = sticker_set_name
217222
self.can_set_sticker_set = can_set_sticker_set
218223
self.members_count = members_count
@@ -346,6 +351,9 @@ async def _parse_full(
346351
parsed_chat.pinned_message = await client.get_messages(
347352
parsed_chat.id, message_ids=full_user.pinned_msg_id
348353
)
354+
355+
if getattr(full_user, "wallpaper", None):
356+
parsed_chat.background = types.ChatBackground._parse(client, full_user.wallpaper)
349357
else:
350358
full_chat = chat_full.full_chat
351359
chat_raw = chats[full_chat.id]
@@ -375,6 +383,9 @@ async def _parse_full(
375383

376384
parsed_chat.send_as_chat = Chat._parse_chat(client, send_as_raw)
377385

386+
if getattr(full_chat, "wallpaper", None):
387+
parsed_chat.background = types.ChatBackground._parse(client, full_chat.wallpaper)
388+
378389
parsed_chat.description = full_chat.about or None
379390

380391
if full_chat.pinned_msg_id:
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Hydrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2023-present Hydrogram <https://hydrogram.org>
3+
#
4+
# This file is part of Hydrogram.
5+
#
6+
# Hydrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Hydrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Hydrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from __future__ import annotations
20+
21+
from typing import TYPE_CHECKING
22+
23+
import hydrogram
24+
from hydrogram import raw, types, utils
25+
from hydrogram.file_id import (
26+
FileId,
27+
FileType,
28+
FileUniqueId,
29+
FileUniqueType,
30+
ThumbnailSource,
31+
)
32+
from hydrogram.types.object import Object
33+
34+
if TYPE_CHECKING:
35+
from datetime import datetime
36+
37+
38+
class ChatBackground(Object):
39+
"""Describes a background set for a specific chat.
40+
41+
Parameters:
42+
file_id (``str``):
43+
Identifier for this file, which can be used to download the file.
44+
45+
file_unique_id (``str``):
46+
Unique identifier for this file, which is supposed to be the same over time and for different accounts.
47+
Can't be used to download or reuse the file.
48+
49+
file_size (``int``):
50+
File size.
51+
52+
date (:py:obj:`~datetime.datetime`):
53+
Date the background was setted.
54+
55+
slug (``str``):
56+
Identifier of the background code.
57+
You can combine it with `https://t.me/bg/{slug}`
58+
to get link for this background.
59+
60+
thumbs (List of :obj:`~pyrogram.types.Thumbnail`, *optional*):
61+
Available thumbnails of this background.
62+
63+
link (``str``, *property*):
64+
Generate a link to this background code.
65+
"""
66+
67+
def __init__(
68+
self,
69+
*,
70+
client: hydrogram.Client = None,
71+
file_id: str,
72+
file_unique_id: str,
73+
file_size: int,
74+
date: datetime,
75+
slug: str,
76+
thumbs: list[types.Thumbnail] | None = None,
77+
):
78+
super().__init__(client)
79+
80+
self.file_id = file_id
81+
self.file_unique_id = file_unique_id
82+
self.file_size = file_size
83+
self.date = date
84+
self.slug = slug
85+
self.thumbs = thumbs
86+
87+
@property
88+
def link(self) -> str:
89+
return f"https://t.me/bg/{self.slug}"
90+
91+
@staticmethod
92+
def _parse(
93+
client,
94+
wallpaper: raw.types.Wallpaper,
95+
) -> ChatBackground:
96+
return ChatBackground(
97+
file_id=FileId(
98+
dc_id=wallpaper.document.dc_id,
99+
file_reference=wallpaper.document.file_reference,
100+
access_hash=wallpaper.document.access_hash,
101+
file_type=FileType.BACKGROUND,
102+
media_id=wallpaper.document.id,
103+
volume_id=0,
104+
local_id=0,
105+
thumbnail_source=ThumbnailSource.THUMBNAIL,
106+
thumbnail_file_type=FileType.BACKGROUND,
107+
).encode(),
108+
file_unique_id=FileUniqueId(
109+
file_unique_type=FileUniqueType.DOCUMENT, media_id=wallpaper.document.id
110+
).encode(),
111+
file_size=wallpaper.document.size,
112+
slug=wallpaper.slug,
113+
date=utils.timestamp_to_datetime(wallpaper.document.date),
114+
thumbs=types.Thumbnail._parse(client, wallpaper.document),
115+
client=client,
116+
)

0 commit comments

Comments
 (0)