Skip to content

Commit

Permalink
Pyrofork: Add edited and deleted bot business message(s) handler
Browse files Browse the repository at this point in the history
Signed-off-by: wulan17 <wulan17@nusantararom.org>
  • Loading branch information
wulan17 committed Apr 6, 2024
1 parent 67a55fc commit aed7eb7
Show file tree
Hide file tree
Showing 10 changed files with 274 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/source/api/decorators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Index
- :meth:`~Client.on_message`
- :meth:`~Client.on_bot_business_message`
- :meth:`~Client.on_edited_message`
- :meth:`~Client.on_edited_bot_business_message`
- :meth:`~Client.on_callback_query`
- :meth:`~Client.on_message_reaction_updated`
- :meth:`~Client.on_message_reaction_count_updated`
Expand All @@ -46,6 +47,7 @@ Index
- :meth:`~Client.on_chat_member_updated`
- :meth:`~Client.on_chat_join_request`
- :meth:`~Client.on_deleted_messages`
- :meth:`~Client.on_edited_bot_business_message`
- :meth:`~Client.on_user_status`
- :meth:`~Client.on_story`
- :meth:`~Client.on_poll`
Expand All @@ -61,6 +63,7 @@ Details
.. autodecorator:: pyrogram.Client.on_message()
.. autodecorator:: pyrogram.Client.on_bot_business_message()
.. autodecorator:: pyrogram.Client.on_edited_message()
.. autodecorator:: pyrogram.Client.on_edited_bot_business_message()
.. autodecorator:: pyrogram.Client.on_callback_query()
.. autodecorator:: pyrogram.Client.on_message_reaction_updated()
.. autodecorator:: pyrogram.Client.on_message_reaction_count_updated()
Expand All @@ -69,6 +72,7 @@ Details
.. autodecorator:: pyrogram.Client.on_chat_member_updated()
.. autodecorator:: pyrogram.Client.on_chat_join_request()
.. autodecorator:: pyrogram.Client.on_deleted_messages()
.. autodecorator:: pyrogram.Client.on_edited_bot_business_message()
.. autodecorator:: pyrogram.Client.on_user_status()
.. autodecorator:: pyrogram.Client.on_story()
.. autodecorator:: pyrogram.Client.on_poll()
Expand Down
4 changes: 4 additions & 0 deletions docs/source/api/handlers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ Index
- :class:`MessageHandler`
- :class:`BotBusinessMessageHandler`
- :class:`EditedMessageHandler`
- :class:`EditedBotBusinessMessageHandler`
- :class:`DeletedMessagesHandler`
- :class:`DeletedBotBusinessMessagesHandler`
- :class:`CallbackQueryHandler`
- :class:`MessageReactionUpdatedHandler`
- :class:`MessageReactionCountUpdatedHandler`
Expand All @@ -60,7 +62,9 @@ Details
.. autoclass:: MessageHandler()
.. autoclass:: BotBusinessMessageHandler()
.. autoclass:: EditedMessageHandler()
.. autoclass:: EditedBotBusinessMessageHandler()
.. autoclass:: DeletedMessagesHandler()
.. autoclass:: DeletedBotBusinessMessagesHandler()
.. autoclass:: CallbackQueryHandler()
.. autoclass:: MessageReactionUpdatedHandler()
.. autoclass:: MessageReactionCountUpdatedHandler()
Expand Down
23 changes: 22 additions & 1 deletion pyrogram/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
CallbackQueryHandler,
MessageHandler,
EditedMessageHandler,
EditedBotBusinessMessageHandler,
DeletedMessagesHandler,
DeletedBotBusinessMessagesHandler,
MessageReactionUpdatedHandler,
MessageReactionCountUpdatedHandler,
UserStatusHandler,
Expand All @@ -44,7 +46,7 @@
)
from pyrogram.raw.types import (
UpdateNewMessage, UpdateNewChannelMessage, UpdateNewScheduledMessage,
UpdateBotNewBusinessMessage,
UpdateBotNewBusinessMessage, UpdateBotDeleteBusinessMessage, UpdateBotEditBusinessMessage,
UpdateEditMessage, UpdateEditChannelMessage,
UpdateDeleteMessages, UpdateDeleteChannelMessages,
UpdateBotCallbackQuery, UpdateInlineBotCallbackQuery,
Expand All @@ -62,7 +64,9 @@ class Dispatcher:
NEW_MESSAGE_UPDATES = (UpdateNewMessage, UpdateNewChannelMessage, UpdateNewScheduledMessage)
NEW_BOT_BUSINESS_MESSAGE_UPDATES = (UpdateBotNewBusinessMessage,)
EDIT_MESSAGE_UPDATES = (UpdateEditMessage, UpdateEditChannelMessage)
EDIT_BOT_BUSINESS_MESSAGE_UPDATES = (UpdateBotEditBusinessMessage,)
DELETE_MESSAGES_UPDATES = (UpdateDeleteMessages, UpdateDeleteChannelMessages)
DELETE_BOT_BUSINESS_MESSAGES_UPDATES = (UpdateBotDeleteBusinessMessage,)
CALLBACK_QUERY_UPDATES = (UpdateBotCallbackQuery, UpdateInlineBotCallbackQuery)
CHAT_MEMBER_UPDATES = (UpdateChatParticipant, UpdateChannelParticipant)
USER_STATUS_UPDATES = (UpdateUserStatus,)
Expand Down Expand Up @@ -115,12 +119,27 @@ async def edited_message_parser(update, users, chats):
EditedMessageHandler
)

