Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to set the History TTL #1384

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions pyrogram/enums/message_service_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,6 @@ class MessageServiceType(AutoName):

WEB_APP_DATA = auto()
"Web app data"

CHAT_TTL_CHANGED = auto()
"Chat TTL changed"
4 changes: 3 additions & 1 deletion pyrogram/methods/chats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -96,6 +97,7 @@ class Chats(
GetChatOnlineCount,
GetSendAsChats,
SetSendAsChat,
SetChatProtectedContent
SetChatProtectedContent,
SetChatTTL,
):
pass
71 changes: 71 additions & 0 deletions pyrogram/methods/chats/set_chat_ttl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2021 Dan <https://github.com/delivrance>
#
# 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 <http://www.gnu.org/licenses/>.

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},
)
17 changes: 12 additions & 5 deletions pyrogram/types/messages_and_media/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`):
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
)
Expand Down Expand Up @@ -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.
Expand All @@ -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.

Expand Down Expand Up @@ -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.
Expand Down
19 changes: 19 additions & 0 deletions pyrogram/types/user_and_chats/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)