-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
Conversation
I'd personally prefer if these filter were made a bit smarter by allowing them to use a username or a User object. |
@bomjacob I think that |
@bomjacob Done |
Okay yeah, that's probably true, looks good then :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@evgfilim1
Thank you for you contribution. I have left some comments on the code to be fixed.
Also, I believe @bomjacob had an additional request before accepting this PR, so you can wait on fixing my comments until @bomjacob replies too.
@@ -324,6 +324,65 @@ def filter(self, message): | |||
|
|||
group = _Group() | |||
|
|||
class user(BaseFilter): |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
telegram/ext/filters.py
Outdated
specified | ||
username(Optional[str]): which username to allow through. Required if user_id is not | ||
specified | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Documentation should be explicit on the fact that one and only one of
user_id
&username
must be specified. - please add a period at the end of your sentences.
- We use the extension of pep8 allowing 99 chars per line, so you can have the lines in matching size.
telegram/ext/filters.py
Outdated
""" | ||
|
||
def __init__(self, user_id=None, username=None): | ||
if (user_id is None and username is None) or (bool(user_id) and bool(username)): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Too complicated. You can do instead:
if not (bool(user_id) ^ bool(username)):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I didn't know about this
telegram/ext/filters.py
Outdated
|
||
def __init__(self, user_id=None, username=None): | ||
if (user_id is None and username is None) or (bool(user_id) and bool(username)): | ||
raise ValueError('You must specify either user_id or username') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer:
raise ValueError('One and only one of user_id or username must be used')
telegram/ext/filters.py
Outdated
return bool(message.from_user and message.from_user.username and | ||
message.from_user.username == self.username) | ||
|
||
class chat(BaseFilter): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment about CamelCase
telegram/ext/filters.py
Outdated
chat_id(Optional[int]): which chat ID to allow through. Required if username is not | ||
specified | ||
username(Optional[str]): which username to allow through. Required if chat_id is not | ||
specified |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comments about documentation.
telegram/ext/filters.py
Outdated
|
||
def __init__(self, chat_id=None, username=None): | ||
if (chat_id is None and username is None) or (bool(chat_id) and bool(username)): | ||
raise ValueError('You must specify either chat_id or username') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same comments about string to give to ValueError and how to make the if
more simple.
tests/test_filters.py
Outdated
def test_filters_chat(self): | ||
with self.assertRaisesRegexp(ValueError, 'either chat_id or username'): | ||
Filters.chat(chat_id=0, username='chat') | ||
Filters.chat() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line will never be tested. I needs to have its own with
statement.
Could you make it so it accepts either what it does now, or a list of said types? Something akin to https://github.com/python-telegram-bot/python-telegram-bot/blob/master/telegram/ext/filters.py#L353-L356 could probably work (and then use in instead of ==) |
@evgfilim1 thank you for your contribution. |
No description provided.