Telethon/telethon/_events/messagedeleted.py

58 lines
2.1 KiB
Python
Raw Normal View History

2018-04-05 21:14:22 +03:00
from .common import EventBuilder, EventCommon, name_inner_event
from .. import _tl
2018-04-05 21:14:22 +03:00
@name_inner_event
class MessageDeleted(EventBuilder):
"""
Occurs whenever a message is deleted. Note that this event isn't 100%
reliable, since Telegram doesn't always notify the clients that a message
was deleted.
.. important::
Telegram **does not** send information about *where* a message
was deleted if it occurs in private conversations with other users
or in small group chats, because message IDs are *unique* and you
can identify the chat with the message ID alone if you saved it
previously.
Telethon **does not** save information of where messages occur,
so it cannot know in which chat a message was deleted (this will
only work in channels, where the channel ID *is* present).
This means that the ``chats=`` parameter will not work reliably,
unless you intend on working with channels and super-groups only.
2020-02-20 12:18:26 +03:00
Example
.. code-block:: python
from telethon import events
@client.on(events.MessageDeleted)
async def handler(event):
# Log all deleted message IDs
for msg_id in event.deleted_ids:
print('Message', msg_id, 'was deleted in', event.chat_id)
2018-04-05 21:14:22 +03:00
"""
2018-07-19 02:47:32 +03:00
@classmethod
def build(cls, update, others=None, self_id=None):
if isinstance(update, _tl.UpdateDeleteMessages):
2019-06-30 14:23:18 +03:00
return cls.Event(
2018-04-05 21:14:22 +03:00
deleted_ids=update.messages,
peer=None
)
elif isinstance(update, _tl.UpdateDeleteChannelMessages):
2019-06-30 14:23:18 +03:00
return cls.Event(
2018-04-05 21:14:22 +03:00
deleted_ids=update.messages,
peer=_tl.PeerChannel(update.channel_id)
2018-04-05 21:14:22 +03:00
)
class Event(EventCommon):
def __init__(self, deleted_ids, peer):
super().__init__(
chat_peer=peer, msg_id=(deleted_ids or [0])[0]
)
self.deleted_id = None if not deleted_ids else deleted_ids[0]
self.deleted_ids = deleted_ids