telegram-webapp-sdk 0.4.0

Telegram WebApp SDK for Rust
Documentation
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
// SPDX-FileCopyrightText: 2025 RAprogramm <andrey.rozanov.vl@gmail.com>
// SPDX-License-Identifier: MIT

use js_sys::{Function, Object, Reflect};
use serde_wasm_bindgen::to_value;
use wasm_bindgen::{JsCast, JsValue, prelude::Closure};

use crate::{
    logger,
    webapp::{
        TelegramWebApp,
        types::{
            BottomButton, BottomButtonParams, EventHandle, SecondaryButtonParams,
            SecondaryButtonPosition
        }
    }
};

impl TelegramWebApp {
    // === Internal bottom button helpers ===

    pub(super) fn bottom_button_object(&self, button: BottomButton) -> Result<Object, JsValue> {
        let name = button.js_name();
        Reflect::get(&self.inner, &name.into())
            .inspect_err(|_| logger::error(&format!("{name} not available")))?
            .dyn_into::<Object>()
            .inspect_err(|_| logger::error(&format!("{name} is not an object")))
    }

    pub(super) fn bottom_button_method(
        &self,
        button: BottomButton,
        method: &str,
        arg: Option<&JsValue>
    ) -> Result<(), JsValue> {
        let name = button.js_name();
        let btn = self.bottom_button_object(button)?;
        let f = Reflect::get(&btn, &method.into())
            .inspect_err(|_| logger::error(&format!("{name}.{method} not available")))?;
        let func = f.dyn_ref::<Function>().ok_or_else(|| {
            logger::error(&format!("{name}.{method} is not a function"));
            JsValue::from_str("not a function")
        })?;
        let result = match arg {
            Some(v) => func.call1(&btn, v),
            None => func.call0(&btn)
        };
        result.inspect_err(|_| logger::error(&format!("{name}.{method} call failed")))?;
        Ok(())
    }

    pub(super) fn bottom_button_property(
        &self,
        button: BottomButton,
        property: &str
    ) -> Option<JsValue> {
        self.bottom_button_object(button)
            .ok()
            .and_then(|object| Reflect::get(&object, &property.into()).ok())
    }

    // === Bottom button operations ===

    /// Call `WebApp.MainButton.show()` or `WebApp.SecondaryButton.show()`.
    ///
    /// # Errors
    /// Returns [`JsValue`] if the underlying JS call fails.
    pub fn show_bottom_button(&self, button: BottomButton) -> Result<(), JsValue> {
        self.bottom_button_method(button, "show", None)
    }

    /// Hide a bottom button.
    ///
    /// # Errors
    /// Returns [`JsValue`] if the underlying JS call fails.
    pub fn hide_bottom_button(&self, button: BottomButton) -> Result<(), JsValue> {
        self.bottom_button_method(button, "hide", None)
    }

    /// Set bottom button text.
    ///
    /// # Errors
    /// Returns [`JsValue`] if the underlying JS call fails.
    pub fn set_bottom_button_text(&self, button: BottomButton, text: &str) -> Result<(), JsValue> {
        self.bottom_button_method(button, "setText", Some(&text.into()))
    }

    /// Set bottom button color (`setColor(color)`).
    ///
    /// # Errors
    /// Returns [`JsValue`] if the underlying JS call fails.
    ///
    /// # Examples
    /// ```no_run
    /// # use telegram_webapp_sdk::webapp::{TelegramWebApp, BottomButton};
    /// # let app = TelegramWebApp::instance().unwrap();
    /// let _ = app.set_bottom_button_color(BottomButton::Main, "#ff0000");
    /// ```
    pub fn set_bottom_button_color(
        &self,
        button: BottomButton,
        color: &str
    ) -> Result<(), JsValue> {
        self.bottom_button_method(button, "setColor", Some(&color.into()))
    }

