Thanks to visit codestin.com
Credit goes to github.com

Skip to content

New filters for handling messages from specific chat/user id #677

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

Merged
merged 7 commits into from
Jun 22, 2017
Merged
Show file tree
Hide file tree
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
73 changes: 73 additions & 0 deletions telegram/ext/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,79 @@ def filter(self, message):

group = _Group()

class user(BaseFilter):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class names are CamelCase according to pep8. Please fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know about it, but in the same file we have classes, which names are not CamelCase (for example, language class below)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we ignore pep8 in this instance @tsnoam

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay, just saw your replies here.

Uhm. I'm not sure.
In contrary to other fillers we provide, this filter still needs to be instantiated and cannot be used otherwise.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after discussing about it with @bomjacob I remove my objection here.
I don't think it is "pretty" but it is reasonable and makes a clear API.

"""Filters messages to allow only those which are from specified user ID.

Notes:
Only one of chat_id or username must be used here.

Args:
user_id(Optional[int|list]): which user ID(s) to allow through.
username(Optional[str|list]): which username(s) to allow through. If username starts
with '@' symbol, it will be ignored.

Raises:
ValueError
"""

def __init__(self, user_id=None, username=None):
if not (bool(user_id) ^ bool(username)):
raise ValueError('One and only one of user_id or username must be used')
if user_id is not None and isinstance(user_id, int):
self.user_ids = [user_id]
else:
self.user_ids = user_id
if username is None:
self.usernames = username
elif isinstance(username, str_type):
self.usernames = [username.replace('@', '')]
else:
self.usernames = [user.replace('@', '') for user in username]

def filter(self, message):
if self.user_ids is not None:
return bool(message.from_user and message.from_user.id in self.user_ids)
else:
# self.usernames is not None
return bool(message.from_user and message.from_user.username and
message.from_user.username in self.usernames)

class chat(BaseFilter):
"""Filters messages to allow only those which are from specified chat ID.

Notes:
Only one of chat_id or username must be used here.

Args:
chat_id(Optional[int|list]): which chat ID(s) to allow through.
username(Optional[str|list]): which username(s) to allow through. If username starts
with '@' symbol, it will be ignored.

Raises:
ValueError
"""

def __init__(self, chat_id=None, username=None):
if not (bool(chat_id) ^ bool(username)):
raise ValueError('One and only one of chat_id or username must be used')
if chat_id is not None and isinstance(chat_id, int):
self.chat_ids = [chat_id]
else:
self.chat_ids = chat_id
if username is None:
self.usernames = username
elif isinstance(username, str_type):
self.usernames = [username.replace('@', '')]
else:
self.usernames = [chat.replace('@', '') for chat in username]

def filter(self, message):
if self.chat_ids is not None:
return bool(message.chat_id in self.chat_ids)
else:
# self.usernames is not None
return bool(message.chat.username and message.chat.username in self.usernames)

class _Invoice(BaseFilter):

def filter(self, message):
Expand Down
45 changes: 45 additions & 0 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,51 @@ def test_group_fileter(self):
self.message.chat.type = "supergroup"
self.assertTrue(Filters.group(self.message))

def test_filters_chat(self):
with self.assertRaisesRegexp(ValueError, 'chat_id or username'):
Filters.chat(chat_id=-1, username='chat')
with self.assertRaisesRegexp(ValueError, 'chat_id or username'):
Filters.chat()

def test_filters_chat_id(self):
self.assertFalse(Filters.chat(chat_id=-1)(self.message))
self.message.chat.id = -1
self.assertTrue(Filters.chat(chat_id=-1)(self.message))
self.message.chat.id = -2
self.assertTrue(Filters.chat(chat_id=[-1, -2])(self.message))
self.assertFalse(Filters.chat(chat_id=-1)(self.message))

def test_filters_chat_username(self):
self.assertFalse(Filters.chat(username='chat')(self.message))
self.message.chat.username = 'chat'
self.assertTrue(Filters.chat(username='@chat')(self.message))
self.assertTrue(Filters.chat(username='chat')(self.message))
self.assertTrue(Filters.chat(username=['chat1', 'chat', 'chat2'])(self.message))
self.assertFalse(Filters.chat(username=['@chat1', 'chat_2'])(self.message))

def test_filters_user(self):
with self.assertRaisesRegexp(ValueError, 'user_id or username'):
Filters.user(user_id=1, username='user')
with self.assertRaisesRegexp(ValueError, 'user_id or username'):
Filters.user()

def test_filters_user_id(self):
self.assertFalse(Filters.user(user_id=1)(self.message))
self.message.from_user.id = 1
self.assertTrue(Filters.user(user_id=1)(self.message))
self.message.from_user.id = 2
self.assertTrue(Filters.user(user_id=[1, 2])(self.message))
self.assertFalse(Filters.user(user_id=1)(self.message))

def test_filters_username(self):
self.assertFalse(Filters.user(username='user')(self.message))
self.assertFalse(Filters.user(username='Testuser')(self.message))
self.message.from_user.username = 'user'
self.assertTrue(Filters.user(username='@user')(self.message))
self.assertTrue(Filters.user(username='user')(self.message))
self.assertTrue(Filters.user(username=['user1', 'user', 'user2'])(self.message))
self.assertFalse(Filters.user(username=['@username', '@user_2'])(self.message))

def test_and_filters(self):
self.message.text = 'test'
self.message.forward_date = True
Expand Down