Add update_chat_notifications
update_chat_notifications is now a high-level method - it has bound methods for chat object - it supports unix time, datetime and timedelta
This commit is contained in:
parent
b8cd08adb0
commit
673a618873
@ -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,
|
||||
|
75
pyrogram/methods/chats/update_chat_notifications.py
Normal file
75
pyrogram/methods/chats/update_chat_notifications.py
Normal file
@ -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
|
@ -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
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user