-
Notifications
You must be signed in to change notification settings - Fork 6k
Update Filters, CommandHandler and MessageHandler #1117
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
Makes it possible to have filters work on an update instead of message, while keeping behavior for current filters
- remove allow_edited (deprecated for a while) - set deprecated defaults to None - Raise deprecation warning when they're used - add sensible defaults for filters. - rework tests
jsmnbom
left a comment
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.
Mostly looks good! :D Added a few comments as usual :)
We gotta remember to update the wiki page too
telegram/ext/commandhandler.py
Outdated
| which is the text following the command split on single or consecutive whitespace characters. | ||
| By default the handler listens to messages as well as edited messages. To change this behavior | ||
| use ``~Filters.update_type.edited_message``. |
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.
add "[...] as the filter argument" maybe?
Oh and this should probably be on the messagehandler too?
| operators (& for and, | for or, ~ for not). | ||
| allow_edited (:obj:`bool`, optional): Determines whether the handler should also accept | ||
| edited messages. Default is ``False``. | ||
| DEPRECATED: Edited is allowed by default. To change this behavior use |
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.
The question is weather edited should be allowed by default, cause I honestly don't think so. If I'm making a bot, I'd probably make it reply to messages it receives, right? Well if it does that with an edited_message, then the bot has sent two messages (one outdated, and the new one in reply to the edited one)..
If we wanna have this be the default, we should at least provide a reply_or_edit or similar convenience method.
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.
Hmm, maybe it;s personal, but I hate bots that don't respond to me fixing my type in a message and it doesn't respond like it should. IMO handling edited message is a sane default.
I don;t really understand what you mean with a reply_or_edit method.
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 agree with the new behaviour of allowing edited messages by default.
telegram/ext/commandhandler.py
Outdated
| self.filters = filters | ||
| self.allow_edited = allow_edited | ||
| if filters: | ||
| self.filters = Filters.update_type.messages & filters |
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.
&= ?
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.
self.filters = Non-existent at this point.
|
|
||
| self.filters = filters | ||
| self.allow_edited = allow_edited | ||
| if filters: |
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.
Should we test that self.filters is a subclass of BaseFilter? Otherwise it will give an obscure crash when we try to & it.
Also, wouldn't this actually break simple function filters? Or have we not supported those for a while?
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.
we never checked before, we could introduce the check here but just let them get an error if they make a mistake imo.. function filters are removed in this version anyway
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.
If we add validation on the type of the filters argument, we should do it with other handlers as well. We can do it with another PR if we find it necessary.
|
|
||
| super(PrefixHandler, self).__init__( | ||
| 'nocommand', callback, filters=filters, allow_edited=allow_edited, pass_args=pass_args, | ||
| 'nocommand', callback, filters=filters, allow_edited=None, pass_args=pass_args, |
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.
Could we change 'nocommand' to None, to make it more clear that it's simply not used?
This of course requires changing a few things in the CommandHandler
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.
That would mean changing CommandHandlers logic to accomodate this. This seemed pretty clean to me.
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 think (second thought?) that PrefixHandler is a redundant code. a RegexHandler with ^prefix is so much cleaner without so much boilerplate.
telegram/ext/filters.py
Outdated
| def filter(self, update): | ||
| return self.messages(update) or self.channel_posts(update) | ||
|
|
||
| update_type = _UpdateType() |
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.
Should we name it updates instead? It's simply shorter and in my opinion as descriptive
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.
yes aggreed
- rename update_types -> updates - added some clarification to docstrings
|
@bomjacob I changed some things accoring to your review, left some comments on others. |
tsnoam
left a comment
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.
See comments on the code
| operators (& for and, | for or, ~ for not). | ||
| allow_edited (:obj:`bool`, optional): Determines whether the handler should also accept | ||
| edited messages. Default is ``False``. | ||
| DEPRECATED: Edited is allowed by default. To change this behavior use |
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 agree with the new behaviour of allowing edited messages by default.
|
|
||
| self.filters = filters | ||
| self.allow_edited = allow_edited | ||
| if filters: |
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.
If we add validation on the type of the filters argument, we should do it with other handlers as well. We can do it with another PR if we find it necessary.
| """ | ||
| if (isinstance(update, Update) and | ||
| (update.message or update.edited_message and self.allow_edited)): | ||
| if (isinstance(update, Update) and update.effective_message): |
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.
nitpick: there are redundant '()'
(they were good before for elegantly handling line breaks)
nitpick2: You can negate the if clause for an early return. Then the indentation of the rest of the method will be easier on the eye.
| update (:class:`telegram.Update`): Incoming telegram update. | ||
| Returns: | ||
| :obj:`bool` |
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.
The return object is not bool any longer.
|
|
||
| super(PrefixHandler, self).__init__( | ||
| 'nocommand', callback, filters=filters, allow_edited=allow_edited, pass_args=pass_args, | ||
| 'nocommand', callback, filters=filters, allow_edited=None, pass_args=pass_args, |
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 think (second thought?) that PrefixHandler is a redundant code. a RegexHandler with ^prefix is so much cleaner without so much boilerplate.
| name = 'Filters.text' | ||
|
|
||
| def filter(self, message): | ||
| print('text_filter_filter {}'.format(repr(self))) |
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.
why do you print here?
also nitpicking: 'text_filter_filter {:r}'.format(self) is more idiomatic
Uh oh!
There was an error while loading. Please reload this page.