Skip to content

Commit 7e81a15

Browse files
committed
Added ability to process list of ids/usernames
1 parent ca9068f commit 7e81a15

File tree

1 file changed

+33
-23
lines changed

1 file changed

+33
-23
lines changed

telegram/ext/filters.py

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,9 @@ class user(BaseFilter):
331331
Only one of chat_id or username must be used here.
332332
333333
Args:
334-
user_id(Optional[int]): which user ID to allow through.
335-
username(Optional[str]): which username to allow through. If username starts with '@'
336-
symbol, it will be ignored.
334+
user_id(Optional[int|list]): which user ID(s) to allow through.
335+
username(Optional[str|list]): which username(s) to allow through. If username starts
336+
with '@' symbol, it will be ignored.
337337
338338
Raises:
339339
ValueError
@@ -342,19 +342,24 @@ class user(BaseFilter):
342342
def __init__(self, user_id=None, username=None):
343343
if not (bool(user_id) ^ bool(username)):
344344
raise ValueError('One and only one of user_id or username must be used')
345-
if username is not None and username.startswith('@'):
346-
self.username = username[1:]
345+
if user_id is not None and isinstance(user_id, int):
346+
self.user_ids = [user_id]
347347
else:
348-
self.username = username
349-
self.user_id = user_id
348+
self.user_ids = user_id
349+
if username is None:
350+
self.usernames = username
351+
elif isinstance(username, str_type):
352+
self.usernames = [username.replace('@', '')]
353+
else:
354+
self.usernames = [user.replace('@', '') for user in username]
350355

351356
def filter(self, message):
352-
if self.user_id is not None:
353-
return bool(message.from_user and message.from_user.id == self.user_id)
357+
if self.user_ids is not None:
358+
return bool(message.from_user and message.from_user.id in self.user_ids)
354359
else:
355-
# self.username is not None
360+
# self.usernames is not None
356361
return bool(message.from_user and message.from_user.username and
357-
message.from_user.username == self.username)
362+
message.from_user.username in self.usernames)
358363

359364
class chat(BaseFilter):
360365
"""Filters messages to allow only those which are from specified chat ID.
@@ -363,29 +368,34 @@ class chat(BaseFilter):
363368
Only one of chat_id or username must be used here.
364369
365370
Args:
366-
chat_id(Optional[int]): which chat ID to allow through.
367-
username(Optional[str]): which username to allow through. If username starts with '@'
368-
symbol, it will be ignored.
371+
chat_id(Optional[int|list]): which chat ID(s) to allow through.
372+
username(Optional[str|list]): which username(s) to allow through. If username starts
373+
with '@' symbol, it will be ignored.
369374
370375
Raises:
371376
ValueError
372377
"""
373378

374379
def __init__(self, chat_id=None, username=None):
375380
if not (bool(chat_id) ^ bool(username)):
376-
raise ValueError('One and only one of user_id or username must be used')
377-
if username is not None and username.startswith('@'):
378-
self.username = username[1:]
381+
raise ValueError('One and only one of chat_id or username must be used')
382+
if chat_id is not None and isinstance(chat_id, int):
383+
self.chat_ids = [chat_id]
384+
else:
385+
self.chat_ids = chat_id
386+
if username is None:
387+
self.usernames = username
388+
elif isinstance(username, str_type):
389+
self.usernames = [username.replace('@', '')]
379390
else:
380-
self.username = username
381-
self.chat_id = chat_id
391+
self.usernames = [chat.replace('@', '') for chat in username]
382392

383393
def filter(self, message):
384-
if self.chat_id is not None:
385-
return message.chat_id == self.chat_id
394+
if self.chat_ids is not None:
395+
return bool(message.chat_id in self.chat_ids)
386396
else:
387-
# self.username is not None
388-
return bool(message.chat.username and message.chat.username == self.username)
397+
# self.usernames is not None
398+
return bool(message.chat.username and message.chat.username in self.usernames)
389399

390400
class _Invoice(BaseFilter):
391401

0 commit comments

Comments
 (0)