async def edited_bot_business_message_parser(update, users, chats):
# Edited messages are parsed the same way as new messages, but the handler is different
parsed, _ = await bot_business_message_parser(update, users, chats)

return (
parsed,
EditedBotBusinessMessageHandler
)

async def deleted_messages_parser(update, users, chats):
return (
utils.parse_deleted_messages(self.client, update),
DeletedMessagesHandler
)

async def deleted_bot_business_messages_parser(update, users, chats):
return (
utils.parse_deleted_messages(self.client, update, business_connection_id=update.connection_id),
DeletedBotBusinessMessagesHandler
)

async def callback_query_parser(update, users, chats):
return (
await pyrogram.types.CallbackQuery._parse(self.client, update, users),
Expand Down Expand Up @@ -185,7 +204,9 @@ async def message_bot_a_reaction_parser(update, users, chats):
Dispatcher.NEW_MESSAGE_UPDATES: message_parser,
Dispatcher.NEW_BOT_BUSINESS_MESSAGE_UPDATES: bot_business_message_parser,
Dispatcher.EDIT_MESSAGE_UPDATES: edited_message_parser,
Dispatcher.EDIT_BOT_BUSINESS_MESSAGE_UPDATES: edited_bot_business_message_parser,
Dispatcher.DELETE_MESSAGES_UPDATES: deleted_messages_parser,
Dispatcher.DELETE_BOT_BUSINESS_MESSAGES_UPDATES: deleted_bot_business_messages_parser,
Dispatcher.CALLBACK_QUERY_UPDATES: callback_query_parser,
Dispatcher.USER_STATUS_UPDATES: user_status_parser,
Dispatcher.BOT_INLINE_QUERY_UPDATES: inline_query_parser,
Expand Down
2 changes: 2 additions & 0 deletions pyrogram/handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
from .conversation_handler import ConversationHandler
from .chosen_inline_result_handler import ChosenInlineResultHandler
from .deleted_messages_handler import DeletedMessagesHandler
from .deleted_bot_business_messages_handler import DeletedBotBusinessMessagesHandler
from .disconnect_handler import DisconnectHandler
from .edited_message_handler import EditedMessageHandler
from .edited_bot_business_message_handler import EditedBotBusinessMessageHandler
from .inline_query_handler import InlineQueryHandler
from .message_handler import MessageHandler
from .poll_handler import PollHandler
Expand Down
62 changes: 62 additions & 0 deletions pyrogram/handlers/deleted_bot_business_messages_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Pyrofork - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
# Copyright (C) 2022-present Mayuri-Chan <https://github.com/Mayuri-Chan>
#
# This file is part of Pyrofork.
#
# Pyrofork 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.
#
# Pyrofork 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 Pyrofork. If not, see <http://www.gnu.org/licenses/>.

from typing import List, Callable

import pyrogram
from pyrogram.filters import Filter
from pyrogram.types import Message
from .handler import Handler


class DeletedBotBusinessMessagesHandler(Handler):
"""The deleted bot business messages handler class. Used to handle deleted messages coming from any chat
(private, group, channel). It is intended to be used with :meth:`~pyrogram.Client.add_handler`
For a nicer way to register this handler, have a look at the
:meth:`~pyrogram.Client.on_deleted_bot_business_messages` decorator.
Parameters:
callback (``Callable``):
Pass a function that will be called when one or more messages have been deleted.
It takes *(client, messages)* as positional arguments (look at the section below for a detailed description).
filters (:obj:`Filters`):
Pass one or more filters to allow only a subset of messages to be passed
in your callback function.
Other parameters:
client (:obj:`~pyrogram.Client`):
The Client itself, useful when you want to call other API methods inside the message handler.
messages (List of :obj:`~pyrogram.types.Message`):
The deleted messages, as list.
"""

def __init__(self, callback: Callable, filters: Filter = None):
super().__init__(callback, filters)

async def check(self, client: "pyrogram.Client", messages: List[Message]):
# Every message should be checked, if at least one matches the filter True is returned
# otherwise, or if the list is empty, False is returned
for message in messages:
if await super().check(client, message):
return True
else:
return False
50 changes: 50 additions & 0 deletions pyrogram/handlers/edited_bot_business_message_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Pyrofork - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
# Copyright (C) 2022-present Mayuri-Chan <https://github.com/Mayuri-Chan>
#
# This file is part of Pyrofork.
#
# Pyrofork 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.
#
# Pyrofork 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 Pyrofork. If not, see <http://www.gnu.org/licenses/>.

from typing import Callable

from .handler import Handler


class EditedBotBusinessMessageHandler(Handler):
"""The EditedBotBusinessMessageHandler handler class. Used to handle edited bot business messages.
It is intended to be used with :meth:`~pyrogram.Client.add_handler`
For a nicer way to register this handler, have a look at the
:meth:`~pyrogram.Client.on_edited_bot_business_message` decorator.
Parameters:
callback (``Callable``):
Pass a function that will be called when a new edited message arrives. It takes *(client, message)*
as positional arguments (look at the section below for a detailed description).
filters (:obj:`Filters`):
Pass one or more filters to allow only a subset of messages to be passed
in your callback function.
Other parameters:
client (:obj:`~pyrogram.Client`):
The Client itself, useful when you want to call other API methods inside the message handler.
edited_message (:obj:`~pyrogram.types.Message`):
The received edited message.
"""

def __init__(self, callback: Callable, filters=None):
super().__init__(callback, filters)
4 changes: 4 additions & 0 deletions pyrogram/methods/decorators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
from .on_chat_member_updated import OnChatMemberUpdated
from .on_chosen_inline_result import OnChosenInlineResult
from .on_deleted_messages import OnDeletedMessages
from .on_deleted_bot_business_messages import OnDeletedBotBusinessMessages
from .on_disconnect import OnDisconnect
from .on_edited_message import OnEditedMessage
from .on_edited_bot_business_message import OnEditedBotBusinessMessage
from .on_inline_query import OnInlineQuery
from .on_message import OnMessage
from .on_poll import OnPoll
Expand All @@ -39,7 +41,9 @@ class Decorators(
OnMessage,
OnBotBusinessMessage,
OnEditedMessage,
OnEditedBotBusinessMessage,
OnDeletedMessages,
OnDeletedBotBusinessMessages,
OnCallbackQuery,
OnRawUpdate,
OnDisconnect,
Expand Down
62 changes: 62 additions & 0 deletions pyrogram/methods/decorators/on_deleted_bot_business_messages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Pyrofork - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
# Copyright (C) 2022-present Mayuri-Chan <https://github.com/Mayuri-Chan>
#
# This file is part of Pyrofork.
#
# Pyrofork 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.
#
# Pyrofork 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 Pyrofork. If not, see <http://www.gnu.org/licenses/>.

from typing import Callable

import pyrogram
from pyrogram.filters import Filter


class OnDeletedBotBusinessMessages:
def on_deleted_bot_business_messages(
self=None,
filters=None,
group: int = 0
) -> Callable:
"""Decorator for handling deleted bot business messages.
This does the same thing as :meth:`~pyrogram.Client.add_handler` using the
:obj:`~pyrogram.handlers.DeletedBotBusinessMessagesHandler`.
Parameters:
filters (:obj:`~pyrogram.filters`, *optional*):
Pass one or more filters to allow only a subset of messages to be passed
in your function.
group (``int``, *optional*):
The group identifier, defaults to 0.
"""

def decorator(func: Callable) -> Callable:
if isinstance(self, pyrogram.Client):
self.add_handler(pyrogram.handlers.DeletedBotBusinessMessagesHandler(func, filters), group)
elif isinstance(self, Filter) or self is None:
if not hasattr(func, "handlers"):
func.handlers = []

func.handlers.append(
(
pyrogram.handlers.DeletedBotBusinessMessagesHandler(func, self),
group if filters is None else filters
)
)

return func

return decorator
62 changes: 62 additions & 0 deletions pyrogram/methods/decorators/on_edited_bot_business_message.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Pyrofork - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
# Copyright (C) 2022-present Mayuri-Chan <https://github.com/Mayuri-Chan>
#
# This file is part of Pyrofork.
#
# Pyrofork 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.
#
# Pyrofork 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 Pyrofork. If not, see <http://www.gnu.org/licenses/>.

from typing import Callable

import pyrogram
from pyrogram.filters import Filter


class OnEditedBotBusinessMessage:
def on_edited_bot_business_message(
self=None,
filters=None,
group: int = 0
) -> Callable:
"""Decorator for handling edited messages.
This does the same thing as :meth:`~pyrogram.Client.add_handler` using the
:obj:`~pyrogram.handlers.EditedBotBusinessMessageHandler`.
Parameters:
filters (:obj:`~pyrogram.filters`, *optional*):
Pass one or more filters to allow only a subset of messages to be passed
in your function.
group (``int``, *optional*):
The group identifier, defaults to 0.
"""

def decorator(func: Callable) -> Callable:
if isinstance(self, pyrogram.Client):
self.add_handler(pyrogram.handlers.EditedBotBusinessMessageHandler(func, filters), group)
elif isinstance(self, Filter) or self is None:
if not hasattr(func, "handlers"):
func.handlers = []

func.handlers.append(
(
pyrogram.handlers.EditedBotBusinessMessageHandler(func, self),
group if filters is None else filters
)
)

return func

return decorator
Loading

0 comments on commit aed7eb7

Please sign in to comment.