Fix pattern= and move pattern_match to events.NewMessage

This commit is contained in:
Lonami Exo 2018-06-20 20:03:44 +02:00
parent a1477a84bf
commit 9e3f6483e8
2 changed files with 21 additions and 22 deletions

View File

@ -85,25 +85,9 @@ class EventCommon(abc.ABC):
"""
Intermediate class with common things to all events.
Attributes:
pattern_match (`obj`):
The resulting object from calling the passed ``pattern`` function.
Here's an example using a string (defaults to regex match):
>>> from telethon import TelegramClient, events
>>> client = TelegramClient(...)
>>>
>>> @client.on(events.NewMessage(pattern=r'hi (\w+)!'))
... def handler(event):
... # In this case, the result is a ``Match`` object
... # since the ``str`` pattern was converted into
... # the ``re.compile(pattern).match`` function.
... print('Welcomed', event.pattern_match.group(1))
...
>>>
original_update (:tl:`Update`):
The original Telegram update object that caused this event.
All events (except `Raw`) have ``is_private``, ``is_group``
and ``is_channel`` boolean properties, as well as an
``original_update`` field containing the original :tl:`Update`.
"""
_event_name = 'Event'
@ -114,8 +98,6 @@ class EventCommon(abc.ABC):
self._message_id = msg_id
self._input_chat = None
self._chat = None
self.pattern_match = None
self.original_update = None
self.is_private = isinstance(chat_peer, types.PeerUser)

View File

@ -67,7 +67,7 @@ class NewMessage(EventBuilder):
# Should we short-circuit? E.g. perform no check at all
self._no_check = all(x is None for x in (
self.chats, self.incoming, self.outgoing,
self.chats, self.incoming, self.outgoing, self.pattern,
self.from_users, self.forwards, self.from_users
))
@ -166,6 +166,22 @@ class NewMessage(EventBuilder):
See `telethon.tl.custom.message.Message` for the rest of
available members and methods.
pattern_match (`obj`):
The resulting object from calling the passed ``pattern`` function.
Here's an example using a string (defaults to regex match):
>>> from telethon import TelegramClient, events
>>> client = TelegramClient(...)
>>>
>>> @client.on(events.NewMessage(pattern=r'hi (\w+)!'))
... def handler(event):
... # In this case, the result is a ``Match`` object
... # since the ``str`` pattern was converted into
... # the ``re.compile(pattern).match`` function.
... print('Welcomed', event.pattern_match.group(1))
...
>>>
"""
def __init__(self, message):
self.__dict__['_init'] = False
@ -179,6 +195,7 @@ class NewMessage(EventBuilder):
super().__init__(chat_peer=chat_peer,
msg_id=message.id, broadcast=bool(message.post))
self.pattern_match = None
self.message = message
def _set_client(self, client):