2018-04-05 21:14:22 +03:00
|
|
|
import abc
|
2018-08-21 13:14:32 +03:00
|
|
|
import asyncio
|
2018-04-05 21:14:22 +03:00
|
|
|
import warnings
|
|
|
|
|
2021-09-12 17:58:06 +03:00
|
|
|
from .. import _tl
|
|
|
|
from .._misc import utils, tlobject
|
|
|
|
from ..types._custom.chatgetter import ChatGetter
|
2018-04-05 21:14:22 +03:00
|
|
|
|
|
|
|
|
2018-06-13 17:20:15 +03:00
|
|
|
async def _into_id_set(client, chats):
|
2018-04-05 21:14:22 +03:00
|
|
|
"""Helper util to turn the input chat or chats into a set of IDs."""
|
|
|
|
if chats is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if not utils.is_list_like(chats):
|
|
|
|
chats = (chats,)
|
|
|
|
|
|
|
|
result = set()
|
|
|
|
for chat in chats:
|
|
|
|
if isinstance(chat, int):
|
2021-09-18 17:29:45 +03:00
|
|
|
result.add(chat)
|
2021-09-12 17:58:06 +03:00
|
|
|
elif isinstance(chat, tlobject.TLObject) and chat.SUBCLASS_OF_ID == 0x2d45687:
|
2018-04-05 21:14:22 +03:00
|
|
|
# 0x2d45687 == crc32(b'Peer')
|
|
|
|
result.add(utils.get_peer_id(chat))
|
|
|
|
else:
|
2018-06-13 17:20:15 +03:00
|
|
|
chat = await client.get_input_entity(chat)
|
2021-09-12 13:16:02 +03:00
|
|
|
if isinstance(chat, _tl.InputPeerSelf):
|
2018-06-13 17:20:15 +03:00
|
|
|
chat = await client.get_me(input_peer=True)
|
2018-04-05 21:14:22 +03:00
|
|
|
result.add(utils.get_peer_id(chat))
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
class EventBuilder(abc.ABC):
|
|
|
|
"""
|
|
|
|
The common event builder, with builtin support to filter per chat.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
chats (`entity`, optional):
|
2018-07-15 12:31:14 +03:00
|
|
|
May be one or more entities (username/peer/etc.), preferably IDs.
|
|
|
|
By default, only matching chats will be handled.
|
2018-04-05 21:14:22 +03:00
|
|
|
|
|
|
|
blacklist_chats (`bool`, optional):
|
|
|
|
Whether to treat the chats as a blacklist instead of
|
|
|
|
as a whitelist (default). This means that every chat
|
|
|
|
will be handled *except* those specified in ``chats``
|
|
|
|
which will be ignored if ``blacklist_chats=True``.
|
2018-09-09 16:48:54 +03:00
|
|
|
|
|
|
|
func (`callable`, optional):
|
2020-05-16 10:58:37 +03:00
|
|
|
A callable (async or not) function that should accept the event as input
|
2018-09-09 16:48:54 +03:00
|
|
|
parameter, and return a value indicating whether the event
|
|
|
|
should be dispatched or not (any truthy value will do, it
|
|
|
|
does not need to be a `bool`). It works like a custom filter:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
@client.on(events.NewMessage(func=lambda e: e.is_private))
|
|
|
|
async def handler(event):
|
|
|
|
pass # code here
|
2018-04-05 21:14:22 +03:00
|
|
|
"""
|
2018-09-09 16:48:54 +03:00
|
|
|
def __init__(self, chats=None, *, blacklist_chats=False, func=None):
|
2018-04-05 21:14:22 +03:00
|
|
|
self.chats = chats
|
2019-03-31 13:14:34 +03:00
|
|
|
self.blacklist_chats = bool(blacklist_chats)
|
2018-08-27 02:19:37 +03:00
|
|
|
self.resolved = False
|
2018-09-09 16:48:54 +03:00
|
|
|
self.func = func
|
2018-08-27 02:19:37 +03:00
|
|
|
self._resolve_lock = None
|
2018-04-05 21:14:22 +03:00
|
|
|
|
2018-07-19 02:47:32 +03:00
|
|
|
@classmethod
|
2018-04-05 21:14:22 +03:00
|
|
|
@abc.abstractmethod
|
2021-12-11 15:31:38 +03:00
|
|
|
def build(cls, update, others, self_id, entities, client):
|
2019-06-30 14:23:18 +03:00
|
|
|
"""
|
|
|
|
Builds an event for the given update if possible, or returns None.
|
|
|
|
|
|
|
|
`others` are the rest of updates that came in the same container
|
|
|
|
as the current `update`.
|
2019-08-07 01:46:19 +03:00
|
|
|
|
|
|
|
`self_id` should be the current user's ID, since it is required
|
|
|
|
for some events which lack this information but still need it.
|
2019-06-30 14:23:18 +03:00
|
|
|
"""
|
2019-08-07 01:46:19 +03:00
|
|
|
# TODO So many parameters specific to only some update types seems dirty
|
2018-04-05 21:14:22 +03:00
|
|
|
|
2018-06-13 17:20:15 +03:00
|
|
|
async def resolve(self, client):
|
2018-04-05 21:14:22 +03:00
|
|
|
"""Helper method to allow event builders to be resolved before usage"""
|
2018-08-27 02:19:37 +03:00
|
|
|
if self.resolved:
|
|
|
|
return
|
|
|
|
|
|
|
|
if not self._resolve_lock:
|
2020-07-25 19:39:35 +03:00
|
|
|
self._resolve_lock = asyncio.Lock()
|
2018-08-27 02:19:37 +03:00
|
|
|
|
|
|
|
async with self._resolve_lock:
|
|
|
|
if not self.resolved:
|
|
|
|
await self._resolve(client)
|
|
|
|
self.resolved = True
|
2018-04-05 21:14:22 +03:00
|
|
|
|
2018-08-27 02:19:37 +03:00
|
|
|
async def _resolve(self, client):
|
|
|
|
self.chats = await _into_id_set(client, self.chats)
|
2018-08-21 13:14:32 +03:00
|
|
|
|
2018-07-11 12:22:43 +03:00
|
|
|
def filter(self, event):
|
2018-04-05 21:14:22 +03:00
|
|
|
"""
|
2020-05-16 10:58:37 +03:00
|
|
|
Returns a truthy value if the event passed the filter and should be
|
|
|
|
used, or falsy otherwise. The return value may need to be awaited.
|
2018-08-21 13:14:32 +03:00
|
|
|
|
|
|
|
The events must have been resolved before this can be called.
|
2018-04-05 21:14:22 +03:00
|
|
|
"""
|
2018-08-21 13:14:32 +03:00
|
|
|
if not self.resolved:
|
2020-05-16 10:58:37 +03:00
|
|
|
return
|
2018-08-21 13:14:32 +03:00
|
|
|
|
2018-04-05 21:14:22 +03:00
|
|
|
if self.chats is not None:
|
2019-03-31 13:07:29 +03:00
|
|
|
# Note: the `event.chat_id` property checks if it's `None` for us
|
|
|
|
inside = event.chat_id in self.chats
|
2018-04-05 21:14:22 +03:00
|
|
|
if inside == self.blacklist_chats:
|
|
|
|
# If this chat matches but it's a blacklist ignore.
|
|
|
|
# If it doesn't match but it's a whitelist ignore.
|
2020-05-16 10:58:37 +03:00
|
|
|
return
|
|
|
|
|
|
|
|
if not self.func:
|
|
|
|
return True
|
2018-09-09 16:48:54 +03:00
|
|
|
|
2020-05-16 10:58:37 +03:00
|
|
|
# Return the result of func directly as it may need to be awaited
|
|
|
|
return self.func(event)
|
2018-04-05 21:14:22 +03:00
|
|
|
|
|
|
|
|
2018-07-10 16:15:22 +03:00
|
|
|
class EventCommon(ChatGetter, abc.ABC):
|
2018-06-20 12:05:33 +03:00
|
|
|
"""
|
|
|
|
Intermediate class with common things to all events.
|
|
|
|
|
2018-07-10 16:15:22 +03:00
|
|
|
Remember that this class implements `ChatGetter
|
|
|
|
<telethon.tl.custom.chatgetter.ChatGetter>` which
|
|
|
|
means you have access to all chat properties and methods.
|
|
|
|
|
|
|
|
In addition, you can access the `original_update`
|
|
|
|
field which contains the original :tl:`Update`.
|
2018-06-20 12:05:33 +03:00
|
|
|
"""
|
2018-04-05 21:14:22 +03:00
|
|
|
_event_name = 'Event'
|
|
|
|
|
2019-06-19 12:46:03 +03:00
|
|
|
def __init__(self, chat_peer=None, msg_id=None, broadcast=None):
|
2019-05-13 00:29:01 +03:00
|
|
|
super().__init__(chat_peer, broadcast=broadcast)
|
2018-04-05 21:14:22 +03:00
|
|
|
self._entities = {}
|
|
|
|
self._client = None
|
|
|
|
self._message_id = msg_id
|
2018-04-28 13:58:41 +03:00
|
|
|
self.original_update = None
|
2018-04-05 21:14:22 +03:00
|
|
|
|
2018-05-31 14:30:22 +03:00
|
|
|
def _set_client(self, client):
|
|
|
|
"""
|
|
|
|
Setter so subclasses can act accordingly when the client is set.
|
|
|
|
"""
|
2021-12-11 15:31:38 +03:00
|
|
|
# TODO Nuke
|
2018-05-31 14:30:22 +03:00
|
|
|
self._client = client
|
2019-05-01 18:07:12 +03:00
|
|
|
if self._chat_peer:
|
2019-05-01 18:52:32 +03:00
|
|
|
self._chat, self._input_chat = utils._get_entity_pair(
|
|
|
|
self.chat_id, self._entities, client._entity_cache)
|
2019-05-01 18:07:12 +03:00
|
|
|
else:
|
|
|
|
self._chat = self._input_chat = None
|
2018-05-31 14:30:22 +03:00
|
|
|
|
2018-04-05 21:14:22 +03:00
|
|
|
@property
|
|
|
|
def client(self):
|
2018-06-20 12:05:33 +03:00
|
|
|
"""
|
|
|
|
The `telethon.TelegramClient` that created this event.
|
|
|
|
"""
|
2018-04-05 21:14:22 +03:00
|
|
|
return self._client
|
|
|
|
|
|
|
|
def __str__(self):
|
2021-09-12 13:16:02 +03:00
|
|
|
return _tl.TLObject.pretty_format(self.to_dict())
|
2018-04-05 21:14:22 +03:00
|
|
|
|
|
|
|
def stringify(self):
|
2021-09-12 13:16:02 +03:00
|
|
|
return _tl.TLObject.pretty_format(self.to_dict(), indent=0)
|
2018-04-05 21:14:22 +03:00
|
|
|
|
|
|
|
def to_dict(self):
|
|
|
|
d = {k: v for k, v in self.__dict__.items() if k[0] != '_'}
|
|
|
|
d['_'] = self._event_name
|
|
|
|
return d
|
|
|
|
|
|
|
|
|
|
|
|
def name_inner_event(cls):
|
|
|
|
"""Decorator to rename cls.Event 'Event' as 'cls.Event'"""
|
|
|
|
if hasattr(cls, 'Event'):
|
|
|
|
cls.Event._event_name = '{}.Event'.format(cls.__name__)
|
|
|
|
else:
|
|
|
|
warnings.warn('Class {} does not have a inner Event'.format(cls))
|
|
|
|
return cls
|