<metaproperty="description"content="Telegram bots can completely replace any website. They support seamless authorization, integrated payments via 15 payment…">
<metaproperty="og:title"content="Web Apps for Bots">
<metaproperty="og:description"content="Telegram bots can completely replace any website. They support seamless authorization, integrated payments via 15 payment…">
<p>Telegram bots can completely replace <strong>any website</strong>. They support <ahref="https://telegram.org/blog/privacy-discussions-web-bots#meet-seamless-web-bots">seamless authorization</a>, <ahref="https://core.telegram.org/bots/payments">integrated payments</a> via <strong>15</strong> payment providers (with <em>Google Pay</em> and <em>Apple Pay</em> out of the box), delivering tailored push notifications to users, and <ahref="https://core.telegram.org/bots">much more</a>.</p>
<p>With <strong>Web Apps</strong>, bots get a whole new dimension. Bot developers can create <strong>infinitely flexible interfaces</strong> with <em>JavaScript</em>, the most widely used programming language in the world.</p>
<divclass="blog_video_player_wrap"style="max-width: 600px; margin: 20px auto 20px;">
<videoclass="blog_video_player tl_blog_vid_autoplay"onclick="videoTogglePlay(this)"autoplayloopcontrolsmutedposter="/file/464001434/100bf/eWprjdgzEbE.100386/644bbea83084f44c8f"style="max-width: 600px;"title="console.log('Vive la révolution')"alt="Bot Revolution">
<li>Added the field <em>platform</em>, the optional parameter <em>options</em> to the method <em>openLink</em> and the methods <em>showScanQrPopup</em>, <em>closeScanQrPopup</em>, <em>readTextFromClipboard</em> to the class <ahref="#initializing-web-apps">WebApp</a>.</li>
<li>Added the events <em>qrTextReceived</em>, <em>clipboardTextReceived</em>.</li>
<li>Added the field <em>isClosingConfirmationEnabled</em> and the methods <em>enableClosingConfirmation</em>, <em>disableClosingConfirmation</em>, <em>showPopup</em>, <em>showAlert</em>, <em>showConfirm</em> to the class <ahref="#initializing-web-apps">WebApp</a>.</li>
<li>Added the field <em>is_premium</em> to the class <ahref="#webappuser">WebAppUser</a>.</li>
<li>Added the ability to use bots added to the attachment menu in group, supergroup and channel chats.</li>
<li>Added support for <ahref="#adding-bots-to-the-attachment-menu">t.me links</a> that can be used to select the chat in which the attachment menu with the bot will be opened.</li>
<li>Added the fields <em>version</em>, <em>headerColor</em>, <em>backgroundColor</em>, <em>BackButton</em>, <em>HapticFeedback</em> and the methods <em>isVersionAtLeast</em>, <em>setHeaderColor</em>, <em>setBackgroundColor</em>, <em>openLink</em>, <em>openTelegramLink</em>, <em>openInvoice</em> to the class <ahref="#initializing-web-apps">WebApp</a>.</li>
<li>Added the field <em>secondary_bg_color</em> to the class <ahref="#themeparams">ThemeParams</a>.</li>
<li>Added the method <em>offClick</em> to the class <ahref="#mainbutton">MainButton</a>.</li>
<li>Added the fields <em>chat</em>, <em>can_send_after</em> to the class <ahref="#webappinitdata">WebAppInitData</a>.</li>
<li>Added the <ahref="#events-available-for-web-apps">events</a><em>backButtonClicked</em>, <em>settingsButtonClicked</em>, <em>invoiceClosed</em>.</li>
<p>Web Apps always receive data about the user's current <strong>color theme</strong> in real time, so you can adjust the appearance of your interfaces to match it. For example, when users switch between <strong>Day and Night</strong> modes or use various <ahref="https://telegram.org/blog/protected-content-delete-by-date-and-more#global-chat-themes-on-android">custom themes</a>.</p>
<divclass="blog_video_player_wrap"style="max-width: 600px; margin: 20px auto 20px;">
<p>Telegram apps are known for being snappy, smooth and following a consistent cross-platform design. Your Web App should ideally reflect these principles.</p>
<ul>
<li>All elements should be responsive and designed with a mobile-first approach.</li>
<li>Interactive elements should mimic the style, behavior and intent of UI components that already exist.</li>
<li>All included animations should be smooth, ideally 60fps.</li>
<li>All inputs and images should contain labels for accessibility purposes.</li>
<li>The app should deliver a seamless experience by monitoring the <ahref="#color-schemes">dynamic theme-based colors</a> provided by the API and using them accordingly.</li>
</ul>
<hr>
<h3><aclass="anchor"name="implementing-web-apps"href="#implementing-web-apps"><iclass="anchor-icon"></i></a>Implementing Web Apps</h3>
<p>Telegram currently supports four different ways of launching Web Apps: from a <ahref="#keyboard-button-web-apps">keyboard button</a>, from an <ahref="#inline-button-web-apps">inline button</a>, from the <ahref="#launching-web-apps-from-the-menu-button">bot menu button</a>– and even from the <ahref="#launching-web-apps-from-the-attachment-menu">attachment menu</a>.</p>
<ahref="/file/464001702/1194c/mBQhTbIWqw8.451557/08727d5d763e24d750"target="_blank"><imgsrc="/file/464001702/1194c/mBQhTbIWqw8.451557/08727d5d763e24d750"title=""alt="Types of buttons"srcset="/file/464001702/1194c/mBQhTbIWqw8.451557/08727d5d763e24d750 , 2x"/></a>
</div>
<h4><aclass="anchor"name="keyboard-button-web-apps"href="#keyboard-button-web-apps"><iclass="anchor-icon"></i></a>Keyboard Button Web Apps</h4>
<p><strong>TL;DR:</strong> Web Apps launched from a <strong>web_app</strong> type <ahref="/bots/api#keyboardbutton">keyboard button</a> can send data back to the bot in a <em>service message</em> using <ahref="#initializing-web-apps">Telegram.WebApp.sendData</a>. This makes it possible for the bot to produce a response without communicating with any external servers.</p>
<p>Users can interact with bots using <ahref="https://core.telegram.org/bots#keyboards">custom keyboards</a>, <ahref="https://core.telegram.org/bots#inline-keyboards-and-on-the-fly-updating">buttons under bot messages</a>, as well as by sending freeform <strong>text messages</strong> or any of the <strong>attachment types</strong> supported by Telegram: photos and videos, files, locations, contacts and polls. For even more flexibility, bots can utilize the full power of <strong>HTML5</strong> to create user-friendly input interfaces.</p>
<p>You can send a <strong>web_app</strong> type <ahref="/bots/api#keyboardbutton">KeyboardButton</a> that opens a Web App from the specified URL.</p>
<p>To transmit data from the user back to the bot, the Web App can call the <ahref="#initializing-web-apps">Telegram.WebApp.sendData</a> method. Data will be transmitted to the bot as a String in a service message. The bot can continue communicating with the user after receiving it.</p>
<li><strong>Сustom data input interfaces</strong> (a personalized calendar for selecting dates; selecting data from a list with advanced search options; a randomizer that lets the user “spin a wheel” and chooses one of the available options, etc.)</li>
<li><strong>Reusable components</strong> that do not depend on a particular bot.</li>
</ul>
<h4><aclass="anchor"name="inline-button-web-apps"href="#inline-button-web-apps"><iclass="anchor-icon"></i></a>Inline Button Web Apps</h4>
<blockquote>
<p><strong>TL;DR:</strong> For more interactive Web Apps like <ahref="https://t.me/durgerkingbot">@DurgerKingBot</a>, use a <strong>web_app</strong> type <ahref="/bots/api#inlinekeyboardbutton">Inline KeyboardButton</a>, which gets basic user information and can be used to send a message on behalf of the user to the chat with the bot.</p>
</blockquote>
<p>If receiving text data alone is insufficient or you need a more advanced and personalized interface, you can open a Web App using a <strong>web_app</strong> type <ahref="/bots/api#inlinekeyboardbutton">Inline KeyboardButton</a>.</p>
<p>From the button, a Web App will open with the URL specified in the button. In addition to the user's <ahref="#color-schemes">theme settings</a>, it will receive basic user information (<code>ID</code>, <code>name</code>, <code>username</code>, <code>language_code</code>) and a unique identifier for the session, <strong>query_id</strong>, which allows messages on behalf of the user to be sent back to the bot.</p>
<p>The bot can call the Bot API method <ahref="/bots/api#answerwebappquery">answerWebAppQuery</a> to send an inline message from the user back to the bot and close the Web App. After receiving the message, the bot can continue communicating with the user.</p>
<p><strong>Good for:</strong></p>
<ul>
<li>Fully-fledged web services and integrations of any kind.</li>
<li>The use cases are effectively <strong>unlimited</strong>.</li>
</ul>
<h4><aclass="anchor"name="launching-web-apps-from-the-menu-button"href="#launching-web-apps-from-the-menu-button"><iclass="anchor-icon"></i></a>Launching Web Apps from the Menu Button</h4>
<blockquote>
<p><strong>TL;DR:</strong> Web Apps can be launched from a customized menu button. This simply offers a quicker way to access the app and is otherwise <strong>identical</strong> to <ahref="#inline-button-web-apps">launching a Web App from an inline button</a>.</p>
<p>By default, chats with bots always show a convenient <strong>menu button</strong> that provides quick access to all listed <ahref="/bots#commands">commands</a>. With <ahref="https://core.telegram.org/bots/api-changelog#april-16-2022">Bot API 6.0</a>, this button can be used to <strong>launch a Web App</strong> instead.</p>
<p>To configure the menu button, you must specify the text it should show and the Web App URL. There are two ways to set these parameters:</p>
<ul>
<li>To customize the button for <strong>all users</strong>, use <ahref="https://t.me/botfather">@BotFather</a> (the <code>/setmenubutton</code> command or <em>Bot Settings > Menu Button</em>).</li>
<li>To customize the button for both <strong>all users</strong> and <strong>specific users</strong>, use the <ahref="/bots/api#setchatmenubutton">setChatMenuButton</a> method in the Bot API. For example, change the button text according to the user's language, or show links to different Web Apps based on a user's settings in your bot.</li>
</ul>
<p>Apart from this, Web Apps opened via the menu button work in the exact same way as when <ahref="#inline-button-web-apps">using inline buttons</a>.</p>
<blockquote>
<p><ahref="https://t.me/durgerkingbot">@DurgerKingBot</a> allows launching its Web App both from an inline button and from the menu button.</p>
</blockquote>
<h4><aclass="anchor"name="launching-web-apps-from-the-attachment-menu"href="#launching-web-apps-from-the-attachment-menu"><iclass="anchor-icon"></i></a>Launching Web Apps from the Attachment Menu</h4>
<p><strong>TL;DR:</strong> Web App Bots can request to be added directly to a user's attachment menu, allowing them to be quickly launched from any chat. To try this mode, open this <ahref="https://t.me/durgerkingbot?startattach">attachment menu link</a> for <em>@DurgerKingBot</em>, then use the <imgclass="icon"src="/file/464001085/2/E4hNXSNQimQ.2503/bf6ffcab3cb3afd43d"alt="Attach"> menu in <strong>any type of chat</strong>.</p>
<p>Web App Bots can request to be added directly to a user's attachment menu, allowing them to be quickly launched from <strong>any type of chat</strong>. You can configure in which types of chats your web app can be started from the attachment menu (private, groups, supergroups or channels).</p>
<p>Attachment menu integration is currently only available for major advertisers on the <ahref="https://promote.telegram.org/basics">Telegram Ad Platform</a>. However, <strong>all bots</strong> can use it in the <ahref="#using-bots-in-the-test-environment">test server environment</a>.</p>
<p>To enable this feature for your bot, open <ahref="https://t.me/botfather">@BotFather</a><ahref="#using-bots-in-the-test-environment">from an account on the test server</a> and send the <code>/setattach</code> command – or go to <em>Bot Settings > Configure Attachment Menu</em>. Then specify the URL that will be opened to launch the bot's Web App via its icon in the attachment menu.</p>
<p>You can add a 'Settings' item to the context menu of your Web App using <ahref="https://t.me/botfather">@BotFather</a>. When users select this option from the menu, your bot will receive a <code>settingsButtonClicked</code> event.</p>
<p>In addition to the user's <ahref="#color-schemes">theme settings</a>, the bot will receive basic user information (<code>ID</code>, <code>name</code>, <code>username</code>, <code>language_code</code>, <code>photo</code>), as well as public info about the chat partner (<code>ID</code>, <code>name</code>, <code>username</code>, <code>photo</code>) or the chat info (<code>ID</code>, <code>type</code>, <code>title</code>, <code>username</code>, <code>photo</code>) and a unique identifier for the web view session <strong>query_id</strong>, which allows messages of any type to be sent to the chat on behalf of the user that opened the bot.</p>
<p>The bot can call the Bot API method <ahref="/bots/api#answerwebappquery">answerWebAppQuery</a>, which sends an inline message from the user via the bot to the chat where it was launched and closes the Web App.</p>
<p>You can read more about adding bots to the attachment menu <ahref="#adding-bots-to-the-attachment-menu">here</a>.</p>
</blockquote>
<hr>
<h3><aclass="anchor"name="initializing-web-apps"href="#initializing-web-apps"><iclass="anchor-icon"></i></a>Initializing Web Apps</h3>
<p>To connect your Web App to the Telegram client, place the script <ahref="https://telegram.org/js/telegram-web-app.js">telegram-web-app.js</a> in the <code><head></code> tag before any other scripts, using this code:</p>
<p>Once the script is connected, a <code>window.Telegram.WebApp</code> object will become available with the following fields:</p>
<tableclass="table">
<thead>
<tr>
<th>Field</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>initData</td>
<td>String</td>
<td>A string with raw data transferred to the Web App, convenient for <ahref="#validating-data-received-via-the-web-app">validating data</a>.<br><strong>WARNING:</strong><ahref="#validating-data-received-via-the-web-app">Validate data</a> from this field before using it on the bot's server.</td>
<td>An object with input data transferred to the Web App.<br><strong>WARNING:</strong> Data from this field should not be trusted. You should only use data from <em>initData</em> on the bot's server and only after it has been <ahref="#validating-data-received-via-the-web-app">validated</a>.</td>
<td>The color scheme currently used in the Telegram app. Either “light” or “dark”.<br>Also available as the CSS variable <code>var(--tg-color-scheme)</code>.</td>
</tr>
<tr>
<td>themeParams</td>
<td><ahref="#themeparams">ThemeParams</a></td>
<td>An object containing the current theme settings used in the Telegram app.</td>
<td><em>True</em>, if the Web App is expanded to the maximum available height. False, if the Web App occupies part of the screen and can be expanded to the full height using the <strong>expand()</strong> method.</td>
<td>The current height of the visible area of the Web App. Also available in CSS as the variable <code>var(--tg-viewport-height)</code>.<br><br>The application can display just the top part of the Web App, with its lower part remaining outside the screen area. From this position, the user can “pull” the Web App to its maximum height, while the bot can do the same by calling the <strong>expand()</strong> method. As the position of the Web App changes, the current height value of the visible area will be updated in real time.<br><br>Please note that the refresh rate of this value is not sufficient to smoothly follow the lower border of the window. It should not be used to pin interface elements to the bottom of the visible area. It's more appropriate to use the value of the <code>viewportStableHeight</code> field for this purpose.</td>
</tr>
<tr>
<td>viewportStableHeight</td>
<td>Float</td>
<td>The height of the visible area of the Web App in its last stable state. Also available in CSS as a variable <code>var(--tg-viewport-stable-height)</code>.<br><br>The application can display just the top part of the Web App, with its lower part remaining outside the screen area. From this position, the user can “pull” the Web App to its maximum height, while the bot can do the same by calling the <strong>expand()</strong> method. Unlike the value of <code>viewportHeight</code>, the value of <code>viewportStableHeight</code> does not change as the position of the Web App changes with user gestures or during animations. The value of <code>viewportStableHeight</code> will be updated after all gestures and animations are completed and the Web App reaches its final size.<br><br><em>Note the <ahref="#events-available-for-web-apps">event</a><code>viewportChanged</code> with the passed parameter <code>isStateStable=true</code>, which will allow you to track when the stable state of the height of the visible area changes.</em></td>
<td><em>True</em>, if the confirmation dialog is enabled while the user is trying to close the Web App. <em>False</em>, if the confirmation dialog is disabled.</td>
<td><mark>Bot API 6.1+</mark> A method that sets the app header color. You can only pass <em>Telegram.WebApp.themeParams.bg_color</em> or <em>Telegram.WebApp.themeParams.secondary_bg_color</em> as a color or you can use keywords <em>bg_color</em>, <em>secondary_bg_color</em> instead.</td>
<td><mark>Bot API 6.1+</mark> A method that sets the app background color in the <code>#RRGGBB</code> format or you can use keywords <em>bg_color</em>, <em>secondary_bg_color</em> instead.</td>
<td>A method that sets the app event handler. Check <ahref="#events-available-for-web-apps">the list of available events</a>.</td>
</tr>
<tr>
<td>offEvent(eventType, eventHandler)</td>
<td>Function</td>
<td>A method that deletes a previously set event handler.</td>
</tr>
<tr>
<td>sendData(data)</td>
<td>Function</td>
<td>A method used to send data to the bot. When this method is called, a service message is sent to the bot containing the data <em>data</em> of the length up to 4096 bytes, and the Web App is closed. See the field <em>web_app_data</em> in the class <ahref="/bots/api#message">Message</a>.<br><br><em>This method is only available for Web Apps launched via a <ahref="#keyboard-button-web-apps">Keyboard button</a>.</em></td>
<td>A method that opens a link in an external browser. The Web App will <em>not</em> be closed.<br><mark>Bot API 6.4+</mark> If the optional <em>options</em> parameter is passed with the field <em>try_instant_view=true</em>, the link will be opened in <ahref="https://instantview.telegram.org/">Instant View</a> mode if possible.<br><br><em>Note that this method can be called only in response to user interaction with the Web App interface (e.g. a click inside the Web App or on the main button)</em></td>
<td><mark>Bot API 6.1+</mark> A method that opens an invoice using the link <em>url</em>. The Web App will receive the <ahref="#events-available-for-web-apps">event</a><em>invoiceClosed</em> when the invoice is closed. If an optional <em>callback</em> parameter was passed, the <em>callback</em> function will be called and the invoice status will be passed as the first argument.</td>
<td><mark>Bot API 6.2+</mark> A method that shows a native popup described by the <em>params</em> argument of the type <ahref="#popupparams">PopupParams</a>. The Web App will receive the <ahref="#events-available-for-web-apps">event</a><em>popupClosed</em> when the popup is closed. If an optional <em>callback</em> parameter was passed, the <em>callback</em> function will be called and the field <em>id</em> of the pressed button will be passed as the first argument.</td>
<td><mark>Bot API 6.2+</mark> A method that shows <em>message</em> in a simple alert with a 'Close' button. If an optional <em>callback</em> parameter was passed, the <em>callback</em> function will be called when the popup is closed.</td>
<td><mark>Bot API 6.2+</mark> A method that shows <em>message</em> in a simple confirmation window with 'OK' and 'Cancel' buttons. If an optional <em>callback</em> parameter was passed, the <em>callback</em> function will be called when the popup is closed and the first argument will be a boolean indicating whether the user pressed the 'OK' button.</td>
<td><mark>Bot API 6.4+</mark> A method that shows a native popup for scanning a QR code described by the <em>params</em> argument of the type <ahref="#scanqrpopupparams">ScanQrPopupParams</a>. The Web App will receive the <ahref="#events-available-for-web-apps">event</a><em>qrTextReceived</em> every time the scanner catches a code with text data. If an optional <em>callback</em> parameter was passed, the <em>callback</em> function will be called and the text from the QR code will be passed as the first argument. Returning <em>true</em> inside this callback function causes the popup to be closed.</td>
<td><mark>Bot API 6.4+</mark> A method that closes the native popup for scanning a QR code opened with the <em>showScanQrPopup</em> method. Run it if you received valid data in the <ahref="#events-available-for-web-apps">event</a><em>qrTextReceived</em>.</td>
<td><mark>Bot API 6.4+</mark> A method that requests text from the clipboard. The Web App will receive the <ahref="#events-available-for-web-apps">event</a><em>clipboardTextReceived</em>. If an optional <em>callback</em> parameter was passed, the <em>callback</em> function will be called and the text from the clipboard will be passed as the first argument.<br><br><em>Note: this method can be called only for Web Apps launched from the attachment menu and only in response to a user interaction with the Web App interface (e.g. a click inside the Web App or on the main button).</em></td>
<td>A method that informs the Telegram app that the Web App is ready to be displayed.<br>It is recommended to call this method as early as possible, as soon as all essential interface elements are loaded. Once this method is called, the loading placeholder is hidden and the Web App is shown.<br>If the method is not called, the placeholder will be hidden only when the page is fully loaded.</td>
<td>A method that expands the Web App to the maximum available height. To find out if the Web App is expanded to the maximum height, refer to the value of the <em>Telegram.WebApp.isExpanded</em> parameter</td>
<p>Web Apps can <ahref="#color-schemes">adjust the appearance</a> of the interface to match the Telegram user's app in real time. This object contains the user's current theme settings:</p>
<tableclass="table">
<thead>
<tr>
<th>Field</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>bg_color</td>
<td>String</td>
<td><em>Optional</em>. Background color in the <code>#RRGGBB</code> format.<br>Also available as the CSS variable <code>var(--tg-theme-bg-color)</code>.</td>
</tr>
<tr>
<td>text_color</td>
<td>String</td>
<td><em>Optional</em>. Main text color in the <code>#RRGGBB</code> format.<br>Also available as the CSS variable <code>var(--tg-theme-text-color)</code>.</td>
</tr>
<tr>
<td>hint_color</td>
<td>String</td>
<td><em>Optional</em>. Hint text color in the <code>#RRGGBB</code> format.<br>Also available as the CSS variable <code>var(--tg-theme-hint-color)</code>.</td>
</tr>
<tr>
<td>link_color</td>
<td>String</td>
<td><em>Optional</em>. Link color in the <code>#RRGGBB</code> format.<br>Also available as the CSS variable <code>var(--tg-theme-link-color)</code>.</td>
</tr>
<tr>
<td>button_color</td>
<td>String</td>
<td><em>Optional</em>. Button color in the <code>#RRGGBB</code> format.<br>Also available as the CSS variable <code>var(--tg-theme-button-color)</code>.</td>
</tr>
<tr>
<td>button_text_color</td>
<td>String</td>
<td><em>Optional</em>. Button text color in the <code>#RRGGBB</code> format.<br>Also available as the CSS variable <code>var(--tg-theme-button-text-color)</code>.</td>
<td><em>Optional</em>. <mark>Bot API 6.1+</mark> Secondary background color in the <code>#RRGGBB</code> format.<br>Also available as the CSS variable <code>var(--tg-theme-secondary-bg-color)</code>.</td>
<td><em>Optional</em>. Identifier of the button, 0-64 characters. Set to empty string by default.<br>If the button is pressed, its <em>id</em> is returned in the callback and the <em>popupClosed</em> event.</td>
</tr>
<tr>
<td>type</td>
<td>String</td>
<td><em>Optional</em>. Type of the button. Set to <em>default</em> by default.<br>Can be one of these values:<br>- <em>default</em>, a button with the default style,<br>- <em>ok</em>, a button with the localized text “OK”,<br>- <em>close</em>, a button with the localized text “Close”,<br>- <em>cancel</em>, a button with the localized text “Cancel”,<br>- <em>destructive</em>, a button with a style that indicates a destructive action (e.g. “Remove”, “Delete”, etc.).</td>
</tr>
<tr>
<td>text</td>
<td>String</td>
<td><em>Optional</em>. The text to be displayed on the button, 0-64 characters. Required if <em>type</em> is <em>default</em> or <em>destructive</em>. Irrelevant for other types.</td>
<td>Shows whether the button is visible. Set to <em>false</em> by default.</td>
</tr>
<tr>
<td>onClick(callback)</td>
<td>Function</td>
<td><mark>Bot API 6.1+</mark> A method that sets the button press event handler. An alias for <code>Telegram.WebApp.onEvent('backButtonClicked', callback)</code></td>
</tr>
<tr>
<td>offClick(callback)</td>
<td>Function</td>
<td><mark>Bot API 6.1+</mark> A method that removes the button press event handler. An alias for <code>Telegram.WebApp.offEvent('backButtonClicked', callback)</code></td>
</tr>
<tr>
<td>show()</td>
<td>Function</td>
<td><mark>Bot API 6.1+</mark> A method to make the button active and visible.</td>
</tr>
<tr>
<td>hide()</td>
<td>Function</td>
<td><mark>Bot API 6.1+</mark> A method to hide the button.</td>
</tr>
</tbody>
</table>
<p>All these methods return the BackButton object so they can be chained.</p>
<td>A method that removes the button press event handler. An alias for <code>Telegram.WebApp.offEvent('mainButtonClicked', callback)</code></td>
<td>A method to make the button visible.<br><em>Note that opening the Web App from the <ahref="#launching-web-apps-from-the-attachment-menu">attachment menu</a> hides the main button until the user interacts with the Web App interface.</em></td>
</tr>
<tr>
<td>hide()</td>
<td>Function</td>
<td>A method to hide the button.</td>
</tr>
<tr>
<td>enable()</td>
<td>Function</td>
<td>A method to enable the button.</td>
</tr>
<tr>
<td>disable()</td>
<td>Function</td>
<td>A method to disable the button.</td>
</tr>
<tr>
<td>showProgress(leaveActive)</td>
<td>Function</td>
<td>A method to show a loading indicator on the button.<br>It is recommended to display loading progress if the action tied to the button may take a long time. By default, the button is disabled while the action is in progress. If the parameter <code>leaveActive=true</code> is passed, the button remains enabled.</td>
</tr>
<tr>
<td>hideProgress()</td>
<td>Function</td>
<td>A method to hide the loading indicator.</td>
</tr>
<tr>
<td>setParams(params)</td>
<td>Function</td>
<td>A method to set the button parameters. The <em>params</em> parameter is an object containing one or several fields that need to be changed:<br><strong>text</strong> - button text;<br><strong>color</strong> - button color;<br><strong>text_color</strong> - button text color;<br><strong>is_active</strong> - enable the button;<br><strong>is_visible</strong> - show the button.</td>
</tr>
</tbody>
</table>
<p>All these methods return the MainButton object so they can be chained.</p>
<td><mark>Bot API 6.1+</mark> A method tells that an impact occurred. The Telegram app may play the appropriate haptics based on style value passed. Style can be one of these values:<br>- <em>light</em>, indicates a collision between small or lightweight UI objects,<br>- <em>medium</em>, indicates a collision between medium-sized or medium-weight UI objects,<br>- <em>heavy</em>, indicates a collision between large or heavyweight UI objects,<br>- <em>rigid</em>, indicates a collision between hard or inflexible UI objects,<br>- <em>soft</em>, indicates a collision between soft or flexible UI objects.</td>
</tr>
<tr>
<td>notificationOccurred(type)</td>
<td>Function</td>
<td><mark>Bot API 6.1+</mark> A method tells that a task or action has succeeded, failed, or produced a warning. The Telegram app may play the appropriate haptics based on type value passed. Type can be one of these values:<br>- <em>error</em>, indicates that a task or action has failed,<br>- <em>success</em>, indicates that a task or action has completed successfully,<br>- <em>warning</em>, indicates that a task or action produced a warning.</td>
</tr>
<tr>
<td>selectionChanged()</td>
<td>Function</td>
<td><mark>Bot API 6.1+</mark> A method tells that the user has changed a selection. The Telegram app may play the appropriate haptics.<br><br><em>Do not use this feedback when the user makes or confirms a selection; use it only when the selection changes.</em></td>
</tr>
</tbody>
</table>
<p>All these methods return the HapticFeedback object so they can be chained.</p>
<p>This object contains data that is transferred to the Web App when it is opened. It is empty if the Web App was launched from a <ahref="#keyboard-button-web-apps">keyboard button</a>.</p>
<td><em>Optional.</em> A unique identifier for the Web App session, required for sending messages via the <ahref="/bots/api#answerwebappquery">answerWebAppQuery</a> method.</td>
<td><em>Optional.</em> An object containing data about the chat partner of the current user in the chat where the bot was launched via the attachment menu. Returned only for private chats and only for Web Apps launched via the attachment menu.</td>
<td><em>Optional.</em> An object containing data about the chat where the bot was launched via the attachment menu. Returned for supergroups, channels and group chats – only for Web Apps launched via the attachment menu.</td>
<td><em>Optional.</em> The value of the <em>startattach</em> parameter, passed <ahref="#adding-bots-to-the-attachment-menu">via link</a>. Only returned for Web Apps when launched from the attachment menu via link.<br><br>The value of the <code>start_param</code> parameter will also be passed in the GET-parameter <code>tgWebAppStartParam</code>, so the Web App can load the correct interface right away.</td>
<td><em>Optional.</em> Time in seconds, after which a message can be sent via the <ahref="/bots/api#answerwebappquery">answerWebAppQuery</a> method.</td>
<p>This object contains the data of the Web App user.</p>
<tableclass="table">
<thead>
<tr>
<th>Field</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>id</td>
<td>Integer</td>
<td>A unique identifier for the user or bot. This number may have more than 32 significant bits and some programming languages may have difficulty/silent defects in interpreting it. It has at most 52 significant bits, so a 64-bit integer or a double-precision float type is safe for storing this identifier.</td>
</tr>
<tr>
<td>is_bot</td>
<td>Boolean</td>
<td><em>Optional</em>. <em>True</em>, if this user is a bot. Returns in the <ahref="#webappinitdata">receiver</a> field only.</td>
</tr>
<tr>
<td>first_name</td>
<td>String</td>
<td>First name of the user or bot.</td>
</tr>
<tr>
<td>last_name</td>
<td>String</td>
<td><em>Optional</em>. Last name of the user or bot.</td>
</tr>
<tr>
<td>username</td>
<td>String</td>
<td><em>Optional</em>. Username of the user or bot.</td>
</tr>
<tr>
<td>language_code</td>
<td>String</td>
<td><em>Optional</em>. <ahref="https://en.wikipedia.org/wiki/IETF_language_tag">IETF language tag</a> of the user's language. Returns in <em>user</em> field only.</td>
<td><em>Optional</em>. URL of the user’s profile photo. The photo can be in .jpeg or .svg formats. Only returned for Web Apps launched from the attachment menu.</td>
<td>Unique identifier for this chat. This number may have more than 32 significant bits and some programming languages may have difficulty/silent defects in interpreting it. But it has at most 52 significant bits, so a signed 64-bit integer or double-precision float type are safe for storing this identifier.</td>
</tr>
<tr>
<td>type</td>
<td>String</td>
<td>Type of chat, can be either “group”, “supergroup” or “channel”</td>
</tr>
<tr>
<td>title</td>
<td>String</td>
<td>Title of the chat</td>
</tr>
<tr>
<td>username</td>
<td>String</td>
<td><em>Optional</em>. Username of the chat</td>
</tr>
<tr>
<td>photo_url</td>
<td>String</td>
<td><em>Optional</em>. URL of the chat’s photo. The photo can be in .jpeg or .svg formats. Only returned for Web Apps launched from the attachment menu.</td>
<h4><aclass="anchor"name="validating-data-received-via-the-web-app"href="#validating-data-received-via-the-web-app"><iclass="anchor-icon"></i></a>Validating data received via the Web App</h4>
<p>To validate data received via the Web App, one should send the data from the <em>Telegram.WebApp.initData</em> field to the bot's backend. The data is a query string, which is composed of a series of field-value pairs.</p>
<p>You can verify the integrity of the data received by comparing the received <em>hash</em> parameter with the hexadecimal representation of the <ahref="https://en.wikipedia.org/wiki/Hash-based_message_authentication_code">HMAC-SHA-256</a> signature of the <strong>data-check-string</strong> with the secret key, which is the <ahref="https://en.wikipedia.org/wiki/Hash-based_message_authentication_code">HMAC-SHA-256</a> signature of the <ahref="/bots#creating-a-new-bot">bot's token</a> with the constant string <code>WebAppData</code> used as a key.</p>
<p><strong>Data-check-string</strong> is a chain of all received fields, sorted alphabetically, in the format <code>key=<value></code> with a <ahref="https://en.wikipedia.org/wiki/Newline">line feed</a> character ('\n', 0x0A) used as separator – e.g., <code>'auth_date=<auth_date>\nquery_id=<query_id>\nuser=<user>'</code>.</p>
if (hex(HMAC_SHA256(data_check_string, secret_key)) == hash) {
// data is from Telegram
}</code></pre>
<p>To prevent the use of outdated data, you can additionally check the <em>auth_date</em> field, which contains a Unix timestamp of when it was received by the Web App.</p>
<p>Once validated, the data may be used on your server. Complex data types are represented as JSON-serialized objects.</p>
<h4><aclass="anchor"name="events-available-for-web-apps"href="#events-available-for-web-apps"><iclass="anchor-icon"></i></a>Events Available for Web Apps</h4>
<p>The Web App can receive events from the Telegram app, onto which a handler can be attached using the <code>Telegram.WebApp.onEvent(eventType, eventHandler)</code> method. Inside <code>eventHandler</code> the <em>this</em> object refers to <em>Telegram.WebApp</em>, the set of parameters sent to the handler depends on the event type. Below is a list of possible events:</p>
<tableclass="table">
<thead>
<tr>
<th>eventType</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>themeChanged</code></td>
<td>Occurs whenever theme settings are changed in the user's Telegram app (including switching to night mode).<br><em>eventHandler</em> receives no parameters, new theme settings and color scheme can be received via <em>this.themeParams</em> and <em>this.colorScheme</em> respectively.</td>
<td>Occurs when the visible section of the Web App is changed.<br><em>eventHandler</em> receives an object with the single field <em>isStateStable</em>. If <em>isStateStable</em> is true, the resizing of the Web App is finished. If it is false, the resizing is ongoing (the user is expanding or collapsing the Web App or an animated object is playing). The current value of the visible section’s height is available in <em>this.viewportHeight</em>.</td>
<td><mark>Bot API 6.1+</mark> Occurrs when the opened invoice is closed.<br><em>eventHandler</em> receives an object with the two fields: <em>url</em>– invoice link provided and <em>status</em>– one of the invoice statuses:<br>- <strong>paid</strong>– invoice was paid successfully,<br>- <strong>cancelled</strong>– user closed this invoice without paying,<br>- <strong>failed</strong>– user tried to pay, but the payment was failed,<br>- <strong>pending</strong>– the payment is still processing. The bot will receive a service message about a <ahref="https://core.telegram.org/bots/api#successfulpayment">successful payment</a> when the payment is successfully paid.</td>
<td><mark>Bot API 6.2+</mark> Occurrs when the opened popup is closed.<br><em>eventHandler</em> receives an object with the single field <em>button_id</em>– the value of the field <em>id</em> of the pressed button. If no buttons were pressed, the field <em>button_id</em> will be <em>null</em>.</td>
<td><mark>Bot API 6.4+</mark> Occurs when the QR code scanner catches a code with text data.<br><em>eventHandler</em> receives an object with the single field <em>data</em> containing text data from the QR code.</td>
<td><mark>Bot API 6.4+</mark> Occurrs when the <code>readTextFromClipboard</code> method is called.<br><em>eventHandler</em> receives an object with the single field <em>data</em> containing text data from the clipboard. If the clipboard contains non-text data, the field <em>data</em> will be an empty string. If the Web App has no access to the clipboard, the field <em>data</em> will be <em>null</em>.</td>
<h4><aclass="anchor"name="adding-bots-to-the-attachment-menu"href="#adding-bots-to-the-attachment-menu"><iclass="anchor-icon"></i></a>Adding Bots to the Attachment Menu</h4>
<p>Attachment menu integration is currently only available for major advertisers on the <ahref="https://promote.telegram.org/basics">Telegram Ad Platform</a>. However, <strong>all bots</strong> can use it in the <ahref="#using-bots-in-the-test-environment">test server environment</a>. Talk to Botfather on the test server to <ahref="#using-bots-in-the-test-environment">set up the integration</a>.</p>
<p>A special link is used to add bots to the attachment menu:</p>
<p>For example, open this <ahref="https://t.me/durgerkingbot?startattach">attachment menu link</a> for <em>@DurgerKingBot</em>, then use the <imgclass="icon"src="/file/464001085/2/E4hNXSNQimQ.2503/bf6ffcab3cb3afd43d"alt="Attach"> menu in any <strong>private chat</strong>.</p>
</blockquote>
<p>Opening the link prompts the user to add the bot to their attachment menu. If the bot has already been added, the attachment menu will open in the current chat and redirect to the bot there (if the link is opened from a 1-on-1 chat). If a non-empty <em>startattach</em> parameter was included in the link, it will be passed to the Web App in the <em>start_param</em> field and in the GET parameter <em>tgWebAppStartParam</em>.</p>
<p>The following link formats are also supported:</p>
<p>These links open the Web App in the attachment menu in the chat with a specific user. If the bot wasn't already added to the attachment menu, the user will be prompted to do so. If a non-empty <em>startattach</em> parameter was included in the link, it will be passed to the Web App in the <em>start_param</em> field and in the GET parameter <em>tgWebAppStartParam</em>.</p>
<p>Opening such a link prompts the user to choose a specific chat and opens the attachment menu in that chat. If the bot wasn't already added to the attachment menu, the user will be prompted to do so. You can specify which types of chats the user will be able to choose from. It can be one or more of the following types: <em>users</em>, <em>bots</em>, <em>groups</em>, <em>channels</em> separated by a <code>+</code> sign. If a non-empty <em>startattach</em> parameter was included in the link, it will be passed to the Web App in the <em>start_param</em> field and in the GET parameter <em>tgWebAppStartParam</em>.</p>
<h4><aclass="anchor"name="using-bots-in-the-test-environment"href="#using-bots-in-the-test-environment"><iclass="anchor-icon"></i></a>Using bots in the test environment</h4>
<p>To log in to the test environment, use either of the following:</p>
<ul>
<li><strong>iOS:</strong> tap 10 times on the Settings icon > Accounts > Login to another account > Test.</li>
<p>The test environment is completely separate from the main environment, so you will need to create a <strong>new user account</strong> and a <strong>new bot</strong> with @BotFather.</p>
<p>After receiving your bot token, you can send requests to the Bot API in this format: </p>
<h4><aclass="anchor"name="debug-mode-for-web-apps"href="#debug-mode-for-web-apps"><iclass="anchor-icon"></i></a>Debug Mode for Web Apps</h4>
<p>Use these tools to find app-specific issues in your Web App:</p>
<p><strong>Android</strong></p>
<ul>
<li><ahref="https://developer.chrome.com/docs/devtools/remote-debugging/">Enable USB-Debugging</a> on your device.</li>
<li>In Telegram Settings, scroll all the way down, press and hold on the <strong>version number</strong> two times.</li>
<li>Choose <em>Enable WebView Debug</em> in the Debug Settings.</li>
<li>Connect your phone to your computer and open <code>chrome://inspect/#devices</code> in Chrome – you will see your Web App there when you launch it on your phone.</li>
</ul>
<p><strong>Telegram Desktop on Windows and Linux</strong></p>
<ul>
<li>Download and launch the <ahref="https://desktop.telegram.org/changelog#beta-version">Beta Version</a> of Telegram Desktop on <strong>Windows</strong> or <strong>Linux</strong> (not supported on Telegram Desktop for macOS yet).</li>