Skip to content
This repository was archived by the owner on Dec 23, 2024. It is now read-only.

Commit aaf7657

Browse files
committed
Merge remote-tracking branch 'ColinShark/history-ttl' into history-ttl
# Conflicts: # pyrogram/methods/chats/__init__.py # pyrogram/types/messages_and_media/message.py # pyrogram/types/user_and_chats/chat.py
2 parents efac171 + 72e656e commit aaf7657

File tree

5 files changed

+109
-7
lines changed

5 files changed

+109
-7
lines changed

pyrogram/enums/message_service_type.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,6 @@ class MessageServiceType(AutoName):
7171

7272
WEB_APP_DATA = auto()
7373
"Web app data"
74+
75+
CHAT_TTL_CHANGED = auto()
76+
"Chat TTL changed"

pyrogram/methods/chats/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
from .unban_chat_member import UnbanChatMember
5656
from .unpin_all_chat_messages import UnpinAllChatMessages
5757
from .unpin_chat_message import UnpinChatMessage
58+
from .set_chat_ttl import SetChatTTL
5859

5960

6061
class Chats(
@@ -96,6 +97,7 @@ class Chats(
9697
GetChatOnlineCount,
9798
GetSendAsChats,
9899
SetSendAsChat,
99-
SetChatProtectedContent
100+
SetChatProtectedContent,
101+
SetChatTTL,
100102
):
101103
pass
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2021 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 typing import Union
20+
21+
import pyrogram
22+
from pyrogram import raw
23+
from pyrogram import types
24+
25+
26+
class SetChatTTL:
27+
async def set_chat_ttl(
28+
self: "pyrogram.Client", chat_id: Union[int, str], period: int
29+
) -> "types.Message":
30+
"""Set the time-to-live for the chat.
31+
32+
Parameters:
33+
chat_id (``int`` | ``str``):
34+
Unique identifier (int) or username (str) of the target chat.
35+
36+
period (``int``):
37+
The time-to-live for the chat.
38+
Either 86000 for 1 day, 604800 for 1 week or 0 (zero) to disable it.
39+
40+
Returns:
41+
:obj:`~pyrogram.types.Message`: On success, the generated Service Message is returned.
42+
43+
Example:
44+
.. code-block:: python
45+
46+
# One Day
47+
app.set_chat_ttl(chat_id, 86400)
48+
49+
# A Week
50+
app.set_chat_ttl(chat_id, 604800)
51+
52+
# Disabling
53+
app.set_chat_ttl(chat_id, 0)
54+
"""
55+
56+
r = await self.invoke(
57+
raw.functions.messages.SetHistoryTTL(
58+
peer=await self.resolve_peer(chat_id),
59+
period=period,
60+
)
61+
)
62+
63+
for i in r.updates:
64+
if isinstance(i, (raw.types.UpdateNewMessage,
65+
raw.types.UpdateNewChannelMessage)):
66+
return await types.Message._parse(
67+
self,
68+
i.message,
69+
{i.id: i for i in r.users},
70+
{i.id: i for i in r.chats},
71+
)

pyrogram/types/messages_and_media/message.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,9 @@ class Message(Object, Update):
257257
258258
views (``int``, *optional*):
259259
Channel post views.
260-
261-
forwards (``int``, *optional*):
262-
Channel post forwards.
260+
261+
forwards (``int``, *optional*):
262+
Channel post forwards.
263263
264264
via_bot (:obj:`~pyrogram.types.User`):
265265
The information of the bot that generated the message from an inline query of a user.
@@ -378,6 +378,7 @@ def __init__(
378378
video_chat_ended: "types.VideoChatEnded" = None,
379379
video_chat_members_invited: "types.VideoChatMembersInvited" = None,
380380
web_app_data: "types.WebAppData" = None,
381+
chat_ttl_period: int = None,
381382
reply_markup: Union[
382383
"types.InlineKeyboardMarkup",
383384
"types.ReplyKeyboardMarkup",
@@ -457,6 +458,7 @@ def __init__(
457458
self.video_chat_members_invited = video_chat_members_invited
458459
self.web_app_data = web_app_data
459460
self.reactions = reactions
461+
self.chat_ttl_period = chat_ttl_period
460462

461463
@staticmethod
462464
async def _parse(
@@ -507,6 +509,7 @@ async def _parse(
507509
video_chat_ended = None
508510
video_chat_members_invited = None
509511
web_app_data = None
512+
chat_ttl_period = None
510513

511514
service_type = None
512515

@@ -556,6 +559,9 @@ async def _parse(
556559
elif isinstance(action, raw.types.MessageActionWebViewDataSentMe):
557560
web_app_data = types.WebAppData._parse(action)
558561
service_type = enums.MessageServiceType.WEB_APP_DATA
562+
elif isinstance(action, raw.types.MessageActionSetMessagesTTL):
563+
chat_ttl_period = action.period
564+
service_type = enums.MessageServiceType.CHAT_TTL_CHANGED
559565

560566
from_user = types.User._parse(client, users.get(user_id, None))
561567
sender_chat = types.Chat._parse(client, message, users, chats, is_chat=False) if not from_user else None
@@ -581,6 +587,7 @@ async def _parse(
581587
video_chat_ended=video_chat_ended,
582588
video_chat_members_invited=video_chat_members_invited,
583589
web_app_data=web_app_data,
590+
chat_ttl_period=chat_ttl_period,
584591
client=client
585592
# TODO: supergroup_chat_created
586593
)
@@ -1538,7 +1545,7 @@ async def reply_document(
15381545
15391546
caption_entities (List of :obj:`~pyrogram.types.MessageEntity`):
15401547
List of special entities that appear in the caption, which can be specified instead of *parse_mode*.
1541-
1548+
15421549
file_name (``str``, *optional*):
15431550
File name of the document sent.
15441551
Defaults to file's path basename.
@@ -1554,7 +1561,7 @@ async def reply_document(
15541561
15551562
reply_to_message_id (``int``, *optional*):
15561563
If the message is a reply, ID of the original message.
1557-
1564+
15581565
schedule_date (:py:obj:`~datetime.datetime`, *optional*):
15591566
Date when the message will be automatically sent.
15601567
@@ -3359,7 +3366,7 @@ async def react(self, emoji: str = "", big: bool = False) -> bool:
33593366
emoji (``str``, *optional*):
33603367
Reaction emoji.
33613368
Pass "" as emoji (default) to retract the reaction.
3362-
3369+
33633370
big (``bool``, *optional*):
33643371
Pass True to show a bigger and longer reaction.
33653372
Defaults to False.

pyrogram/types/user_and_chats/chat.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,3 +961,22 @@ async def unpin_all_messages(self) -> bool:
961961
"""
962962

963963
return await self._client.unpin_all_chat_messages(self.id)
964+
965+
async def set_ttl(self, period: int) -> "types.Message":
966+
"""Bound method *set_ttl* of :obj:`~pyrogram.types.Chat`.
967+
968+
Use as a shortcut for:
969+
970+
.. code-block:: python
971+
972+
client.set_chat_ttl(chat_id, 604800)
973+
974+
Example:
975+
.. code-block:: python
976+
977+
chat.set_ttl(604800)
978+
979+
Returns:
980+
:obj:`~pyrogram.types.Message`: On success, the generated service message is returned.
981+
"""
982+
return await self._client.set_chat_ttl(self.id, period=period)

0 commit comments

Comments
 (0)