2018-07-10 16:15:22 +03:00
|
|
|
import abc
|
|
|
|
|
|
|
|
|
|
|
|
class SenderGetter(abc.ABC):
|
|
|
|
"""
|
|
|
|
Helper base class that introduces the `sender`, `input_sender`
|
|
|
|
and `sender_id` properties and `get_sender` and `get_input_sender`
|
|
|
|
methods.
|
|
|
|
"""
|
2019-05-12 15:00:12 +03:00
|
|
|
def __init__(self, sender_id=None, *, sender=None, input_sender=None):
|
|
|
|
self._sender_id = sender_id
|
|
|
|
self._sender = sender
|
|
|
|
self._input_sender = input_sender
|
|
|
|
self._client = None
|
2019-05-03 14:59:17 +03:00
|
|
|
|
2018-07-10 16:15:22 +03:00
|
|
|
@property
|
|
|
|
def sender(self):
|
|
|
|
"""
|
2018-09-06 11:33:56 +03:00
|
|
|
Returns the :tl:`User` or :tl:`Channel` that sent this object.
|
2019-07-06 13:10:25 +03:00
|
|
|
It may be `None` if Telegram didn't send the sender.
|
2018-07-10 16:15:22 +03:00
|
|
|
|
2019-06-01 17:27:53 +03:00
|
|
|
If you only need the ID, use `sender_id` instead.
|
|
|
|
|
|
|
|
If you need to call a method which needs
|
|
|
|
this chat, use `input_sender` instead.
|
|
|
|
|
|
|
|
If you're using `telethon.events`, use `get_sender()` instead.
|
2018-07-10 16:15:22 +03:00
|
|
|
"""
|
|
|
|
return self._sender
|
|
|
|
|
|
|
|
async def get_sender(self):
|
|
|
|
"""
|
|
|
|
Returns `sender`, but will make an API call to find the
|
|
|
|
sender unless it's already cached.
|
2019-06-01 17:27:53 +03:00
|
|
|
|
|
|
|
If you only need the ID, use `sender_id` instead.
|
|
|
|
|
|
|
|
If you need to call a method which needs
|
|
|
|
this sender, use `get_input_sender()` instead.
|
2018-07-10 16:15:22 +03:00
|
|
|
"""
|
2019-04-05 20:28:31 +03:00
|
|
|
# ``sender.min`` is present both in :tl:`User` and :tl:`Channel`.
|
|
|
|
# It's a flag that will be set if only minimal information is
|
|
|
|
# available (such as display name, but username may be missing),
|
|
|
|
# in which case we want to force fetch the entire thing because
|
|
|
|
# the user explicitly called a method. If the user is okay with
|
|
|
|
# cached information, they may use the property instead.
|
2021-05-09 16:33:01 +03:00
|
|
|
if (self._sender is None or getattr(self._sender, 'min', None)) \
|
2019-04-05 20:28:31 +03:00
|
|
|
and await self.get_input_sender():
|
2022-10-03 14:11:48 +03:00
|
|
|
# self.get_input_sender may refresh in which case the sender may no longer be min
|
|
|
|
# However it could still incur a cost so the cheap check is done twice instead.
|
|
|
|
if self._sender is None or getattr(self._sender, 'min', None):
|
|
|
|
try:
|
|
|
|
self._sender =\
|
|
|
|
await self._client.get_entity(self._input_sender)
|
|
|
|
except ValueError:
|
|
|
|
await self._refetch_sender()
|
2018-07-10 16:15:22 +03:00
|
|
|
return self._sender
|
|
|
|
|
|
|
|
@property
|
|
|
|
def input_sender(self):
|
|
|
|
"""
|
2018-09-06 11:33:56 +03:00
|
|
|
This :tl:`InputPeer` is the input version of the user/channel who
|
2019-06-01 17:27:53 +03:00
|
|
|
sent the message. Similarly to `input_chat
|
|
|
|
<telethon.tl.custom.chatgetter.ChatGetter.input_chat>`, this doesn't
|
|
|
|
have things like username or similar, but still useful in some cases.
|
2018-07-10 16:15:22 +03:00
|
|
|
|
|
|
|
Note that this might not be available if the library can't
|
|
|
|
find the input chat, or if the message a broadcast on a channel.
|
|
|
|
"""
|
2019-05-02 19:44:28 +03:00
|
|
|
if self._input_sender is None and self._sender_id and self._client:
|
2018-07-10 16:15:22 +03:00
|
|
|
try:
|
2019-05-02 19:51:10 +03:00
|
|
|
self._input_sender = \
|
|
|
|
self._client._entity_cache[self._sender_id]
|
2019-05-04 20:30:33 +03:00
|
|
|
except KeyError:
|
2018-07-10 16:15:22 +03:00
|
|
|
pass
|
|
|
|
return self._input_sender
|
|
|
|
|
|
|
|
async def get_input_sender(self):
|
|
|
|
"""
|
|
|
|
Returns `input_sender`, but will make an API call to find the
|
|
|
|
input sender unless it's already cached.
|
|
|
|
"""
|
2019-05-02 19:44:28 +03:00
|
|
|
if self.input_sender is None and self._sender_id and self._client:
|
2018-07-10 16:15:22 +03:00
|
|
|
await self._refetch_sender()
|
|
|
|
return self._input_sender
|
|
|
|
|
|
|
|
@property
|
|
|
|
def sender_id(self):
|
|
|
|
"""
|
|
|
|
Returns the marked sender integer ID, if present.
|
2019-06-01 17:27:53 +03:00
|
|
|
|
|
|
|
If there is a sender in the object, `sender_id` will *always* be set,
|
|
|
|
which is why you should use it instead of `sender.id <sender>`.
|
2018-07-10 16:15:22 +03:00
|
|
|
"""
|
|
|
|
return self._sender_id
|
|
|
|
|
|
|
|
async def _refetch_sender(self):
|
|
|
|
"""
|
|
|
|
Re-fetches sender information through other means.
|
|
|
|
"""
|