Some filter updates

Hinrich Mahler 2022-04-18 11:18:27 +02:00
parent 30270b15cc
commit 9aa2ae7ac2
2 changed files with 19 additions and 9 deletions

@ -5,15 +5,17 @@ When using `MessageHandler` it is sometimes useful to have more than one filter.
## Examples
#### Message is either video, photo, or document (generic file)
``` python
from telegram.ext import MessageHandler, Filters
from telegram.ext import MessageHandler, filters
handler = MessageHandler(Filters.video | Filters.photo | Filters.document,
callback)
handler = MessageHandler(
filters.VIDEO | filters.PHOTO | filters.DOCUMET.ALL,
callback
)
```
#### Message is a forwarded photo
``` python
handler = MessageHandler(Filters.forwarded & Filters.photo, callback)
handler = MessageHandler(filters.FORWARDED & filters.PHOTO, callback)
```
#### Message is text and contains a link
@ -21,14 +23,20 @@ handler = MessageHandler(Filters.forwarded & Filters.photo, callback)
from telegram import MessageEntity
handler = MessageHandler(
Filters.text & (Filters.entity(MessageEntity.URL) |
Filters.entity(MessageEntity.TEXT_LINK)),
callback)
filters.TEXT & (
filters.entity(MessageEntity.URL) |
filters.entity(MessageEntity.TEXT_LINK)
),
callback
)
```
#### Message is a photo and it's not forwarded
``` python
handler = MessageHandler(Filters.photo & (~ Filters.forwarded), callback)
handler = MessageHandler(
filters.PHOTO & (~ filters.FORWARDED),
callback
)
```
# Custom filters
@ -61,7 +69,7 @@ awesome_handler = MessageHandler(filter_awesome, callback)
## `Filters` and `CallbackContext`
You may have noticed that when using `Filters.regex`, the attributes `context.matches` and `context.match` are set to the corresponding matches. To achieve something like this for your custom filter, you can do the following:
You may have noticed that when using `filters.regex`, the attributes `context.matches` and `context.match` are set to the corresponding matches. To achieve something like this for your custom filter, you can do the following:
1. Set `self.data_filter=True` for your filter.
2. If the update should be handled return a dictionary of the form `{attribute_name: [values]}`. This dict will be merged with the internal dict of the `context` argument making `value` available as `context.attribute_name`. This currently works with `MessageHandler`, `CommandHandler` and `PrefixHandler`, which are the only handlers that accept filters.

@ -142,6 +142,8 @@ The `ext.filters` module was rewritten almost from scratch and uses a new namesp
2. Already instantiated filters that don't need arguments are now in `SCREAMING_SNAKE_CASE`, e.g. `filters.TEXT`. Filter *classes* that do need arguments to be used are now in `CamelCase`, e.g. `filters.User`.
3. For filters that are closely related, we now use a namespace class to group them. For example, `filters.Document` can not be used in `MessageHandler`. To filter for messages with *any* document attached, use `filters.Document.ALL` instead.
Moreover, filters are no longer callable. To check if a filter accepts an update, use the new syntax `my_filter.check_update(update)` instead.
### `JobQueue`
#### New arguments `{chat, user}_id`