Decorators

Decorators are able to register callback functions for handling updates in a much easier and cleaner way compared to Handlers; they do so by instantiating the correct handler and calling add_handler() automatically. All you need to do is adding the decorators on top of your functions.

from pyrogram import Client

app = Client("my_account")


@app.on_message()
def log(client, message):
    print(message)


app.run()

Index


Details

@pyrogram.Client.on_message

Decorator for handling new messages.

This does the same thing as add_handler() using the MessageHandler.

Usable by Users Bots
Parameters:
  • filters (filters, optional) – Pass one or more filters to allow only a subset of messages to be passed in your function.

  • group (int, optional) – The group identifier, defaults to 0.

@pyrogram.Client.on_edited_message

Decorator for handling edited messages.

This does the same thing as add_handler() using the EditedMessageHandler.

Usable by Users Bots
Parameters:
  • filters (filters, optional) – Pass one or more filters to allow only a subset of messages to be passed in your function.

  • group (int, optional) – The group identifier, defaults to 0.

@pyrogram.Client.on_business_message

Decorator for handling new messages from business connection.

This does the same thing as add_handler() using the BusinessMessageHandler.

Usable by Users Bots
Parameters:
  • filters (filters, optional) – Pass one or more filters to allow only a subset of messages to be passed in your function.

  • group (int, optional) – The group identifier, defaults to 0.

@pyrogram.Client.on_edited_business_message

Decorator for handling edited messages from business connection.

This does the same thing as add_handler() using the EditedBusinessMessageHandler.

Usable by Users Bots
Parameters:
  • filters (filters, optional) – Pass one or more filters to allow only a subset of messages to be passed in your function.

  • group (int, optional) – The group identifier, defaults to 0.

@pyrogram.Client.on_deleted_messages

Decorator for handling deleted messages.

This does the same thing as add_handler() using the DeletedMessagesHandler.

Usable by Users Bots
Parameters:
  • filters (filters, optional) – Pass one or more filters to allow only a subset of messages to be passed in your function.

  • group (int, optional) – The group identifier, defaults to 0.

@pyrogram.Client.on_deleted_business_messages

Decorator for handling deleted messages from business connection.

This does the same thing as add_handler() using the DeletedBusinessMessagesHandler.

Usable by Users Bots
Parameters:
  • filters (filters, optional) – Pass one or more filters to allow only a subset of messages to be passed in your function.

  • group (int, optional) – The group identifier, defaults to 0.

@pyrogram.Client.on_message_reaction_count

Decorator for handling anonymous reaction changes on messages.

This does the same thing as add_handler() using the MessageReactionCountHandler.

Usable by Users Bots
Parameters:
  • filters (filters, optional) – Pass one or more filters to allow only a subset of updates to be passed in your function.

  • group (int, optional) – The group identifier, defaults to 0.

@pyrogram.Client.on_message_reaction

Decorator for handling reaction changes on messages.

This does the same thing as add_handler() using the MessageReactionHandler.

Usable by Users Bots
Parameters:
  • filters (filters, optional) – Pass one or more filters to allow only a subset of updates to be passed in your function.

  • group (int, optional) – The group identifier, defaults to 0.

@pyrogram.Client.on_business_connection

Decorator for handling changes in business connection.

This does the same thing as add_handler() using the BusinessConnectionHandler.

Usable by Users Bots
Parameters:
  • filters (filters, optional) – Pass one or more filters to allow only a subset of updates to be passed in your function.

  • group (int, optional) – The group identifier, defaults to 0.

@pyrogram.Client.on_story

Decorator for handling new stories.

This does the same thing as add_handler() using the StoryHandler.

Usable by Users Bots
Parameters:
  • filters (filters, optional) – Pass one or more filters to allow only a subset of stories to be passed in your function.

  • group (int, optional) – The group identifier, defaults to 0.

@pyrogram.Client.on_callback_query

Decorator for handling callback queries.

This does the same thing as add_handler() using the CallbackQueryHandler.

Usable by Users Bots
Parameters:
  • filters (filters, optional) – Pass one or more filters to allow only a subset of callback queries to be passed in your function.

  • group (int, optional) – The group identifier, defaults to 0.

@pyrogram.Client.on_chat_boost

Decorator for handling applied chat boosts.

This does the same thing as add_handler() using the ChatBoostHandler.

Usable by Users Bots
Parameters:
  • filters (filters, optional) – Pass one or more filters to allow only a subset of callback queries to be passed in your function.

  • group (int, optional) – The group identifier, defaults to 0.

@pyrogram.Client.on_chat_join_request

Decorator for handling chat join requests.

This does the same thing as add_handler() using the ChatJoinRequestHandler.

