tgbot 0.47.0

A Telegram Bot library
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
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::{
    api::{Method, Payload, PayloadError},
    types::{Chat, ChatId, ChatInviteLink, ChatPermissions, Integer, User},
};

/// Represents a member of a chat.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
#[serde(rename_all = "snake_case")]
#[serde(tag = "status")]
pub enum ChatMember {
    /// Represents a chat administrator.
    Administrator(ChatMemberAdministrator),
    /// Represents a chat creator.
    Creator(ChatMemberCreator),
    /// Represents a kicked user.
    Kicked(ChatMemberKicked),
    /// Represents a user who left the chat.
    #[serde(deserialize_with = "ChatMemberUser::deserialize_value")]
    #[serde(serialize_with = "ChatMemberUser::serialize_value")]
    Left(User),
    /// Represents a regular chat member.
    Member {
        /// Information about the user.
        user: User,
        /// Tag of the member.
        tag: Option<String>,
        /// Date when the user's subscription will expire; unix time.
        until_date: Option<Integer>,
    },
    /// Represents a restricted user.
    Restricted(ChatMemberRestricted),
}

impl ChatMember {
    /// Returns the user object associated with the chat member.
    pub fn get_user(&self) -> &User {
        use self::ChatMember::*;
        match self {
            Administrator(admin) => &admin.user,
            Creator(creator) => &creator.user,
            Kicked(kicked) => &kicked.user,
            Left(user) => user,
            Member { user, .. } => user,
            Restricted(restricted) => &restricted.user,
        }
    }

    /// Checks if a user is a member of the chat.
    pub fn is_member(&self) -> bool {
        use self::ChatMember::*;
        match self {
            Administrator(_) | Creator(_) | Member { .. } => true,
            Kicked(_) | Left(_) => false,
            Restricted(restricted) => restricted.is_member,
        }
    }
}

#[derive(Deserialize, Serialize)]
struct ChatMemberUser {
    user: User,
}

impl ChatMemberUser {
    fn deserialize_value<'de, T>(deserializer: T) -> Result<User, T::Error>
    where
        T: Deserializer<'de>,
    {
        ChatMemberUser::deserialize(deserializer).map(|x| x.user)
    }

    fn serialize_value<T>(value: &User, serializer: T) -> Result<T::Ok, T::Error>
    where
        T: Serializer,
    {
        let value = ChatMemberUser { user: value.clone() };
        value.serialize(serializer)
    }
}

/// Represents a chat administrator.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
pub struct ChatMemberAdministrator {
    /// Information about the user.
    pub user: User,
    /// Indicates whether a bot is allowed to edit administrator privileges of that user.
    pub can_be_edited: bool,
    /// Indicates whether the administrator can change the chat title, photo and other settings.
    pub can_change_info: bool,
    /// Indicates whether the administrator can delete messages of other users.
    pub can_delete_messages: bool,
    /// Indicates whether the administrator can delete stories posted by other users;
    /// channels only.
    pub can_delete_stories: Option<bool>,
    /// Indicates whether the administrator can edit messages
    /// of other users and can pin messages; channels only.
    pub can_edit_messages: Option<bool>,
    /// Indicates whether the administrator can edit stories posted by other users; channels only.
    pub can_edit_stories: Option<bool>,
    /// Indicates whether the administrator can invite new users to the chat.
    pub can_invite_users: bool,
    /// Indicates whether the administrator can access the chat event log,
    /// chat statistics, message statistics in channels, see channel members,
    /// see anonymous administrators in supergroups and ignore slow mode;
    /// implied by any other administrator privilege.
    pub can_manage_chat: bool,
    /// Whether the administrator can manage direct messages
    /// of the channel and decline suggested posts; for channels only.
    pub can_manage_direct_messages: Option<bool>,
    /// Whether the administrator can edit tags of regular members;
    /// for groups and supergroups only.
    ///
    /// If omitted defaults to the value of `can_pin_messages`.
    pub can_manage_tags: Option<bool>,
    /// Indicates whether the administrator is allowed to
    /// create, rename, close, and reopen forum topics; supergroups only.
    pub can_manage_topics: Option<bool>,
    /// Indicates whether the administrator can manage video chats.
    pub can_manage_video_chats: bool,
    /// Indicates whether the administrator can pin messages; groups and supergroups only.
    pub can_pin_messages: Option<bool>,
    /// Indicates whether the administrator can post in the channel; channels only.
    pub can_post_messages: Option<bool>,
    /// Indicates whether the administrator can post stories in the channel; channels only.
    pub can_post_stories: Option<bool>,
    /// Indicates whether the administrator can add new administrators with a subset
    /// of his own privileges or demote administrators that he has promoted,
    /// directly or indirectly (promoted by administrators that were appointed by the user).
    pub can_promote_members: bool,
    /// Indicates whether the administrator can restrict, ban or unban chat members.
    pub can_restrict_members: bool,
    /// Custom title for the administrator.
    pub custom_title: Option<String>,
    /// Indicates whether the administrator's presence in the chat is hidden.
    pub is_anonymous: bool,
}

