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

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

merged 7 commits into from
Jun 22, 2017

Conversation

evgfilim1
Copy link
Contributor

No description provided.

@jsmnbom
Copy link
Member

jsmnbom commented Jun 19, 2017

I'd personally prefer if these filter were made a bit smarter by allowing them to use a username or a User object.
Either by doing something akin to:
def __init__(self, user_id=None, username=None, user=None):
or by doing some type checking using isinstance, so you don't need several kwargs (this might be cleaner since they would be mutually exclusive?) (or maybe not, since type checking isn't really pythonic?)

@evgfilim1
Copy link
Contributor Author

evgfilim1 commented Jun 20, 2017

@bomjacob I think that User object is not very necessary because filters are usually used in handlers. I don't think that anyone would create User instance when they can specify user id or username

@evgfilim1
Copy link
Contributor Author

@bomjacob Done

@jsmnbom
Copy link
Member

jsmnbom commented Jun 20, 2017

Okay yeah, that's probably true, looks good then :)
As far as error checking goes, we could probably have gotten away with simply:
if not (chat_id or username):
But I guess this works too :)

@tsnoam tsnoam requested a review from jsmnbom June 20, 2017 20:33
@tsnoam tsnoam assigned tsnoam and unassigned tsnoam Jun 20, 2017
@tsnoam tsnoam self-requested a review June 20, 2017 20:33
Copy link
Member

@tsnoam tsnoam left a 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):
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.

specified
username(Optional[str]): which username to allow through. Required if user_id is not
specified

Copy link
Member

Choose a reason for hiding this comment

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

  1. Documentation should be explicit on the fact that one and only one of user_id & username must be specified.
  2. please add a period at the end of your sentences.
  3. We use the extension of pep8 allowing 99 chars per line, so you can have the lines in matching size.

"""

def __init__(self, user_id=None, username=None):
if (user_id is None and username is None) or (bool(user_id) and bool(username)):
Copy link
Member

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)):

Copy link
Contributor Author

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


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')
Copy link
Member

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')

return bool(message.from_user and message.from_user.username and
message.from_user.username == self.username)

class chat(BaseFilter):
Copy link
Member

Choose a reason for hiding this comment

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

Same comment about CamelCase

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
Copy link
Member

Choose a reason for hiding this comment

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

Same comments about documentation.


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')
Copy link
Member

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.

def test_filters_chat(self):
with self.assertRaisesRegexp(ValueError, 'either chat_id or username'):
Filters.chat(chat_id=0, username='chat')
Filters.chat()
Copy link
Member

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.

@jsmnbom
Copy link
Member

jsmnbom commented Jun 20, 2017

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
Copy link
Contributor Author

@tsnoam @bomjacob Done

@tsnoam tsnoam merged commit 470ee86 into python-telegram-bot:master Jun 22, 2017
@tsnoam
Copy link
Member

tsnoam commented Jun 22, 2017

@evgfilim1 thank you for your contribution.

@evgfilim1 evgfilim1 deleted the new-filters branch June 22, 2017 19:07
@github-actions github-actions bot locked and limited conversation to collaborators Aug 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants