Message.click()

Message.click()

Bound method click of Message.

Use as a shortcut for clicking a button attached to the message instead of:

  • Clicking inline buttons:

await client.request_callback_answer(
    chat_id=message.chat.id,
    message_id=message.id,
    callback_data=message.reply_markup[i][j].callback_data
)
  • Clicking normal buttons:

await client.send_message(
    chat_id=message.chat.id,
    text=message.reply_markup[i][j].text
)

Example

This method can be used in three different ways:

  1. Pass one integer argument only (e.g.: .click(2), to click a button at index 2). Buttons are counted left to right, starting from the top.

  2. Pass two integer arguments (e.g.: .click(1, 0), to click a button at position (1, 0)). The origin (0, 0) is top-left.

  3. Pass one string argument only (e.g.: .click("Settings"), to click a button by using its label). Only the first matching button will be pressed.

Parameters:
  • x (int | str) – Used as integer index, integer abscissa (in pair with y) or as string label. Defaults to 0 (first button).

  • y (int, optional) – Used as ordinate only (in pair with x).

  • quote (bool, optional) – Useful for normal buttons only, where pressing it will result in a new message sent. If True, the message will be sent as a reply to this message. Defaults to True in group chats and False in private chats.

  • timeout (int, optional) – Timeout in seconds.

  • request_write_access (bool, optional) – Only used in case of LoginUrl button. True, if the bot can send messages to the user. Defaults to True.

  • password (str, optional) – When clicking certain buttons (such as BotFather’s confirmation button to transfer ownership), if your account has 2FA enabled, you need to provide your account’s password. The 2-step verification password for the current user. Only applicable, if the InlineKeyboardButton contains requires_password.

Returns:

  • The result of request_callback_answer() in case of inline callback button clicks.

  • The result of reply() in case of normal button clicks.

  • A string in case the inline button is a URL, a switch_inline_query or a switch_inline_query_current_chat button.

  • A string URL with the user details, in case of a WebApp button.

  • A Chat object in case of a KeyboardButtonUserProfile button.

Raises:
  • RPCError – In case of a Telegram RPC error.

  • ValueError – In case the provided index or position is out of range or the button label was not found.

  • TimeoutError – In case, after clicking an inline button, the bot fails to answer within the timeout.