pub struct TelegramWebApp { /* private fields */ }Expand description
Safe wrapper around window.Telegram.WebApp
Implementations§
Source§impl TelegramWebApp
impl TelegramWebApp
Enable a bottom button, allowing user interaction.
§Examples
use telegram_webapp_sdk::webapp::{BottomButton, TelegramWebApp};
if let Some(app) = TelegramWebApp::instance() {
let _ = app.enable_bottom_button(BottomButton::Main);
}Disable a bottom button, preventing user interaction.
§Examples
use telegram_webapp_sdk::webapp::{BottomButton, TelegramWebApp};
if let Some(app) = TelegramWebApp::instance() {
let _ = app.disable_bottom_button(BottomButton::Main);
}Show the circular loading indicator on a bottom button.
§Examples
use telegram_webapp_sdk::webapp::{BottomButton, TelegramWebApp};
if let Some(app) = TelegramWebApp::instance() {
let _ = app.show_bottom_button_progress(BottomButton::Main, false);
}Hide the loading indicator on a bottom button.
§Examples
use telegram_webapp_sdk::webapp::{BottomButton, TelegramWebApp};
if let Some(app) = TelegramWebApp::instance() {
let _ = app.hide_bottom_button_progress(BottomButton::Main);
}Returns whether the specified bottom button is currently visible.
§Examples
use telegram_webapp_sdk::webapp::{BottomButton, TelegramWebApp};
if let Some(app) = TelegramWebApp::instance() {
let _ = app.is_bottom_button_visible(BottomButton::Main);
}Returns whether the specified bottom button is active (enabled).
§Examples
use telegram_webapp_sdk::webapp::{BottomButton, TelegramWebApp};
if let Some(app) = TelegramWebApp::instance() {
let _ = app.is_bottom_button_active(BottomButton::Main);
}Returns whether the progress indicator is visible on the button.
§Examples
use telegram_webapp_sdk::webapp::{BottomButton, TelegramWebApp};
if let Some(app) = TelegramWebApp::instance() {
let _ = app.is_bottom_button_progress_visible(BottomButton::Main);
}Returns the current text displayed on the button.
§Examples
use telegram_webapp_sdk::webapp::{BottomButton, TelegramWebApp};
if let Some(app) = TelegramWebApp::instance() {
let _ = app.bottom_button_text(BottomButton::Main);
}Returns the current text color of the button.
§Examples
use telegram_webapp_sdk::webapp::{BottomButton, TelegramWebApp};
if let Some(app) = TelegramWebApp::instance() {
let _ = app.bottom_button_text_color(BottomButton::Main);
}Returns the current background color of the button.
§Examples
use telegram_webapp_sdk::webapp::{BottomButton, TelegramWebApp};
if let Some(app) = TelegramWebApp::instance() {
let _ = app.bottom_button_color(BottomButton::Main);
}Returns whether the shine effect is enabled on the button.
§Examples
use telegram_webapp_sdk::webapp::{BottomButton, TelegramWebApp};
if let Some(app) = TelegramWebApp::instance() {
let _ = app.bottom_button_has_shine_effect(BottomButton::Main);
}Update bottom button state via setParams.
§Examples
use telegram_webapp_sdk::webapp::{BottomButton, BottomButtonParams, TelegramWebApp};
if let Some(app) = TelegramWebApp::instance() {
let params = BottomButtonParams {
text: Some("Send"),
..Default::default()
};
let _ = app.set_bottom_button_params(BottomButton::Main, ¶ms);
}Update secondary button state via setParams, including position.
§Examples
use telegram_webapp_sdk::webapp::{
SecondaryButtonParams, SecondaryButtonPosition, TelegramWebApp
};
if let Some(app) = TelegramWebApp::instance() {
let params = SecondaryButtonParams {
position: Some(SecondaryButtonPosition::Left),
..Default::default()
};
let _ = app.set_secondary_button_params(¶ms);
}Returns the configured position of the secondary button, if available.
§Examples
use telegram_webapp_sdk::webapp::{SecondaryButtonPosition, TelegramWebApp};
if let Some(app) = TelegramWebApp::instance() {
let _ = app.secondary_button_position();
}Set callback for onClick() on a bottom button.
Returns an EventHandle that can be used to remove the callback.
§Errors
Returns JsValue if the underlying JS call fails.
Registers a callback for the native back button.
Returns an EventHandle that can be passed to
remove_back_button_callback.
§Examples
let handle = app.set_back_button_callback(|| {}).expect("callback");
app.remove_back_button_callback(handle).unwrap();§Errors
Returns JsValue if the underlying JS call fails.
Legacy alias for Self::show_bottom_button with
BottomButton::Main.
Legacy alias for Self::hide_bottom_button with
BottomButton::Main.
Legacy alias for Self::set_bottom_button_text with
BottomButton::Main.
Legacy alias for Self::set_bottom_button_color with
BottomButton::Main.
Legacy alias for Self::set_bottom_button_text_color with
BottomButton::Main.
Enable the main bottom button.
§Examples
use telegram_webapp_sdk::webapp::TelegramWebApp;
if let Some(app) = TelegramWebApp::instance() {
let _ = app.enable_main_button();
}Disable the main bottom button.
§Examples
use telegram_webapp_sdk::webapp::TelegramWebApp;
if let Some(app) = TelegramWebApp::instance() {
let _ = app.disable_main_button();
}Show progress on the main bottom button.
§Examples
use telegram_webapp_sdk::webapp::TelegramWebApp;
if let Some(app) = TelegramWebApp::instance() {
let _ = app.show_main_button_progress(false);
}Hide progress indicator from the main bottom button.
§Examples
use telegram_webapp_sdk::webapp::TelegramWebApp;
if let Some(app) = TelegramWebApp::instance() {
let _ = app.hide_main_button_progress();
}Update the main button state via
set_bottom_button_params.
Legacy alias for Self::set_bottom_button_callback with
BottomButton::Main.
Legacy alias for Self::remove_bottom_button_callback.
Show the secondary bottom button.
Hide the secondary bottom button.
Set text for the secondary bottom button.
Set color for the secondary bottom button.
Set text color for the secondary bottom button.
Enable the secondary bottom button.
§Examples
use telegram_webapp_sdk::webapp::TelegramWebApp;
if let Some(app) = TelegramWebApp::instance() {
let _ = app.enable_secondary_button();
}Disable the secondary bottom button.
§Examples
use telegram_webapp_sdk::webapp::TelegramWebApp;
if let Some(app) = TelegramWebApp::instance() {
let _ = app.disable_secondary_button();
}Show progress on the secondary bottom button.
§Examples
use telegram_webapp_sdk::webapp::TelegramWebApp;
if let Some(app) = TelegramWebApp::instance() {
let _ = app.show_secondary_button_progress(false);
}Hide progress indicator from the secondary bottom button.
§Examples
use telegram_webapp_sdk::webapp::TelegramWebApp;
if let Some(app) = TelegramWebApp::instance() {
let _ = app.hide_secondary_button_progress();
}Set callback for the secondary bottom button.
Remove callback for the secondary bottom button.
Source§impl TelegramWebApp
impl TelegramWebApp
Sourcepub fn try_instance() -> Result<Self, JsValue>
pub fn try_instance() -> Result<Self, JsValue>
Sourcepub fn get_raw_init_data() -> Result<String, &'static str>
pub fn get_raw_init_data() -> Result<String, &'static str>
Returns the raw initData string as provided by Telegram.
This is the URL-encoded initData string captured during SDK initialization, suitable for server-side signature validation.
§Errors
Returns an error if the SDK has not been initialized via
crate::core::init::init_sdk.
§Examples
use telegram_webapp_sdk::TelegramWebApp;
match TelegramWebApp::get_raw_init_data() {
Ok(raw) => {
// Send to backend for validation
println!("Raw initData: {}", raw);
}
Err(e) => eprintln!("SDK not initialized: {}", e)
}Source§impl TelegramWebApp
impl TelegramWebApp
Sourcepub fn show_popup<F>(
&self,
params: &JsValue,
callback: F,
) -> Result<(), JsValue>
pub fn show_popup<F>( &self, params: &JsValue, callback: F, ) -> Result<(), JsValue>
Call WebApp.showPopup(params, callback).
§Examples
let params = Object::new();
app.show_popup(¶ms.into(), |id| {
let _ = id;
})
.unwrap();Source§impl TelegramWebApp
impl TelegramWebApp
Sourcepub fn on_event<F>(
&self,
event: &str,
callback: F,
) -> Result<EventHandle<dyn FnMut(JsValue)>, JsValue>
pub fn on_event<F>( &self, event: &str, callback: F, ) -> Result<EventHandle<dyn FnMut(JsValue)>, JsValue>
Register event handler (web_app_event_name, callback).
Returns an EventHandle that can be passed to
off_event.
§Errors
Returns JsValue if the underlying JS call fails.
Sourcepub fn on_background_event<F>(
&self,
event: BackgroundEvent,
callback: F,
) -> Result<EventHandle<dyn FnMut(JsValue)>, JsValue>
pub fn on_background_event<F>( &self, event: BackgroundEvent, callback: F, ) -> Result<EventHandle<dyn FnMut(JsValue)>, JsValue>
Register a callback for a background event.
Returns an EventHandle that can be passed to
off_event.
§Errors
Returns JsValue if the underlying JS call fails.
Sourcepub fn on_theme_changed<F>(
&self,
callback: F,
) -> Result<EventHandle<dyn FnMut()>, JsValue>where
F: 'static + Fn(),
pub fn on_theme_changed<F>(
&self,
callback: F,
) -> Result<EventHandle<dyn FnMut()>, JsValue>where
F: 'static + Fn(),
Register a callback for theme changes.
Returns an EventHandle that can be passed to
off_event.
§Errors
Returns JsValue if the underlying JS call fails.
Sourcepub fn on_safe_area_changed<F>(
&self,
callback: F,
) -> Result<EventHandle<dyn FnMut()>, JsValue>where
F: 'static + Fn(),
pub fn on_safe_area_changed<F>(
&self,
callback: F,
) -> Result<EventHandle<dyn FnMut()>, JsValue>where
F: 'static + Fn(),
Register a callback for safe area changes.
Returns an EventHandle that can be passed to
off_event.
§Errors
Returns JsValue if the underlying JS call fails.
Sourcepub fn on_content_safe_area_changed<F>(
&self,
callback: F,
) -> Result<EventHandle<dyn FnMut()>, JsValue>where
F: 'static + Fn(),
pub fn on_content_safe_area_changed<F>(
&self,
callback: F,
) -> Result<EventHandle<dyn FnMut()>, JsValue>where
F: 'static + Fn(),
Register a callback for content safe area changes.
Returns an EventHandle that can be passed to
off_event.
§Errors
Returns JsValue if the underlying JS call fails.
Sourcepub fn on_viewport_changed<F>(
&self,
callback: F,
) -> Result<EventHandle<dyn FnMut()>, JsValue>where
F: 'static + Fn(),
pub fn on_viewport_changed<F>(
&self,
callback: F,
) -> Result<EventHandle<dyn FnMut()>, JsValue>where
F: 'static + Fn(),
Register a callback for viewport changes.
Returns an EventHandle that can be passed to
off_event.
§Errors
Returns JsValue if the underlying JS call fails.
Sourcepub fn on_clipboard_text_received<F>(
&self,
callback: F,
) -> Result<EventHandle<dyn FnMut(JsValue)>, JsValue>
pub fn on_clipboard_text_received<F>( &self, callback: F, ) -> Result<EventHandle<dyn FnMut(JsValue)>, JsValue>
Register a callback for received clipboard text.
Returns an EventHandle that can be passed to
off_event.
§Errors
Returns JsValue if the underlying JS call fails.
Sourcepub fn on_invoice_closed<F>(
&self,
callback: F,
) -> Result<EventHandle<dyn FnMut(String)>, JsValue>
pub fn on_invoice_closed<F>( &self, callback: F, ) -> Result<EventHandle<dyn FnMut(String)>, JsValue>
Register a callback for invoice payment result.
Returns an EventHandle that can be passed to
off_event.
§Examples
let handle = app
.on_invoice_closed(|status| {
let _ = status;
})
.unwrap();
app.off_event(handle).unwrap();§Errors
Returns JsValue if the underlying JS call fails.
Source§impl TelegramWebApp
impl TelegramWebApp
Sourcepub fn enable_closing_confirmation(&self) -> Result<(), JsValue>
pub fn enable_closing_confirmation(&self) -> Result<(), JsValue>
Sourcepub fn disable_closing_confirmation(&self) -> Result<(), JsValue>
pub fn disable_closing_confirmation(&self) -> Result<(), JsValue>
Sourcepub fn is_closing_confirmation_enabled(&self) -> bool
pub fn is_closing_confirmation_enabled(&self) -> bool
Returns whether closing confirmation is currently enabled.
§Examples
let _ = app.is_closing_confirmation_enabled();Sourcepub fn request_fullscreen(&self) -> Result<(), JsValue>
pub fn request_fullscreen(&self) -> Result<(), JsValue>
Sourcepub fn exit_fullscreen(&self) -> Result<(), JsValue>
pub fn exit_fullscreen(&self) -> Result<(), JsValue>
Sourcepub fn is_fullscreen(&self) -> bool
pub fn is_fullscreen(&self) -> bool
Returns whether the app is displayed in fullscreen mode.
§Examples
use telegram_webapp_sdk::webapp::TelegramWebApp;
if let Some(app) = TelegramWebApp::instance() {
let _ = app.is_fullscreen();
}Sourcepub fn unlock_orientation(&self) -> Result<(), JsValue>
pub fn unlock_orientation(&self) -> Result<(), JsValue>
Sourcepub fn is_orientation_locked(&self) -> bool
pub fn is_orientation_locked(&self) -> bool
Returns whether the orientation is locked.
§Examples
use telegram_webapp_sdk::webapp::TelegramWebApp;
if let Some(app) = TelegramWebApp::instance() {
let _ = app.is_orientation_locked();
}Sourcepub fn enable_vertical_swipes(&self) -> Result<(), JsValue>
pub fn enable_vertical_swipes(&self) -> Result<(), JsValue>
Sourcepub fn disable_vertical_swipes(&self) -> Result<(), JsValue>
pub fn disable_vertical_swipes(&self) -> Result<(), JsValue>
Sourcepub fn is_vertical_swipes_enabled(&self) -> bool
pub fn is_vertical_swipes_enabled(&self) -> bool
Returns whether vertical swipes are currently enabled.
§Examples
use telegram_webapp_sdk::webapp::TelegramWebApp;
if let Some(app) = TelegramWebApp::instance() {
let _ = app.is_vertical_swipes_enabled();
}Sourcepub fn is_active(&self) -> bool
pub fn is_active(&self) -> bool
Returns whether the mini app is currently active (visible to the user).
§Examples
use telegram_webapp_sdk::webapp::TelegramWebApp;
if let Some(app) = TelegramWebApp::instance() {
let _ = app.is_active();
}pub fn is_expanded(&self) -> bool
Source§impl TelegramWebApp
impl TelegramWebApp
Sourcepub fn open_link(
&self,
url: &str,
options: Option<&OpenLinkOptions>,
) -> Result<(), JsValue>
pub fn open_link( &self, url: &str, options: Option<&OpenLinkOptions>, ) -> Result<(), JsValue>
Sourcepub fn open_telegram_link(&self, url: &str) -> Result<(), JsValue>
pub fn open_telegram_link(&self, url: &str) -> Result<(), JsValue>
Call WebApp.openTelegramLink(url).
§Examples
app.open_telegram_link("https://t.me/telegram").unwrap();Sourcepub fn switch_inline_query(
&self,
query: &str,
choose_chat_types: Option<&JsValue>,
) -> Result<(), JsValue>
pub fn switch_inline_query( &self, query: &str, choose_chat_types: Option<&JsValue>, ) -> Result<(), JsValue>
Sourcepub fn join_voice_chat(
&self,
chat_id: &str,
invite_hash: Option<&str>,
) -> Result<(), JsValue>
pub fn join_voice_chat( &self, chat_id: &str, invite_hash: Option<&str>, ) -> Result<(), JsValue>
Source§impl TelegramWebApp
impl TelegramWebApp
Sourcepub fn request_write_access<F>(&self, callback: F) -> Result<(), JsValue>
pub fn request_write_access<F>(&self, callback: F) -> Result<(), JsValue>
Sourcepub fn request_emoji_status_access<F>(&self, callback: F) -> Result<(), JsValue>
pub fn request_emoji_status_access<F>(&self, callback: F) -> Result<(), JsValue>
Sourcepub fn open_invoice<F>(&self, url: &str, callback: F) -> Result<(), JsValue>
pub fn open_invoice<F>(&self, url: &str, callback: F) -> Result<(), JsValue>
Call WebApp.openInvoice(url, callback).
§Examples
app.open_invoice("https://invoice", |status| {
let _ = status;
})
.unwrap();Sourcepub fn download_file<F>(
&self,
params: DownloadFileParams<'_>,
callback: F,
) -> Result<(), JsValue>
pub fn download_file<F>( &self, params: DownloadFileParams<'_>, callback: F, ) -> Result<(), JsValue>
Call WebApp.downloadFile(params, callback).
§Examples
let params = DownloadFileParams {
url: "https://example.com/file",
file_name: None,
mime_type: None
};
app.download_file(params, |file_id| {
let _ = file_id;
})
.unwrap();§Errors
Returns JsValue if the underlying JS call fails or the parameters
fail to serialize.
Source§impl TelegramWebApp
impl TelegramWebApp
Source§impl TelegramWebApp
impl TelegramWebApp
Sourcepub fn viewport_height(&self) -> Option<f64>
pub fn viewport_height(&self) -> Option<f64>
Sourcepub fn viewport_width(&self) -> Option<f64>
pub fn viewport_width(&self) -> Option<f64>
Sourcepub fn viewport_stable_height(&self) -> Option<f64>
pub fn viewport_stable_height(&self) -> Option<f64>
Sourcepub fn expand_viewport(&self) -> Result<(), JsValue>
pub fn expand_viewport(&self) -> Result<(), JsValue>
Sourcepub fn safe_area_inset(&self) -> Option<SafeAreaInset>
pub fn safe_area_inset(&self) -> Option<SafeAreaInset>
Returns the safe area insets reported by Telegram.
§Examples
use telegram_webapp_sdk::webapp::TelegramWebApp;
if let Some(app) = TelegramWebApp::instance() {
let _ = app.safe_area_inset();
}Sourcepub fn content_safe_area_inset(&self) -> Option<SafeAreaInset>
pub fn content_safe_area_inset(&self) -> Option<SafeAreaInset>
Returns the content safe area insets reported by Telegram.
§Examples
use telegram_webapp_sdk::webapp::TelegramWebApp;
if let Some(app) = TelegramWebApp::instance() {
let _ = app.content_safe_area_inset();
}Trait Implementations§
Source§impl Clone for TelegramWebApp
impl Clone for TelegramWebApp
Source§fn clone(&self) -> TelegramWebApp
fn clone(&self) -> TelegramWebApp
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more