1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// SPDX-FileCopyrightText: 2025 RAprogramm <andrey.rozanov.vl@gmail.com>
// SPDX-License-Identifier: MIT
use js_sys::{Function, Reflect};
use serde_wasm_bindgen::to_value;
use wasm_bindgen::{JsCast, JsValue};
use crate::webapp::{TelegramWebApp, types::CloseOptions};
impl TelegramWebApp {
/// Call `WebApp.expand()`.
///
/// # Errors
/// Returns [`JsValue`] if the underlying JS call fails.
pub fn expand(&self) -> Result<(), JsValue> {
self.call0("expand")
}
/// Call `WebApp.close()`.
///
/// # Errors
/// Returns [`JsValue`] if the underlying JS call fails.
pub fn close(&self) -> Result<(), JsValue> {
self.call0("close")
}
/// Call `WebApp.close(options)` (Bot API 7.6+ for `return_back`).
///
/// On older Telegram clients the option is silently ignored on the JS side.
///
/// # Examples
/// ```no_run
/// # use telegram_webapp_sdk::webapp::{CloseOptions, TelegramWebApp};
/// # let app = TelegramWebApp::instance().unwrap();
/// app.close_with_options(&CloseOptions {
/// return_back: Some(true)
/// })
/// .unwrap();
/// ```
///
/// # Errors
/// Returns [`JsValue`] if the underlying JS call fails or the options fail
/// to serialize.
pub fn close_with_options(&self, options: &CloseOptions) -> Result<(), JsValue> {
let payload = to_value(options).map_err(|err| JsValue::from_str(&err.to_string()))?;
let f = Reflect::get(&self.inner, &"close".into())?;
let func = f
.dyn_ref::<Function>()
.ok_or_else(|| JsValue::from_str("close is not a function"))?;
func.call1(&self.inner, &payload)?;
Ok(())
}
/// Call `WebApp.enableClosingConfirmation()`.
///
/// # Examples
/// ```no_run
/// # use telegram_webapp_sdk::webapp::TelegramWebApp;
/// # let app = TelegramWebApp::instance().unwrap();
/// app.enable_closing_confirmation().unwrap();
/// ```
///
/// # Errors
/// Returns [`JsValue`] if the underlying JS call fails.
pub fn enable_closing_confirmation(&self) -> Result<(), JsValue> {
self.call0("enableClosingConfirmation")
}
/// Call `WebApp.disableClosingConfirmation()`.
///
/// # Examples
/// ```no_run
/// # use telegram_webapp_sdk::webapp::TelegramWebApp;
/// # let app = TelegramWebApp::instance().unwrap();
/// app.disable_closing_confirmation().unwrap();
/// ```
///
/// # Errors
/// Returns [`JsValue`] if the underlying JS call fails.
pub fn disable_closing_confirmation(&self) -> Result<(), JsValue> {
self.call0("disableClosingConfirmation")
}
/// Returns whether closing confirmation is currently enabled.
///
/// # Examples
/// ```no_run
/// # use telegram_webapp_sdk::webapp::TelegramWebApp;
/// # let app = TelegramWebApp::instance().unwrap();
/// let _ = app.is_closing_confirmation_enabled();
/// ```
pub fn is_closing_confirmation_enabled(&self) -> bool {
Reflect::get(&self.inner, &"isClosingConfirmationEnabled".into())
.ok()
.and_then(|v| v.as_bool())
.unwrap_or(false)
}
/// Call `WebApp.requestFullscreen()`.
///
/// # Examples
/// ```no_run
/// # use telegram_webapp_sdk::webapp::TelegramWebApp;
/// # let app = TelegramWebApp::instance().unwrap();
/// app.request_fullscreen().unwrap();
/// ```
///
/// # Errors
/// Returns [`JsValue`] if the underlying JS call fails.
pub fn request_fullscreen(&self) -> Result<(), JsValue> {
self.call0("requestFullscreen")
}
/// Call `WebApp.exitFullscreen()`.
///
/// # Examples
/// ```no_run
/// # use telegram_webapp_sdk::webapp::TelegramWebApp;
/// # let app = TelegramWebApp::instance().unwrap();
/// app.exit_fullscreen().unwrap();
/// ```
///
/// # Errors
/// Returns [`JsValue`] if the underlying JS call fails.
pub fn exit_fullscreen(&self) -> Result<(), JsValue> {
self.call0("exitFullscreen")
}
/// Returns whether the app is displayed in fullscreen mode.
///
/// # Examples
/// ```no_run
/// use telegram_webapp_sdk::webapp::TelegramWebApp;
///
/// if let Some(app) = TelegramWebApp::instance() {
/// let _ = app.is_fullscreen();
/// }
/// ```
pub fn is_fullscreen(&self) -> bool {
Reflect::get(&self.inner, &"isFullscreen".into())
.ok()
.and_then(|v| v.as_bool())
.unwrap_or(false)
}
/// Call `WebApp.lockOrientation(orientation)`.
///
/// # Examples
/// ```no_run
/// # use telegram_webapp_sdk::webapp::TelegramWebApp;
/// # let app = TelegramWebApp::instance().unwrap();
/// app.lock_orientation("portrait").unwrap();
/// ```
///
/// # Errors
/// Returns [`JsValue`] if the underlying JS call fails.
pub fn lock_orientation(&self, orientation: &str) -> Result<(), JsValue> {
self.call1("lockOrientation", &orientation.into())
}
/// Call `WebApp.unlockOrientation()`.
///
/// # Examples
/// ```no_run
/// # use telegram_webapp_sdk::webapp::TelegramWebApp;
/// # let app = TelegramWebApp::instance().unwrap();
/// app.unlock_orientation().unwrap();
/// ```
///
/// # Errors
/// Returns [`JsValue`] if the underlying JS call fails.
pub fn unlock_orientation(&self) -> Result<(), JsValue> {
self.call0("unlockOrientation")
}
/// Returns whether the orientation is locked.
///
/// # Examples
/// ```no_run
/// use telegram_webapp_sdk::webapp::TelegramWebApp;
///
/// if let Some(app) = TelegramWebApp::instance() {
/// let _ = app.is_orientation_locked();
/// }
/// ```
pub fn is_orientation_locked(&self) -> bool {
Reflect::get(&self.inner, &"isOrientationLocked".into())
.ok()
.and_then(|v| v.as_bool())
.unwrap_or(false)
}
/// Call `WebApp.enableVerticalSwipes()`.
///
/// # Examples
/// ```no_run
/// # use telegram_webapp_sdk::webapp::TelegramWebApp;
/// # let app = TelegramWebApp::instance().unwrap();
/// app.enable_vertical_swipes().unwrap();
/// ```
///
/// # Errors
/// Returns [`JsValue`] if the underlying JS call fails.
pub fn enable_vertical_swipes(&self) -> Result<(), JsValue> {
self.call0("enableVerticalSwipes")
}
/// Call `WebApp.disableVerticalSwipes()`.
///
/// # Examples
/// ```no_run
/// # use telegram_webapp_sdk::webapp::TelegramWebApp;
/// # let app = TelegramWebApp::instance().unwrap();
/// app.disable_vertical_swipes().unwrap();
/// ```
///
/// # Errors
/// Returns [`JsValue`] if the underlying JS call fails.
pub fn disable_vertical_swipes(&self) -> Result<(), JsValue> {
self.call0("disableVerticalSwipes")
}
/// Returns whether vertical swipes are currently enabled.
///
/// # Examples
/// ```no_run
/// use telegram_webapp_sdk::webapp::TelegramWebApp;
///
/// if let Some(app) = TelegramWebApp::instance() {
/// let _ = app.is_vertical_swipes_enabled();
/// }
/// ```
pub fn is_vertical_swipes_enabled(&self) -> bool {
Reflect::get(&self.inner, &"isVerticalSwipesEnabled".into())
.ok()
.and_then(|v| v.as_bool())
.unwrap_or(false)
}
/// Returns whether the mini app is currently active (visible to the user).
///
/// # Examples
/// ```no_run
/// use telegram_webapp_sdk::webapp::TelegramWebApp;
///
/// if let Some(app) = TelegramWebApp::instance() {
/// let _ = app.is_active();
/// }
/// ```
pub fn is_active(&self) -> bool {
Reflect::get(&self.inner, &"isActive".into())
.ok()
.and_then(|v| v.as_bool())
.unwrap_or(false)
}
pub fn is_expanded(&self) -> bool {
Reflect::get(&self.inner, &"isExpanded".into())
.ok()
.and_then(|v| v.as_bool())
.unwrap_or(false)
}
}
#[cfg(test)]
mod tests {
use js_sys::{Function, Object, Reflect};
use wasm_bindgen_test::{wasm_bindgen_test, wasm_bindgen_test_configure};
use web_sys::window;
use crate::webapp::{TelegramWebApp, types::CloseOptions};
wasm_bindgen_test_configure!(run_in_browser);
fn setup_webapp() -> Object {
let win = window().expect("window");
let telegram = Object::new();
let webapp = Object::new();
let _ = Reflect::set(&win, &"Telegram".into(), &telegram);
let _ = Reflect::set(&telegram, &"WebApp".into(), &webapp);
webapp
}
#[wasm_bindgen_test]
#[allow(dead_code, clippy::unused_unit)]
fn close_with_options_passes_return_back() {
let webapp = setup_webapp();
let capture = Function::new_with_args("opts", "this.captured_close = opts;");
let _ = Reflect::set(&webapp, &"close".into(), &capture);
let app = TelegramWebApp::instance().expect("instance");
app.close_with_options(&CloseOptions {
return_back: Some(true)
})
.expect("ok");
let opts = Reflect::get(&webapp, &"captured_close".into()).expect("captured");
let val = Reflect::get(&opts, &"return_back".into()).expect("field");
assert_eq!(val.as_bool(), Some(true));
}
#[wasm_bindgen_test]
#[allow(dead_code, clippy::unused_unit)]
fn close_with_options_omits_unset_fields() {
let webapp = setup_webapp();
let capture = Function::new_with_args("opts", "this.captured_close = opts;");
let _ = Reflect::set(&webapp, &"close".into(), &capture);
let app = TelegramWebApp::instance().expect("instance");
app.close_with_options(&CloseOptions::default())
.expect("ok");
let opts = Reflect::get(&webapp, &"captured_close".into()).expect("captured");
let val = Reflect::get(&opts, &"return_back".into()).expect("field");
assert!(val.is_undefined());
}
}