Skip to content
This repository was archived by the owner on Dec 23, 2024. It is now read-only.

update_chat_notifications new high-level method #477

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pyrogram/methods/chats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
from .unarchive_chats import UnarchiveChats
from .unban_chat_member import UnbanChatMember
from .unpin_chat_message import UnpinChatMessage
from .update_chat_notifications import UpdateChatNotifications
from .update_chat_username import UpdateChatUsername


Expand All @@ -73,6 +74,7 @@ class Chats(
GetChatMembersCount,
IterDialogs,
IterChatMembers,
UpdateChatNotifications,
UpdateChatUsername,
SetChatPermissions,
GetDialogsCount,
Expand Down
67 changes: 67 additions & 0 deletions pyrogram/methods/chats/update_chat_notifications.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from typing import Union

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: int = 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``, *optional*):
When notifications shall be switched off. Unix time.
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 time import time

app.update_chat_notifications(
chat_id,
silent=True,
mute_until=time() + 600
)

# Unmute a chat
app.update_chat_notifications(chat_id, silent=False)
"""

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
81 changes: 81 additions & 0 deletions pyrogram/types/user_and_chats/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -861,3 +861,84 @@ async def add_members(
user_ids=user_ids,
forward_limit=forward_limit
)

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*):
When notifications shall be switched off. Unix time.
Default to forever.

Returns:
``bool``: True on success, False otherwise.
"""

return await self._client.update_chat_notifications(
self.chat.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*):
When notifications shall be switched off. Unix time.
Default to forever.

Returns:
``bool``: True on success, False otherwise.
"""

return await self._client.update_chat_notifications(
self.chat.id,
show_previews=show_previews or None,
silent=True,
mute_until=mute_until or None
)