mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-08-05 12:40:22 +03:00
Add a pattern Argument
Add a pattern argument to the NewMessage event. The event will only be passed if the string passes the pattern.
This commit is contained in:
parent
7258d359f1
commit
246df07114
|
@ -1,6 +1,7 @@
|
|||
import abc
|
||||
import datetime
|
||||
import itertools
|
||||
import re
|
||||
|
||||
from .. import utils
|
||||
from ..errors import RPCError
|
||||
|
@ -179,13 +180,14 @@ class NewMessage(_EventBuilder):
|
|||
would not return such thing, this is a custom modification).
|
||||
"""
|
||||
def __init__(self, incoming=None, outgoing=None,
|
||||
chats=None, blacklist_chats=False):
|
||||
chats=None, blacklist_chats=False, pattern=None):
|
||||
if incoming and outgoing:
|
||||
raise ValueError('Can only set either incoming or outgoing')
|
||||
|
||||
super().__init__(chats=chats, blacklist_chats=blacklist_chats)
|
||||
self.incoming = incoming
|
||||
self.outgoing = outgoing
|
||||
self.pattern = pattern
|
||||
|
||||
def build(self, update):
|
||||
if isinstance(update,
|
||||
|
@ -229,13 +231,15 @@ class NewMessage(_EventBuilder):
|
|||
return
|
||||
|
||||
# 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
|
||||
|
||||
if self.incoming and event.message.out:
|
||||
return
|
||||
if self.outgoing and not event.message.out:
|
||||
return
|
||||
if self.regex and not re.match(self.regex, event.raw_text):
|
||||
return
|
||||
|
||||
return self._filter_event(event)
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user