/// Represents a chat member that owns the chat and has all administrator privileges.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
pub struct ChatMemberCreator {
    /// Indicates whether the creator's presence in the chat is hidden.
    pub is_anonymous: bool,
    /// Information about the user.
    pub user: User,
    /// Custom title for the creator.
    pub custom_title: Option<String>,
}

/// Represents a kicked chat member.
#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
pub struct ChatMemberKicked {
    /// Date when restrictions will be lifted for this user; unix time.
    pub until_date: Integer,
    /// Information about the user.
    pub user: User,
}

/// Represents a restricted user.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
pub struct ChatMemberRestricted {
    /// Information about the user.
    pub user: User,
    /// Whether the user may add web page previews to his messages.
    pub can_add_web_page_previews: bool,
    /// Whether the user is allowed to change the chat title, photo and other settings.
    pub can_change_info: bool,
    /// Whether the user is allowed to edit their own tag.
    pub can_edit_tag: bool,
    /// Whether the user is allowed to invite new users to the chat.
    pub can_invite_users: bool,
    /// Whether the user is allowed to create forum topics.
    pub can_manage_topics: bool,
    /// Whether the user is allowed to pin messages; groups and supergroups only.
    pub can_pin_messages: bool,
    /// Whether the user is allowed to react to messages.
    pub can_react_to_messages: bool,
    /// Whether the user is allowed to send audios.
    pub can_send_audios: bool,
    /// Whether the user is allowed to send documents.
    pub can_send_documents: bool,
    /// Whether the user can send text messages, contacts, locations and venues.
    pub can_send_messages: bool,
    /// Whether the user can send animations, games, stickers and use inline bots.
    pub can_send_other_messages: bool,
    /// Whether the user is allowed to send photos.
    pub can_send_photos: bool,
    /// Whether the user is allowed to send polls.
    pub can_send_polls: bool,
    /// Whether the user is allowed to send video notes.
    pub can_send_video_notes: bool,
    /// Whether the user is allowed to send videos.
    pub can_send_videos: bool,
    /// Whether the user is allowed to send voice notes.
    pub can_send_voice_notes: bool,
    /// Whether the user is a member of the chat at the moment of the request.
    pub is_member: bool,
    /// Date when restrictions will be lifted for this user; unix time.
    pub until_date: Integer,
    /// Tag of the member.
    pub tag: Option<String>,
}

/// Represents changes in a status of a chat member.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct ChatMemberUpdated {
    /// Chat the user belongs to.
    pub chat: Chat,
    /// Date the change was done in Unix time.
    pub date: Integer,
    /// Performer of the action, which resulted in the change.
    pub from: User,
    /// New information about the chat member.
    pub new_chat_member: ChatMember,
    /// Previous information about the chat member.
    pub old_chat_member: ChatMember,
    /// Chat invite link, which was used by the user to join the chat;
    /// for joining by invite link events only.
    pub invite_link: Option<ChatInviteLink>,
    /// Indicates whether the user joined the chat via a chat folder invite link.
    pub via_chat_folder_invite_link: Option<bool>,
    /// Indicates whether the user joined the chat after sending a direct join request
    /// and being approved by an administrator.
    pub via_join_request: Option<bool>,
}

