From 448a04a7c5be07d28cfe63c8a16f2eab5762c7f0 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Thu, 22 Feb 2018 21:01:18 +0100 Subject: [PATCH] Stop using InputPeerSelf() on events and special case edit() Used to fail on the chat with your own (where messages are "incoming" instead outgoing). Now the ID of the chat and sender are compared to achieve the same effect. Fixes #632. --- telethon/events/__init__.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/telethon/events/__init__.py b/telethon/events/__init__.py index 99dd5bda..1d7beab2 100644 --- a/telethon/events/__init__.py +++ b/telethon/events/__init__.py @@ -21,8 +21,7 @@ def _into_id_set(client, chats): for chat in chats: chat = client.get_input_entity(chat) if isinstance(chat, types.InputPeerSelf): - chat = getattr(_into_id_set, 'me', None) or client.get_me() - _into_id_set.me = chat + chat = client.get_me(input_peer=True) result.add(utils.get_peer_id(chat)) return result @@ -43,6 +42,7 @@ class _EventBuilder(abc.ABC): def __init__(self, chats=None, blacklist_chats=False): self.chats = chats self.blacklist_chats = blacklist_chats + self._self_id = None @abc.abstractmethod def build(self, update): @@ -51,6 +51,7 @@ class _EventBuilder(abc.ABC): def resolve(self, client): """Helper method to allow event builders to be resolved before usage""" self.chats = _into_id_set(client, self.chats) + self._self_id = client.get_me(input_peer=True).user_id def _filter_event(self, event): """ @@ -179,11 +180,6 @@ class NewMessage(_EventBuilder): You can specify a regex-like string which will be matched against the message, a callable function that returns ``True`` if a message is acceptable, or a compiled regex pattern. - - Notes: - The ``message.from_id`` might not only be an integer or ``None``, - but also ``InputPeerSelf()`` for short private messages (the API - would not return such thing, this is a custom modification). """ def __init__(self, incoming=None, outgoing=None, chats=None, blacklist_chats=False, pattern=None): @@ -216,7 +212,7 @@ class NewMessage(_EventBuilder): silent=update.silent, id=update.id, to_id=types.PeerUser(update.user_id), - from_id=types.InputPeerSelf() if update.out else update.user_id, + from_id=self._self_id if update.out else update.user_id, message=update.message, date=update.date, fwd_from=update.fwd_from, @@ -317,7 +313,11 @@ class NewMessage(_EventBuilder): or the edited message otherwise. """ if not self.message.out: - return None + if not isinstance(self.message.to_id, types.PeerUser): + return None + me = self._client.get_me(input_peer=True) + if self.message.to_id.user_id != me.user_id: + return None return self._client.edit_message(self.input_chat, self.message,