    /// Set bottom button text color (`setTextColor(color)`).
    ///
    /// # Errors
    /// Returns [`JsValue`] if the underlying JS call fails.
    ///
    /// # Examples
    /// ```no_run
    /// # use telegram_webapp_sdk::webapp::{TelegramWebApp, BottomButton};
    /// # let app = TelegramWebApp::instance().unwrap();
    /// let _ = app.set_bottom_button_text_color(BottomButton::Main, "#ffffff");
    /// ```
    pub fn set_bottom_button_text_color(
        &self,
        button: BottomButton,
        color: &str
    ) -> Result<(), JsValue> {
        self.bottom_button_method(button, "setTextColor", Some(&color.into()))
    }

    /// Set custom emoji icon for a bottom button (Bot API 9.5+).
    ///
    /// Sets the custom emoji ID to be displayed as an icon on the button.
    /// Use an empty string to remove the icon.
    ///
    /// # Arguments
    /// * `button` - The button to update (Main or Secondary)
    /// * `icon_id` - The custom emoji ID (e.g., "123456789")
    ///
    /// # Errors
    /// Returns [`JsValue`] if the underlying JS call fails.
    ///
    /// # Examples
    /// ```no_run
    /// use telegram_webapp_sdk::webapp::{BottomButton, TelegramWebApp};
    ///
    /// if let Some(app) = TelegramWebApp::instance() {
    ///     let _ = app.set_bottom_button_icon_custom_emoji_id(BottomButton::Main, "123456789");
    /// }
    /// ```
    pub fn set_bottom_button_icon_custom_emoji_id(
        &self,
        button: BottomButton,
        icon_id: &str
    ) -> Result<(), JsValue> {
        self.bottom_button_method(button, "setIconCustomEmojiId", Some(&icon_id.into()))
    }

    /// Enable a bottom button, allowing user interaction.
    ///
    /// # Examples
    /// ```no_run
    /// use telegram_webapp_sdk::webapp::{BottomButton, TelegramWebApp};
    ///
    /// if let Some(app) = TelegramWebApp::instance() {
    ///     let _ = app.enable_bottom_button(BottomButton::Main);
    /// }
    /// ```
    pub fn enable_bottom_button(&self, button: BottomButton) -> Result<(), JsValue> {
        self.bottom_button_method(button, "enable", None)
    }

    /// Disable a bottom button, preventing user interaction.
    ///
    /// # Examples
    /// ```no_run
    /// use telegram_webapp_sdk::webapp::{BottomButton, TelegramWebApp};
    ///
    /// if let Some(app) = TelegramWebApp::instance() {
    ///     let _ = app.disable_bottom_button(BottomButton::Main);
    /// }
    /// ```
    pub fn disable_bottom_button(&self, button: BottomButton) -> Result<(), JsValue> {
        self.bottom_button_method(button, "disable", None)
    }

    /// Show the circular loading indicator on a bottom button.
    ///
    /// # Examples
    /// ```no_run
    /// use telegram_webapp_sdk::webapp::{BottomButton, TelegramWebApp};
    ///
    /// if let Some(app) = TelegramWebApp::instance() {
    ///     let _ = app.show_bottom_button_progress(BottomButton::Main, false);
    /// }
    /// ```
    pub fn show_bottom_button_progress(
        &self,
        button: BottomButton,
        leave_active: bool
    ) -> Result<(), JsValue> {
        let leave_active = JsValue::from_bool(leave_active);
        self.bottom_button_method(button, "showProgress", Some(&leave_active))
    }

    /// Hide the loading indicator on a bottom button.
    ///
    /// # Examples
    /// ```no_run
    /// use telegram_webapp_sdk::webapp::{BottomButton, TelegramWebApp};
    ///
    /// if let Some(app) = TelegramWebApp::instance() {
    ///     let _ = app.hide_bottom_button_progress(BottomButton::Main);
    /// }
    /// ```
    pub fn hide_bottom_button_progress(&self, button: BottomButton) -> Result<(), JsValue> {
        self.bottom_button_method(button, "hideProgress", None)
    }