Usable by Users Bots
Parameters:
  • filters (filters, optional) – Pass one or more filters to allow only a subset of updates to be passed in your function.

  • group (int, optional) – The group identifier, defaults to 0.

@pyrogram.Client.on_chat_member_updated

Decorator for handling event changes on chat members.

This does the same thing as add_handler() using the ChatMemberUpdatedHandler.

Usable by Users Bots
Parameters:
  • filters (filters, optional) – Pass one or more filters to allow only a subset of updates to be passed in your function.

  • group (int, optional) – The group identifier, defaults to 0.

@pyrogram.Client.on_chosen_inline_result

Decorator for handling chosen inline results.

This does the same thing as add_handler() using the ChosenInlineResultHandler.

Usable by Users Bots
Parameters:
  • filters (filters, optional) – Pass one or more filters to allow only a subset of chosen inline results to be passed in your function.

  • group (int, optional) – The group identifier, defaults to 0.

@pyrogram.Client.on_inline_query

Decorator for handling inline queries.

This does the same thing as add_handler() using the InlineQueryHandler.

Usable by Users Bots
Parameters:
  • filters (filters, optional) – Pass one or more filters to allow only a subset of inline queries to be passed in your function.

  • group (int, optional) – The group identifier, defaults to 0.

@pyrogram.Client.on_poll

Decorator for handling poll updates.

This does the same thing as add_handler() using the PollHandler.

Usable by Users Bots
Parameters:
  • filters (filters, optional) – Pass one or more filters to allow only a subset of polls to be passed in your function.

  • group (int, optional) – The group identifier, defaults to 0.

@pyrogram.Client.on_pre_checkout_query

Decorator for handling pre-checkout queries.

This does the same thing as add_handler() using the PreCheckoutQueryHandler.

Usable by Users Bots
Parameters:
  • filters (filters, optional) – Pass one or more filters to allow only a subset of callback queries to be passed in your function.

  • group (int, optional) – The group identifier, defaults to 0.

@pyrogram.Client.on_purchased_paid_media

Decorator for handling purchased paid media.

This does the same thing as add_handler() using the PurchasedPaidMediaHandler.

Usable by Users Bots
Parameters:
  • filters (filters, optional) – Pass one or more filters to allow only a subset of updates to be passed in your function.

  • group (int, optional) – The group identifier, defaults to 0.

@pyrogram.Client.on_shipping_query

Decorator for handling shipping queries.

This does the same thing as add_handler() using the ShippingQueryHandler.

Usable by Users Bots
Parameters:
  • filters (filters, optional) – Pass one or more filters to allow only a subset of callback queries to be passed in your function.

  • group (int, optional) – The group identifier, defaults to 0.

@pyrogram.Client.on_user_status

Decorator for handling user status updates. This does the same thing as add_handler() using the UserStatusHandler.

Usable by Users Bots
Parameters:
  • filters (filters, optional) – Pass one or more filters to allow only a subset of UserStatus updated to be passed in your function.

  • group (int, optional) – The group identifier, defaults to 0.

@pyrogram.Client.on_start

Decorator for handling client start.

This does the same thing as add_handler() using the StartHandler.

Usable by Users Bots
@pyrogram.Client.on_stop

Decorator for handling client stop.

This does the same thing as add_handler() using the StopHandler.

Usable by Users Bots
@pyrogram.Client.on_connect

Decorator for handling connections.

This does the same thing as add_handler() using the ConnectHandler.

Usable by Users Bots
@pyrogram.Client.on_disconnect

Decorator for handling disconnections.

This does the same thing as add_handler() using the DisconnectHandler.

Usable by Users Bots
@pyrogram.Client.on_error

Decorator for handling unexpected errors.

This does the same thing as add_handler() using the ErrorHandler.

Usable by Users Bots
Parameters:
  • exceptions (Exception | List of Exception, optional) – An exception type or a sequence of exception types that this handler should handle. If None, the handler will catch any exception that is a subclass of Exception.

  • filters (filters, optional) – Pass one or more filters to allow only a subset of messages to be passed in your function.

  • group (int, optional) – The group identifier, defaults to 0.

@pyrogram.Client.on_managed_bot

Decorator for handling new managed bot creation updates.

This does the same thing as add_handler() using the ManagedBotUpdatedHandler.

Usable by Users Bots
Parameters:
  • filters (filters, optional) – Pass one or more filters to allow only a subset of updates to be passed in your function.

  • group (int, optional) – The group identifier, defaults to 0.

@pyrogram.Client.on_raw_update

Decorator for handling raw updates.

This does the same thing as add_handler() using the RawUpdateHandler.

Usable by Users Bots
Parameters:
  • filters (filters, optional) – Pass one or more filters to allow only a subset of updates to be passed in your function.

  • group (int, optional) – The group identifier, defaults to 0.