diff --git a/pyrogram/methods/chats/__init__.py b/pyrogram/methods/chats/__init__.py index 8aeb5fe4..09b22682 100644 --- a/pyrogram/methods/chats/__init__.py +++ b/pyrogram/methods/chats/__init__.py @@ -52,6 +52,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_chat_username import UpdateChatUsername @@ -75,6 +76,7 @@ class Chats( GetChatMembersCount, IterDialogs, IterChatMembers, + UpdateChatNotifications, UpdateChatUsername, SetChatPermissions, GetDialogsCount, diff --git a/pyrogram/methods/chats/update_chat_notifications.py b/pyrogram/methods/chats/update_chat_notifications.py new file mode 100644 index 00000000..08e5640b --- /dev/null +++ b/pyrogram/methods/chats/update_chat_notifications.py @@ -0,0 +1,75 @@ +from typing import Union +from datetime import datetime, timedelta + +from pyrogram.raw.functions.account import UpdateNotifySettings +from pyrogram.raw.types import InputNotifyPeer, InputPeerNotifySettings +from pyrogram.scaffold import Scaffold + + +class UpdateChatNotifications(Scaffold): + async def update_chat_notifications( + self, + chat_id: Union[int, str], + show_previews: bool = None, + silent: bool = None, + mute_until: Union[int, datetime, timedelta] = None + ) -> bool: + """Update the notification settings for the selected chat + + Parameters: + chat_id (``int`` | ``str``): + Unique identifier (int) or username (str) of the target chat. + + show_previews (``bool``, *optional*): + If the text of the message shall be displayed in notification. + + silent (``bool``, *optional*): + If the chat shall be muted. + + mute_until (``int`` | ``datetime.datetime`` | ``datetime.timdelta``, *optional*): + Unix timestamp, datetime or timedelta object that sets up when notifications shall be switched off. + Default to forever. + + Returns: + ``bool``: True on success, False otherwise. + + Example: + .. code-block:: python + + # Mute a chat permanently + app.update_chat_notifications(chat_id, silent=True) + + # Mute a chat for 10 minutes + from datetime import timedelta + + app.update_chat_notifications( + chat_id, + silent=True + mute_until=timedelta(minutes=10) + ) + + # Unmute a chat + app.update_chat_notifications(chat_id, silent=False) + """ + + if isinstance(mute_until, datetime): + mute_until = mute_until.timestamp() + + if isinstance(mute_until, timedelta): + now = datetime.now() + mute_until = now.timestamp() + mute_until.total_seconds() + + peer = await self.resolve_peer(chat_id) + + r = await self.send( + UpdateNotifySettings( + peer=InputNotifyPeer(peer=peer), + settings=InputPeerNotifySettings( + show_previews=show_previews or None, + silent=silent or None, + mute_until=mute_until or None + ) + ) + ) + + return r diff --git a/pyrogram/types/user_and_chats/chat.py b/pyrogram/types/user_and_chats/chat.py index 200a31eb..0f3a552a 100644 --- a/pyrogram/types/user_and_chats/chat.py +++ b/pyrogram/types/user_and_chats/chat.py @@ -967,3 +967,83 @@ class Chat(Object): """ return await self._client.mark_chat_unread(self.id) + + async def enable_notifications( + self, + show_previews: bool = None, + mute_until: int = None + ) -> bool: + """Bound method *enable_notifications* of :obj:`~pyrogram.types.Chat`. + + Use as a shortcut for: + + .. code-block:: python + + client.update_chat_notifications( + chat_id=chat_id, + silent=False + ) + + Example: + .. code-block:: python + + chat.enable_notifications() + + Parameters: + show_previews (``bool``, *optional*): + If the text of the message shall be displayed in notification. + + mute_until (``int``, *optional*): + Unix date until which all notifications shall be switched off. + Default to forever. + + Returns: + ``bool``: True on success, False otherwise. + """ + + return await self._client.update_chat_notifications( + self.id, + show_previews=show_previews or None, + silent=False, + mute_until=mute_until or None + ) + + async def disable_notifications( + self, + show_previews: bool = None, + mute_until: int = None + ) -> bool: + """Bound method *disable_notifications* of :obj:`~pyrogram.types.Chat`. + + Use as a shortcut for: + + .. code-block:: python + + client.update_chat_notifications( + chat_id=chat_id, + silent=True + ) + + Example: + .. code-block:: python + + chat.disable_notifications() + + Parameters: + show_previews (``bool``, *optional*): + If the text of the message shall be displayed in notification. + + mute_until (``int``, *optional*): + Unix date until which all notifications shall be switched off. + Default to forever. + + Returns: + ``bool``: True on success, False otherwise. + """ + + return await self._client.update_chat_notifications( + self.id, + show_previews=show_previews or None, + silent=True, + mute_until=mute_until or None + )