diff --git a/pyrogram/enums/message_service_type.py b/pyrogram/enums/message_service_type.py index 8a60e29e6e..451ab878c0 100644 --- a/pyrogram/enums/message_service_type.py +++ b/pyrogram/enums/message_service_type.py @@ -71,3 +71,6 @@ class MessageServiceType(AutoName): WEB_APP_DATA = auto() "Web app data" + + CHAT_TTL_CHANGED = auto() + "Chat TTL changed" diff --git a/pyrogram/methods/chats/__init__.py b/pyrogram/methods/chats/__init__.py index a2bdde08a4..74993f628f 100644 --- a/pyrogram/methods/chats/__init__.py +++ b/pyrogram/methods/chats/__init__.py @@ -55,6 +55,7 @@ from .unban_chat_member import UnbanChatMember from .unpin_all_chat_messages import UnpinAllChatMessages from .unpin_chat_message import UnpinChatMessage +from .set_chat_ttl import SetChatTTL class Chats( @@ -96,6 +97,7 @@ class Chats( GetChatOnlineCount, GetSendAsChats, SetSendAsChat, - SetChatProtectedContent + SetChatProtectedContent, + SetChatTTL, ): pass diff --git a/pyrogram/methods/chats/set_chat_ttl.py b/pyrogram/methods/chats/set_chat_ttl.py new file mode 100644 index 0000000000..d4a6b8ade0 --- /dev/null +++ b/pyrogram/methods/chats/set_chat_ttl.py @@ -0,0 +1,71 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2021 Dan +# +# This file is part of Pyrogram. +# +# Pyrogram 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. +# +# Pyrogram 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 Pyrogram. If not, see . + +from typing import Union + +import pyrogram +from pyrogram import raw +from pyrogram import types + + +class SetChatTTL: + async def set_chat_ttl( + self: "pyrogram.Client", chat_id: Union[int, str], period: int + ) -> "types.Message": + """Set the time-to-live for the chat. + + Parameters: + chat_id (``int`` | ``str``): + Unique identifier (int) or username (str) of the target chat. + + period (``int``): + The time-to-live for the chat. + Either 86000 for 1 day, 604800 for 1 week or 0 (zero) to disable it. + + Returns: + :obj:`~pyrogram.types.Message`: On success, the generated Service Message is returned. + + Example: + .. code-block:: python + + # One Day + app.set_chat_ttl(chat_id, 86400) + + # A Week + app.set_chat_ttl(chat_id, 604800) + + # Disabling + app.set_chat_ttl(chat_id, 0) + """ + + r = await self.invoke( + raw.functions.messages.SetHistoryTTL( + peer=await self.resolve_peer(chat_id), + period=period, + ) + ) + + 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}, + ) diff --git a/pyrogram/types/messages_and_media/message.py b/pyrogram/types/messages_and_media/message.py index 62a85b7593..263bf77a61 100644 --- a/pyrogram/types/messages_and_media/message.py +++ b/pyrogram/types/messages_and_media/message.py @@ -257,8 +257,8 @@ class Message(Object, Update): views (``int``, *optional*): Channel post views. - - forwards (``int``, *optional*): + + forwards (``int``, *optional*): Channel post forwards. via_bot (:obj:`~pyrogram.types.User`): @@ -378,6 +378,7 @@ def __init__( video_chat_ended: "types.VideoChatEnded" = None, video_chat_members_invited: "types.VideoChatMembersInvited" = None, web_app_data: "types.WebAppData" = None, + chat_ttl_period: int = None, reply_markup: Union[ "types.InlineKeyboardMarkup", "types.ReplyKeyboardMarkup", @@ -457,6 +458,7 @@ def __init__( self.video_chat_members_invited = video_chat_members_invited self.web_app_data = web_app_data self.reactions = reactions + self.chat_ttl_period = chat_ttl_period @staticmethod async def _parse( @@ -507,6 +509,7 @@ async def _parse( video_chat_ended = None video_chat_members_invited = None web_app_data = None + chat_ttl_period = None service_type = None @@ -556,6 +559,9 @@ async def _parse( elif isinstance(action, raw.types.MessageActionWebViewDataSentMe): web_app_data = types.WebAppData._parse(action) service_type = enums.MessageServiceType.WEB_APP_DATA + elif isinstance(action, raw.types.MessageActionSetMessagesTTL): + chat_ttl_period = action.period + service_type = enums.MessageServiceType.CHAT_TTL_CHANGED from_user = types.User._parse(client, users.get(user_id, None)) 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( video_chat_ended=video_chat_ended, video_chat_members_invited=video_chat_members_invited, web_app_data=web_app_data, + chat_ttl_period=chat_ttl_period, client=client # TODO: supergroup_chat_created ) @@ -1538,7 +1545,7 @@ async def reply_document( caption_entities (List of :obj:`~pyrogram.types.MessageEntity`): List of special entities that appear in the caption, which can be specified instead of *parse_mode*. - + file_name (``str``, *optional*): File name of the document sent. Defaults to file's path basename. @@ -1554,7 +1561,7 @@ async def reply_document( reply_to_message_id (``int``, *optional*): If the message is a reply, ID of the original message. - + schedule_date (:py:obj:`~datetime.datetime`, *optional*): Date when the message will be automatically sent. @@ -3359,7 +3366,7 @@ async def react(self, emoji: str = "", big: bool = False) -> bool: emoji (``str``, *optional*): Reaction emoji. Pass "" as emoji (default) to retract the reaction. - + big (``bool``, *optional*): Pass True to show a bigger and longer reaction. Defaults to False. diff --git a/pyrogram/types/user_and_chats/chat.py b/pyrogram/types/user_and_chats/chat.py index f37542fb0c..d444722cf4 100644 --- a/pyrogram/types/user_and_chats/chat.py +++ b/pyrogram/types/user_and_chats/chat.py @@ -961,3 +961,22 @@ async def unpin_all_messages(self) -> bool: """ return await self._client.unpin_all_chat_messages(self.id) + + async def set_ttl(self, period: int) -> "types.Message": + """Bound method *set_ttl* of :obj:`~pyrogram.types.Chat`. + + Use as a shortcut for: + + .. code-block:: python + + client.set_chat_ttl(chat_id, 604800) + + Example: + .. code-block:: python + + chat.set_ttl(604800) + + Returns: + :obj:`~pyrogram.types.Message`: On success, the generated service message is returned. + """ + return await self._client.set_chat_ttl(self.id, period=period)