mirror of
https://github.com/pyrogram/pyrogram
synced 2025-08-29 13:27:47 +00:00
Add Handler to deleted messages update
This commit is contained in:
parent
b4d69d0b21
commit
e6c6bd839c
@ -38,6 +38,6 @@ from .client.types.reply_markup import (
|
|||||||
)
|
)
|
||||||
from .client import (
|
from .client import (
|
||||||
Client, ChatAction, ParseMode, Emoji,
|
Client, ChatAction, ParseMode, Emoji,
|
||||||
MessageHandler, CallbackQueryHandler, RawUpdateHandler,
|
MessageHandler, DeletedMessagesHandler, CallbackQueryHandler,
|
||||||
DisconnectHandler, Filters
|
RawUpdateHandler, DisconnectHandler, Filters
|
||||||
)
|
)
|
||||||
|
@ -20,6 +20,7 @@ from .client import Client
|
|||||||
from .ext import BaseClient, ChatAction, Emoji, ParseMode
|
from .ext import BaseClient, ChatAction, Emoji, ParseMode
|
||||||
from .filters import Filters
|
from .filters import Filters
|
||||||
from .handlers import (
|
from .handlers import (
|
||||||
MessageHandler, CallbackQueryHandler,
|
MessageHandler, DeletedMessagesHandler,
|
||||||
RawUpdateHandler, DisconnectHandler
|
CallbackQueryHandler, RawUpdateHandler,
|
||||||
|
DisconnectHandler
|
||||||
)
|
)
|
||||||
|
@ -25,7 +25,7 @@ from threading import Thread
|
|||||||
import pyrogram
|
import pyrogram
|
||||||
from pyrogram.api import types
|
from pyrogram.api import types
|
||||||
from ..ext import utils
|
from ..ext import utils
|
||||||
from ..handlers import RawUpdateHandler, CallbackQueryHandler, MessageHandler
|
from ..handlers import RawUpdateHandler, CallbackQueryHandler, MessageHandler, DeletedMessagesHandler
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -41,6 +41,11 @@ class Dispatcher:
|
|||||||
types.UpdateEditChannelMessage
|
types.UpdateEditChannelMessage
|
||||||
)
|
)
|
||||||
|
|
||||||
|
DELETE_MESSAGE_UPDATES = (
|
||||||
|
types.UpdateDeleteMessages,
|
||||||
|
types.UpdateDeleteChannelMessages
|
||||||
|
)
|
||||||
|
|
||||||
MESSAGE_UPDATES = NEW_MESSAGE_UPDATES + EDIT_MESSAGE_UPDATES
|
MESSAGE_UPDATES = NEW_MESSAGE_UPDATES + EDIT_MESSAGE_UPDATES
|
||||||
|
|
||||||
def __init__(self, client, workers):
|
def __init__(self, client, workers):
|
||||||
@ -97,6 +102,9 @@ class Dispatcher:
|
|||||||
or update.edited_message
|
or update.edited_message
|
||||||
or update.edited_channel_post)
|
or update.edited_channel_post)
|
||||||
|
|
||||||
|
deleted_messages = (update.deleted_channel_posts
|
||||||
|
or update.deleted_messages)
|
||||||
|
|
||||||
callback_query = update.callback_query
|
callback_query = update.callback_query
|
||||||
|
|
||||||
if message and isinstance(handler, MessageHandler):
|
if message and isinstance(handler, MessageHandler):
|
||||||
@ -104,6 +112,11 @@ class Dispatcher:
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
args = (self.client, message)
|
args = (self.client, message)
|
||||||
|
elif deleted_messages and isinstance(handler, DeletedMessagesHandler):
|
||||||
|
if not handler.check(deleted_messages):
|
||||||
|
continue
|
||||||
|
|
||||||
|
args = (self.client, deleted_messages)
|
||||||
elif callback_query and isinstance(handler, CallbackQueryHandler):
|
elif callback_query and isinstance(handler, CallbackQueryHandler):
|
||||||
if not handler.check(callback_query):
|
if not handler.check(callback_query):
|
||||||
continue
|
continue
|
||||||
@ -161,6 +174,22 @@ class Dispatcher:
|
|||||||
else None)
|
else None)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
elif isinstance(update, Dispatcher.DELETE_MESSAGE_UPDATES):
|
||||||
|
is_channel = hasattr(update, 'channel_id')
|
||||||
|
|
||||||
|
messages = utils.parse_deleted_messages(
|
||||||
|
update.messages,
|
||||||
|
(update.channel_id if is_channel else None)
|
||||||
|
)
|
||||||
|
|
||||||
|
self.dispatch(
|
||||||
|
pyrogram.Update(
|
||||||
|
deleted_messages=(messages if not is_channel else None),
|
||||||
|
deleted_channel_posts=(messages if is_channel else None)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
elif isinstance(update, types.UpdateBotCallbackQuery):
|
elif isinstance(update, types.UpdateBotCallbackQuery):
|
||||||
self.dispatch(
|
self.dispatch(
|
||||||
pyrogram.Update(
|
pyrogram.Update(
|
||||||
|
@ -711,6 +711,22 @@ def parse_messages(
|
|||||||
return parsed_messages if is_list else parsed_messages[0]
|
return parsed_messages if is_list else parsed_messages[0]
|
||||||
|
|
||||||
|
|
||||||
|
def parse_deleted_messages(
|
||||||
|
messages: list,
|
||||||
|
channel_id: int
|
||||||
|
) -> pyrogram_types.Messages:
|
||||||
|
parsed_messages = []
|
||||||
|
|
||||||
|
for message in messages:
|
||||||
|
m = pyrogram_types.Message(
|
||||||
|
message_id=message,
|
||||||
|
chat=(pyrogram_types.Chat(id=channel_id, type="channel") if channel_id is not None else None)
|
||||||
|
)
|
||||||
|
parsed_messages.append(m)
|
||||||
|
|
||||||
|
return pyrogram_types.Messages(len(parsed_messages), parsed_messages)
|
||||||
|
|
||||||
|
|
||||||
def get_peer_id(input_peer) -> int:
|
def get_peer_id(input_peer) -> int:
|
||||||
return (
|
return (
|
||||||
input_peer.user_id if isinstance(input_peer, types.InputPeerUser)
|
input_peer.user_id if isinstance(input_peer, types.InputPeerUser)
|
||||||
|
@ -89,13 +89,13 @@ class Filters:
|
|||||||
venue = build("Venue", lambda _, m: bool(m.venue))
|
venue = build("Venue", lambda _, m: bool(m.venue))
|
||||||
"""Filter messages that contain :obj:`Venue <pyrogram.api.types.pyrogram.Venue>` objects."""
|
"""Filter messages that contain :obj:`Venue <pyrogram.api.types.pyrogram.Venue>` objects."""
|
||||||
|
|
||||||
private = build("Private", lambda _, m: bool(m.chat.type == "private"))
|
private = build("Private", lambda _, m: bool(m.chat and m.chat.type == "private"))
|
||||||
"""Filter messages sent in private chats."""
|
"""Filter messages sent in private chats."""
|
||||||
|
|
||||||
group = build("Group", lambda _, m: bool(m.chat.type in {"group", "supergroup"}))
|
group = build("Group", lambda _, m: bool(m.chat and m.chat.type in {"group", "supergroup"}))
|
||||||
"""Filter messages sent in group or supergroup chats."""
|
"""Filter messages sent in group or supergroup chats."""
|
||||||
|
|
||||||
channel = build("Channel", lambda _, m: bool(m.chat.type == "channel"))
|
channel = build("Channel", lambda _, m: bool(m.chat and m.chat.type == "channel"))
|
||||||
"""Filter messages sent in channels."""
|
"""Filter messages sent in channels."""
|
||||||
|
|
||||||
new_chat_members = build("NewChatMembers", lambda _, m: bool(m.new_chat_members))
|
new_chat_members = build("NewChatMembers", lambda _, m: bool(m.new_chat_members))
|
||||||
|
@ -19,4 +19,5 @@
|
|||||||
from .callback_query_handler import CallbackQueryHandler
|
from .callback_query_handler import CallbackQueryHandler
|
||||||
from .disconnect_handler import DisconnectHandler
|
from .disconnect_handler import DisconnectHandler
|
||||||
from .message_handler import MessageHandler
|
from .message_handler import MessageHandler
|
||||||
|
from .deleted_messages_handler import DeletedMessagesHandler
|
||||||
from .raw_update_handler import RawUpdateHandler
|
from .raw_update_handler import RawUpdateHandler
|
||||||
|
52
pyrogram/client/handlers/deleted_messages_handler.py
Normal file
52
pyrogram/client/handlers/deleted_messages_handler.py
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
# Pyrogram - Telegram MTProto API Client Library for Python
|
||||||
|
# Copyright (C) 2017-2018 Dan Tès <https://github.com/delivrance>
|
||||||
|
#
|
||||||
|
# This file is part of Pyrogram.
|
||||||
|
#
|
||||||
|
# Pyrogram 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.
|
||||||
|
#
|
||||||
|
# Pyrogram 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 Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
from .handler import Handler
|
||||||
|
|
||||||
|
|
||||||
|
class DeletedMessagesHandler(Handler):
|
||||||
|
"""The Deleted Message handler class. Used to handle deleted messages coming from any chat
|
||||||
|
(private, group, channel). It is intended to be used with
|
||||||
|
:meth:`add_handler() <pyrogram.Client.add_handler>`
|
||||||
|
|
||||||
|
Args:
|
||||||
|
callback (``callable``):
|
||||||
|
Pass a function that will be called when a new Message arrives. It takes *(client, message)*
|
||||||
|
as positional arguments (look at the section below for a detailed description).
|
||||||
|
|
||||||
|
filters (:obj:`Filters <pyrogram.Filters>`):
|
||||||
|
Pass one or more filters to allow only a subset of messages to be passed
|
||||||
|
in your callback function.
|
||||||
|
|
||||||
|
Other parameters:
|
||||||
|
client (:obj:`Client <pyrogram.Client>`):
|
||||||
|
The Client itself, useful when you want to call other API methods inside the message handler.
|
||||||
|
|
||||||
|
message (:obj:`Message <pyrogram.Message>`):
|
||||||
|
The received message.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, callback: callable, filters=None):
|
||||||
|
super().__init__(callback, filters)
|
||||||
|
|
||||||
|
def check(self, messages):
|
||||||
|
return (
|
||||||
|
self.filters(messages.messages[0])
|
||||||
|
if self.filters
|
||||||
|
else True
|
||||||
|
)
|
@ -19,8 +19,9 @@
|
|||||||
from .on_callback_query import OnCallbackQuery
|
from .on_callback_query import OnCallbackQuery
|
||||||
from .on_disconnect import OnDisconnect
|
from .on_disconnect import OnDisconnect
|
||||||
from .on_message import OnMessage
|
from .on_message import OnMessage
|
||||||
|
from .on_deleted_messages import OnDeletedMessages
|
||||||
from .on_raw_update import OnRawUpdate
|
from .on_raw_update import OnRawUpdate
|
||||||
|
|
||||||
|
|
||||||
class Decorators(OnMessage, OnCallbackQuery, OnRawUpdate, OnDisconnect):
|
class Decorators(OnMessage, OnDeletedMessages, OnCallbackQuery, OnRawUpdate, OnDisconnect):
|
||||||
pass
|
pass
|
||||||
|
42
pyrogram/client/methods/decorators/on_deleted_messages.py
Normal file
42
pyrogram/client/methods/decorators/on_deleted_messages.py
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
# Pyrogram - Telegram MTProto API Client Library for Python
|
||||||
|
# Copyright (C) 2017-2018 Dan Tès <https://github.com/delivrance>
|
||||||
|
#
|
||||||
|
# This file is part of Pyrogram.
|
||||||
|
#
|
||||||
|
# Pyrogram 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.
|
||||||
|
#
|
||||||
|
# Pyrogram 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 Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import pyrogram
|
||||||
|
from ...ext import BaseClient
|
||||||
|
|
||||||
|
|
||||||
|
class OnDeletedMessages(BaseClient):
|
||||||
|
def on_deleted_messages(self, filters=None, group: int = 0):
|
||||||
|
"""Use this decorator to automatically register a function for handling
|
||||||
|
deleted messages. This does the same thing as :meth:`add_handler` using the
|
||||||
|
DeletedMessagesHandler.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
filters (:obj:`Filters <pyrogram.Filters>`):
|
||||||
|
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):
|
||||||
|
self.add_handler(pyrogram.DeletedMessagesHandler(func, filters), group)
|
||||||
|
return func
|
||||||
|
|
||||||
|
return decorator
|
@ -30,12 +30,18 @@ class Update(Object):
|
|||||||
edited_message (:obj:`Message <pyrogram.Message>`, *optional*):
|
edited_message (:obj:`Message <pyrogram.Message>`, *optional*):
|
||||||
New version of a message that is known to the bot and was edited.
|
New version of a message that is known to the bot and was edited.
|
||||||
|
|
||||||
|
deleted_messages (:obj:`Message <pyrogram.Message>`, *optional*):
|
||||||
|
Deleted messages.
|
||||||
|
|
||||||
channel_post (:obj:`Message <pyrogram.Message>`, *optional*):
|
channel_post (:obj:`Message <pyrogram.Message>`, *optional*):
|
||||||
New incoming channel post of any kind — text, photo, sticker, etc.
|
New incoming channel post of any kind — text, photo, sticker, etc.
|
||||||
|
|
||||||
edited_channel_post (:obj:`Message <pyrogram.Message>`, *optional*):
|
edited_channel_post (:obj:`Message <pyrogram.Message>`, *optional*):
|
||||||
New version of a channel post that is known to the bot and was edited.
|
New version of a channel post that is known to the bot and was edited.
|
||||||
|
|
||||||
|
deleted_channel_posts (:obj:`Message <pyrogram.Message>`, *optional*):
|
||||||
|
Deleted channel posts.
|
||||||
|
|
||||||
inline_query (:obj:`InlineQuery <pyrogram.InlineQuery>`, *optional*):
|
inline_query (:obj:`InlineQuery <pyrogram.InlineQuery>`, *optional*):
|
||||||
New incoming inline query.
|
New incoming inline query.
|
||||||
|
|
||||||
@ -60,8 +66,10 @@ class Update(Object):
|
|||||||
self,
|
self,
|
||||||
message=None,
|
message=None,
|
||||||
edited_message=None,
|
edited_message=None,
|
||||||
|
deleted_messages=None,
|
||||||
channel_post=None,
|
channel_post=None,
|
||||||
edited_channel_post=None,
|
edited_channel_post=None,
|
||||||
|
deleted_channel_posts=None,
|
||||||
inline_query=None,
|
inline_query=None,
|
||||||
chosen_inline_result=None,
|
chosen_inline_result=None,
|
||||||
callback_query=None,
|
callback_query=None,
|
||||||
@ -70,8 +78,10 @@ class Update(Object):
|
|||||||
):
|
):
|
||||||
self.message = message
|
self.message = message
|
||||||
self.edited_message = edited_message
|
self.edited_message = edited_message
|
||||||
|
self.deleted_messages = deleted_messages
|
||||||
self.channel_post = channel_post
|
self.channel_post = channel_post
|
||||||
self.edited_channel_post = edited_channel_post
|
self.edited_channel_post = edited_channel_post
|
||||||
|
self.deleted_channel_posts = deleted_channel_posts
|
||||||
self.inline_query = inline_query
|
self.inline_query = inline_query
|
||||||
self.chosen_inline_result = chosen_inline_result
|
self.chosen_inline_result = chosen_inline_result
|
||||||
self.callback_query = callback_query
|
self.callback_query = callback_query
|
||||||
|
Loading…
x
Reference in New Issue
Block a user