Expand description
Filters are main part of the library used to filter incoming updates and allow call handlers by their data (text, chat, user, command, etc.),
context
(state, db, etc.) and other conditions.
Filter
is a trait that accepts bot
, update
and context
and returns true
if the filter passes, otherwise false
.
You can use Filter
trait to create your own filters or use one of the ready-made implementations.
Most likely you will have to write your filters, so we recommend you to check out the examples/text_case_filter
to see how to create your own filters
and check ready-made implementations.
Filters can be combined with logical operators And
and Or
and inverted with Invert
.
Each filter has a method Filter::invert
, Filter::and
and Filter::or
to create Invert
, And
and Or
filters respectively.
Ready-made implementations:
ChatType
: Filter for checking the type of chat. Usually used withChatTypeEnum
(or its string representation) to check the type of chat. Creates withone
ormany
methods.Command
: This filter checks if the message is a command. Filter acceptscommand pattern type
that represents a command pattern type for verification, for example, text,BotCommand
(just alias to text of command) orRegex
. You can create a filter withnew
method with transferring all necessary data at once, or useCommandBuilder
to create a filter step by step. Instead ofCommandBuilder
you can useCommand
one
,one_with_prefix
,many
,many_with_prefix
methods.ContentType
: Filter for checking the type of the message content. Usually used withContentTypeEnum
(or its string representation) to check the type of content. Creates withone
ormany
methods.State
: Filter for checking the state of the user/chat/etc. Filter acceptsStateType
that represents a state type for verification, for example, equal, any or none. You can create a filter withone
ormany
if you want to check the state with the exact value or useany
ornone
if you want to check the state with any value or without state, respectively.Text
: This filter checks if the text matches the specified pattern. Gets the text from theupdate
, the text of the message, the text of the inline query, the data of the callback query, etc. Filter acceptstext pattern type
that represents a text pattern type to check for equality, so you can useRegex
orCow
to check the text. You can create a filter withone
ormany
if you want to check the text with the exact value. If you want to check the text withcontains
,starts_with
orends_with
methods that accept onlyCow
, you can create a filter withcontains_single
,contains
,starts_with_single
,starts_with
,ends_with_single
, orends_with
methods, or useTextBuilder
to create a filter step by step.User
: Filter for checking the user. This filter checks if the user username, first name, last name, language code or ID is equal to one of the specified. You can create a filter withnew
method with transferring all necessary data at once, or useUserBuilder
to create a filter step by step. Instead ofUserBuilder
you can useUser
username
,usernames
,first_name
,first_names
,last_name
,last_names
,language_code
,language_codes
,id
orids
methods. This filter checks user data step by step using the logical operatoror
, so if at least one check is successful, the filter will return the valuetrue
.
Re-exports§
pub use base::Filter;
pub use chat_type::ChatType;
pub use command::Builder as CommandBuilder;
pub use command::Command;
pub use command::CommandObject;
pub use content_type::ContentType;
pub use logical::And;
pub use logical::Invert;
pub use logical::Or;
pub use state::State;
pub use state::StateType;
pub use text::Builder as TextBuilder;
pub use text::Text;
pub use user::Builder as UserBuilder;
pub use user::User;
Modules§
- Logical filters that allow you to combine other filters.