diff --git a/pyrogram/methods/chats/__init__.py b/pyrogram/methods/chats/__init__.py index 31ffe4fd2f..c37723c707 100644 --- a/pyrogram/methods/chats/__init__.py +++ b/pyrogram/methods/chats/__init__.py @@ -54,6 +54,7 @@ from .unpin_chat_message import UnpinChatMessage from .update_chat_username import UpdateChatUsername from .get_chat_online_count import GetChatOnlineCount +from .set_chat_history_ttl import SetChatHistoryTTL class Chats( @@ -94,6 +95,7 @@ class Chats( UnpinAllChatMessages, MarkChatUnread, GetChatEventLog, - GetChatOnlineCount + GetChatOnlineCount, + SetChatHistoryTTL, ): pass diff --git a/pyrogram/methods/chats/set_chat_history_ttl.py b/pyrogram/methods/chats/set_chat_history_ttl.py new file mode 100644 index 0000000000..ea49235610 --- /dev/null +++ b/pyrogram/methods/chats/set_chat_history_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, Optional + +from pyrogram import raw +from pyrogram import types +from pyrogram.scaffold import Scaffold + + +class SetChatHistoryTTL(Scaffold): + async def set_chat_history_ttl( + self, chat_id: Union[int, str], period: int + ) -> "types.Message": + """Set the time-to-live for the chat history. + + Parameters: + chat_id (``int`` | ``str``): + Unique identifier (int) or username (str) of the target chat. + + period (``int``): + The time-to-live for the chat history. + 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_history_ttl(chat_id, 86400) + + # A Week + app.set_chat_history_ttl(chat_id, 604800) + + # Disabling + app.set_chat_history_ttl(chat_id, 0) + """ + + r = await self.send( + 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 f349d1d76d..72d1b92101 100644 --- a/pyrogram/types/messages_and_media/message.py +++ b/pyrogram/types/messages_and_media/message.py @@ -111,7 +111,7 @@ class Message(Object, Update): A service message has one and only one of these fields set: new_chat_members, left_chat_member, new_chat_title, new_chat_photo, delete_chat_photo, group_chat_created, channel_chat_created, migrate_to_chat_id, migrate_from_chat_id, pinned_message, game_high_score, voice_chat_started, - voice_chat_ended, voice_chat_scheduled, voice_chat_members_invited. + voice_chat_ended, voice_chat_scheduled, voice_chat_members_invited, chat_history_ttl. media (``str``, *optional*): The message is a media message. This field will contain the name of the media message. @@ -352,6 +352,7 @@ def __init__( voice_chat_started: "types.VoiceChatStarted" = None, voice_chat_ended: "types.VoiceChatEnded" = None, voice_chat_members_invited: "types.VoiceChatMembersInvited" = None, + chat_history_ttl: int = None, reply_markup: Union[ "types.InlineKeyboardMarkup", "types.ReplyKeyboardMarkup", @@ -423,6 +424,7 @@ def __init__( self.voice_chat_started = voice_chat_started self.voice_chat_ended = voice_chat_ended self.voice_chat_members_invited = voice_chat_members_invited + self.chat_history_ttl = chat_history_ttl @staticmethod async def _parse( @@ -452,6 +454,7 @@ async def _parse( voice_chat_started = None voice_chat_ended = None voice_chat_members_invited = None + chat_history_ttl = None service_type = None @@ -498,6 +501,9 @@ async def _parse( elif isinstance(action, raw.types.MessageActionInviteToGroupCall): voice_chat_members_invited = types.VoiceChatMembersInvited._parse(client, action, users) service_type = "voice_chat_members_invited" + elif isinstance(action, raw.types.MessageActionSetMessagesTTL): + chat_history_ttl = action.period + service_type = "chat_history_ttl" user = utils.get_raw_peer_id(message.from_id) or utils.get_raw_peer_id(message.peer_id) from_user = types.User._parse(client, users.get(user, None)) @@ -523,6 +529,7 @@ async def _parse( voice_chat_started=voice_chat_started, voice_chat_ended=voice_chat_ended, voice_chat_members_invited=voice_chat_members_invited, + chat_history_ttl=chat_history_ttl, client=client # TODO: supergroup_chat_created ) diff --git a/pyrogram/types/user_and_chats/chat.py b/pyrogram/types/user_and_chats/chat.py index 8930bf3afb..8eadf39157 100644 --- a/pyrogram/types/user_and_chats/chat.py +++ b/pyrogram/types/user_and_chats/chat.py @@ -979,3 +979,22 @@ async def mark_unread(self, ) -> bool: """ return await self._client.mark_chat_unread(self.id) + + async def set_history_ttl(self, period: int) -> "types.Message": + """Bound method *set_history_ttl* of :obj:`~pyrogram.types.Chat`. + + Use as a shortcut for: + + .. code-block:: python + + client.set_chat_history_ttl(chat_id, 604800) + + Example: + .. code-block:: python + + chat.set_history_ttl(604800) + + Returns: + :obj:`~pyrogram.types.Message`: On success, the generated service message is returned. + """ + return await self._client.set_chat_history_ttl(self.id, period=period)