Handle edited messages using a separate handler · AnsCodex/pyrogram@ecc90ca · GitHub
Skip to content

Commit ecc90ca

Browse files
committed
Handle edited messages using a separate handler
1 parent 0e3c2e4 commit ecc90ca

10 files changed

Lines changed: 184 additions & 59 deletions

File tree

docs/source/api/decorators.rst

Lines changed: 2 additions & 0 deletions

docs/source/api/handlers.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Index
3636
:columns: 3
3737

3838
- :class:`MessageHandler`
39+
- :class:`EditedMessageHandler`
3940
- :class:`DeletedMessagesHandler`
4041
- :class:`CallbackQueryHandler`
4142
- :class:`InlineQueryHandler`
@@ -53,6 +54,7 @@ Details
5354

5455
.. Handlers
5556
.. autoclass:: MessageHandler()
57+
.. autoclass:: EditedMessageHandler()
5658
.. autoclass:: DeletedMessagesHandler()
5759
.. autoclass:: CallbackQueryHandler()
5860
.. autoclass:: InlineQueryHandler()

pyrogram/dispatcher.py

Lines changed: 64 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import pyrogram
2525
from pyrogram import utils
2626
from pyrogram.handlers import (
27-
CallbackQueryHandler, MessageHandler, DeletedMessagesHandler,
27+
CallbackQueryHandler, MessageHandler, EditedMessageHandler, DeletedMessagesHandler,
2828
UserStatusHandler, RawUpdateHandler, InlineQueryHandler, PollHandler,
2929
ChosenInlineResultHandler, ChatMemberUpdatedHandler, ChatJoinRequestHandler
3030
)
@@ -42,33 +42,16 @@
4242

4343

4444
class Dispatcher:
45-
NEW_MESSAGE_UPDATES = (
46-
UpdateNewMessage,
47-
UpdateNewChannelMessage,
48-
UpdateNewScheduledMessage
49-
)
50-
51-
EDIT_MESSAGE_UPDATES = (
52-
UpdateEditMessage,
53-
UpdateEditChannelMessage,
54-
)
55-
56-
DELETE_MESSAGES_UPDATES = (
57-
UpdateDeleteMessages,
58-
UpdateDeleteChannelMessages
59-
)
60-
61-
CALLBACK_QUERY_UPDATES = (
62-
UpdateBotCallbackQuery,
63-
UpdateInlineBotCallbackQuery
64-
)
65-
66-
CHAT_MEMBER_UPDATES = (
67-
UpdateChatParticipant,
68-
UpdateChannelParticipant
69-
)
70-
71-
MESSAGE_UPDATES = NEW_MESSAGE_UPDATES + EDIT_MESSAGE_UPDATES
45+
NEW_MESSAGE_UPDATES = (UpdateNewMessage, UpdateNewChannelMessage, UpdateNewScheduledMessage)
46+
EDIT_MESSAGE_UPDATES = (UpdateEditMessage, UpdateEditChannelMessage)
47+
DELETE_MESSAGES_UPDATES = (UpdateDeleteMessages, UpdateDeleteChannelMessages)
48+
CALLBACK_QUERY_UPDATES = (UpdateBotCallbackQuery, UpdateInlineBotCallbackQuery)
49+
CHAT_MEMBER_UPDATES = (UpdateChatParticipant, UpdateChannelParticipant)
50+
USER_STATUS_UPDATES = (UpdateUserStatus,)
51+
BOT_INLINE_QUERY_UPDATES = (UpdateBotInlineQuery,)
52+
POLL_UPDATES = (UpdateMessagePoll,)
53+
CHOSEN_INLINE_RESULT_UPDATES = (UpdateBotInlineSend,)
54+
CHAT_JOIN_REQUEST_UPDATES = (UpdateBotChatInviteRequester,)
7255

7356
def __init__(self, client: "pyrogram.Client"):
7457
self.client = client
@@ -81,45 +64,80 @@ def __init__(self, client: "pyrogram.Client"):
8164
self.groups = OrderedDict()
8265

8366
async def message_parser(update, users, chats):
84-
return await pyrogram.types.Message._parse(
85-
self.client, update.message, users, chats,
86-
isinstance(update, UpdateNewScheduledMessage)
87-
), MessageHandler
67+
return (
68+
await pyrogram.types.Message._parse(self.client, update.message, users, chats,
69+
isinstance(update, UpdateNewScheduledMessage)),
70+
MessageHandler
71+
)
72+
73+
async def edited_message_parser(update, users, chats):
74+
# Edited messages are parsed the same way as new messages, but the handler is different
75+
parsed, _ = await message_parser(update, users, chats)
76+
77+
return (
78+
parsed,
79+
EditedMessageHandler
80+
)
8881

