mirror of
https://github.com/Mayuri-Chan/pyrofork.git
synced 2025-12-29 12:04:51 +00:00
Pyrofork: Add edited and deleted bot business message(s) handler
Signed-off-by: wulan17 <wulan17@nusantararom.org>
This commit is contained in:
parent
7ddd53f61b
commit
cc91ee9f44
10 changed files with 274 additions and 2 deletions
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
62
pyrogram/handlers/deleted_bot_business_messages_handler.py
Normal file
62
pyrogram/handlers/deleted_bot_business_messages_handler.py
Normal file
|
|
@ -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
pyrogram/handlers/edited_bot_business_message_handler.py
Normal file
50
pyrogram/handlers/edited_bot_business_message_handler.py
Normal file
|
|
@ -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)
|
||||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
)
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in a new issue