Skip to content

Commit 2afc7bc

Browse files
authored
Add ChatBackground type (#40)
1 parent b19aaa0 commit 2afc7bc

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

compiler/docs/compiler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,7 @@ def get_title_list(s: str) -> list:
616616
VideoChatScheduled
617617
VideoChatStarted
618618
RtmpUrl
619+
ChatBackground
619620
""",
620621
)
621622

pyrogram/types/user_and_chats/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
from .video_chat_scheduled import VideoChatScheduled
4646
from .video_chat_started import VideoChatStarted
4747
from .rtmp_url import RtmpUrl
48+
from .chat_background import ChatBackground
4849

4950
__all__ = [
5051
"Birthdate",
@@ -76,4 +77,5 @@
7677
"VideoChatScheduled",
7778
"VideoChatStarted",
7879
"RtmpUrl",
80+
"ChatBackground"
7981
]

pyrogram/types/user_and_chats/chat.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ class Chat(Object):
9292
emoji_status (:obj:`~pyrogram.types.EmojiStatus`, *optional*):
9393
Emoji status.
9494
95+
background (:obj:`~pyrogram.types.ChatBackground`, *optional*):
96+
A chat background.
97+
9598
bio (``str``, *optional*):
9699
Bio of the other party in a private chat.
97100
Returned only in :meth:`~pyrogram.Client.get_chat`.
@@ -254,6 +257,7 @@ def __init__(
254257
accent_color: "types.ChatColor" = None,
255258
profile_color: "types.ChatColor" = None,
256259
emoji_status: "types.EmojiStatus" = None,
260+
background: "types.ChatBackground" = None,
257261
has_visible_history: bool = None,
258262
has_hidden_members: bool = None,
259263
has_aggressive_anti_spam_enabled: bool = None,
@@ -308,6 +312,7 @@ def __init__(
308312
self.accent_color = accent_color
309313
self.profile_color = profile_color
310314
self.emoji_status = emoji_status
315+
self.background = background
311316
self.has_visible_history = has_visible_history
312317
self.has_hidden_members = has_hidden_members
313318
self.has_aggressive_anti_spam_enabled = has_aggressive_anti_spam_enabled
@@ -326,6 +331,7 @@ def __init__(
326331
self.business_location = business_location
327332
self.business_opening_hours = business_opening_hours
328333
self.active_usernames = active_usernames
334+
self.max_reaction_count = max_reaction_count
329335
self._raw = _raw
330336

331337
@staticmethod
@@ -609,6 +615,9 @@ async def _parse_full(client, chat_full: Union[raw.types.messages.ChatFull, raw.
609615
if isinstance(full_chat.exported_invite, raw.types.ChatInviteExported):
610616
parsed_chat.invite_link = full_chat.exported_invite.link
611617

618+
if hasattr(full_chat, "wallpaper") and getattr(full_chat, "wallpaper"):
619+
parsed_chat.background = types.ChatBackground._parse(client, full_chat.wallpaper)
620+
612621
parsed_chat.available_reactions = types.ChatReactions._parse(
613622
client,
614623
full_chat.available_reactions,
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram 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+
# Pyrogram 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 Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from datetime import datetime
20+
from typing import List
21+
22+
import pyrogram
23+
from pyrogram import raw, utils
24+
from pyrogram import types
25+
from pyrogram.file_id import (
26+
FileId,
27+
FileType,
28+
FileUniqueId,
29+
FileUniqueType,
30+
ThumbnailSource,
31+
)
32+
from ..object import Object
33+
34+
35+
class ChatBackground(Object):
36+
"""Describes a background set for a specific chat.
37+
38+
Parameters:
39+
file_id (``str``):
40+
Identifier for this file, which can be used to download the file.
41+
42+
file_unique_id (``str``):
43+
Unique identifier for this file, which is supposed to be the same over time and for different accounts.
44+
Can't be used to download or reuse the file.
45+
46+
file_size (``int``):
47+
File size.
48+
49+
date (:py:obj:`~datetime.datetime`):
50+
Date the background was setted.
51+
52+
slug (``str``):
53+
Identifier of the background code.
54+
You can combine it with `https://t.me/bg/{slug}`
55+
to get link for this background.
56+
57+
thumbs (List of :obj:`~pyrogram.types.Thumbnail`, *optional*):
58+
Available thumbnails of this background.
59+
60+
link (``str``, *property*):
61+
Generate a link to this background code.
62+
"""
63+
64+
def __init__(
65+
self,
66+
*,
67+
client: "pyrogram.Client" = None,
68+
file_id: str,
69+
file_unique_id: str,
70+
file_size: int,
71+
date: datetime,
72+
slug: str,
73+
thumbs: List["types.Thumbnail"] = None,
74+
):
75+
super().__init__(client)
76+
77+
self.file_id = file_id
78+
self.file_unique_id = file_unique_id
79+
self.file_size = file_size
80+
self.date = date
81+
self.slug = slug
82+
self.thumbs = thumbs
83+
84+
@property
85+
def link(self) -> str:
86+
return f"https://t.me/bg/{self.slug}"
87+
88+
@staticmethod
89+
def _parse(
90+
client,
91+
wallpaper: "raw.types.Wallpaper",
92+
) -> "ChatBackground":
93+
return ChatBackground(
94+
file_id=FileId(
95+
dc_id=wallpaper.document.dc_id,
96+
file_reference=wallpaper.document.file_reference,
97+
access_hash=wallpaper.document.access_hash,
98+
file_type=FileType.BACKGROUND,
99+
media_id=wallpaper.document.id,
100+
volume_id=0,
101+
local_id=0,
102+
thumbnail_source=ThumbnailSource.THUMBNAIL,
103+
thumbnail_file_type=FileType.BACKGROUND,
104+
).encode(),
105+
file_unique_id=FileUniqueId(
106+
file_unique_type=FileUniqueType.DOCUMENT, media_id=wallpaper.document.id
107+
).encode(),
108+
file_size=wallpaper.document.size,
109+
slug=wallpaper.slug,
110+
date=utils.timestamp_to_datetime(wallpaper.document.date),
111+
thumbs=types.Thumbnail._parse(client, wallpaper.document),
112+
client=client,
113+
)

0 commit comments

Comments
 (0)