Using Filters

Filters are a powerful mechanism in Irenogram that let you control which updates your handlers should process. They work by inspecting the incoming Message (or other update) and deciding whether the handler should be invoked.

Built-in Filters

Irenogram ships with a rich set of ready-to-use filters available in pyrogram.filters.

from pyrogram import Client, filters

app = Client("my_account")

@app.on_message(filters.private & filters.text)
async def echo(client, message):
    await message.reply(message.text)

app.run()

Combining Filters

Filters can be combined using standard Python bitwise operators:

  • & — both filters must match (AND)

  • | — at least one filter must match (OR)

  • ~ — the filter must NOT match (NOT)

# Private messages that contain text OR a photo
filters.private & (filters.text | filters.photo)

# Any message that is NOT a command
~filters.command("start")

Filtering by Chat

You can restrict a handler to specific chats using chat:

@app.on_message(filters.chat("@my_channel") & filters.text)
async def channel_text(client, message):
    print(message.text)

Filtering by User

Restrict a handler to specific users using user:

@app.on_message(filters.user("admin_username") & filters.command("reload"))
async def admin_reload(client, message):
    await message.reply("Reloading...")

Command Filters

The command filter matches messages that start with a bot command:

@app.on_message(filters.command("start"))
async def start(client, message):
    await message.reply("Hello!")

# Multiple commands in one handler
@app.on_message(filters.command(["help", "info"]))
async def help_info(client, message):
    await message.reply("Help & Info")

Regex Filters

Use regex to match messages against a regular expression:

import re

@app.on_message(filters.regex(r"^hello", re.IGNORECASE))
async def hello_handler(client, message):
    await message.reply("Hey there!")

See also

Creating Custom Filters — How to build your own custom filters.