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
use std::{error::Error, fmt};
use serde::{Deserialize, Serialize};
use crate::{
api::{Method, Payload},
types::{Audio, Integer, ParseMode, PhotoSize},
};
/// Represents the date of birth of a user.
#[serde_with::skip_serializing_none]
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
pub struct Birthdate {
/// Day of the user's birth; 1-31
pub day: Integer,
/// Month of the user's birth; 1-12
pub month: Integer,
/// Year of the user's birth
pub year: Option<Integer>,
}
impl Birthdate {
/// Creates a new `Birthdate`.
///
/// # Arguments
///
/// * `day` - Day.
/// * `month` - Month.
pub fn new(day: Integer, month: Integer) -> Self {
Self { day, month, year: None }
}
/// Sets a new year.
///
/// # Arguments
///
/// * `year` - Year.
pub fn with_year(mut self, value: Integer) -> Self {
self.year = Some(value);
self
}
}
/// Contains information about a user that
/// was shared with the bot using a [`crate::types::KeyboardButtonRequestUsers`] button.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
pub struct SharedUser {
/// Identifier of the shared user.
///
/// The bot may not have access to the user and could be unable to use this identifier,
/// unless the user is already known to the bot by some other means.
pub user_id: Integer,
/// First name of the user, if the name was requested by the bot.
pub first_name: Option<String>,
/// Last name of the user, if the name was requested by the bot.
pub last_name: Option<String>,
/// Available sizes of the user photo, if the photo was requested by the bot
pub photo: Option<Vec<PhotoSize>>,
/// Username of the user, if the username was requested by the bot.
pub username: Option<String>,
}
impl SharedUser {
/// Creates a new `SharedUser`.
///
/// # Arguments
///
/// * `user_id` - Identifier of the shared user.
pub fn new(user_id: Integer) -> Self {
Self {
user_id,
first_name: None,
last_name: None,
photo: None,
username: None,
}
}
/// Sets a new first name.
///
/// # Arguments
///
/// * `value` - First name.
pub fn with_first_name<T>(mut self, value: T) -> Self
where
T: Into<String>,
{
self.first_name = Some(value.into());
self
}
/// Sets a new last name.
///
/// # Arguments
///
/// * `value` - Last name.
pub fn with_last_name<T>(mut self, value: T) -> Self
where
T: Into<String>,
{
self.last_name = Some(value.into());
self
}
/// Sets new photo sizes.
///
/// # Arguments
///
/// * `value` - Available sizes of photo.
pub fn with_photo<T>(mut self, value: T) -> Self
where
T: IntoIterator<Item = PhotoSize>,
{
self.photo = Some(value.into_iter().collect());
self
}
/// Sets a new username.
///
/// # Arguments
///
/// * `value` - Username.
pub fn with_username<T>(mut self, value: T) -> Self
where
T: Into<String>,
{
self.username = Some(value.into());
self
}
}
/// Represents a user.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
pub struct User {
/// First name of the user.
pub first_name: String,
/// Unique identifier of the user.
pub id: UserPeerId,
/// Indicates whether the user is a bot.
pub is_bot: bool,
/// Indicates whether the user added the bot to the attachment menu.
pub added_to_attachment_menu: Option<bool>,
/// Indicates whether the user is a Telegram Premium user.
pub is_premium: Option<bool>,
/// [IETF language tag][1] of the user's language.
///
/// [1]: https://en.wikipedia.org/wiki/IETF_language_tag
pub language_code: Option<String>,
/// Last name of the user.
pub last_name: Option<String>,
/// Username of the user.
pub username: Option<UserUsername>,
}
impl User {
/// Creates a new `User`.
///
/// # Arguments
///
/// * `id` - Unique identifier of the user.
/// * `first_name` - First name of the user.
/// * `is_bot` - Indicates whether the user is a bot.
pub fn new<A, B>(id: A, first_name: B, is_bot: bool) -> Self
where
A: Into<UserPeerId>,
B: Into<String>,
{
Self {
first_name: first_name.into(),
id: id.into(),
is_bot,
added_to_attachment_menu: None,
is_premium: None,
language_code: None,
last_name: None,
username: None,
}
}
/// Returns the full name of the user (first name + last name).
pub fn get_full_name(&self) -> String {
let mut full_name = self.first_name.clone();
if let Some(ref last_name) = self.last_name {
full_name.push(' ');
full_name += last_name;
}
full_name
}
/// Returns the link to the user (`tg://user?id=xxx`).
///
/// These links will work only if they are used inside an inline link.
/// For example, they will not work,
/// when used in an inline keyboard button or in a message text.
pub fn get_link(&self) -> String {
format!("tg://user?id={}", self.id.0)
}
/// Returns the mention for the user.
///
/// # Arguments
///
/// * `parse_mode` - A parse mode for formatting the mention.
///
/// These mentions are only guaranteed to work if the user has contacted the bot in the past,
/// has sent a callback query to the bot via inline button or is a member
/// in the group where he was mentioned.
pub fn get_link_mention(&self, parse_mode: ParseMode) -> Result<String, MentionError> {
let full_name = parse_mode.escape(self.get_full_name());
let user_link = self.get_link();
Ok(match parse_mode {
ParseMode::Markdown => return Err(MentionError::UnsupportedParseMode(parse_mode)),
ParseMode::MarkdownV2 => format!(r#"[{full_name}]({user_link})"#),
ParseMode::Html => format!(r#"<a href="{user_link}">{full_name}</a>"#),
})
}
/// Sets a new value for the `added_to_attachment_menu` flag.
///
/// # Arguments
///
/// * `value` - Indicates whether the user added the bot to the attachment menu.
pub fn with_added_to_attachment_menu(mut self, value: bool) -> Self {
self.added_to_attachment_menu = Some(value);
self
}
/// Sets a new value for the `is_premium` flag.
///
/// # Arguments
///
/// * `value` - Indicates whether the user is a Telegram Premium user.
pub fn with_is_premium(mut self, value: bool) -> Self {
self.is_premium = Some(value);
self
}
/// Sets a new language code.
///
/// # Arguments
///
/// * `value` - Language code.
pub fn with_language_code<T>(mut self, value: T) -> Self
where
T: Into<String>,
{
self.language_code = Some(value.into());
self
}
/// Sets a new last name.
///
/// # Arguments
///
/// * `value` - Last name.
pub fn with_last_name<T>(mut self, value: T) -> Self
where
T: Into<String>,
{
self.last_name = Some(value.into());
self
}
/// Sets a new username.
///
/// # Arguments
///
/// * `value` - Username.
pub fn with_username<T>(mut self, value: T) -> Self
where
T: Into<UserUsername>,
{
self.username = Some(value.into());
self
}
}
/// Represents an error occurred when getting user mention.
#[derive(Debug)]
pub enum MentionError {
/// Parse mode is not supported
UnsupportedParseMode(ParseMode),
}
impl Error for MentionError {}
impl fmt::Display for MentionError {
fn fmt(&self, out: &mut fmt::Formatter) -> fmt::Result {
match self {
MentionError::UnsupportedParseMode(parse_mode) => {
write!(out, "can not mention with {parse_mode} parse mode")
}
}
}
}
/// Represents the audios displayed on a user's profile.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct UserProfileAudios {
/// Requested profile audios.
pub audios: Vec<Audio>,
/// Total number of profile audios for the target user.
pub total_count: Integer,
}
impl UserProfileAudios {
/// Creates a new `UserProfileAudios`.
///
/// # Arguments
///
/// * `audios` - A list of audios.
/// * `total_count` - Total number of audios.
pub fn new<T>(audios: T, total_count: Integer) -> Self
where
T: IntoIterator<Item = Audio>,
{
Self {
audios: audios.into_iter().collect(),
total_count,
}
}
}
/// Represents a list of profile pictures of a user.
#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
pub struct UserProfilePhotos {
/// Requested profile pictures (in up to 4 sizes each).
pub photos: Vec<Vec<PhotoSize>>,
/// Total number of profile pictures the target user has.
pub total_count: Integer,
}
impl UserProfilePhotos {
/// Creates a new `UserProfilePhotos`.
///
/// # Arguments
///
/// * `photos` - A list of photos.
/// * `total_count` - Total number of photos.
pub fn new<A, B>(photos: A, total_count: Integer) -> Self
where
A: IntoIterator<Item = B>,
B: IntoIterator<Item = PhotoSize>,
{
Self {
photos: photos.into_iter().map(|x| x.into_iter().collect()).collect(),
total_count,
}
}
}
/// ID of a user.
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(from = "Integer", into = "Integer")]
pub struct UserPeerId(Integer);
impl From<Integer> for UserPeerId {
fn from(value: Integer) -> Self {
Self(value)
}
}
impl From<UserPeerId> for Integer {
fn from(value: UserPeerId) -> Self {
value.0
}
}
impl fmt::Display for UserPeerId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl PartialEq<Integer> for UserPeerId {
fn eq(&self, other: &Integer) -> bool {
self.0.eq(other)
}
}
/// Describes the rating of a user based on their Telegram Star spendings.
#[derive(Copy, Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
pub struct UserRating {
/// The rating value required to get the current level.
pub current_level_rating: Integer,
/// Current level of the user, indicating their reliability when purchasing digital goods and services.
///
/// A higher level suggests a more trustworthy customer; a negative level is likely reason for concern.
pub level: Integer,
/// Numerical value of the user's rating; the higher the rating, the better.
pub rating: Integer,
/// The rating value required to get to the next level; omitted if the maximum level was reached.
pub next_level_rating: Option<Integer>,
}
/// Username of a user in the format `@username`.
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(from = "String", into = "String")]
pub struct UserUsername(String);
impl From<&str> for UserUsername {
fn from(value: &str) -> Self {
Self(String::from(value))
}
}
impl From<String> for UserUsername {
fn from(value: String) -> Self {
Self(value)
}
}
impl From<UserUsername> for String {
fn from(value: UserUsername) -> Self {
value.0
}
}
impl fmt::Display for UserUsername {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl PartialEq<String> for UserUsername {
fn eq(&self, other: &String) -> bool {
self.0.eq(other)
}
}
impl PartialEq<str> for UserUsername {
fn eq(&self, other: &str) -> bool {
self.0.eq(other)
}
}
/// Represents a user ID.
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, PartialOrd, Serialize)]
#[serde(untagged)]
pub enum UserId {
/// ID of a user.
Id(UserPeerId),
/// @username of a user.
Username(UserUsername),
}
impl From<&str> for UserId {
fn from(username: &str) -> UserId {
UserId::Username(String::from(username).into())
}
}
impl From<String> for UserId {
fn from(username: String) -> UserId {
UserId::Username(username.into())
}
}
impl From<Integer> for UserId {
fn from(id: Integer) -> UserId {
UserId::Id(id.into())
}
}
impl fmt::Display for UserId {
fn fmt(&self, out: &mut fmt::Formatter) -> fmt::Result {
match self {
UserId::Id(chat_id) => write!(out, "{}", chat_id.0),
UserId::Username(username) => write!(out, "{}", username.0),
}
}
}
impl From<UserPeerId> for UserId {
fn from(value: UserPeerId) -> Self {
UserId::Id(value)
}
}
impl From<UserUsername> for UserId {
fn from(value: UserUsername) -> Self {
UserId::Username(value)
}
}
/// Returns a list of profile audios for a user.
#[serde_with::skip_serializing_none]
#[derive(Clone, Copy, Debug, Serialize)]
pub struct GetUserProfileAudios {
user_id: Integer,
limit: Option<Integer>,
offset: Option<Integer>,
}
impl GetUserProfileAudios {
/// Creates a new `GetUserProfileAudios`.
///
/// # Arguments
///
/// * `user_id` - Unique identifier of the target user.
pub fn new(user_id: Integer) -> Self {
Self {
user_id,
limit: None,
offset: None,
}
}
/// Sets a new limit.
///
/// # Arguments
///
/// * `value` - Sequential number of the first audio to be returned.
///
/// By default, all audios are returned.
pub fn with_limit(mut self, value: Integer) -> Self {
self.limit = Some(value);
self
}
/// Sets a new offset
///
/// # Arguments
///
/// * `value` - Limits the number of audios to be retrieved.
///
/// Values between 1-100 are accepted. Defaults to 100.
pub fn with_offset(mut self, value: Integer) -> Self {
self.offset = Some(value);
self
}
}
impl Method for GetUserProfileAudios {
type Response = UserProfileAudios;
fn into_payload(self) -> Payload {
Payload::json("getUserProfileAudios", self)
}
}
/// Returns a list of profile pictures for a user.
#[serde_with::skip_serializing_none]
#[derive(Clone, Copy, Debug, Serialize)]
pub struct GetUserProfilePhotos {
user_id: Integer,
limit: Option<Integer>,
offset: Option<Integer>,
}
impl GetUserProfilePhotos {
/// Creates a new `GetUserProfilePhotos`.
///
/// # Arguments
///
/// * `user_id` - Unique identifier of the target user.
pub fn new(user_id: Integer) -> Self {
GetUserProfilePhotos {
user_id,
offset: None,
limit: None,
}
}
/// Sets a new limit.
///
/// # Arguments
///
/// * `value` - Limit of the number of photos to be retrieved; 1—100; default - 100.
pub fn with_limit(mut self, limit: Integer) -> Self {
self.limit = Some(limit);
self
}
/// Sets a new offset.
///
/// # Arguments
///
/// * `value` - Sequential number of the first photo to be returned.
///
/// By default, all photos are returned.
pub fn with_offset(mut self, offset: Integer) -> Self {
self.offset = Some(offset);
self
}
}
impl Method for GetUserProfilePhotos {
type Response = UserProfilePhotos;
fn into_payload(self) -> Payload {
Payload::json("getUserProfilePhotos", self)
}
}
/// Changes the emoji status for a given user
/// that previously allowed the bot to manage their emoji status
/// via the Mini App method `requestEmojiStatusAccess`.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Serialize)]
pub struct SetUserEmojiStatus {
user_id: Integer,
emoji_status_custom_emoji_id: Option<String>,
emoji_status_expiration_date: Option<Integer>,
}
impl SetUserEmojiStatus {
/// Creates a new `SetUserEmojiStatus`.
///
/// # Arguments
///
/// * `user_id` - Unique identifier of the target user
pub fn new(user_id: Integer) -> Self {
Self {
user_id,
emoji_status_custom_emoji_id: None,
emoji_status_expiration_date: None,
}
}
/// Sets a new emoji ID.
///
/// # Arguments
///
/// * `value` - Custom emoji identifier of the emoji status to set.
/// Pass an empty string to remove the status.
pub fn with_emoji_id<T>(mut self, value: T) -> Self
where
T: Into<String>,
{
self.emoji_status_custom_emoji_id = Some(value.into());
self
}
/// Sets a new expiration date.
///
/// # Arguments
///
/// * `value` - Expiration date of the emoji status, if any.
pub fn with_expiration_date(mut self, value: Integer) -> Self {
self.emoji_status_expiration_date = Some(value);
self
}
}
impl Method for SetUserEmojiStatus {
type Response = bool;
fn into_payload(self) -> Payload {
Payload::json("setUserEmojiStatus", self)
}
}