/// Bans a user in a chat.
///
/// In the case of supergroups and channels,
/// the user will not be able to return to the chat on their own using invite links,
/// etc., unless unbanned first.
///
/// The bot must be an administrator in the chat
/// for this to work and must have the appropriate admin rights.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Serialize)]
pub struct BanChatMember {
    chat_id: ChatId,
    user_id: Integer,
    revoke_messages: Option<bool>,
    until_date: Option<Integer>,
}

impl BanChatMember {
    /// Creates a new `BanChatMember`.
    ///
    /// # Arguments
    ///
    /// * `chat_id` - Unique identifier of the target chat.
    /// * `user_id` - Unique identifier of the target user.
    pub fn new<T>(chat_id: T, user_id: Integer) -> Self
    where
        T: Into<ChatId>,
    {
        Self {
            chat_id: chat_id.into(),
            user_id,
            until_date: None,
            revoke_messages: None,
        }
    }

    /// Sets a new value for the `revoke_messages` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Delete all messages from the chat for the user that is being removed;
    ///   if `false`, the user will be able to see messages in the group that were
    ///   sent before the user was removed;
    ///   always `true` for supergroups and channels.
    pub fn with_revoke_messages(mut self, value: bool) -> Self {
        self.revoke_messages = Some(value);
        self
    }

    /// Sets a new until date.
    ///
    /// # Arguments
    ///
    /// * `value` - The date when the user will be unbanned, unix time.
    ///
    /// If user is banned for more than 366 days or less than 30 seconds
    /// from the current time they are considered to be banned forever.
    pub fn with_until_date(mut self, value: Integer) -> Self {
        self.until_date = Some(value);
        self
    }
}

impl Method for BanChatMember {
    type Response = bool;

    fn into_payload(self) -> Result<Payload, PayloadError> {
        Payload::json("banChatMember", self)
    }
}

/// Returns a list of administrators in a chat.
///
/// On success, returns an array of [`ChatMember`] objects that contains
/// information about all chat administrators except other bots.
///
/// If the chat is a group or a supergroup and no administrators
/// were appointed, only the creator will be returned.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Serialize)]
pub struct GetChatAdministrators {
    chat_id: ChatId,
    return_bots: Option<bool>,
}

impl GetChatAdministrators {
    /// Creates a new `GetChatAdministrators`.
    ///
    /// # Arguments
    ///
    /// * `chat_id` - Unique identifier of the target chat.
    pub fn new<T>(chat_id: T) -> Self
    where
        T: Into<ChatId>,
    {
        Self {
            chat_id: chat_id.into(),
            return_bots: None,
        }
    }

    /// Sets a new value for the `return_bots` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Whether to receive all bots that are administrators of the chat;
    ///   by default, bots other tthan current bot are omitted.
    pub fn with_return_bots(mut self, value: bool) -> Self {
        self.return_bots = Some(value);
        self
    }
}

impl Method for GetChatAdministrators {
    type Response = Vec<ChatMember>;

    fn into_payload(self) -> Result<Payload, PayloadError> {
        Payload::json("getChatAdministrators", self)
    }
}

/// Returns a member of a chat.
#[derive(Clone, Debug, Serialize)]
pub struct GetChatMember {
    chat_id: ChatId,
    user_id: Integer,
}

impl GetChatMember {
    /// Creates a new `GetChatMember`
    ///
    /// # Arguments
    ///
    /// * `chat_id` - Unique identifier of the target chat.
    /// * `user_id` - Unique identifier of the target user.
    pub fn new<T>(chat_id: T, user_id: Integer) -> Self
    where
        T: Into<ChatId>,
    {
        Self {
            chat_id: chat_id.into(),
            user_id,
        }
    }
}

