<metaproperty="description"content="Users can interact with your bot via buttons or even inline buttons, straight from inline messages in any chat. ">
<metaproperty="og:title"content="Buttons">
<metaproperty="og:image"content="">
<metaproperty="og:description"content="Users can interact with your bot via buttons or even inline buttons, straight from inline messages in any chat. ">
<p>Users can interact with your bot via <strong>buttons</strong> or even <strong>inline buttons</strong>, straight from inline <strong>messages</strong> in <strong>any</strong> chat.<br>
This article describes the full button flow, using the MTProto API. </p>
<p>Bots can attach a <ahref="/type/ReplyMarkup">ReplyMarkup</a> constructor to outgoing messages, to attach an <ahref="/bots/features#inline-keyboards">inline keyboard</a> or a <ahref="/bots/features#keyboards">custom reply keyboard</a>:</p>
<li><ahref="/constructor/replyKeyboardMarkup">replyKeyboardMarkup</a> - Sends a <ahref="/bots/features#keyboards">custom reply keyboard</a>. <br> User clients receiving such a constructor should display a <ahref="/bots/features#keyboards">special keyboard</a> with custom reply options.</li>
<li><ahref="/constructor/replyKeyboardHide">replyKeyboardHide</a> - Hides the <ahref="/bots/features#keyboards">custom reply keyboard</a>. <br> User clients receiving this constructor should hide the <ahref="/bots/features#keyboards">custom reply keyboard</a> opened by <ahref="/constructor/replyKeyboardMarkup">replyKeyboardMarkup</a></li>
<li><ahref="/constructor/replyKeyboardForceReply">replyKeyboardForceReply</a> - Sends a <ahref="/bots/api#forcereply">force reply</a> constructor <br> User clients receiving a message with this constructor should act as if the user had clicked on the reply button of the message, displaying the reply UI. </li>
<li><ahref="/constructor/replyInlineMarkup">replyInlineMarkup</a> - Attaches an <ahref="/bots/features#inline-keyboards">inline keyboard</a> to the message, allowing users to send callback data to the bot without sending actual messages to the current chat.</li>
<p>Both <ahref="/bots/features#keyboards">reply</a> and <ahref="/bots/features#inline-keyboards">inline</a> keyboards are composed of a vector of <ahref="/constructor/keyboardButtonRow">rows</a>, each row containing a vector of <ahref="/type/KeyboardButton">buttons</a>, for each column.<br>
<li><ahref="/constructor/keyboardButton">keyboardButton</a> - Send a message to the chat, replying to the message that attached the <ahref="/bots/features#keyboards">reply keyboard</a></li>
<li><ahref="/constructor/keyboardButtonRequestPhone">keyboardButtonRequestPhone</a> - Only in private chats, send the current user's contact to the chat, replying to the message that attached the <ahref="/bots/features#keyboards">reply keyboard</a></li>
<li><ahref="/constructor/keyboardButtonRequestGeoLocation">keyboardButtonRequestGeoLocation</a> - Only in private chats, send the current user's geolocation to the chat, replying to the message that attached the <ahref="/bots/features#keyboards">reply keyboard</a></li>
<li><ahref="/constructor/keyboardButtonRequestPoll">keyboardButtonRequestPoll</a> - Only in private chats, prompt the user to create and send a <ahref="/api/poll">poll</a> (or a quiz poll, depending on the <code>quiz</code> flag), replying to the message that attached the <ahref="/bots/features#keyboards">reply keyboard</a></li>
<p>Buttons available only in inline keyboards:</p>
<ul>
<li><ahref="/constructor/keyboardButtonUrl">keyboardButtonUrl</a> - Open the URL, showing a "Do you want to open this URL?" prompt (unless the URL is one of the <ahref="https://github.com/DrKLO/Telegram/blob/002c01ecd37cd08ed07b3ed84d79318d091dfc85/TMessagesProj/src/main/java/org/telegram/messenger/browser/Browser.java#L351">internal URIs</a>, in which case the URL should be opened right away)</li>
<li><ahref="/constructor/keyboardButtonCallback">keyboardButtonCallback</a> - Send the callback data to the bot, optionally providing the user's 2FA SRP payload for identity verification, see <ahref="#callback-queries">here for more info »</a></li>
<li>If <code>keyboardButtonSwitchInline.same_peer</code> is set, insert the bot's username and <code>keyboardButtonSwitchInline.query</code> in the current chat's input field, triggering an <ahref="/api/bots/inline">inline query</a>.</li>
<li>If <code>keyboardButtonSwitchInline.same_peer</code> is not set, prompt the user to select one of their chats, and then insert the bot's username and <code>keyboardButtonSwitchInline.query</code> in the current chat's input field, triggering an <ahref="/api/bots/inline">inline query</a>.</li>
</ul>
</li>
<li><ahref="/constructor/keyboardButtonGame">keyboardButtonGame</a> - Open the <ahref="/constructor/game">game</a> from the attached <ahref="/constructor/messageMediaGame">messageMediaGame</a> constructor, for more info <ahref="/api/bots/games">see here »</a></li>
<li><ahref="/constructor/keyboardButtonBuy">keyboardButtonBuy</a> - Proceed to initiating the payment flow, for more info <ahref="/api/payments">see here »</a></li>
<li><ahref="/constructor/keyboardButtonUrlAuth">keyboardButtonUrlAuth</a> - Log into a website using the user's Telegram account, as specified <ahref="/api/url-authorization">here »</a></li>
<p><ahref="/constructor/keyboardButtonCallback">keyboardButtonCallback</a> buttons can be used to send the specified <code>data</code> payload back to the bot, when they are clicked.<br>
Additionally, a bot can verify a user's identity by requiring they verify their 2FA password with <ahref="/api/srp">SRP</a>. </p>
<h4><aclass="anchor"href="#sending-a-callback-query"id="sending-a-callback-query"name="sending-a-callback-query"><iclass="anchor-icon"></i></a>Sending a callback query</h4>
<p>When the user clicks on a <ahref="/constructor/keyboardButtonCallback">keyboardButtonCallback</a> in a message sent by a bot, or generated by an <ahref="/api/bots/inline">inline query</a>, <ahref="/method/messages.getBotCallbackAnswer">messages.getBotCallbackAnswer</a> should be called, passing the peer and ID of the message.<br>
The same should happen when clicking on <ahref="/constructor/keyboardButtonGame">keyboardButtonGame</a> buttons, with the difference that the <code>game</code> flag must be set instead of the <code>data</code> parameter. </p>
<p>Make sure to properly handle bot timeouts in the form of <code>BOT_RESPONSE_TIMEOUT</code> RPC errors, as the bot may be offline and unable to reply. </p>
<p>The returned <ahref="/constructor/messages.botCallbackAnswer">messages.botCallbackAnswer</a> constructor contains:</p>
<ul>
<li><code>message</code> if specified, a message that should be shown in a non-blocking toast notification</li>
<li><code>alert</code> indicates whether the <code>message</code> should be shown as a dismissible prompt, instead of a simple toast notification</li>
<li><code>has_url</code> Whether an URL is present</li>
<li><code>url</code> if specified, the client should open the URL, without showing a confirmation prompt. <br> This is safe and allowed, because here bots can only return: <ul>
<li><ahref="/api/links#bot-links">Deep links to themselves »</a></li>
<li><ahref="/api/links#game-links">Deep links to a valid game they own »</a>, if the bot has manually configured games, and the clicked button was a <ahref="/constructor/keyboardButtonGame">keyboardButtonGame</a>. </li>
<p>If the <code>requires_password</code> flag is set, the <ahref="/api/srp">SRP 2FA payload</a> must also be generated and attached to the query, to verify the identity of the user. </p>
<p><strong>Note that the bot will NOT be able to access your password or the SRP payload</strong>. </p>
<p>The SRP payload will be processed exclusively on the Telegram's servers, simply returning an RPC error without passing the query to the bot if the verification fails.<br>
This is just a way of verifying the identity of the user, mainly used by the official <ahref="https://t.me/botfather">@botfather</a> bot to allow securely transferring the ownership of a bot to another user.</p>
<h4><aclass="anchor"href="#answering-a-callback-query"id="answering-a-callback-query"name="answering-a-callback-query"><iclass="anchor-icon"></i></a>Answering a callback query</h4>
<p>After the user invokes <ahref="/method/messages.getBotCallbackAnswer">messages.getBotCallbackAnswer</a>, an <ahref="/constructor/updateBotCallbackQuery">updateBotCallbackQuery</a> or <ahref="/constructor/updateInlineBotCallbackQuery">updateInlineBotCallbackQuery</a> is generated and sent to the bot, depending on whether the query originated from a normal message sent by the bot, or from a message sent from an <ahref="/api/bots/inline">inline query</a>. </p>
<p>Either way, bots must reply to the query as quickly as possible using <ahref="/method/messages.setBotCallbackAnswer">messages.setBotCallbackAnswer</a>: </p>
<ul>
<li><code>query_id</code> is the <code>query_id</code> from <ahref="/method/messages.getBotCallbackAnswer">messages.getBotCallbackAnswer</a>, an <ahref="/constructor/updateBotCallbackQuery">updateBotCallbackQuery</a> or <ahref="/constructor/updateInlineBotCallbackQuery">updateInlineBotCallbackQuery</a></li>
<li><code>message</code>, <code>alert</code>, <code>url</code> can contain messages and URLs to trigger different client behaviour, as <ahref="#sending-a-callback-query">specified above »</a></li>
<li><code>cache_time</code> indicates the maximum amount of time in seconds that the result of the callback query may be cached by the <strong>client</strong>. </li>
</ul>
<p>If a <code>game_short_name</code> is present in the update, the bot should return the URL of the game with the specified name.<br>
The <ahref="/method/messages.setBotCallbackAnswer">messages.setBotCallbackAnswer</a> method must be called anyway, even if no <code>message</code> or <code>url</code> is returned, to avoid timeouts on the client.</p></div>