    /// Returns whether the specified bottom button is currently visible.
    ///
    /// # Examples
    /// ```no_run
    /// use telegram_webapp_sdk::webapp::{BottomButton, TelegramWebApp};
    ///
    /// if let Some(app) = TelegramWebApp::instance() {
    ///     let _ = app.is_bottom_button_visible(BottomButton::Main);
    /// }
    /// ```
    pub fn is_bottom_button_visible(&self, button: BottomButton) -> bool {
        self.bottom_button_property(button, "isVisible")
            .and_then(|v| v.as_bool())
            .unwrap_or(false)
    }

    /// Returns whether the specified bottom button is active (enabled).
    ///
    /// # Examples
    /// ```no_run
    /// use telegram_webapp_sdk::webapp::{BottomButton, TelegramWebApp};
    ///
    /// if let Some(app) = TelegramWebApp::instance() {
    ///     let _ = app.is_bottom_button_active(BottomButton::Main);
    /// }
    /// ```
    pub fn is_bottom_button_active(&self, button: BottomButton) -> bool {
        self.bottom_button_property(button, "isActive")
            .and_then(|v| v.as_bool())
            .unwrap_or(false)
    }

    /// Returns whether the progress indicator is visible on the button.
    ///
    /// # Examples
    /// ```no_run
    /// use telegram_webapp_sdk::webapp::{BottomButton, TelegramWebApp};
    ///
    /// if let Some(app) = TelegramWebApp::instance() {
    ///     let _ = app.is_bottom_button_progress_visible(BottomButton::Main);
    /// }
    /// ```
    pub fn is_bottom_button_progress_visible(&self, button: BottomButton) -> bool {
        self.bottom_button_property(button, "isProgressVisible")
            .and_then(|v| v.as_bool())
            .unwrap_or(false)
    }

    /// Returns the current text displayed on the button.
    ///
    /// # Examples
    /// ```no_run
    /// use telegram_webapp_sdk::webapp::{BottomButton, TelegramWebApp};
    ///
    /// if let Some(app) = TelegramWebApp::instance() {
    ///     let _ = app.bottom_button_text(BottomButton::Main);
    /// }
    /// ```
    pub fn bottom_button_text(&self, button: BottomButton) -> Option<String> {
        self.bottom_button_property(button, "text")?.as_string()
    }

    /// Returns the current custom emoji icon ID of the button (Bot API 9.5+).
    ///
    /// # Arguments
    /// * `button` - The button to query (Main or Secondary)
    ///
    /// # Examples
    /// ```no_run
    /// use telegram_webapp_sdk::webapp::{BottomButton, TelegramWebApp};
    ///
    /// if let Some(app) = TelegramWebApp::instance() {
    ///     if let Some(icon_id) = app.bottom_button_icon_custom_emoji_id(BottomButton::Main) {
    ///         println!("Icon ID: {}", icon_id);
    ///     }
    /// }
    /// ```
    pub fn bottom_button_icon_custom_emoji_id(&self, button: BottomButton) -> Option<String> {
        self.bottom_button_property(button, "iconCustomEmojiId")?
            .as_string()
    }

    /// Returns the current text color of the button.
    ///
    /// # Examples
    /// ```no_run
    /// use telegram_webapp_sdk::webapp::{BottomButton, TelegramWebApp};
    ///
    /// if let Some(app) = TelegramWebApp::instance() {
    ///     let _ = app.bottom_button_text_color(BottomButton::Main);
    /// }
    /// ```
    pub fn bottom_button_text_color(&self, button: BottomButton) -> Option<String> {
        self.bottom_button_property(button, "textColor")?
            .as_string()
    }

    /// Returns the current background color of the button.
    ///
    /// # Examples
    /// ```no_run
    /// use telegram_webapp_sdk::webapp::{BottomButton, TelegramWebApp};
    ///
    /// if let Some(app) = TelegramWebApp::instance() {
    ///     let _ = app.bottom_button_color(BottomButton::Main);
    /// }
    /// ```
    pub fn bottom_button_color(&self, button: BottomButton) -> Option<String> {
        self.bottom_button_property(button, "color")?.as_string()
    }