impl Method for GetChatMember {
    type Response = ChatMember;

    fn into_payload(self) -> Result<Payload, PayloadError> {
        Payload::json("getChatMember", self)
    }
}

/// Returns a number of members in a chat.
#[derive(Clone, Debug, Serialize)]
pub struct GetChatMemberCount {
    chat_id: ChatId,
}

impl GetChatMemberCount {
    /// Creates a new `GetChatMemberCount`.
    ///
    /// # Arguments
    ///
    /// * `chat_id` - Unique identifier of the target chat.
    pub fn new<T>(chat_id: T) -> Self
    where
        T: Into<ChatId>,
    {
        Self {
            chat_id: chat_id.into(),
        }
    }
}

impl Method for GetChatMemberCount {
    type Response = Integer;

    fn into_payload(self) -> Result<Payload, PayloadError> {
        Payload::json("getChatMemberCount", self)
    }
}

/// Promotes or demotes a user in a chat.
///
/// The bot must be an administrator in the chat
/// for this to work and must have the appropriate admin rights.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Serialize)]
pub struct PromoteChatMember {
    chat_id: ChatId,
    user_id: Integer,
    can_change_info: Option<bool>,
    can_delete_messages: Option<bool>,
    can_delete_stories: Option<bool>,
    can_edit_messages: Option<bool>,
    can_edit_stories: Option<bool>,
    can_invite_users: Option<bool>,
    can_manage_chat: Option<bool>,
    can_manage_direct_messages: Option<bool>,
    can_manage_tags: Option<bool>,
    can_manage_topics: Option<bool>,
    can_manage_video_chats: Option<bool>,
    can_pin_messages: Option<bool>,
    can_post_messages: Option<bool>,
    can_post_stories: Option<bool>,
    can_promote_members: Option<bool>,
    can_restrict_members: Option<bool>,
    is_anonymous: Option<bool>,
}

impl PromoteChatMember {
    /// Creates a new `PromoteChatMember`
    ///
    /// # Arguments
    ///
    /// * `chat_id` - Unique identifier of the target chat.
    /// * `user_id` - Unique identifier of the target user.
    pub fn new<T>(chat_id: T, user_id: Integer) -> Self
    where
        T: Into<ChatId>,
    {
        Self {
            chat_id: chat_id.into(),
            user_id,
            can_change_info: None,
            can_delete_messages: None,
            can_delete_stories: None,
            can_edit_messages: None,
            can_edit_stories: None,
            can_invite_users: None,
            can_manage_chat: None,
            can_manage_direct_messages: None,
            can_manage_tags: None,
            can_manage_topics: None,
            can_manage_video_chats: None,
            can_pin_messages: None,
            can_post_messages: None,
            can_post_stories: None,
            can_promote_members: None,
            can_restrict_members: None,
            is_anonymous: None,
        }
    }

    /// Promotes all privileges.
    pub fn promote_all(mut self) -> Self {
        self.is_anonymous = Some(true);
        self.can_change_info = Some(true);
        self.can_delete_messages = Some(true);
        self.can_delete_stories = Some(true);
        self.can_edit_messages = Some(true);
        self.can_edit_stories = Some(true);
        self.can_invite_users = Some(true);
        self.can_manage_chat = Some(true);
        self.can_manage_direct_messages = Some(true);
        self.can_manage_tags = Some(true);
        self.can_manage_topics = Some(true);
        self.can_manage_video_chats = Some(true);
        self.can_pin_messages = Some(true);
        self.can_post_messages = Some(true);
        self.can_post_stories = Some(true);
        self.can_promote_members = Some(true);
        self.can_restrict_members = Some(true);
        self
    }

