diff --git a/pyrogram/methods/chats/__init__.py b/pyrogram/methods/chats/__init__.py index 3a1ce822..c3e792ca 100644 --- a/pyrogram/methods/chats/__init__.py +++ b/pyrogram/methods/chats/__init__.py @@ -61,6 +61,7 @@ from .unarchive_chats import UnarchiveChats from .unban_chat_member import UnbanChatMember from .unpin_all_chat_messages import UnpinAllChatMessages from .unpin_chat_message import UnpinChatMessage +from .update_chat_notifications import UpdateChatNotifications from .update_color import UpdateColor @@ -80,6 +81,7 @@ class Chats( SetChatDescription, PinChatMessage, UnpinChatMessage, + UpdateChatNotifications, UpdateColor, GetDialogs, GetChatMembersCount, diff --git a/pyrogram/methods/chats/update_chat_notifications.py b/pyrogram/methods/chats/update_chat_notifications.py new file mode 100644 index 00000000..c7905e15 --- /dev/null +++ b/pyrogram/methods/chats/update_chat_notifications.py @@ -0,0 +1,93 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present 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 . + +import datetime +from typing import Union + +import pyrogram +from pyrogram import raw +from pyrogram import types +from pyrogram import utils + +class UpdateChatNotifications: + async def update_chat_notifications( + self: "pyrogram.Client", + chat_id: Union[int, str], + mute: bool = None, + mute_until: datetime = utils.zero_datetime(), + stories_muted: bool = None, + stories_hide_sender: bool = None, + show_previews: bool = None + ) -> "types.Chat": + """Update the notification settings for the selected chat + + Parameters: + chat_id (``int`` | ``str``): + Unique identifier (int) or username (str) of the target chat. + + mute (``bool``, *optional*): + Pass True if you want to mute chat. + + until_date (:py:obj:`~datetime.datetime`, *optional*): + Date when the user will be unmuted. Defaults to epoch (mute forever). + + stories_muted (``bool``, *optional*): + N/A + + stories_hide_sender (``bool``, *optional*): + N/A + + show_previews (``bool``, *optional*): + If the text of the message shall be displayed in notification. + + Returns: + ``bool``: True on success, False otherwise. + + Example: + .. code-block:: python + + # Mute a chat permanently + app.update_chat_notifications(chat_id, mute=True) + + # Mute a chat for 10 minutes + app.update_chat_notifications( + chat_id, + mute=True + mute_until=datetime.timedelta(minutes=10) + ) + + # Unmute a chat + app.update_chat_notifications(chat_id, mute=False) + """ + if isinstance(mute_until, datetime.timedelta): + mute_until = datetime.datetime.now() + mute_until + + r = await self.invoke( + raw.functions.account.UpdateNotifySettings( + peer=raw.types.InputNotifyPeer(peer=await self.resolve_peer(chat_id)), + settings=raw.types.InputPeerNotifySettings( + show_previews=show_previews, + silent=mute, + mute_until=utils.datetime_to_timestamp(mute_until), + stories_muted=stories_muted, + stories_hide_sender=stories_hide_sender, + ) + ) + ) + + return r