8982
async def deleted_messages_parser(update, users, chats):
90-
return utils.parse_deleted_messages(self.client, update), DeletedMessagesHandler
83+
return (
84+
utils.parse_deleted_messages(self.client, update),
85+
DeletedMessagesHandler
86+
)
9187

9288
async def callback_query_parser(update, users, chats):
93-
return await pyrogram.types.CallbackQuery._parse(self.client, update, users), CallbackQueryHandler
89+
return (
90+
await pyrogram.types.CallbackQuery._parse(self.client, update, users),
91+
CallbackQueryHandler
92+
)
9493

9594
async def user_status_parser(update, users, chats):
96-
return pyrogram.types.User._parse_user_status(self.client, update), UserStatusHandler
95+
return (
96+
pyrogram.types.User._parse_user_status(self.client, update),
97+
UserStatusHandler
98+
)
9799

98100
async def inline_query_parser(update, users, chats):
99-
return pyrogram.types.InlineQuery._parse(self.client, update, users), InlineQueryHandler
101+
return (
102+
pyrogram.types.InlineQuery._parse(self.client, update, users),
103+
InlineQueryHandler
104+
)
100105

101106
async def poll_parser(update, users, chats):
102-
return pyrogram.types.Poll._parse_update(self.client, update), PollHandler
107+
return (
108+
pyrogram.types.Poll._parse_update(self.client, update),
109+
PollHandler
110+
)
103111

104112
async def chosen_inline_result_parser(update, users, chats):
105-
return pyrogram.types.ChosenInlineResult._parse(self.client, update, users), ChosenInlineResultHandler
113+
return (
114+
pyrogram.types.ChosenInlineResult._parse(self.client, update, users),
115+
ChosenInlineResultHandler
116+
)
106117

107118
async def chat_member_updated_parser(update, users, chats):
108-
return pyrogram.types.ChatMemberUpdated._parse(self.client, update, users, chats), ChatMemberUpdatedHandler
119+
return (
120+
pyrogram.types.ChatMemberUpdated._parse(self.client, update, users, chats),
121+
ChatMemberUpdatedHandler
122+
)
109123

110124
async def chat_join_request_parser(update, users, chats):
111-
return pyrogram.types.ChatJoinRequest._parse(self.client, update, users, chats), ChatJoinRequestHandler
125+
return (
126+
pyrogram.types.ChatJoinRequest._parse(self.client, update, users, chats),
127+
ChatJoinRequestHandler
128+
)
112129

113130
self.update_parsers = {
114-
Dispatcher.MESSAGE_UPDATES: message_parser,
131+
Dispatcher.NEW_MESSAGE_UPDATES: message_parser,
132+
Dispatcher.EDIT_MESSAGE_UPDATES: edited_message_parser,
115133
Dispatcher.DELETE_MESSAGES_UPDATES: deleted_messages_parser,
116134
Dispatcher.CALLBACK_QUERY_UPDATES: callback_query_parser,
117-
(UpdateUserStatus,): user_status_parser,
118-
(UpdateBotInlineQuery,): inline_query_parser,
119-
(UpdateMessagePoll,): poll_parser,
120-
(UpdateBotInlineSend,): chosen_inline_result_parser,
135+
Dispatcher.USER_STATUS_UPDATES: user_status_parser,
136+
Dispatcher.BOT_INLINE_QUERY_UPDATES: inline_query_parser,
137+
Dispatcher.POLL_UPDATES: poll_parser,
138+
Dispatcher.CHOSEN_INLINE_RESULT_UPDATES: chosen_inline_result_parser,
121139
Dispatcher.CHAT_MEMBER_UPDATES: chat_member_updated_parser,
122-
(UpdateBotChatInviteRequester,): chat_join_request_parser
140+
Dispatcher.CHAT_JOIN_REQUEST_UPDATES: chat_join_request_parser
123141
}
124142

125143
self.update_parsers = {key: value for key_tuple, value in self.update_parsers.items() for key in key_tuple}

pyrogram/filters.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -250,16 +250,6 @@ async def caption_filter(_, __, m: Message):
250250

251251
# endregion
252252

