From cc91ee9f44355d2966ed8c68f2c4523f659f522b Mon Sep 17 00:00:00 2001 From: wulan17 Date: Sat, 6 Apr 2024 13:50:36 +0700 Subject: [PATCH] Pyrofork: Add edited and deleted bot business message(s) handler Signed-off-by: wulan17 --- docs/source/api/decorators.rst | 4 ++ docs/source/api/handlers.rst | 4 ++ pyrogram/dispatcher.py | 23 ++++++- pyrogram/handlers/__init__.py | 2 + .../deleted_bot_business_messages_handler.py | 62 +++++++++++++++++++ .../edited_bot_business_message_handler.py | 50 +++++++++++++++ pyrogram/methods/decorators/__init__.py | 4 ++ .../on_deleted_bot_business_messages.py | 62 +++++++++++++++++++ .../on_edited_bot_business_message.py | 62 +++++++++++++++++++ pyrogram/utils.py | 3 +- 10 files changed, 274 insertions(+), 2 deletions(-) create mode 100644 pyrogram/handlers/deleted_bot_business_messages_handler.py create mode 100644 pyrogram/handlers/edited_bot_business_message_handler.py create mode 100644 pyrogram/methods/decorators/on_deleted_bot_business_messages.py create mode 100644 pyrogram/methods/decorators/on_edited_bot_business_message.py diff --git a/docs/source/api/decorators.rst b/docs/source/api/decorators.rst index c518891d..c145c3d1 100644 --- a/docs/source/api/decorators.rst +++ b/docs/source/api/decorators.rst @@ -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` @@ -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` @@ -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() @@ -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() diff --git a/docs/source/api/handlers.rst b/docs/source/api/handlers.rst index 6b39a3e6..127fd9fe 100644 --- a/docs/source/api/handlers.rst +++ b/docs/source/api/handlers.rst @@ -38,7 +38,9 @@ Index - :class:`MessageHandler` - :class:`BotBusinessMessageHandler` - :class:`EditedMessageHandler` + - :class:`EditedBotBusinessMessageHandler` - :class:`DeletedMessagesHandler` + - :class:`DeletedBotBusinessMessagesHandler` - :class:`CallbackQueryHandler` - :class:`MessageReactionUpdatedHandler` - :class:`MessageReactionCountUpdatedHandler` @@ -60,7 +62,9 @@ Details .. autoclass:: MessageHandler() .. autoclass:: BotBusinessMessageHandler() .. autoclass:: EditedMessageHandler() +.. autoclass:: EditedBotBusinessMessageHandler() .. autoclass:: DeletedMessagesHandler() +.. autoclass:: DeletedBotBusinessMessagesHandler() .. autoclass:: CallbackQueryHandler() .. autoclass:: MessageReactionUpdatedHandler() .. autoclass:: MessageReactionCountUpdatedHandler() diff --git a/pyrogram/dispatcher.py b/pyrogram/dispatcher.py index 5e0e4416..1f32147b 100644 --- a/pyrogram/dispatcher.py +++ b/pyrogram/dispatcher.py @@ -29,7 +29,9 @@ from pyrogram.handlers import ( CallbackQueryHandler, MessageHandler, EditedMessageHandler, + EditedBotBusinessMessageHandler, DeletedMessagesHandler, + DeletedBotBusinessMessagesHandler, MessageReactionUpdatedHandler, MessageReactionCountUpdatedHandler, UserStatusHandler, @@ -44,7 +46,7 @@ from pyrogram.handlers import ( ) from pyrogram.raw.types import ( UpdateNewMessage, UpdateNewChannelMessage, UpdateNewScheduledMessage, - UpdateBotNewBusinessMessage, + UpdateBotNewBusinessMessage, UpdateBotDeleteBusinessMessage, UpdateBotEditBusinessMessage, UpdateEditMessage, UpdateEditChannelMessage, UpdateDeleteMessages, UpdateDeleteChannelMessages, UpdateBotCallbackQuery, UpdateInlineBotCallbackQuery, @@ -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,) @@ -115,12 +119,27 @@ class Dispatcher: 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), @@ -185,7 +204,9 @@ class Dispatcher: 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, diff --git a/pyrogram/handlers/__init__.py b/pyrogram/handlers/__init__.py index ae118ad6..a5d26992 100644 --- a/pyrogram/handlers/__init__.py +++ b/pyrogram/handlers/__init__.py @@ -24,8 +24,10 @@ from .chat_member_updated_handler import ChatMemberUpdatedHandler 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 diff --git a/pyrogram/handlers/deleted_bot_business_messages_handler.py b/pyrogram/handlers/deleted_bot_business_messages_handler.py new file mode 100644 index 00000000..0e214e27 --- /dev/null +++ b/pyrogram/handlers/deleted_bot_business_messages_handler.py @@ -0,0 +1,62 @@ +# Pyrofork - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present Dan +# Copyright (C) 2022-present 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 . + +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 diff --git a/pyrogram/handlers/edited_bot_business_message_handler.py b/pyrogram/handlers/edited_bot_business_message_handler.py new file mode 100644 index 00000000..efac301a --- /dev/null +++ b/pyrogram/handlers/edited_bot_business_message_handler.py @@ -0,0 +1,50 @@ +# Pyrofork - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present Dan +# Copyright (C) 2022-present 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 . + +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) diff --git a/pyrogram/methods/decorators/__init__.py b/pyrogram/methods/decorators/__init__.py index b3b1dea5..1cd79b12 100644 --- a/pyrogram/methods/decorators/__init__.py +++ b/pyrogram/methods/decorators/__init__.py @@ -23,8 +23,10 @@ from .on_chat_join_request import OnChatJoinRequest 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 @@ -39,7 +41,9 @@ class Decorators( OnMessage, OnBotBusinessMessage, OnEditedMessage, + OnEditedBotBusinessMessage, OnDeletedMessages, + OnDeletedBotBusinessMessages, OnCallbackQuery, OnRawUpdate, OnDisconnect, diff --git a/pyrogram/methods/decorators/on_deleted_bot_business_messages.py b/pyrogram/methods/decorators/on_deleted_bot_business_messages.py new file mode 100644 index 00000000..c5b7b701 --- /dev/null +++ b/pyrogram/methods/decorators/on_deleted_bot_business_messages.py @@ -0,0 +1,62 @@ +# Pyrofork - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present Dan +# Copyright (C) 2022-present 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 . + +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 diff --git a/pyrogram/methods/decorators/on_edited_bot_business_message.py b/pyrogram/methods/decorators/on_edited_bot_business_message.py new file mode 100644 index 00000000..f80fb12c --- /dev/null +++ b/pyrogram/methods/decorators/on_edited_bot_business_message.py @@ -0,0 +1,62 @@ +# Pyrofork - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present Dan +# Copyright (C) 2022-present 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 . + +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 diff --git a/pyrogram/utils.py b/pyrogram/utils.py index 007e3381..2d6a17f4 100644 --- a/pyrogram/utils.py +++ b/pyrogram/utils.py @@ -207,7 +207,7 @@ async def parse_messages( return types.List(parsed_messages) -def parse_deleted_messages(client, update) -> List["types.Message"]: +def parse_deleted_messages(client, update, bussiness_connection_id: str = None) -> List["types.Message"]: messages = update.messages channel_id = getattr(update, "channel_id", None) @@ -222,6 +222,7 @@ def parse_deleted_messages(client, update) -> List["types.Message"]: type=enums.ChatType.CHANNEL, client=client ) if channel_id is not None else None, + business_connection_id=bussiness_connection_id, client=client ) )