Skip to content
↑ ↓ move↵ openEvery page also as Markdown: add .md

Guide

Filters

Filters

A filter is a test that decides whether a handler runs for one update. Every on_* handler method takes one, and the filters:: module builds them.

Example

use vaegram::prelude::*;

app.on_message(filters::command("start") & filters::private(), |msg| async move {
    msg.answer("Welcome to the private chat").await?;
    Ok(())
});
See it run
filters::command("start")filters::private()

The handler answers "Welcome to the private chat" to each update that passes.

  1. Private/startis the command,private,Runs
  2. Private/STARTis the command,private,Runs
  3. Group/startis the command,not private,Skipped
  4. Group/start@your_botis the command,not private,Skipped
  5. Group/start@another_botis not the command,not private,Skipped
  6. Privatehellois not the command,private,Skipped

2 of 6 reach the handler

How it works

The Filter type

Filter<T> is a value, not a trait: filters::command("start") returns a Filter<Message>, which on_message accepts. On messages and callback queries, built-in filters are checked against the raw update before it is parsed, so a miss costs no parsing. Filter::check(&update) runs the same test by hand, which is handy for a message you fetched.

use vaegram::prelude::*;

let start_in_private: Filter<Message> = filters::command("start") & filters::private();
let from_admins: Filter<Message> = filters::user([42, 43]);
let not_a_service: Filter<Message> = !filters::service();
let text_or_photo: Filter<Message> = filters::text() | filters::photo();

& binds tighter than |; parenthesise when you mix them.

Every filter

Group Functions
Any update all()
Chat type private(), direct() (people, not bots), group(), supergroup(), channel()
Ids chat(ids), user(ids): one i64, [i64; N], &[i64] or Vec<i64>
Text text(), caption(), text_starts_with(prefix)
Commands command(names), command_with(prefixes, names), command_with_prefixes(prefixes, names), command_case_sensitive(prefixes, names), command_with_prefixes_case_sensitive(prefixes, names)
Media media(), photo(), video(), animation(), audio(), voice(), video_note(), document(), sticker(), live_photo(), contact(), location(), venue(), dice(), poll(), game(), invoice(), checklist(), story(), giveaway(), giveaway_winners(), paid_media(), widget(), web_page()
Message facts reply(), forwarded(), via_bot(), mentioned(), quote(), from_scheduled(), scheduled(), protected(), media_group(), service(), media_spoiler(), linked_channel(), live_location(), self_destruction(), paid_message(), business(), reacted(), inline_keyboard(), reply_keyboard()
Direction and sender outgoing(), incoming(), me(), bot(), sender_chat()
Chat facts forum(), admin()
Topics topic(id)
Service kinds service_kind(kinds), plus new_chat_members(), left_chat_member(), new_chat_title(), new_chat_photo(), delete_chat_photo(), group_chat_created(), supergroup_chat_created(), channel_chat_created(), migrate_to_chat_id(), migrate_from_chat_id(), pinned_message(), game_high_score(), video_chat_started(), video_chat_ended(), video_chat_members_invited(), successful_payment(), users_shared(), chat_shared(), gift_code(), gift()
Gift offers gift_offer(), gift_offer_accepted(), gift_offer_rejected()
Button data data(bytes), data_prefix(bytes)
Your own test create(closure)

Each filter only applies to update kinds that carry what it tests: filters::user needs a sender, filters::text needs text, filters::data needs callback data. A filter that does not apply is a compile error, not a filter that never matches. Ids are Bot-API style: users positive, basic groups negative, channels and supergroups -100….

With the regex feature, filters::regex(re) takes an owned Regex and returns a filter that matches an update whose text, caption, callback data or payload the pattern finds. filters::matches(&re, &item) is not a filter: it returns every match’s capture groups for one item, for reading them inside a create(..) closure or a handler.

A filter from a closure

use vaegram::prelude::*;

let long: Filter<Message> = filters::create(|msg: &Message| msg.text().len() > 40);
let long_from_admin = filters::user(42) & long;

A closure sees the parsed update, so it is the one filter that always needs parsing. Put the cheap filters first with &: filters::private() & filters::create(..) skips parsing every group message.

State

Dialogue steps take a second kind of filter, StateFilter<S>, built by state(f), state_is(value), any_state() and no_state(). It tests the dialogue’s stored state instead of the update, and only DialogueRouter takes it. See Dialogues.

Common mistakes

  • Matching commands with text_starts_with("/start"): it also takes /starter and /start@another_bot. filters::command("start") reads the name and the mention instead.
  • Putting create(..) first in a chain: every update of that kind then gets parsed. Put it last.
  • Combining filters of different update types with &: both sides must be Filter<T> for the same T, so a mismatch fails to compile.
  • Expecting filters::text() to match a photo’s caption: that is filters::caption().
  • Expecting filters::media() to match a text with a link preview: a preview is not media; filters::web_page() matches it alone.

See also