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.

static Callback.callback(query: Optional[Union[str, list[str]]] = None, startswith: bool = False, owner_only: bool = False, group: int = 0, filters=None)

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

Parameters:
query (str | List of str, optional) -

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

startswith (bool, optional) -

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

owner_only (bool, optional) -

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

group (int, optional) -

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

filters (optional) -

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

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)

# 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'))