Telethon/telethon/_events/raw.py

54 lines
1.6 KiB
Python
Raw Normal View History

from .common import EventBuilder
2021-09-12 14:27:13 +03:00
from .._misc import utils
class Raw(EventBuilder):
"""
Raw events are not actual events. Instead, they are the raw
:tl:`Update` object that Telegram sends. You normally shouldn't
need these.
Args:
types (`list` | `tuple` | `type`, optional):
The type or types that the :tl:`Update` instance must be.
Equivalent to ``if not isinstance(update, types): return``.
2020-02-20 12:18:26 +03:00
Example
.. code-block:: python
from telethon import events
@client.on(events.Raw)
async def handler(update):
# Print all incoming updates
print(update.stringify())
"""
def __init__(self, types=None, *, func=None):
super().__init__(func=func)
if not types:
self.types = None
elif not utils.is_list_like(types):
if not isinstance(types, type):
raise TypeError('Invalid input type given: {}'.format(types))
self.types = types
else:
if not all(isinstance(x, type) for x in types):
raise TypeError('Invalid input types given: {}'.format(types))
self.types = tuple(types)
2018-06-13 17:20:15 +03:00
async def resolve(self, client):
self.resolved = True
2018-07-19 02:47:32 +03:00
@classmethod
2021-12-11 15:31:38 +03:00
def build(cls, update, others=None, self_id=None, *todo, **todo2):
2018-07-11 12:22:43 +03:00
return update
def filter(self, event):
if not self.types or isinstance(event, self.types):
if self.func:
# Return the result of func directly as it may need to be awaited
return self.func(event)
2018-07-11 12:22:43 +03:00
return event