253-
# region edited_filter
254-
async def edited_filter(_, __, m: Message):
255-
return bool(m.edit_date)
256-
257-
258-
edited = create(edited_filter)
259-
"""Filter edited messages."""
260-
261-
262-
# endregion
263253

264254
# region audio_filter
265255
async def audio_filter(_, __, m: Message):

pyrogram/handlers/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from .chosen_inline_result_handler import ChosenInlineResultHandler
2323
from .deleted_messages_handler import DeletedMessagesHandler
2424
from .disconnect_handler import DisconnectHandler
25+
from .edited_message_handler import EditedMessageHandler
2526
from .inline_query_handler import InlineQueryHandler
2627
from .message_handler import MessageHandler
2728
from .poll_handler import PollHandler
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from typing import Callable
20+
21+
from .handler import Handler
22+
23+
24+
class EditedMessageHandler(Handler):
25+
"""The EditedMessage handler class. Used to handle edited messages.
26+
It is intended to be used with :meth:`~pyrogram.Client.add_handler`
27+
28+
For a nicer way to register this handler, have a look at the
29+
:meth:`~pyrogram.Client.on_edited_message` decorator.
30+
31+
Parameters:
32+
callback (``Callable``):
33+
Pass a function that will be called when a new edited message arrives. It takes *(client, message)*
34+
as positional arguments (look at the section below for a detailed description).
35+
36+
filters (:obj:`Filters`):
37+
Pass one or more filters to allow only a subset of messages to be passed
38+
in your callback function.
39+
40+
Other parameters:
41+
client (:obj:`~pyrogram.Client`):
42+
The Client itself, useful when you want to call other API methods inside the message handler.
43+
44+
edited_message (:obj:`~pyrogram.types.Message`):
45+
The received edited message.
46+
"""
47+
48+
def __init__(self, callback: Callable, filters=None):
49+
super().__init__(callback, filters)

pyrogram/handlers/message_handler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222

2323

2424
class MessageHandler(Handler):
25-
"""The Message handler class. Used to handle text, media and service messages coming from
26-
any chat (private, group, channel). It is intended to be used with :meth:`~pyrogram.Client.add_handler`
25+
"""The Message handler class. Used to handle new messages.
26+
It is intended to be used with :meth:`~pyrogram.Client.add_handler`
2727
2828
For a nicer way to register this handler, have a look at the
2929
:meth:`~pyrogram.Client.on_message` decorator.

pyrogram/methods/decorators/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from .on_chosen_inline_result import OnChosenInlineResult
2323
from .on_deleted_messages import OnDeletedMessages
2424
from .on_disconnect import OnDisconnect
25+
from .on_edited_message import OnEditedMessage
2526
from .on_inline_query import OnInlineQuery
2627
from .on_message import OnMessage
2728
from .on_poll import OnPoll
@@ -31,6 +32,7 @@
3132

3233
class Decorators(
3334
OnMessage,
35+
OnEditedMessage,
3436
OnDeletedMessages,
3537
OnCallbackQuery,
3638
OnRawUpdate,
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from typing import Callable
20+
21+
import pyrogram
22+
from pyrogram.filters import Filter
23+
24+
25+
class OnEditedMessage:
26+
def on_edited_message(
27+
self=None,
28+
filters=None,
29+
group: int = 0
30+
) -> Callable:
31+
"""Decorator for handling edited messages.
32+
33+
This does the same thing as :meth:`~pyrogram.Client.add_handler` using the
34+
:obj:`~pyrogram.handlers.EditedMessageHandler`.
35+
36+
Parameters:
37+
filters (:obj:`~pyrogram.filters`, *optional*):
38+
Pass one or more filters to allow only a subset of messages to be passed
39+
in your function.
40+
41+
group (``int``, *optional*):
42+
The group identifier, defaults to 0.
43+
"""
44+
45+
def decorator(func: Callable) -> Callable:
46+
if isinstance(self, pyrogram.Client):
47+
self.add_handler(pyrogram.handlers.EditedMessageHandler(func, filters), group)
48+
elif isinstance(self, Filter) or self is None:
49+
if not hasattr(func, "handlers"):
50+
func.handlers = []
51+
52+
func.handlers.append(
53+
(
54+
pyrogram.handlers.MessageHandler(func, self),
55+
group if filters is None else filters
56+
)
57+
)
58+
59+
return func
60+
61+
return decorator

pyrogram/methods/decorators/on_message.py

Lines changed: 1 addition & 1 deletion

0 commit comments

Comments
 (0)