diff --git a/telethon/events/newmessage.py b/telethon/events/newmessage.py index 1b21224f..50a450ec 100644 --- a/telethon/events/newmessage.py +++ b/telethon/events/newmessage.py @@ -211,8 +211,24 @@ class NewMessage(EventBuilder): m = self.message m._chat, m._input_chat = self._get_entity_pair(m.chat_id) m._sender, m._input_sender = self._get_entity_pair(m.sender_id) - return m._input_chat is not None and ( - not m.sender_id or m._input_sender is not None) + m._via_bot, m._via_input_bot = self._get_entity_pair(m.via_bot_id) + if not m.forward: + forward_ok = True + else: + f = m.forward + f._chat, f._input_chat = self._get_entity_pair(f.chat_id) + f._sender, f._input_sender = self._get_entity_pair(f.sender_id) + forward_ok = ( + (not f.chat_id or f._input_chat is not None) + and (not f.sender_id or f._input_sender is not None) + ) + + return ( + m._input_chat is not None + and (not m.sender_id or m._input_sender is not None) + and (not m.via_bot_id or m._via_input_bot is not None) + and forward_ok + ) def __getattr__(self, item): if item in self.__dict__: diff --git a/telethon/tl/custom/message.py b/telethon/tl/custom/message.py index 9fcdda70..a6a449f8 100644 --- a/telethon/tl/custom/message.py +++ b/telethon/tl/custom/message.py @@ -183,6 +183,8 @@ class Message(ChatGetter, SenderGetter, TLObject, abc.ABC): self._sender_id = from_id self._sender = None self._input_sender = None + self._via_bot = None + self._via_input_bot = None self._action_entities = None if not out and isinstance(to_id, types.PeerUser): @@ -223,6 +225,13 @@ class Message(ChatGetter, SenderGetter, TLObject, abc.ABC): except TypeError: self._input_chat = None + self._via_bot = entities.get(self.via_bot_id) + if self._via_bot: + try: + self._via_input_bot = utils.get_input_peer(self._via_bot) + except TypeError: + self._via_input_bot = None + if self.fwd_from: self._forward = Forward(self._client, self.fwd_from, entities) @@ -530,6 +539,24 @@ class Message(ChatGetter, SenderGetter, TLObject, abc.ABC): """ return self._action_entities + @property + def via_bot(self): + """ + If this message was sent via some bot (i.e. `via_bot_id` is not + ``None``), this property returns the :tl:`User` of the bot that + was used to send this message. + + Returns ``None`` otherwise (or if the bot entity is unknown). + """ + return self._via_bot + + @property + def via_input_bot(self): + """ + Returns the input variant of `via_bot`. + """ + return self._via_input_bot + # endregion Public Properties # region Public Methods @@ -779,7 +806,6 @@ class Message(ChatGetter, SenderGetter, TLObject, abc.ABC): # region Private Methods - # TODO Make a property for via_bot and via_input_bot, as well as get_* async def _reload_message(self): """ Re-fetches this message to reload the sender and chat entities, @@ -797,6 +823,9 @@ class Message(ChatGetter, SenderGetter, TLObject, abc.ABC): self._input_sender = msg._input_sender self._chat = msg._chat self._input_chat = msg._input_chat + self._via_bot = msg._via_bot + self._via_input_bot = msg._via_input_bot + self._forward = msg._forward self._action_entities = msg._action_entities async def _refetch_sender(self):