Compare commits

...

5 Commits

Author SHA1 Message Date
Dan
f153350ab5 Merge branch 'CyanBook-master' into notifications 2021-03-20 07:48:47 +01:00
CyanBook
6ad4272c5c Remove any reference with timedelta 2021-03-20 07:48:15 +01:00
CyanBook
024f89d3fa Update update_chat_notifications bound methods to only accept unix time 2021-03-20 07:48:15 +01:00
CyanBook
1940c97c6d Update update_chat_notifications to accept unix time only 2021-03-20 07:48:15 +01:00
CyanBook
673a618873 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
2021-03-20 07:48:15 +01:00
3 changed files with 149 additions and 0 deletions

View File

@ -52,6 +52,7 @@ from .unarchive_chats import UnarchiveChats
from .unban_chat_member import UnbanChatMember from .unban_chat_member import UnbanChatMember
from .unpin_all_chat_messages import UnpinAllChatMessages from .unpin_all_chat_messages import UnpinAllChatMessages
from .unpin_chat_message import UnpinChatMessage from .unpin_chat_message import UnpinChatMessage
from .update_chat_notifications import UpdateChatNotifications
from .update_chat_username import UpdateChatUsername from .update_chat_username import UpdateChatUsername
@ -75,6 +76,7 @@ class Chats(
GetChatMembersCount, GetChatMembersCount,
IterDialogs, IterDialogs,
IterChatMembers, IterChatMembers,
UpdateChatNotifications,
UpdateChatUsername, UpdateChatUsername,
SetChatPermissions, SetChatPermissions,
GetDialogsCount, GetDialogsCount,

View File

@ -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

View File

@ -967,3 +967,83 @@ class Chat(Object):
""" """
return await self._client.mark_chat_unread(self.id) 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*):
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.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.id,
show_previews=show_previews or None,
silent=True,
mute_until=mute_until or None
)