    /// Returns whether the shine effect is enabled on the button.
    ///
    /// # Examples
    /// ```no_run
    /// use telegram_webapp_sdk::webapp::{BottomButton, TelegramWebApp};
    ///
    /// if let Some(app) = TelegramWebApp::instance() {
    ///     let _ = app.bottom_button_has_shine_effect(BottomButton::Main);
    /// }
    /// ```
    pub fn bottom_button_has_shine_effect(&self, button: BottomButton) -> bool {
        self.bottom_button_property(button, "hasShineEffect")
            .and_then(|v| v.as_bool())
            .unwrap_or(false)
    }

    /// Update bottom button state via `setParams`.
    ///
    /// # Examples
    /// ```no_run
    /// 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, &params);
    /// }
    /// ```
    pub fn set_bottom_button_params(
        &self,
        button: BottomButton,
        params: &BottomButtonParams<'_>
    ) -> Result<(), JsValue> {
        let value = to_value(params).map_err(|err| JsValue::from_str(&err.to_string()))?;
        self.bottom_button_method(button, "setParams", Some(&value))
    }

    /// Update secondary button state via `setParams`, including position.
    ///
    /// # Examples
    /// ```no_run
    /// 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(&params);
    /// }
    /// ```
    pub fn set_secondary_button_params(
        &self,
        params: &SecondaryButtonParams<'_>
    ) -> Result<(), JsValue> {
        let value = to_value(params).map_err(|err| JsValue::from_str(&err.to_string()))?;
        self.bottom_button_method(BottomButton::Secondary, "setParams", Some(&value))
    }

    /// Returns the configured position of the secondary button, if available.
    ///
    /// # Examples
    /// ```no_run
    /// use telegram_webapp_sdk::webapp::{SecondaryButtonPosition, TelegramWebApp};
    ///
    /// if let Some(app) = TelegramWebApp::instance() {
    ///     let _ = app.secondary_button_position();
    /// }
    /// ```
    pub fn secondary_button_position(&self) -> Option<SecondaryButtonPosition> {
        self.bottom_button_property(BottomButton::Secondary, "position")
            .and_then(SecondaryButtonPosition::from_js_value)
    }

    /// 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.
    pub fn set_bottom_button_callback<F>(
        &self,
        button: BottomButton,
        callback: F
    ) -> Result<EventHandle<dyn FnMut()>, JsValue>
    where
        F: 'static + Fn()
    {
        let btn_val = Reflect::get(&self.inner, &button.js_name().into())?;
        let btn = btn_val.dyn_into::<Object>()?;
        let cb = Closure::<dyn FnMut()>::new(callback);
        let f = Reflect::get(&btn, &"onClick".into())?;
        let func = f
            .dyn_ref::<Function>()
            .ok_or_else(|| JsValue::from_str("onClick is not a function"))?;
        func.call1(&btn, cb.as_ref().unchecked_ref())?;
        Ok(EventHandle::new(btn, "offClick", None, cb))
    }

    /// Remove previously set bottom button callback.
    ///
    /// # Errors
    /// Returns [`JsValue`] if the underlying JS call fails.
    pub fn remove_bottom_button_callback(
        &self,
        handle: EventHandle<dyn FnMut()>
    ) -> Result<(), JsValue> {
        handle.unregister()
    }

    // === Back button operations ===

    /// Show back button.
    ///
    /// # Errors
    /// Returns [`JsValue`] if the underlying JS call fails.
    pub fn show_back_button(&self) -> Result<(), JsValue> {
        self.call_nested0("BackButton", "show")
    }

    /// Hide back button.
    ///
    /// # Errors
    /// Returns [`JsValue`] if the underlying JS call fails.
    pub fn hide_back_button(&self) -> Result<(), JsValue> {
        self.call_nested0("BackButton", "hide")
    }

