From 31c5b919910c4808863801100f471d376e5f0d54 Mon Sep 17 00:00:00 2001 From: Colin Date: Thu, 13 May 2021 23:12:00 +0200 Subject: [PATCH 1/6] Add method to set the History TTL `app.set_history_ttl(chat_id, period)` to automatically delete messages after either a day or a week. 0 to disable. --- pyrogram/methods/chats/__init__.py | 4 +- pyrogram/methods/chats/set_history_ttl.py | 83 ++++++++++++++++++++ pyrogram/types/messages_and_media/message.py | 7 ++ 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 pyrogram/methods/chats/set_history_ttl.py diff --git a/pyrogram/methods/chats/__init__.py b/pyrogram/methods/chats/__init__.py index 31ffe4fd2f..18b9107def 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_history_ttl import SetHistoryTTL class Chats( @@ -94,6 +95,7 @@ class Chats( UnpinAllChatMessages, MarkChatUnread, GetChatEventLog, - GetChatOnlineCount + GetChatOnlineCount, + SetHistoryTTL, ): pass diff --git a/pyrogram/methods/chats/set_history_ttl.py b/pyrogram/methods/chats/set_history_ttl.py new file mode 100644 index 0000000000..7122f83e24 --- /dev/null +++ b/pyrogram/methods/chats/set_history_ttl.py @@ -0,0 +1,83 @@ +# 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 SetHistoryTTL(Scaffold): + async def set_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 or 608000 for 1 week. + 0 (zero) can be used to disable it. + Other values are not supported by Telegram. + + Returns: + :obj:`~pyrogram.types.Message`: On success, the generated Service Message is returned. + + Example: + .. code-block:: python + + # One Day + app.set_history_ttl("PyrogramChat", 86400) + + # A Week + app.set_history_ttl("PyrogramChat", 604800) + + # Disabling + app.set_history_ttl("PyrogramChat", 0) + + Raises: + ValueError: In case the chat_id doesn't belong to a supergroup or the time-to-live isn't 1 day or week. + """ + + peer = await self.resolve_peer(chat_id) + if not isinstance(peer, raw.types.InputPeerChannel): + raise ValueError(f'The chat_id "{chat_id}" does not belong to a channel') + + if period not in (0, 86400, 604800): + raise ValueError(f'The time-to-live "{period}" needs to be either 86400 (1 day) or 604800 (1 week).') + + else: + 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.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}, + ) \ No newline at end of file diff --git a/pyrogram/types/messages_and_media/message.py b/pyrogram/types/messages_and_media/message.py index f349d1d76d..5dc638e2f8 100644 --- a/pyrogram/types/messages_and_media/message.py +++ b/pyrogram/types/messages_and_media/message.py @@ -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, + 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.history_ttl = 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 + 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): + history_ttl = action.period + service_type = "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, + history_ttl=history_ttl, client=client # TODO: supergroup_chat_created ) From bf5882579e0b99963663a6023c1ad6855f061ba6 Mon Sep 17 00:00:00 2001 From: Colin Date: Fri, 14 May 2021 17:55:42 +0200 Subject: [PATCH 2/6] Addressing requested changes `set_history_ttl` -> `set_chat_history_ttl` in all occurences --- pyrogram/methods/chats/__init__.py | 4 +- ...history_ttl.py => set_chat_history_ttl.py} | 40 ++++++++----------- pyrogram/types/messages_and_media/message.py | 12 +++--- 3 files changed, 24 insertions(+), 32 deletions(-) rename pyrogram/methods/chats/{set_history_ttl.py => set_chat_history_ttl.py} (68%) diff --git a/pyrogram/methods/chats/__init__.py b/pyrogram/methods/chats/__init__.py index 18b9107def..c37723c707 100644 --- a/pyrogram/methods/chats/__init__.py +++ b/pyrogram/methods/chats/__init__.py @@ -54,7 +54,7 @@ from .unpin_chat_message import UnpinChatMessage from .update_chat_username import UpdateChatUsername from .get_chat_online_count import GetChatOnlineCount -from .set_history_ttl import SetHistoryTTL +from .set_chat_history_ttl import SetChatHistoryTTL class Chats( @@ -96,6 +96,6 @@ class Chats( MarkChatUnread, GetChatEventLog, GetChatOnlineCount, - SetHistoryTTL, + SetChatHistoryTTL, ): pass diff --git a/pyrogram/methods/chats/set_history_ttl.py b/pyrogram/methods/chats/set_chat_history_ttl.py similarity index 68% rename from pyrogram/methods/chats/set_history_ttl.py rename to pyrogram/methods/chats/set_chat_history_ttl.py index 7122f83e24..7f7be30545 100644 --- a/pyrogram/methods/chats/set_history_ttl.py +++ b/pyrogram/methods/chats/set_chat_history_ttl.py @@ -23,8 +23,8 @@ from pyrogram.scaffold import Scaffold -class SetHistoryTTL(Scaffold): - async def set_history_ttl( +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. @@ -58,26 +58,18 @@ async def set_history_ttl( ValueError: In case the chat_id doesn't belong to a supergroup or the time-to-live isn't 1 day or week. """ - peer = await self.resolve_peer(chat_id) - if not isinstance(peer, raw.types.InputPeerChannel): - raise ValueError(f'The chat_id "{chat_id}" does not belong to a channel') - - if period not in (0, 86400, 604800): - raise ValueError(f'The time-to-live "{period}" needs to be either 86400 (1 day) or 604800 (1 week).') - - else: - r = await self.send( - raw.functions.messages.SetHistoryTTL( - peer=await self.resolve_peer(chat_id), - period=period, - ) + 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.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}, - ) \ No newline at end of file + ) + + for i in r.updates: + if isinstance(i, 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 5dc638e2f8..981fd32272 100644 --- a/pyrogram/types/messages_and_media/message.py +++ b/pyrogram/types/messages_and_media/message.py @@ -352,7 +352,7 @@ def __init__( voice_chat_started: "types.VoiceChatStarted" = None, voice_chat_ended: "types.VoiceChatEnded" = None, voice_chat_members_invited: "types.VoiceChatMembersInvited" = None, - history_ttl: int = None, + chat_history_ttl: int = None, reply_markup: Union[ "types.InlineKeyboardMarkup", "types.ReplyKeyboardMarkup", @@ -424,7 +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.history_ttl = history_ttl + self.chat_history_ttl = chat_history_ttl @staticmethod async def _parse( @@ -454,7 +454,7 @@ async def _parse( voice_chat_started = None voice_chat_ended = None voice_chat_members_invited = None - history_ttl = None + chat_history_ttl = None service_type = None @@ -502,8 +502,8 @@ async def _parse( voice_chat_members_invited = types.VoiceChatMembersInvited._parse(client, action, users) service_type = "voice_chat_members_invited" elif isinstance(action, raw.types.MessageActionSetMessagesTTL): - history_ttl = action.period - service_type = "history_ttl" + 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)) @@ -529,7 +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, - history_ttl=history_ttl, + chat_history_ttl=chat_history_ttl, client=client # TODO: supergroup_chat_created ) From 6bc0329a384fae922db8ba8c2493c5aff7804953 Mon Sep 17 00:00:00 2001 From: Colin Date: Fri, 14 May 2021 17:56:30 +0200 Subject: [PATCH 3/6] Add bound method Chat.set_history_ttl() (Still the original name ha!) --- pyrogram/types/user_and_chats/chat.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pyrogram/types/user_and_chats/chat.py b/pyrogram/types/user_and_chats/chat.py index 8930bf3afb..14ef2f7e41 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() + + Example: + .. code-block:: python + + chat.set_history_ttl(68400) + + 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) From 583ffc7c0e597bd2df6df2bb59d136bc61b6c6b4 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sat, 15 May 2021 07:43:55 +0200 Subject: [PATCH 4/6] Update set_chat_history_ttl.py --- pyrogram/methods/chats/set_chat_history_ttl.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/pyrogram/methods/chats/set_chat_history_ttl.py b/pyrogram/methods/chats/set_chat_history_ttl.py index 7f7be30545..ea49235610 100644 --- a/pyrogram/methods/chats/set_chat_history_ttl.py +++ b/pyrogram/methods/chats/set_chat_history_ttl.py @@ -35,9 +35,7 @@ async def set_chat_history_ttl( period (``int``): The time-to-live for the chat history. - Either 86000 for 1 day or 608000 for 1 week. - 0 (zero) can be used to disable it. - Other values are not supported by Telegram. + 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. @@ -46,16 +44,13 @@ async def set_chat_history_ttl( .. code-block:: python # One Day - app.set_history_ttl("PyrogramChat", 86400) + app.set_chat_history_ttl(chat_id, 86400) # A Week - app.set_history_ttl("PyrogramChat", 604800) + app.set_chat_history_ttl(chat_id, 604800) # Disabling - app.set_history_ttl("PyrogramChat", 0) - - Raises: - ValueError: In case the chat_id doesn't belong to a supergroup or the time-to-live isn't 1 day or week. + app.set_chat_history_ttl(chat_id, 0) """ r = await self.send( @@ -66,7 +61,8 @@ async def set_chat_history_ttl( ) for i in r.updates: - if isinstance(i, raw.types.UpdateNewChannelMessage): + if isinstance(i, (raw.types.UpdateNewMessage, + raw.types.UpdateNewChannelMessage)): return await types.Message._parse( self, i.message, From 8e641352fb5c015a0dc4e6d6f285289873aae9fe Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sat, 15 May 2021 07:44:51 +0200 Subject: [PATCH 5/6] Update message.py --- pyrogram/types/messages_and_media/message.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyrogram/types/messages_and_media/message.py b/pyrogram/types/messages_and_media/message.py index 981fd32272..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. From 72e656edecd57e9135d1c4b348d60796b9cef749 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sat, 15 May 2021 07:46:02 +0200 Subject: [PATCH 6/6] Update chat.py --- pyrogram/types/user_and_chats/chat.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyrogram/types/user_and_chats/chat.py b/pyrogram/types/user_and_chats/chat.py index 14ef2f7e41..8eadf39157 100644 --- a/pyrogram/types/user_and_chats/chat.py +++ b/pyrogram/types/user_and_chats/chat.py @@ -987,12 +987,12 @@ async def set_history_ttl(self, period: int) -> "types.Message": .. code-block:: python - client.set_chat_history_ttl() + client.set_chat_history_ttl(chat_id, 604800) Example: .. code-block:: python - chat.set_history_ttl(68400) + chat.set_history_ttl(604800) Returns: :obj:`~pyrogram.types.Message`: On success, the generated service message is returned.