    /// Demotes all privileges.
    pub fn demote_all(mut self) -> Self {
        self.is_anonymous = Some(false);
        self.can_change_info = Some(false);
        self.can_delete_messages = Some(false);
        self.can_delete_stories = Some(false);
        self.can_edit_messages = Some(false);
        self.can_edit_stories = Some(false);
        self.can_invite_users = Some(false);
        self.can_manage_chat = Some(false);
        self.can_manage_direct_messages = Some(false);
        self.can_manage_tags = Some(false);
        self.can_manage_topics = Some(false);
        self.can_manage_video_chats = Some(false);
        self.can_pin_messages = Some(false);
        self.can_post_messages = Some(false);
        self.can_post_stories = Some(false);
        self.can_promote_members = Some(false);
        self.can_restrict_members = Some(false);
        self
    }

    /// Sets a new value for the `can_change_info` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Indicates whether the administrator can change chat title,
    ///   photo and other settings.
    pub fn with_can_change_info(mut self, value: bool) -> Self {
        self.can_change_info = Some(value);
        self
    }

    /// Sets a new value for the `can_delete_messages` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Indicates whether the administrator can delete messages of other users.
    pub fn with_can_delete_messages(mut self, value: bool) -> Self {
        self.can_delete_messages = Some(value);
        self
    }

    /// Sets a new value for the `can_delete_stories` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Indicates whether the administrator can delete stories posted by other users; channels only.
    pub fn with_can_delete_stories(mut self, value: bool) -> Self {
        self.can_delete_stories = Some(value);
        self
    }

    /// Sets a new value for the `can_edit_messages` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Indicates whether the administrator can edit messages
    ///   of other users and can pin messages; channels only.
    pub fn with_can_edit_messages(mut self, value: bool) -> Self {
        self.can_edit_messages = Some(value);
        self
    }

    /// Sets a new value for the `can_edit_stories` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Indicates whether the administrator can
    ///   edit stories posted by other users; channels only.
    pub fn with_can_edit_stories(mut self, value: bool) -> Self {
        self.can_edit_stories = Some(value);
        self
    }

    /// Sets a new value for the `can_invite_users` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Indicates whether the administrator can invite new users to the chat.
    pub fn with_can_invite_users(mut self, value: bool) -> Self {
        self.can_invite_users = Some(value);
        self
    }

    /// Sets a new value for the `can_manage_chat` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Indicates whether the administrator can access the chat event log,
    ///   chat statistics, message statistics in channels, see channel members,
    ///   see anonymous administrators in supergroups and ignore slow mode;
    ///   implied by any other administrator privilege.
    pub fn with_can_manage_chat(mut self, value: bool) -> Self {
        self.can_manage_chat = Some(value);
        self
    }

    /// Sets a new value for the `can_manage_direct_messages` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Whether the administrator can manage direct messages
    ///   within the channel and decline suggested posts; for channels only.
    pub fn with_can_manage_direct_messages(mut self, value: bool) -> Self {
        self.can_manage_direct_messages = Some(value);
        self
    }

    /// Sets a new value for the `can_manage_tags` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Whether the administrator can edit the tags of regular members;
    ///   for groups and supergroups only.
    pub fn with_can_manage_tags(mut self, value: bool) -> Self {
        self.can_manage_tags = Some(value);
        self
    }

    /// Sets a new value for the `can_manage_topics` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - User is allowed to create, rename, close, and reopen forum topics; supergroups only.
    pub fn with_can_manage_topics(mut self, value: bool) -> Self {
        self.can_manage_topics = Some(value);
        self
    }

    /// Sets a new value for the `can_manage_video_chats` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Indicates whether the administrator can manage video chats; supergroups only.
    pub fn with_can_manage_video_chats(mut self, value: bool) -> Self {
        self.can_manage_video_chats = Some(value);
        self
    }

    /// Sets a new value for the `can_pin_messages` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Indicates whether the administrator can pin messages; supergroups only.
    pub fn with_can_pin_messages(mut self, value: bool) -> Self {
        self.can_pin_messages = Some(value);
        self
    }