    /// Registers a callback for the native back button.
    ///
    /// Returns an [`EventHandle`] that can be passed to
    /// [`remove_back_button_callback`](Self::remove_back_button_callback).
    ///
    /// # Examples
    /// ```no_run
    /// # use telegram_webapp_sdk::webapp::TelegramWebApp;
    /// # let app = TelegramWebApp::instance().unwrap();
    /// 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.
    pub fn set_back_button_callback<F>(
        &self,
        callback: F
    ) -> Result<EventHandle<dyn FnMut()>, JsValue>
    where
        F: 'static + Fn()
    {
        let back_button_val = Reflect::get(&self.inner, &"BackButton".into())?;
        let back_button = back_button_val.dyn_into::<Object>()?;
        let cb = Closure::<dyn FnMut()>::new(callback);
        let f = Reflect::get(&back_button, &"onClick".into())?;
        let func = f
            .dyn_ref::<Function>()
            .ok_or_else(|| JsValue::from_str("onClick is not a function"))?;
        func.call1(&back_button, cb.as_ref().unchecked_ref())?;
        Ok(EventHandle::new(back_button, "offClick", None, cb))
    }

    /// Remove previously set back button callback.
    ///
    /// # Errors
    /// Returns [`JsValue`] if the underlying JS call fails.
    pub fn remove_back_button_callback(
        &self,
        handle: EventHandle<dyn FnMut()>
    ) -> Result<(), JsValue> {
        handle.unregister()
    }

    /// Returns whether the native back button is visible.
    ///
    /// # Examples
    /// ```no_run
    /// # use telegram_webapp_sdk::webapp::TelegramWebApp;
    /// # let app = TelegramWebApp::instance().unwrap();
    /// let _ = app.is_back_button_visible();
    /// ```
    pub fn is_back_button_visible(&self) -> bool {
        Reflect::get(&self.inner, &"BackButton".into())
            .ok()
            .and_then(|bb| Reflect::get(&bb, &"isVisible".into()).ok())
            .and_then(|v| v.as_bool())
            .unwrap_or(false)
    }

    // === Legacy aliases for main button ===

    /// Legacy alias for [`Self::show_bottom_button`] with
    /// [`BottomButton::Main`].
    pub fn show_main_button(&self) -> Result<(), JsValue> {
        self.show_bottom_button(BottomButton::Main)
    }

    /// Legacy alias for [`Self::hide_bottom_button`] with
    /// [`BottomButton::Main`].
    pub fn hide_main_button(&self) -> Result<(), JsValue> {
        self.hide_bottom_button(BottomButton::Main)
    }

    /// Legacy alias for [`Self::set_bottom_button_text`] with
    /// [`BottomButton::Main`].
    pub fn set_main_button_text(&self, text: &str) -> Result<(), JsValue> {
        self.set_bottom_button_text(BottomButton::Main, text)
    }

    /// Legacy alias for [`Self::set_bottom_button_color`] with
    /// [`BottomButton::Main`].
    pub fn set_main_button_color(&self, color: &str) -> Result<(), JsValue> {
        self.set_bottom_button_color(BottomButton::Main, color)
    }

    /// Legacy alias for [`Self::set_bottom_button_text_color`] with
    /// [`BottomButton::Main`].
    pub fn set_main_button_text_color(&self, color: &str) -> Result<(), JsValue> {
        self.set_bottom_button_text_color(BottomButton::Main, color)
    }

    /// Set custom emoji icon for the main button (Bot API 9.5+).
    ///
    /// Legacy alias for [`Self::set_bottom_button_icon_custom_emoji_id`] with
    /// [`BottomButton::Main`].
    ///
    /// # Examples
    /// ```no_run
    /// use telegram_webapp_sdk::webapp::TelegramWebApp;
    ///
    /// if let Some(app) = TelegramWebApp::instance() {
    ///     let _ = app.set_main_button_icon_custom_emoji_id("123456789");
    /// }
    /// ```
    pub fn set_main_button_icon_custom_emoji_id(&self, icon_id: &str) -> Result<(), JsValue> {
        self.set_bottom_button_icon_custom_emoji_id(BottomButton::Main, icon_id)
    }

