Skip to content

Commit e6c6bd8

Browse files
vitalibrmateusc-ciandt
authored andcommitted
Add Handler to deleted messages update
1 parent b4d69d0 commit e6c6bd8

File tree

10 files changed

+161
-9
lines changed

10 files changed

+161
-9
lines changed

pyrogram/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@
3838
)
3939
from .client import (
4040
Client, ChatAction, ParseMode, Emoji,
41-
MessageHandler, CallbackQueryHandler, RawUpdateHandler,
42-
DisconnectHandler, Filters
41+
MessageHandler, DeletedMessagesHandler, CallbackQueryHandler,
42+
RawUpdateHandler, DisconnectHandler, Filters
4343
)

pyrogram/client/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from .ext import BaseClient, ChatAction, Emoji, ParseMode
2121
from .filters import Filters
2222
from .handlers import (
23-
MessageHandler, CallbackQueryHandler,
24-
RawUpdateHandler, DisconnectHandler
23+
MessageHandler, DeletedMessagesHandler,
24+
CallbackQueryHandler, RawUpdateHandler,
25+
DisconnectHandler
2526
)

pyrogram/client/dispatcher/dispatcher.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import pyrogram
2626
from pyrogram.api import types
2727
from ..ext import utils
28-
from ..handlers import RawUpdateHandler, CallbackQueryHandler, MessageHandler
28+
from ..handlers import RawUpdateHandler, CallbackQueryHandler, MessageHandler, DeletedMessagesHandler
2929

3030
log = logging.getLogger(__name__)
3131

@@ -41,6 +41,11 @@ class Dispatcher:
4141
types.UpdateEditChannelMessage
4242
)
4343

44+
DELETE_MESSAGE_UPDATES = (
45+
types.UpdateDeleteMessages,
46+
types.UpdateDeleteChannelMessages
47+
)
48+
4449
MESSAGE_UPDATES = NEW_MESSAGE_UPDATES + EDIT_MESSAGE_UPDATES
4550

4651
def __init__(self, client, workers):
@@ -97,13 +102,21 @@ def dispatch(self, update, users: dict = None, chats: dict = None, is_raw: bool
97102
or update.edited_message
98103
or update.edited_channel_post)
99104

105+
deleted_messages = (update.deleted_channel_posts
106+
or update.deleted_messages)
107+
100108
callback_query = update.callback_query
101109

102110
if message and isinstance(handler, MessageHandler):
103111
if not handler.check(message):
104112
continue
105113

