Skip to content

Stark.callback

Python decorator of class Stark to handle callback queries. Callback queries are the queries which originate when bot's button is clicked/tapped on.

This decorator is used to handle callback queries. All arguments are optional.

Parameters:

Name Type Description Default
query str | list[str]

Query on which your function is called. Defaults to None, to handle all queries if your function handles all or if using 'filters'

None
startswith bool

Set to True if you want your function to handle all queries starting with the query_string. Defaults to False.

False
owner_only bool

Allow only owner to use this command. Defaults to False.

False
sudo_only bool

Allow only sudos to use this command. Includes owner as sudo automatically. Defaults to False.

False
group int

Define a group for this handler. Defaults to 0. Read More

0
filters pyrogram.filters

Extra filters to apply in your function. Import filters from pyrogram or pystark to use this. See example below.

None

Examples:

from pystark import Stark

# The normal and easiest way.
# Bot will execute function, if button with callback_data 'first_button' is pressed/clicked.
@Stark.callback('first_button')

# Handle multiple callback queries in one function. Mainly used to show same result or do some other pythonic thing, like if-else loop.
# Bot will execute same function, if 'first_button' or 'second_button' is pressed/clicked.
@Stark.callback(['first_button', 'second_button'])

# Function will only be triggered if owner presses the button, that is, the user whose id is set as OWNER_ID in environment variables.
# Others will be ignored.
@Stark.callback('first_button', owner_only=True)

# Function will only be triggered if sudo users or owner presses the button, that is, users set as SUDO_USERS or OWNER_ID in environment variables.
# Others will be ignored.
@Stark.callback('first_button', sudo_only=True)

# Filter/Handle all queries.

# Use positive integer to execute after executing another function in default group that also filtered this query.
@Stark.callback(group=1)

# or Use negative integer to execute before executing another function in default group that also filtered this query.
@Stark.callback(group=-1)

# Don't use this as other functions that handle queries won't work.
@Stark.callback()

# Filter other type of queries using 'filters' argument.

# Import filters from pyrogram or pystark.
from pystark import filters

# Filter only queries by 'StarkProgrammer' and 'Designatory'.
@Stark.callback(filters=filters.user(['StarkProgrammer', 'Designatory']))

# Filter only queries done in 'StarkBotsChat'
@Stark.callback(filters=filters.chat('StarkBotsChat'))

# Filter only queries ending with the word 'baby'.
@Stark.callback(filters=filters.regex(r'baby$'))

# Filter all queries with the word 'hello' AND which are done in 'StarkBotsChat'.
@Stark.callback(filters=filters.chat('StarkBotsChat') & filters.regex('hello'))
# or
@Stark.callback('hello', filters=filters.chat('StarkBotsChat'))

# Filter all queries with the word 'bots' OR which are done in 'StarkBotsChat'
@Stark.callback(filters=filters.chat('StarkBotsChat') | filters.regex('hello'))

# Filter all queries with the word 'bots' BUT which are NOT done in 'StarkBotsChat'
@Stark.callback(filters=~filters.chat('StarkBotsChat') & filters.regex('hello'))
# or
@Stark.callback(filters=filters.regex('hello') & ~filters.chat('StarkBotsChat'))
Back to top