    /// Enable the main bottom button.
    ///
    /// # Examples
    /// ```no_run
    /// use telegram_webapp_sdk::webapp::TelegramWebApp;
    ///
    /// if let Some(app) = TelegramWebApp::instance() {
    ///     let _ = app.enable_main_button();
    /// }
    /// ```
    pub fn enable_main_button(&self) -> Result<(), JsValue> {
        self.enable_bottom_button(BottomButton::Main)
    }

    /// Disable the main bottom button.
    ///
    /// # Examples
    /// ```no_run
    /// use telegram_webapp_sdk::webapp::TelegramWebApp;
    ///
    /// if let Some(app) = TelegramWebApp::instance() {
    ///     let _ = app.disable_main_button();
    /// }
    /// ```
    pub fn disable_main_button(&self) -> Result<(), JsValue> {
        self.disable_bottom_button(BottomButton::Main)
    }

    /// Show progress on the main bottom button.
    ///
    /// # Examples
    /// ```no_run
    /// use telegram_webapp_sdk::webapp::TelegramWebApp;
    ///
    /// if let Some(app) = TelegramWebApp::instance() {
    ///     let _ = app.show_main_button_progress(false);
    /// }
    /// ```
    pub fn show_main_button_progress(&self, leave_active: bool) -> Result<(), JsValue> {
        self.show_bottom_button_progress(BottomButton::Main, leave_active)
    }

    /// Hide progress indicator from the main bottom button.
    ///
    /// # Examples
    /// ```no_run
    /// use telegram_webapp_sdk::webapp::TelegramWebApp;
    ///
    /// if let Some(app) = TelegramWebApp::instance() {
    ///     let _ = app.hide_main_button_progress();
    /// }
    /// ```
    pub fn hide_main_button_progress(&self) -> Result<(), JsValue> {
        self.hide_bottom_button_progress(BottomButton::Main)
    }

    /// Update the main button state via
    /// [`set_bottom_button_params`](Self::set_bottom_button_params).
    pub fn set_main_button_params(&self, params: &BottomButtonParams<'_>) -> Result<(), JsValue> {
        self.set_bottom_button_params(BottomButton::Main, params)
    }

    /// Legacy alias for [`Self::set_bottom_button_callback`] with
    /// [`BottomButton::Main`].
    pub fn set_main_button_callback<F>(
        &self,
        callback: F
    ) -> Result<EventHandle<dyn FnMut()>, JsValue>
    where
        F: 'static + Fn()
    {
        self.set_bottom_button_callback(BottomButton::Main, callback)
    }

    /// Remove callback for the main button.
    ///
    /// Legacy alias for [`Self::remove_bottom_button_callback`].
    pub fn remove_main_button_callback(
        &self,
        handle: EventHandle<dyn FnMut()>
    ) -> Result<(), JsValue> {
        self.remove_bottom_button_callback(handle)
    }

    // === Secondary button convenience methods ===

    /// Show the secondary bottom button.
    pub fn show_secondary_button(&self) -> Result<(), JsValue> {
        self.show_bottom_button(BottomButton::Secondary)
    }

    /// Hide the secondary bottom button.
    pub fn hide_secondary_button(&self) -> Result<(), JsValue> {
        self.hide_bottom_button(BottomButton::Secondary)
    }

    /// Set text for the secondary bottom button.
    pub fn set_secondary_button_text(&self, text: &str) -> Result<(), JsValue> {
        self.set_bottom_button_text(BottomButton::Secondary, text)
    }

    /// Set color for the secondary bottom button.
    pub fn set_secondary_button_color(&self, color: &str) -> Result<(), JsValue> {
        self.set_bottom_button_color(BottomButton::Secondary, color)
    }

    /// Set text color for the secondary bottom button.
    pub fn set_secondary_button_text_color(&self, color: &str) -> Result<(), JsValue> {
        self.set_bottom_button_text_color(BottomButton::Secondary, color)
    }

