English فارسی

Events & dispatcher

Two decorator styles on purpose — Telethon's @client.on_event(NewMessage) and Pyrogram's @client.on_message(filters.command("start")). Handlers may be sync or async.

The three decorators

on_message(filter=None)

Register a handler for NewMessage. The callback receives a Message.

async on_event(*event_classes)

Register a handler for one or more event classes, such as Reaction or ChatAction.

on_update(handler)

Raw ws_update frames as dicts — no decoding, maximum fidelity.

from balexyz import Client, NewMessage, filters

client = Client("256049633:eyJ…")


@client.on_message()
async def everything(message):
    print(message.text)


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


@client.on_message(filters.command("ping"))
async def ping(message):
    await message.reply("pong")


@client.on_message(filters.regex(r"^order #(\d+)$"))
async def order(message):
    print("order id:", message.matches[1])


@client.on_update
def raw(update: dict):
    print("raw frame", update.get("update"))


await client.run_forever()

Filters

FilterMatches
filters.command("start")/start and /start@bot
filters.regex(pattern)Regex on message.text; groups in message.matches
filters.textHas non-empty text
filters.private / group / channelChat type
filters.incoming / outgoingDirection
filters.photo / document / voice …Media type
filters.chat(id) / from_user(id)Scoping
filters.allEverything (default)
f1 & f2, f1 | f2, ~f1And, or, not
from balexyz import filters

@client.on_message(filters.private & filters.text)
async def dm(message): ...

@client.on_message(filters.chat("987654321|2") & filters.command("deploy"))
async def deploy(message): ...

@client.on_message(filters.photo | filters.video,)
async def media(message): ...

@client.on_message(~filters.outgoing)
async def only_incoming(message): ...

Event types

ClassFires whenUseful members
NewMessageA message arrives or is senttext, reply(), delete(), react(), get_fsm_context()
MessageEditedContent changesmessage
MessageSentDelivery confirmation for your own sendraw
MessageDeletedA message is removedraw
MessageReadRead receiptsis_inbox
ReactionSomeone reactsraw
ChatActionJoin / leave / kick / title changeuser_joined(), user_left(), user_kicked(), reply()
CallbackQueryInline button presseddata_str, answer(), edit()
UserTypingTyping stateis_typing
UserUpdateOnline / offline / profile changeis_online, is_offline
JoinRequestSomeone asks to join a groupapprove(), decline()
RawAnything the library does not model yetkind, data
from balexyz.events import CallbackQuery, ChatAction

@client.on_event(CallbackQuery)
async def button(event):
    if event.data_str == "confirm":
        await event.answer("done")
        await event.edit("confirmed ✅")

@client.on_event(ChatAction)
async def membership(event):
    if event.user_joined():
        await event.reply("welcome!")

Pulling instead of pushing

Useful when you want a manual loop:

# raw frames
frame = await client.next_update(timeout=10)

async for frame in client.iter_updates():
    print(frame)

# typed events
event = await client.next_event(timeout=10)
async for event in client.iter_events():
    print(type(event).__name__)

Running

# await a single coroutine after connecting
async def work(client):
    await client.send_message("me", "started")

await client.run(work)

# or block like Telethon
client.run_forever()
Handler errors are isolated.

If a handler raises, the exception is logged through logging.getLogger("balexyz.client") and handed to the Dispatcher's error handlers, which are called as (error, client). With no error handler registered the exception propagates out of the update task. Add one to alert on it:

client.dispatcher.add_error_handler(lambda error, client: print("failed:", error))
bale xyz 1.0.0 · MIT licensed · unofficial client © 2026 bale xyz contributors