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
// 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, prelude::Closure};
use crate::webapp::{
TelegramWebApp,
core::{await_one_shot, one_shot_promise},
types::OpenLinkOptions
};
impl TelegramWebApp {
/// Call `WebApp.openLink(url)`.
///
/// # Examples
/// ```no_run
/// # use telegram_webapp_sdk::webapp::TelegramWebApp;
/// # let app = TelegramWebApp::instance().unwrap();
/// app.open_link("https://example.com", None).unwrap();
/// ```
pub fn open_link(&self, url: &str, options: Option<&OpenLinkOptions>) -> Result<(), JsValue> {
let f = Reflect::get(&self.inner, &"openLink".into())?;
let func = f
.dyn_ref::<Function>()
.ok_or_else(|| JsValue::from_str("openLink is not a function"))?;
match options {
Some(opts) => {
let value = to_value(opts).map_err(|err| JsValue::from_str(&err.to_string()))?;
func.call2(&self.inner, &url.into(), &value)?;
}
None => {
func.call1(&self.inner, &url.into())?;
}
}
Ok(())
}
/// Call `WebApp.openTelegramLink(url)`.
///
/// # Examples
/// ```no_run
/// # use telegram_webapp_sdk::webapp::TelegramWebApp;
/// # let app = TelegramWebApp::instance().unwrap();
/// app.open_telegram_link("https://t.me/telegram").unwrap();
/// ```
pub fn open_telegram_link(&self, url: &str) -> Result<(), JsValue> {
Reflect::get(&self.inner, &"openTelegramLink".into())?
.dyn_into::<Function>()?
.call1(&self.inner, &url.into())?;
Ok(())
}
/// Call `WebApp.switchInlineQuery(query, choose_chat_types)`.
///
/// # Examples
/// ```no_run
/// # use telegram_webapp_sdk::webapp::TelegramWebApp;
/// # let app = TelegramWebApp::instance().unwrap();
/// app.switch_inline_query("query", None).unwrap();
/// ```
///
/// # Errors
/// Returns [`JsValue`] if the underlying JS call fails.
pub fn switch_inline_query(
&self,
query: &str,
choose_chat_types: Option<&JsValue>
) -> Result<(), JsValue> {
let f = Reflect::get(&self.inner, &"switchInlineQuery".into())?;
let func = f
.dyn_ref::<Function>()
.ok_or_else(|| JsValue::from_str("switchInlineQuery is not a function"))?;
match choose_chat_types {
Some(types) => func.call2(&self.inner, &query.into(), types)?,
None => func.call1(&self.inner, &query.into())?
};
Ok(())
}
/// Callback variant of [`Self::share_message`].
///
/// # Errors
/// Returns [`JsValue`] if the underlying JS call fails.
pub fn share_message_with_callback<F>(&self, msg_id: &str, callback: F) -> Result<(), JsValue>
where
F: 'static + FnOnce(bool)
{
let cb = Closure::once_into_js(move |v: JsValue| {
callback(v.as_bool().unwrap_or(false));
});
let f = Reflect::get(&self.inner, &"shareMessage".into())?;
let func = f
.dyn_ref::<Function>()
.ok_or_else(|| JsValue::from_str("shareMessage is not a function"))?;
func.call2(&self.inner, &msg_id.into(), &cb)?;
Ok(())
}
/// Async wrapper over `WebApp.shareMessage`. Resolves with `true` when the
/// prepared message was sent.
///
/// # Examples
/// ```no_run
/// # use telegram_webapp_sdk::webapp::TelegramWebApp;
/// # async fn run() -> Result<(), wasm_bindgen::JsValue> {
/// let app = TelegramWebApp::try_instance()?;
/// let sent: bool = app.share_message("id123").await?;
/// let _ = sent;
/// # Ok(())
/// # }
/// ```
///
/// # Errors
/// Returns [`JsValue`] if the underlying JS call fails.
pub async fn share_message(&self, msg_id: &str) -> Result<bool, JsValue> {
let webapp = self.inner.clone();
let msg_id = msg_id.to_owned();
let promise = one_shot_promise(move |resolve, _reject| {
let cb = Closure::once_into_js(move |v: JsValue| {
let _ = resolve.call1(&JsValue::NULL, &v);
});
let f = Reflect::get(&webapp, &"shareMessage".into())?;
let func = f
.dyn_ref::<Function>()
.ok_or_else(|| JsValue::from_str("shareMessage is not a function"))?;
func.call2(&webapp, &msg_id.into(), &cb)?;
Ok(())
});
let value = await_one_shot(promise).await?;
Ok(value.as_bool().unwrap_or(false))
}
/// Call `WebApp.shareToStory(media_url, params)`.
///
/// # Examples
/// ```no_run
/// # use js_sys::Object;
/// # use telegram_webapp_sdk::webapp::TelegramWebApp;
/// # let app = TelegramWebApp::instance().unwrap();
/// let params = Object::new();
/// app.share_to_story("https://example.com/image.png", Some(¶ms.into()))
/// .unwrap();
/// ```
///
/// # Errors
/// Returns [`JsValue`] if the underlying JS call fails.
pub fn share_to_story(
&self,
media_url: &str,
params: Option<&JsValue>
) -> Result<(), JsValue> {
let f = Reflect::get(&self.inner, &"shareToStory".into())?;
let func = f
.dyn_ref::<Function>()
.ok_or_else(|| JsValue::from_str("shareToStory is not a function"))?;
match params {
Some(p) => func.call2(&self.inner, &media_url.into(), p)?,
None => func.call1(&self.inner, &media_url.into())?
};
Ok(())
}
/// Call `WebApp.shareURL(url, text)`.
///
/// # Examples
/// ```no_run
/// # use telegram_webapp_sdk::webapp::TelegramWebApp;
/// # let app = TelegramWebApp::instance().unwrap();
/// app.share_url("https://example.com", Some("Check this"))
/// .unwrap();
/// ```
///
/// # Errors
/// Returns [`JsValue`] if the underlying JS call fails.
pub fn share_url(&self, url: &str, text: Option<&str>) -> Result<(), JsValue> {
let f = Reflect::get(&self.inner, &"shareURL".into())?;
let func = f
.dyn_ref::<Function>()
.ok_or_else(|| JsValue::from_str("shareURL is not a function"))?;
match text {
Some(t) => func.call2(&self.inner, &url.into(), &t.into())?,
None => func.call1(&self.inner, &url.into())?
};
Ok(())
}
/// Callback variant of [`Self::request_chat`] (Bot API 9.6+).
///
/// # Errors
/// Returns [`JsValue`] if the underlying JS call fails.
pub fn request_chat_with_callback<F>(&self, req_id: i32, callback: F) -> Result<(), JsValue>
where
F: 'static + FnOnce(bool)
{
let cb = Closure::once_into_js(move |v: JsValue| {
callback(v.as_bool().unwrap_or(false));
});
let f = Reflect::get(&self.inner, &"requestChat".into())?;
let func = f
.dyn_ref::<Function>()
.ok_or_else(|| JsValue::from_str("requestChat is not a function"))?;
func.call2(&self.inner, &req_id.into(), &cb)?;
Ok(())
}
/// Async wrapper over `WebApp.requestChat` (Bot API 9.6+). Resolves with
/// `true` when the user picks a chat, `false` on cancel/failure.
///
/// # Examples
/// ```no_run
/// # use telegram_webapp_sdk::webapp::TelegramWebApp;
/// # async fn run() -> Result<(), wasm_bindgen::JsValue> {
/// let app = TelegramWebApp::try_instance()?;
/// let sent: bool = app.request_chat(42).await?;
/// let _ = sent;
/// # Ok(())
/// # }
/// ```
///
/// # Errors
/// Returns [`JsValue`] if the underlying JS call fails (including when the
/// running Telegram client predates Bot API 9.6).
pub async fn request_chat(&self, req_id: i32) -> Result<bool, JsValue> {
let webapp = self.inner.clone();
let promise = one_shot_promise(move |resolve, _reject| {
let cb = Closure::once_into_js(move |v: JsValue| {
let _ = resolve.call1(&JsValue::NULL, &v);
});
let f = Reflect::get(&webapp, &"requestChat".into())?;
let func = f
.dyn_ref::<Function>()
.ok_or_else(|| JsValue::from_str("requestChat is not a function"))?;
func.call2(&webapp, &req_id.into(), &cb)?;
Ok(())
});
let value = await_one_shot(promise).await?;
Ok(value.as_bool().unwrap_or(false))
}
/// Call `WebApp.addToHomeScreen()` and return whether the prompt was shown.
///
/// # Examples
/// ```no_run
/// # use telegram_webapp_sdk::webapp::TelegramWebApp;
/// # let app = TelegramWebApp::instance().unwrap();
/// let _shown = app.add_to_home_screen().unwrap();
/// ```
pub fn add_to_home_screen(&self) -> Result<bool, JsValue> {
let f = Reflect::get(&self.inner, &"addToHomeScreen".into())?;
let func = f
.dyn_ref::<Function>()
.ok_or_else(|| JsValue::from_str("addToHomeScreen is not a function"))?;
let result = func.call0(&self.inner)?;
Ok(result.as_bool().unwrap_or(false))
}
/// Callback variant of [`Self::check_home_screen_status`].
pub fn check_home_screen_status_with_callback<F>(&self, callback: F) -> Result<(), JsValue>
where
F: 'static + FnOnce(String)
{
let cb = Closure::once_into_js(move |status: JsValue| {
callback(status.as_string().unwrap_or_default());
});
let f = Reflect::get(&self.inner, &"checkHomeScreenStatus".into())?;
let func = f
.dyn_ref::<Function>()
.ok_or_else(|| JsValue::from_str("checkHomeScreenStatus is not a function"))?;
func.call1(&self.inner, &cb)?;
Ok(())
}
/// Async wrapper over `WebApp.checkHomeScreenStatus`. Resolves with the
/// status string Telegram returns (e.g. `"added"`, `"missed"`).
///
/// # Errors
/// Returns [`JsValue`] if the underlying JS call fails.
pub async fn check_home_screen_status(&self) -> Result<String, JsValue> {
let webapp = self.inner.clone();
let promise = one_shot_promise(move |resolve, _reject| {
let cb = Closure::once_into_js(move |status: JsValue| {
let _ = resolve.call1(&JsValue::NULL, &status);
});
let f = Reflect::get(&webapp, &"checkHomeScreenStatus".into())?;
let func = f
.dyn_ref::<Function>()
.ok_or_else(|| JsValue::from_str("checkHomeScreenStatus is not a function"))?;
func.call1(&webapp, &cb)?;
Ok(())
});
let value = await_one_shot(promise).await?;
Ok(value.as_string().unwrap_or_default())
}
}