    /// Sets a new value for the `can_post_messages` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Indicates whether the administrator can create channel posts; channels only.
    pub fn with_can_post_messages(mut self, value: bool) -> Self {
        self.can_post_messages = Some(value);
        self
    }

    /// Sets a new value for the `can_post_stories` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Indicates whether the administrator can post stories in the channel; channels only.
    pub fn with_can_post_stories(mut self, value: bool) -> Self {
        self.can_post_stories = Some(value);
        self
    }

    /// Sets a new value for the `can_promote_members` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Indicates whether the administrator can add new administrators with a subset
    ///   of his own privileges or demote administrators that he has promoted,
    ///   directly or indirectly (promoted by administrators that were appointed by him).
    pub fn with_can_promote_members(mut self, value: bool) -> Self {
        self.can_promote_members = Some(value);
        self
    }

    /// Sets a new value for the `can_restrict_members` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Indicates whether the administrator can restrict, ban or unban chat members.
    pub fn with_can_restrict_members(mut self, value: bool) -> Self {
        self.can_restrict_members = Some(value);
        self
    }

    /// Sets a new value for the `is_anonymous` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Indicates whether the administrator's presence in the chat is hidden.
    pub fn with_is_anonymous(mut self, value: bool) -> Self {
        self.is_anonymous = Some(value);
        self
    }
}

impl Method for PromoteChatMember {
    type Response = bool;

    fn into_payload(self) -> Result<Payload, PayloadError> {
        Payload::json("promoteChatMember", self)
    }
}

/// Restricts a user in a supergroup.
///
/// The bot must be an administrator in the supergroup
/// for this to work and must have the appropriate admin rights.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Serialize)]
pub struct RestrictChatMember {
    chat_id: ChatId,
    permissions: ChatPermissions,
    user_id: Integer,
    until_date: Option<Integer>,
    use_independent_chat_permissions: Option<bool>,
}

impl RestrictChatMember {
    /// Creates a new `RestrictChatMember`.
    ///
    /// # Arguments
    ///
    /// * `chat_id` - Unique identifier for the target chat.
    /// * `user_id` - Unique identifier of the target user.
    pub fn new<T>(chat_id: T, user_id: Integer) -> Self
    where
        T: Into<ChatId>,
    {
        RestrictChatMember {
            chat_id: chat_id.into(),
            permissions: ChatPermissions::default(),
            user_id,
            until_date: None,
            use_independent_chat_permissions: None,
        }
    }

    /// Allows everything.
    pub fn allow_all(mut self) -> Self {
        self.permissions = ChatPermissions::allowed();
        self
    }

    /// Restricts everything.
    pub fn restrict_all(mut self) -> Self {
        self.permissions = ChatPermissions::restricted();
        self
    }

    /// Replaces current permissions with the new one.
    ///
    /// # Arguments
    ///
    /// * `value` - The new permissions.
    pub fn with_permissions(mut self, value: ChatPermissions) -> Self {
        self.permissions = value;
        self
    }

    /// Sets a new until date.
    ///
    /// # Arguments
    ///
    /// * `value` - The date when restrictions will be lifted for the user, unix time.
    ///
    /// If user is restricted for more than 366 days or less than 30 seconds
    /// from the current time, they are considered to be restricted forever.
    pub fn with_until_date(mut self, value: Integer) -> Self {
        self.until_date = Some(value);
        self
    }

    /// Sets a new value for the `use_independent_chat_permissions` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Indicates whether the chat permissions are set independently.
    ///
    /// If `false`, the `can_send_other_messages` and `can_add_web_page_previews` permissions
    /// will imply the `can_send_messages`, `can_send_audios`, `can_send_documents`,
    /// `can_send_photos`, `can_send_videos`, `can_send_video_notes`,
    /// and `can_send_voice_notes` permissions; the `can_send_polls` permission
    /// will imply the `can_send_messages` permission.
    pub fn with_use_independent_chat_permissions(mut self, value: bool) -> Self {
        self.use_independent_chat_permissions = Some(value);
        self
    }
}

