Add pattern argument

This commit is contained in:
Jannik 2018-02-19 23:27:19 +01:00 committed by GitHub
parent 4050d1ca00
commit 69a85b863f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,7 @@
import abc import abc
import datetime import datetime
import itertools import itertools
import re
from .. import utils from .. import utils
from ..errors import RPCError from ..errors import RPCError
@ -172,6 +173,10 @@ class NewMessage(_EventBuilder):
outgoing (:obj:`bool`, optional): outgoing (:obj:`bool`, optional):
If set to ``True``, only **outgoing** messages will be handled. If set to ``True``, only **outgoing** messages will be handled.
Mutually exclusive with ``incoming`` (can only set one of either). Mutually exclusive with ``incoming`` (can only set one of either).
pattern (:obj:`str` | :obj:`Pattern`, optional): Regex pattern. If not ``None``,
``re.match`` is used on :attr:`event.message.message` to determine if an event
should be handled.
Notes: Notes:
The ``message.from_id`` might not only be an integer or ``None``, The ``message.from_id`` might not only be an integer or ``None``,
@ -179,13 +184,14 @@ class NewMessage(_EventBuilder):
would not return such thing, this is a custom modification). would not return such thing, this is a custom modification).
""" """
def __init__(self, incoming=None, outgoing=None, def __init__(self, incoming=None, outgoing=None,
chats=None, blacklist_chats=False): chats=None, blacklist_chats=False, pattern=None):
if incoming and outgoing: if incoming and outgoing:
raise ValueError('Can only set either incoming or outgoing') raise ValueError('Can only set either incoming or outgoing')
super().__init__(chats=chats, blacklist_chats=blacklist_chats) super().__init__(chats=chats, blacklist_chats=blacklist_chats)
self.incoming = incoming self.incoming = incoming
self.outgoing = outgoing self.outgoing = outgoing
self.pattern = pattern
def build(self, update): def build(self, update):
if isinstance(update, if isinstance(update,
@ -229,13 +235,15 @@ class NewMessage(_EventBuilder):
return return
# Short-circuit if we let pass all events # Short-circuit if we let pass all events
if all(x is None for x in (self.incoming, self.outgoing, self.chats)): if all(x is None for x in (self.incoming, self.outgoing, self.chats, self.pattern)):
return event return event
if self.incoming and event.message.out: if self.incoming and event.message.out:
return return
if self.outgoing and not event.message.out: if self.outgoing and not event.message.out:
return return
if self.pattern and not re.match(self.pattern, event.message.message):
return
return self._filter_event(event) return self._filter_event(event)