106114
args = (self.client, message)
115+
elif deleted_messages and isinstance(handler, DeletedMessagesHandler):
116+
if not handler.check(deleted_messages):
117+
continue
118+
119+
args = (self.client, deleted_messages)
107120
elif callback_query and isinstance(handler, CallbackQueryHandler):
108121
if not handler.check(callback_query):
109122
continue
@@ -161,6 +174,22 @@ def update_worker(self):
161174
else None)
162175
)
163176
)
177+
178+
elif isinstance(update, Dispatcher.DELETE_MESSAGE_UPDATES):
179+
is_channel = hasattr(update, 'channel_id')
180+
181+
messages = utils.parse_deleted_messages(
182+
update.messages,
183+
(update.channel_id if is_channel else None)
184+
)
185+
186+
self.dispatch(
187+
pyrogram.Update(
188+
deleted_messages=(messages if not is_channel else None),
189+
deleted_channel_posts=(messages if is_channel else None)
190+
)
191+
)
192+
164193
elif isinstance(update, types.UpdateBotCallbackQuery):
165194
self.dispatch(
166195
pyrogram.Update(

pyrogram/client/ext/utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,22 @@ def parse_messages(
711711
return parsed_messages if is_list else parsed_messages[0]
712712

713713

714+
def parse_deleted_messages(
715+
messages: list,
716+
channel_id: int
717+
) -> pyrogram_types.Messages:
718+
parsed_messages = []
719+
720+
for message in messages:
721+
m = pyrogram_types.Message(
722+
message_id=message,
723+
chat=(pyrogram_types.Chat(id=channel_id, type="channel") if channel_id is not None else None)
724+
)
725+
parsed_messages.append(m)
726+
727+
return pyrogram_types.Messages(len(parsed_messages), parsed_messages)
728+
729+
714730
def get_peer_id(input_peer) -> int:
715731
return (
716732
input_peer.user_id if isinstance(input_peer, types.InputPeerUser)

pyrogram/client/filters/filters.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ class Filters:
8989
venue = build("Venue", lambda _, m: bool(m.venue))
9090
"""Filter messages that contain :obj:`Venue <pyrogram.api.types.pyrogram.Venue>` objects."""
9191

92-
private = build("Private", lambda _, m: bool(m.chat.type == "private"))
92+
private = build("Private", lambda _, m: bool(m.chat and m.chat.type == "private"))
9393
"""Filter messages sent in private chats."""
9494

95-
group = build("Group", lambda _, m: bool(m.chat.type in {"group", "supergroup"}))
95+
group = build("Group", lambda _, m: bool(m.chat and m.chat.type in {"group", "supergroup"}))
9696
"""Filter messages sent in group or supergroup chats."""
9797

98-
channel = build("Channel", lambda _, m: bool(m.chat.type == "channel"))
98+
channel = build("Channel", lambda _, m: bool(m.chat and m.chat.type == "channel"))
9999
"""Filter messages sent in channels."""
100100

101101
new_chat_members = build("NewChatMembers", lambda _, m: bool(m.new_chat_members))

pyrogram/client/handlers/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@
1919
from .callback_query_handler import CallbackQueryHandler
2020
from .disconnect_handler import DisconnectHandler
2121
from .message_handler import MessageHandler
22+
from .deleted_messages_handler import DeletedMessagesHandler
2223
from .raw_update_handler import RawUpdateHandler
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2018 Dan Tès <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 .handler import Handler
20+
21+
22+
class DeletedMessagesHandler(Handler):
23+
"""The Deleted Message handler class. Used to handle deleted messages coming from any chat
24+
(private, group, channel). It is intended to be used with
25+
:meth:`add_handler() <pyrogram.Client.add_handler>`
26+
27+
Args:
28+
callback (``callable``):
29+
Pass a function that will be called when a new Message arrives. It takes *(client, message)*
30+
as positional arguments (look at the section below for a detailed description).
31+
32+
filters (:obj:`Filters <pyrogram.Filters>`):
33+
Pass one or more filters to allow only a subset of messages to be passed
34+
in your callback function.
35+
36+
Other parameters:
37+
client (:obj:`Client <pyrogram.Client>`):
38+
The Client itself, useful when you want to call other API methods inside the message handler.
39+
40+
message (:obj:`Message <pyrogram.Message>`):
41+
The received message.
42+
"""
43+
44+
def __init__(self, callback: callable, filters=None):
45+
super().__init__(callback, filters)
46+
47+
def check(self, messages):
48+
return (
49+
self.filters(messages.messages[0])
50+
if self.filters
51+
else True
52+
)

pyrogram/client/methods/decorators/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
from .on_callback_query import OnCallbackQuery
2020
from .on_disconnect import OnDisconnect
2121
from .on_message import OnMessage
22+
from .on_deleted_messages import OnDeletedMessages
2223
from .on_raw_update import OnRawUpdate
2324

2425

25-
class Decorators(OnMessage, OnCallbackQuery, OnRawUpdate, OnDisconnect):
26+
class Decorators(OnMessage, OnDeletedMessages, OnCallbackQuery, OnRawUpdate, OnDisconnect):
2627
pass
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2018 Dan Tès <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+
import pyrogram
20+
from ...ext import BaseClient
21+
22+
23+
class OnDeletedMessages(BaseClient):
24+
def on_deleted_messages(self, filters=None, group: int = 0):
25+
"""Use this decorator to automatically register a function for handling
26+
deleted messages. This does the same thing as :meth:`add_handler` using the
27+
DeletedMessagesHandler.
28+
29+
Args:
30+
filters (:obj:`Filters <pyrogram.Filters>`):
31+
Pass one or more filters to allow only a subset of messages to be passed
32+
in your function.
33+
34+
group (``int``, *optional*):
35+
The group identifier, defaults to 0.
36+
"""
37+
38+
def decorator(func):
39+
self.add_handler(pyrogram.DeletedMessagesHandler(func, filters), group)
40+
return func
41+
42+
return decorator

pyrogram/client/types/update.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,18 @@ class Update(Object):
3030
edited_message (:obj:`Message <pyrogram.Message>`, *optional*):
3131
New version of a message that is known to the bot and was edited.
3232
33+
deleted_messages (:obj:`Message <pyrogram.Message>`, *optional*):
34+
Deleted messages.
35+
3336
channel_post (:obj:`Message <pyrogram.Message>`, *optional*):
3437
New incoming channel post of any kind — text, photo, sticker, etc.
3538
3639
edited_channel_post (:obj:`Message <pyrogram.Message>`, *optional*):
3740
New version of a channel post that is known to the bot and was edited.
3841
42+
deleted_channel_posts (:obj:`Message <pyrogram.Message>`, *optional*):
43+
Deleted channel posts.
44+
3945
inline_query (:obj:`InlineQuery <pyrogram.InlineQuery>`, *optional*):
4046
New incoming inline query.
4147
@@ -60,8 +66,10 @@ def __init__(
6066
self,
6167
message=None,
6268
edited_message=None,
69+
deleted_messages=None,
6370
channel_post=None,
6471
edited_channel_post=None,
72+
deleted_channel_posts=None,
6573
inline_query=None,
6674
chosen_inline_result=None,
6775
callback_query=None,
@@ -70,8 +78,10 @@ def __init__(
7078
):
7179
self.message = message
7280
self.edited_message = edited_message
81+
self.deleted_messages = deleted_messages
7382
self.channel_post = channel_post
7483
self.edited_channel_post = edited_channel_post
84+
self.deleted_channel_posts = deleted_channel_posts
7585
self.inline_query = inline_query
7686
self.chosen_inline_result = chosen_inline_result
7787
self.callback_query = callback_query

0 commit comments

Comments
 (0)