Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle callable from_users for NewMessage events #4456

Open
wants to merge 1 commit into
base: v1
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion telethon/events/newmessage.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class NewMessage(EventBuilder):
If set to `True`, only **outgoing** messages will be handled.
Mutually exclusive with ``incoming`` (can only set one of either).

from_users (`entity`, optional):
from_users (`entity`, `callable`, optional):
Unlike `chats`, this parameter filters the *senders* of the
message. That is, only messages *sent by these users* will be
handled. Use `chats` if you want private messages with this/these
Expand Down Expand Up @@ -74,6 +74,7 @@ def __init__(self, chats=None, *, blacklist_chats=False, func=None,
self.outgoing = outgoing
self.from_users = from_users
self.forwards = forwards

if isinstance(pattern, str):
self.pattern = re.compile(pattern).match
elif not pattern or callable(pattern):
Expand All @@ -83,6 +84,9 @@ def __init__(self, chats=None, *, blacklist_chats=False, func=None,
else:
raise TypeError('Invalid pattern type given')

if callable(from_users):
self.from_users = from_users()

# 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.pattern,
Expand Down