mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-10-24 01:46:22 +02:00
Some filter updates
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
|
## Examples
|
||||||
#### Message is either video, photo, or document (generic file)
|
#### Message is either video, photo, or document (generic file)
|
||||||
``` python
|
``` python
|
||||||
from telegram.ext import MessageHandler, Filters
|
from telegram.ext import MessageHandler, filters
|
||||||
|
|
||||||
handler = MessageHandler(Filters.video | Filters.photo | Filters.document,
|
handler = MessageHandler(
|
||||||
callback)
|
filters.VIDEO | filters.PHOTO | filters.DOCUMET.ALL,
|
||||||
|
callback
|
||||||
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Message is a forwarded photo
|
#### Message is a forwarded photo
|
||||||
``` python
|
``` python
|
||||||
handler = MessageHandler(Filters.forwarded & Filters.photo, callback)
|
handler = MessageHandler(filters.FORWARDED & filters.PHOTO, callback)
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Message is text and contains a link
|
#### Message is text and contains a link
|
||||||
|
@ -21,14 +23,20 @@ handler = MessageHandler(Filters.forwarded & Filters.photo, callback)
|
||||||
from telegram import MessageEntity
|
from telegram import MessageEntity
|
||||||
|
|
||||||
handler = MessageHandler(
|
handler = MessageHandler(
|
||||||
Filters.text & (Filters.entity(MessageEntity.URL) |
|
filters.TEXT & (
|
||||||
Filters.entity(MessageEntity.TEXT_LINK)),
|
filters.entity(MessageEntity.URL) |
|
||||||
callback)
|
filters.entity(MessageEntity.TEXT_LINK)
|
||||||
|
),
|
||||||
|
callback
|
||||||
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Message is a photo and it's not forwarded
|
#### Message is a photo and it's not forwarded
|
||||||
``` python
|
``` python
|
||||||
handler = MessageHandler(Filters.photo & (~ Filters.forwarded), callback)
|
handler = MessageHandler(
|
||||||
|
filters.PHOTO & (~ filters.FORWARDED),
|
||||||
|
callback
|
||||||
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
# Custom filters
|
# Custom filters
|
||||||
|
@ -61,7 +69,7 @@ awesome_handler = MessageHandler(filter_awesome, callback)
|
||||||
|
|
||||||
## `Filters` and `CallbackContext`
|
## `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.
|
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.
|
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`.
|
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.
|
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`
|
### `JobQueue`
|
||||||
|
|
||||||
#### New arguments `{chat, user}_id`
|
#### New arguments `{chat, user}_id`
|
||||||
|
|
Loading…
Reference in a new issue