pub struct TelegramWebApp { /* private fields */ }
Expand description
Safe wrapper around window.Telegram.WebApp
Implementations§
Source§impl TelegramWebApp
impl TelegramWebApp
Sourcepub fn try_instance() -> Result<Self, JsValue>
pub fn try_instance() -> Result<Self, JsValue>
Sourcepub fn validate_init_data(
init_data: &str,
key: ValidationKey<'_>,
) -> Result<(), ValidationError>
pub fn validate_init_data( init_data: &str, key: ValidationKey<'_>, ) -> Result<(), ValidationError>
Validate an initData
payload using either HMAC-SHA256 or Ed25519.
Pass ValidationKey::BotToken
to verify the hash
parameter using
the bot token. Use ValidationKey::Ed25519PublicKey
to verify the
signature
parameter with an Ed25519 public key.
§Errors
Returns validate_init_data::ValidationError
if validation fails.
§Examples
use telegram_webapp_sdk::{TelegramWebApp, validate_init_data::ValidationKey};
let bot_token = "123456:ABC";
let query = "a=1&b=2&hash=9e5e8d7c0b1f9f3a";
TelegramWebApp::validate_init_data(query, ValidationKey::BotToken(bot_token)).unwrap();
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 unlock_orientation(&self) -> Result<(), JsValue>
pub fn unlock_orientation(&self) -> Result<(), JsValue>
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 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 is_version_at_least(&self, version: &str) -> Result<bool, JsValue>
pub fn is_version_at_least(&self, version: &str) -> Result<bool, JsValue>
Returns whether the WebApp version is at least the provided value.
§Examples
use telegram_webapp_sdk::webapp::TelegramWebApp;
if let Some(app) = TelegramWebApp::instance() {
let _ = app.is_version_at_least("9.0");
}
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 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>
Sourcepub fn add_to_home_screen(&self) -> Result<bool, JsValue>
pub fn add_to_home_screen(&self) -> Result<bool, JsValue>
Call WebApp.addToHomeScreen()
and return whether the prompt was shown.
§Examples
let _shown = app.add_to_home_screen().unwrap();
Sourcepub fn check_home_screen_status<F>(&self, callback: F) -> Result<(), JsValue>
pub fn check_home_screen_status<F>(&self, callback: F) -> Result<(), JsValue>
Call WebApp.checkHomeScreenStatus(callback)
.
§Examples
app.check_home_screen_status(|status| {
let _ = status;
})
.unwrap();
Sourcepub fn request_write_access<F>(&self, callback: F) -> Result<(), JsValue>
pub fn request_write_access<F>(&self, callback: F) -> Result<(), JsValue>
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.
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 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();
Sourcepub fn show_scan_qr_popup<F>(
&self,
text: &str,
callback: F,
) -> Result<(), JsValue>
pub fn show_scan_qr_popup<F>( &self, text: &str, callback: F, ) -> Result<(), JsValue>
Call WebApp.showScanQrPopup(text, callback)
.
§Examples
app.show_scan_qr_popup("Scan", |text| {
let _ = text;
})
.unwrap();
Sourcepub fn close_scan_qr_popup(&self) -> Result<(), JsValue>
pub fn close_scan_qr_popup(&self) -> Result<(), JsValue>
Sourcepub fn hide_keyboard(&self) -> Result<(), JsValue>
pub fn hide_keyboard(&self) -> Result<(), JsValue>
Sourcepub fn read_text_from_clipboard<F>(&self, callback: F) -> Result<(), JsValue>
pub fn read_text_from_clipboard<F>(&self, callback: F) -> Result<(), JsValue>
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.
Legacy alias for Self::show_bottom_button
with
BottomButton::Main
.
Show the secondary bottom button.
Legacy alias for Self::hide_bottom_button
with
BottomButton::Main
.
Hide the secondary bottom button.
Legacy alias for Self::set_bottom_button_text
with
BottomButton::Main
.
Set text for the secondary bottom button.
Legacy alias for Self::set_bottom_button_color
with
BottomButton::Main
.
Set color for the secondary bottom button.
Legacy alias for Self::set_bottom_button_text_color
with
BottomButton::Main
.
Set text color for the secondary bottom button.
Enable the main bottom button.
§Examples
use telegram_webapp_sdk::webapp::TelegramWebApp;
if let Some(app) = TelegramWebApp::instance() {
let _ = app.enable_main_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 main bottom button.
§Examples
use telegram_webapp_sdk::webapp::TelegramWebApp;
if let Some(app) = TelegramWebApp::instance() {
let _ = app.disable_main_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 main bottom button.
§Examples
use telegram_webapp_sdk::webapp::TelegramWebApp;
if let Some(app) = TelegramWebApp::instance() {
let _ = app.show_main_button_progress(false);
}
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 main bottom button.
§Examples
use telegram_webapp_sdk::webapp::TelegramWebApp;
if let Some(app) = TelegramWebApp::instance() {
let _ = app.hide_main_button_progress();
}
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();
}
Update the main button state via
set_bottom_button_params
.
Legacy alias for Self::set_bottom_button_callback
with
BottomButton::Main
.
Set callback for the secondary bottom button.
Legacy alias for Self::remove_bottom_button_callback
.
Remove callback for the secondary bottom button.
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 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>
pub fn is_expanded(&self) -> bool
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();
}
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 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 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 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();
}
Sourcepub fn expand_viewport(&self) -> Result<(), JsValue>
pub fn expand_viewport(&self) -> Result<(), JsValue>
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.
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.
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