    /// Set custom emoji icon for the secondary button (Bot API 9.5+).
    ///
    /// Convenience alias for [`Self::set_bottom_button_icon_custom_emoji_id`]
    /// with [`BottomButton::Secondary`].
    ///
    /// # Examples
    /// ```no_run
    /// use telegram_webapp_sdk::webapp::TelegramWebApp;
    ///
    /// if let Some(app) = TelegramWebApp::instance() {
    ///     let _ = app.set_secondary_button_icon_custom_emoji_id("123456789");
    /// }
    /// ```
    pub fn set_secondary_button_icon_custom_emoji_id(&self, icon_id: &str) -> Result<(), JsValue> {
        self.set_bottom_button_icon_custom_emoji_id(BottomButton::Secondary, icon_id)
    }

    /// Enable the secondary bottom button.
    ///
    /// # Examples
    /// ```no_run
    /// use telegram_webapp_sdk::webapp::TelegramWebApp;
    ///
    /// if let Some(app) = TelegramWebApp::instance() {
    ///     let _ = app.enable_secondary_button();
    /// }
    /// ```
    pub fn enable_secondary_button(&self) -> Result<(), JsValue> {
        self.enable_bottom_button(BottomButton::Secondary)
    }

    /// Disable the secondary bottom button.
    ///
    /// # Examples
    /// ```no_run
    /// use telegram_webapp_sdk::webapp::TelegramWebApp;
    ///
    /// if let Some(app) = TelegramWebApp::instance() {
    ///     let _ = app.disable_secondary_button();
    /// }
    /// ```
    pub fn disable_secondary_button(&self) -> Result<(), JsValue> {
        self.disable_bottom_button(BottomButton::Secondary)
    }

    /// Show progress on the secondary bottom button.
    ///
    /// # Examples
    /// ```no_run
    /// use telegram_webapp_sdk::webapp::TelegramWebApp;
    ///
    /// if let Some(app) = TelegramWebApp::instance() {
    ///     let _ = app.show_secondary_button_progress(false);
    /// }
    /// ```
    pub fn show_secondary_button_progress(&self, leave_active: bool) -> Result<(), JsValue> {
        self.show_bottom_button_progress(BottomButton::Secondary, leave_active)
    }

    /// Hide progress indicator from the secondary bottom button.
    ///
    /// # Examples
    /// ```no_run
    /// use telegram_webapp_sdk::webapp::TelegramWebApp;
    ///
    /// if let Some(app) = TelegramWebApp::instance() {
    ///     let _ = app.hide_secondary_button_progress();
    /// }
    /// ```
    pub fn hide_secondary_button_progress(&self) -> Result<(), JsValue> {
        self.hide_bottom_button_progress(BottomButton::Secondary)
    }

    /// Set callback for the secondary bottom button.
    pub fn set_secondary_button_callback<F>(
        &self,
        callback: F
    ) -> Result<EventHandle<dyn FnMut()>, JsValue>
    where
        F: 'static + Fn()
    {
        self.set_bottom_button_callback(BottomButton::Secondary, callback)
    }

    /// Remove callback for the secondary bottom button.
    pub fn remove_secondary_button_callback(
        &self,
        handle: EventHandle<dyn FnMut()>
    ) -> Result<(), JsValue> {
        self.remove_bottom_button_callback(handle)
    }

    /// Hide the on-screen keyboard.
    /// Call `WebApp.hideKeyboard()`.
    ///
    /// # Examples
    /// ```no_run
    /// # use telegram_webapp_sdk::webapp::TelegramWebApp;
    /// # let app = TelegramWebApp::instance().unwrap();
    /// app.hide_keyboard().unwrap();
    /// ```
    ///
    /// # Errors
    /// Returns [`JsValue`] if the underlying JS call fails.
    pub fn hide_keyboard(&self) -> Result<(), JsValue> {
        self.call0("hideKeyboard")
    }
}