impl Method for RestrictChatMember {
    type Response = bool;

    fn into_payload(self) -> Result<Payload, PayloadError> {
        Payload::json("restrictChatMember", self)
    }
}

/// Sets a custom title for an administrator in a supergroup promoted by the bot.
#[derive(Clone, Debug, Serialize)]
pub struct SetChatAdministratorCustomTitle {
    chat_id: ChatId,
    user_id: Integer,
    custom_title: String,
}

impl SetChatAdministratorCustomTitle {
    /// Creates a new `SetChatAdministratorCustomTitle`.
    ///
    /// # Arguments
    ///
    /// * `chat_id` - Unique identifier of the target chat.
    /// * `custom_title` - New custom title for the administrator; 0-16 characters; emoji are not allowed.
    /// * `user_id` - Unique identifier of the target user.
    pub fn new<A, B>(chat_id: A, custom_title: B, user_id: Integer) -> Self
    where
        A: Into<ChatId>,
        B: Into<String>,
    {
        Self {
            chat_id: chat_id.into(),
            custom_title: custom_title.into(),
            user_id,
        }
    }
}

impl Method for SetChatAdministratorCustomTitle {
    type Response = bool;

    fn into_payload(self) -> Result<Payload, PayloadError> {
        Payload::json("setChatAdministratorCustomTitle", self)
    }
}

/// Sets a tag for a regular member in a group or a supergroup.
///
/// The bot must be an administrator in the chat for this to work
/// and must have the `can_manage_tags` administrator right.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Serialize)]
pub struct SetChatMemberTag {
    chat_id: ChatId,
    user_id: Integer,
    tag: Option<String>,
}

impl SetChatMemberTag {
    /// Creates a new `SetChatMemberTag`.
    ///
    /// # Arguments
    ///
    /// * `chat_id` - Unique identifier of the target chat.
    /// * `user_id` - Unique identifier of the target user.
    pub fn new<T>(chat_id: T, user_id: Integer) -> Self
    where
        T: Into<ChatId>,
    {
        Self {
            chat_id: chat_id.into(),
            user_id,
            tag: None,
        }
    }

    /// Sets a new tag.
    ///
    /// # Arguments
    ///
    /// * `value` - New tag for the member; 0-16 characters, emoji are not allowed.
    pub fn with_tag<T>(mut self, value: T) -> Self
    where
        T: Into<String>,
    {
        self.tag = Some(value.into());
        self
    }
}

impl Method for SetChatMemberTag {
    type Response = bool;

    fn into_payload(self) -> Result<Payload, PayloadError> {
        Payload::json("setChatMemberTag", self)
    }
}

/// Unbans a previously kicked user in a supergroup or channel.
///
/// The user will not return to the group or channel
/// automatically, but will be able to join via link, etc.
///
/// The bot must be an administrator for this to work
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Serialize)]
pub struct UnbanChatMember {
    chat_id: ChatId,
    user_id: Integer,
    only_if_banned: Option<bool>,
}

impl UnbanChatMember {
    /// Creates a new `UnbanChatMember`.
    ///
    /// # Arguments
    ///
    /// * `chat_id` - Unique identifier of the target chat.
    /// * `user_id` - Unique identifier of the target user.
    pub fn new<T>(chat_id: T, user_id: Integer) -> Self
    where
        T: Into<ChatId>,
    {
        UnbanChatMember {
            chat_id: chat_id.into(),
            user_id,
            only_if_banned: None,
        }
    }

    /// Sets a new value for the `only_if_banned` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - If `true` - do nothing if the user is not banned.
    pub fn with_only_if_banned(mut self, only_if_banned: bool) -> Self {
        self.only_if_banned = Some(only_if_banned);
        self
    }
}

impl Method for UnbanChatMember {
    type Response = bool;

    fn into_payload(self) -> Result<Payload, PayloadError> {
        Payload::json("unbanChatMember", self)
    }
}