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
// Copyright (c) Ankit Chaubey <ankitchaubey.dev@gmail.com>
//
// ferogram: async Telegram MTProto client in Rust
// https://github.com/ankit-chaubey/ferogram
//
// Licensed under either the MIT License or the Apache License 2.0.
// See the LICENSE-MIT or LICENSE-APACHE file in this repository:
// https://github.com/ankit-chaubey/ferogram
//
// Feel free to use, modify, and share this code.
// Please keep this notice when redistributing.
use ferogram_tl_types as tl;
// Button
/// A single inline keyboard button.
#[derive(Clone)]
pub struct Button {
inner: tl::enums::KeyboardButton,
}
impl Button {
/// A button that sends a callback data payload when pressed.
pub fn callback(text: impl Into<String>, data: impl Into<Vec<u8>>) -> Self {
Self {
inner: tl::enums::KeyboardButton::Callback(tl::types::KeyboardButtonCallback {
requires_password: false,
text: text.into(),
data: data.into(),
style: None,
}),
}
}
/// A button that opens a URL in the browser.
pub fn url(text: impl Into<String>, url: impl Into<String>) -> Self {
Self {
inner: tl::enums::KeyboardButton::Url(tl::types::KeyboardButtonUrl {
text: text.into(),
url: url.into(),
style: None,
}),
}
}
/// A button that opens a user-profile or bot link in Telegram.
pub fn url_auth(
text: impl Into<String>,
url: impl Into<String>,
fwd_text: Option<String>,
bot: tl::enums::InputUser,
) -> Self {
Self {
inner: tl::enums::KeyboardButton::InputKeyboardButtonUrlAuth(
tl::types::InputKeyboardButtonUrlAuth {
request_write_access: false,
text: text.into(),
fwd_text,
url: url.into(),
bot,
style: None,
},
),
}
}
/// A button that switches to inline mode in the current chat.
pub fn switch_inline(text: impl Into<String>, query: impl Into<String>) -> Self {
Self {
inner: tl::enums::KeyboardButton::SwitchInline(tl::types::KeyboardButtonSwitchInline {
same_peer: true,
peer_types: None,
text: text.into(),
query: query.into(),
style: None,
}),
}
}
/// A plain text button (for reply keyboards, not inline).
pub fn text(label: impl Into<String>) -> Self {
Self {
inner: tl::enums::KeyboardButton::KeyboardButton(tl::types::KeyboardButton {
text: label.into(),
style: None,
}),
}
}
/// A button that switches to inline mode in a different (user-chosen) chat.
pub fn switch_elsewhere(text: impl Into<String>, query: impl Into<String>) -> Self {
Self {
inner: tl::enums::KeyboardButton::SwitchInline(tl::types::KeyboardButtonSwitchInline {
same_peer: false,
peer_types: None,
text: text.into(),
query: query.into(),
style: None,
}),
}
}
/// A button that opens a mini-app (full WebView with JS bridge).
pub fn mini_app(text: impl Into<String>, url: impl Into<String>) -> Self {
Self {
inner: tl::enums::KeyboardButton::WebView(tl::types::KeyboardButtonWebView {
text: text.into(),
url: url.into(),
style: None,
}),
}
}
/// A button that opens a simple mini-app (no JS bridge, no query_id).
pub fn mini_app_simple(text: impl Into<String>, url: impl Into<String>) -> Self {
Self {
inner: tl::enums::KeyboardButton::SimpleWebView(
tl::types::KeyboardButtonSimpleWebView {
text: text.into(),
url: url.into(),
style: None,
},
),
}
}
/// A button that requests the user's phone number (reply keyboards only).
pub fn request_phone(text: impl Into<String>) -> Self {
Self {
inner: tl::enums::KeyboardButton::RequestPhone(tl::types::KeyboardButtonRequestPhone {
text: text.into(),
style: None,
}),
}
}
/// A button that requests the user's location (reply keyboards only).
pub fn request_geo(text: impl Into<String>) -> Self {
Self {
inner: tl::enums::KeyboardButton::RequestGeoLocation(
tl::types::KeyboardButtonRequestGeoLocation {
text: text.into(),
style: None,
},
),
}
}
/// A button that requests the user to create/share a poll.
pub fn request_poll(text: impl Into<String>) -> Self {
Self {
inner: tl::enums::KeyboardButton::RequestPoll(tl::types::KeyboardButtonRequestPoll {
quiz: None,
text: text.into(),
style: None,
}),
}
}
/// A button that requests the user to create/share a quiz.
pub fn request_quiz(text: impl Into<String>) -> Self {
Self {
inner: tl::enums::KeyboardButton::RequestPoll(tl::types::KeyboardButtonRequestPoll {
quiz: Some(true),
text: text.into(),
style: None,
}),
}
}
/// A button that launches a game (bots only).
pub fn game(text: impl Into<String>) -> Self {
Self {
inner: tl::enums::KeyboardButton::Game(tl::types::KeyboardButtonGame {
text: text.into(),
style: None,
}),
}
}
/// A buy button for payments (bots only).
pub fn buy(text: impl Into<String>) -> Self {
Self {
inner: tl::enums::KeyboardButton::Buy(tl::types::KeyboardButtonBuy {
text: text.into(),
style: None,
}),
}
}
/// A copy-to-clipboard button.
pub fn copy_text(text: impl Into<String>, copy_text: impl Into<String>) -> Self {
Self {
inner: tl::enums::KeyboardButton::Copy(tl::types::KeyboardButtonCopy {
text: text.into(),
copy_text: copy_text.into(),
style: None,
}),
}
}
/// Consume into the raw TL type.
pub fn into_raw(self) -> tl::enums::KeyboardButton {
self.inner
}
}
// InlineKeyboard
/// Builder for an inline keyboard reply markup.
///
/// Each call to [`row`](InlineKeyboard::row) adds a new horizontal row of
/// buttons. Rows are displayed top-to-bottom.
///
/// # Example
/// ```rust,no_run
/// use ferogram::keyboard::{InlineKeyboard, Button};
///
/// let kb = InlineKeyboard::new()
/// .row([Button::callback("Option A", b"a"),
/// Button::callback("Option B", b"b")])
/// .row([Button::url("More info", "https://example.com")]);
/// ```
#[derive(Clone, Default)]
pub struct InlineKeyboard {
rows: Vec<Vec<Button>>,
}
impl InlineKeyboard {
/// Create an empty keyboard. Add rows with [`row`](Self::row).
pub fn new() -> Self {
Self::default()
}
/// Append a row of buttons.
pub fn row(mut self, buttons: impl IntoIterator<Item = Button>) -> Self {
self.rows.push(buttons.into_iter().collect());
self
}
/// Convert to the `ReplyMarkup` TL type expected by message-sending functions.
pub fn into_markup(self) -> tl::enums::ReplyMarkup {
let rows = self
.rows
.into_iter()
.map(|row| {
tl::enums::KeyboardButtonRow::KeyboardButtonRow(tl::types::KeyboardButtonRow {
buttons: row.into_iter().map(Button::into_raw).collect(),
})
})
.collect();
tl::enums::ReplyMarkup::ReplyInlineMarkup(tl::types::ReplyInlineMarkup { rows })
}
}
impl From<InlineKeyboard> for tl::enums::ReplyMarkup {
fn from(kb: InlineKeyboard) -> Self {
kb.into_markup()
}
}
// ReplyKeyboard
/// Builder for a reply keyboard (shown below the message input box).
#[derive(Clone, Default)]
pub struct ReplyKeyboard {
rows: Vec<Vec<Button>>,
resize: bool,
single_use: bool,
selective: bool,
}
impl ReplyKeyboard {
/// Create a new empty reply keyboard.
pub fn new() -> Self {
Self::default()
}
/// Append a row of text buttons.
pub fn row(mut self, buttons: impl IntoIterator<Item = Button>) -> Self {
self.rows.push(buttons.into_iter().collect());
self
}
/// Resize keyboard to fit its content (recommended).
pub fn resize(mut self) -> Self {
self.resize = true;
self
}
/// Hide keyboard after a single press.
pub fn single_use(mut self) -> Self {
self.single_use = true;
self
}
/// Show keyboard only to mentioned/replied users.
pub fn selective(mut self) -> Self {
self.selective = true;
self
}
/// Convert to `ReplyMarkup`.
pub fn into_markup(self) -> tl::enums::ReplyMarkup {
let rows = self
.rows
.into_iter()
.map(|row| {
tl::enums::KeyboardButtonRow::KeyboardButtonRow(tl::types::KeyboardButtonRow {
buttons: row.into_iter().map(Button::into_raw).collect(),
})
})
.collect();
tl::enums::ReplyMarkup::ReplyKeyboardMarkup(tl::types::ReplyKeyboardMarkup {
resize: self.resize,
single_use: self.single_use,
selective: self.selective,
persistent: false,
rows,
placeholder: None,
})
}
}
impl From<ReplyKeyboard> for tl::enums::ReplyMarkup {
fn from(kb: ReplyKeyboard) -> Self {
kb.into_markup()
}
}