tgbot/types/chat/member/mod.rs
1use serde::{Deserialize, Deserializer, Serialize, Serializer};
2
3use crate::{
4 api::{Method, Payload},
5 types::{Chat, ChatId, ChatInviteLink, ChatPermissions, Integer, User},
6};
7
8#[cfg(test)]
9mod tests;
10
11/// Represents a member of a chat.
12#[serde_with::skip_serializing_none]
13#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
14#[serde(rename_all = "snake_case")]
15#[serde(tag = "status")]
16pub enum ChatMember {
17 /// Represents a chat administrator.
18 Administrator(ChatMemberAdministrator),
19 /// Represents a chat creator.
20 Creator(ChatMemberCreator),
21 /// Represents a kicked user.
22 Kicked(ChatMemberKicked),
23 /// Represents a user who left the chat.
24 #[serde(deserialize_with = "ChatMemberUser::deserialize_value")]
25 #[serde(serialize_with = "ChatMemberUser::serialize_value")]
26 Left(User),
27 /// Represents a regular chat member.
28 Member {
29 /// Information about the user
30 user: User,
31 /// Date when the user's subscription will expire; Unix time
32 until_date: Option<Integer>,
33 },
34 /// Represents a restricted user.
35 Restricted(ChatMemberRestricted),
36}
37
38impl ChatMember {
39 /// Returns the user object associated with the chat member.
40 pub fn get_user(&self) -> &User {
41 use self::ChatMember::*;
42 match self {
43 Administrator(admin) => &admin.user,
44 Creator(creator) => &creator.user,
45 Kicked(kicked) => &kicked.user,
46 Left(user) => user,
47 Member { user, .. } => user,
48 Restricted(restricted) => &restricted.user,
49 }
50 }
51
52 /// Checks if a user is a member of the chat.
53 pub fn is_member(&self) -> bool {
54 use self::ChatMember::*;
55 match self {
56 Administrator(_) | Creator(_) | Member { .. } => true,
57 Kicked(_) | Left(_) => false,
58 Restricted(restricted) => restricted.is_member,
59 }
60 }
61}
62
63#[derive(Deserialize, Serialize)]
64struct ChatMemberUser {
65 user: User,
66}
67
68impl ChatMemberUser {
69 fn deserialize_value<'de, T>(deserializer: T) -> Result<User, T::Error>
70 where
71 T: Deserializer<'de>,
72 {
73 ChatMemberUser::deserialize(deserializer).map(|x| x.user)
74 }
75
76 fn serialize_value<T>(value: &User, serializer: T) -> Result<T::Ok, T::Error>
77 where
78 T: Serializer,
79 {
80 let value = ChatMemberUser { user: value.clone() };
81 value.serialize(serializer)
82 }
83}
84
85/// Represents a chat administrator.
86#[serde_with::skip_serializing_none]
87#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
88pub struct ChatMemberAdministrator {
89 /// Information about the user.
90 pub user: User,
91 /// Indicates whether a bot is allowed to edit administrator privileges of that user.
92 pub can_be_edited: bool,
93 /// Indicates whether the administrator can change the chat title, photo and other settings.
94 pub can_change_info: bool,
95 /// Indicates whether the administrator can delete messages of other users.
96 pub can_delete_messages: bool,
97 /// Indicates whether the administrator can delete stories posted by other users;
98 /// channels only.
99 pub can_delete_stories: Option<bool>,
100 /// Indicates whether the administrator can edit messages
101 /// of other users and can pin messages; channels only.
102 pub can_edit_messages: Option<bool>,
103 /// Indicates whether the administrator can edit stories posted by other users; channels only.
104 pub can_edit_stories: Option<bool>,
105 /// Indicates whether the administrator can invite new users to the chat.
106 pub can_invite_users: bool,
107 /// Indicates whether the administrator can access the chat event log,
108 /// chat statistics, message statistics in channels, see channel members,
109 /// see anonymous administrators in supergroups and ignore slow mode;
110 /// implied by any other administrator privilege.
111 pub can_manage_chat: bool,
112 /// Indicates whether the administrator is allowed to
113 /// create, rename, close, and reopen forum topics; supergroups only.
114 pub can_manage_topics: Option<bool>,
115 /// Indicates whether the administrator can manage video chats.
116 pub can_manage_video_chats: bool,
117 /// Indicates whether the administrator can pin messages; groups and supergroups only.
118 pub can_pin_messages: Option<bool>,
119 /// Indicates whether the administrator can post in the channel; channels only.
120 pub can_post_messages: Option<bool>,
121 /// Indicates whether the administrator can post stories in the channel; channels only.
122 pub can_post_stories: Option<bool>,
123 /// Indicates whether the administrator can add new administrators with a subset
124 /// of his own privileges or demote administrators that he has promoted,
125 /// directly or indirectly (promoted by administrators that were appointed by the user).
126 pub can_promote_members: bool,
127 /// Indicates whether the administrator can restrict, ban or unban chat members.
128 pub can_restrict_members: bool,
129 /// Custom title for the administrator.
130 pub custom_title: Option<String>,
131 /// Indicates whether the administrator's presence in the chat is hidden.
132 pub is_anonymous: bool,
133}
134
135impl ChatMemberAdministrator {
136 /// Creates a new `ChatMemberAdministrator`
137 ///
138 /// # Arguments
139 ///
140 /// * `user` - Information about the user.
141 pub fn new(user: User) -> Self {
142 Self {
143 user,
144 can_be_edited: false,
145 can_change_info: false,
146 can_delete_messages: false,
147 can_delete_stories: None,
148 can_edit_messages: None,
149 can_edit_stories: None,
150 can_invite_users: false,
151 can_manage_chat: false,
152 can_manage_topics: None,
153 can_manage_video_chats: false,
154 can_pin_messages: None,
155 can_post_messages: None,
156 can_post_stories: None,
157 can_promote_members: false,
158 can_restrict_members: false,
159 is_anonymous: false,
160 custom_title: None,
161 }
162 }
163
164 /// Sets a new value for the `can_be_edited` flag.
165 ///
166 /// # Arguments
167 ///
168 /// * `value` - Indicates whether a bot is allowed to edit privileges of that administrator.
169 pub fn with_can_be_edited(mut self, value: bool) -> Self {
170 self.can_be_edited = value;
171 self
172 }
173
174 /// Sets a new value for the `can_change_info` flag.
175 ///
176 /// # Arguments
177 ///
178 /// * `value` - Indicates whether the administrator can change the chat title,
179 /// photo and other settings.
180 pub fn with_can_change_info(mut self, value: bool) -> Self {
181 self.can_change_info = value;
182 self
183 }
184
185 /// Sets a new value for the `can_delete_messages` flag.
186 ///
187 /// # Arguments
188 ///
189 /// * `value` - Indicates whether the administrator can delete messages of other users.
190 pub fn with_can_delete_messages(mut self, value: bool) -> Self {
191 self.can_delete_messages = value;
192 self
193 }
194
195 /// Sets a new value for the `can_delete_stories` flag.
196 ///
197 /// # Arguments
198 ///
199 /// * `value` - Indicates whether the administrator
200 /// can delete stories posted by other users; channels only.
201 pub fn with_can_delete_stories(mut self, value: bool) -> Self {
202 self.can_delete_stories = Some(value);
203 self
204 }
205
206 /// Sets a new value for the `can_edit_messages` flag.
207 ///
208 /// # Arguments
209 ///
210 /// * `value` - Indicates whether the administrator can edit messages
211 /// of other users and can pin messages; channels only.
212 pub fn with_can_edit_messages(mut self, value: bool) -> Self {
213 self.can_edit_messages = Some(value);
214 self
215 }
216
217 /// Sets a new value for the `can_edit_stories` flag.
218 ///
219 /// # Arguments
220 ///
221 /// * `value` - Indicates whether the administrator can
222 /// edit stories posted by other users; channels only.
223 pub fn with_can_edit_stories(mut self, value: bool) -> Self {
224 self.can_edit_stories = Some(value);
225 self
226 }
227
228 /// Sets a new value for the `can_invite_users` flag.
229 ///
230 /// # Arguments
231 ///
232 /// * `value` - Indicates whether the administrator can invite new users to the chat.
233 pub fn with_can_invite_users(mut self, value: bool) -> Self {
234 self.can_invite_users = value;
235 self
236 }
237
238 /// Sets a new value for the `can_manage_chat` flag.
239 ///
240 /// # Arguments
241 ///
242 /// * `value` - Indicates whether the administrator can access the chat event log,
243 /// chat statistics, message statistics in channels, see channel members,
244 /// see anonymous administrators in supergroups and ignore slow mode;
245 /// implied by any other administrator privilege.
246 pub fn with_can_manage_chat(mut self, value: bool) -> Self {
247 self.can_manage_chat = value;
248 self
249 }
250
251 /// Sets a new value for the `can_manage_topics` flag.
252 ///
253 /// # Arguments
254 ///
255 /// * `value` - Indicates whether the administrator is allowed to
256 /// create, rename, close, and reopen forum topics; supergroups only.
257 pub fn with_can_manage_topics(mut self, value: bool) -> Self {
258 self.can_manage_topics = Some(value);
259 self
260 }
261
262 /// Sets a new value for the `can_manage_video_chats` flag.
263 ///
264 /// # Arguments
265 ///
266 /// * `value` - Indicates whether the administrator can manage video chats.
267 pub fn with_can_manage_video_chats(mut self, value: bool) -> Self {
268 self.can_manage_video_chats = value;
269 self
270 }
271
272 /// Sets a new value for the `can_pin_messages` flag.
273 ///
274 /// # Arguments
275 ///
276 /// * `value` - Indicates whether the administrator can pin messages;
277 /// groups and supergroups only.
278 pub fn with_can_pin_messages(mut self, value: bool) -> Self {
279 self.can_pin_messages = Some(value);
280 self
281 }
282
283 /// Sets a new value for the `can_post_messages` flag.
284 ///
285 /// # Arguments
286 ///
287 /// * `value` - Indicates whether the administrator can post in the channel; channels only.
288 pub fn with_can_post_messages(mut self, value: bool) -> Self {
289 self.can_post_messages = Some(value);
290 self
291 }
292
293 /// Sets a new value for the `can_post_stories` flag.
294 ///
295 /// # Arguments
296 ///
297 /// * `value` - Indicates whether the administrator can
298 /// post stories in the channel; channels only.
299 pub fn with_can_post_stories(mut self, value: bool) -> Self {
300 self.can_post_stories = Some(value);
301 self
302 }
303
304 /// Sets a new value for the `can_promote_members` flag.
305 ///
306 /// # Arguments
307 ///
308 /// * `value` - Indicates whether the administrator can add new administrators with a subset
309 /// of his own privileges or demote administrators that he has promoted,
310 /// directly or indirectly (promoted by administrators
311 /// that were appointed by the user).
312 pub fn with_can_promote_members(mut self, value: bool) -> Self {
313 self.can_promote_members = value;
314 self
315 }
316
317 /// Sets a new value for the `can_restrict_members` flag.
318 ///
319 /// # Arguments
320 ///
321 /// * `value` - Indicates whether the administrator can restrict, ban or unban chat members.
322 pub fn with_can_restrict_members(mut self, value: bool) -> Self {
323 self.can_restrict_members = value;
324 self
325 }
326
327 /// Sets a new custom title.
328 ///
329 /// # Arguments
330 ///
331 /// * `value` - Custom title for the administrator.
332 pub fn with_custom_title<T>(mut self, value: T) -> Self
333 where
334 T: Into<String>,
335 {
336 self.custom_title = Some(value.into());
337 self
338 }
339
340 /// Sets a new value for the `is_anonymous` flag.
341 ///
342 /// # Arguments
343 ///
344 /// * `value` - Indicates whether the administrator's presence in the chat is hidden.
345 pub fn with_is_anonymous(mut self, value: bool) -> Self {
346 self.is_anonymous = value;
347 self
348 }
349}
350
351/// Represents a chat member that owns the chat and has all administrator privileges.
352#[serde_with::skip_serializing_none]
353#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
354pub struct ChatMemberCreator {
355 /// Indicates whether the creator's presence in the chat is hidden.
356 pub is_anonymous: bool,
357 /// Information about the user.
358 pub user: User,
359 /// Custom title for the creator.
360 pub custom_title: Option<String>,
361}
362
363impl ChatMemberCreator {
364 /// Creates a new `ChatMemberCreator`.
365 ///
366 /// # Arguments
367 ///
368 /// * `user` - Information about the user.
369 pub fn new(user: User) -> Self {
370 Self {
371 is_anonymous: false,
372 user,
373 custom_title: None,
374 }
375 }
376
377 /// Sets a new custom title.
378 ///
379 /// # Arguments
380 ///
381 /// * `value` - Custom title for the creator.
382 pub fn with_custom_title<T>(mut self, value: T) -> Self
383 where
384 T: Into<String>,
385 {
386 self.custom_title = Some(value.into());
387 self
388 }
389
390 /// Sets a new value of an `is_anonymous` flag.
391 ///
392 /// # Arguments
393 ///
394 /// * `value` - Indicates whether the creator's presence in the chat is hidden.
395 pub fn with_is_anonymous(mut self, value: bool) -> Self {
396 self.is_anonymous = value;
397 self
398 }
399}
400
401/// Represents a kicked chat member.
402#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
403pub struct ChatMemberKicked {
404 /// Date when restrictions will be lifted for this user; unix time.
405 pub until_date: Integer,
406 /// Information about the user.
407 pub user: User,
408}
409
410impl ChatMemberKicked {
411 /// Creates a new `ChatMemberKicked`.
412 ///
413 /// # Arguments
414 ///
415 /// * `until_date` - Date when restrictions will be lifted for this user; unix time.
416 /// * `user` - Information about the user.
417 pub fn new(until_date: Integer, user: User) -> Self {
418 Self { user, until_date }
419 }
420}
421
422/// Represents a restricted user.
423#[serde_with::skip_serializing_none]
424#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
425pub struct ChatMemberRestricted {
426 /// Information about the user.
427 pub user: User,
428 /// Indicates whether the user may add web page previews to his messages.
429 pub can_add_web_page_previews: bool,
430 /// Indicates whether the user allowed to change the chat title, photo and other settings.
431 pub can_change_info: bool,
432 /// Indicates whether the user allowed to invite new users to the chat.
433 pub can_invite_users: bool,
434 /// Indicates whether the user is allowed to create forum topics.
435 pub can_manage_topics: bool,
436 /// Indicates whether the user allowed to pin messages; groups and supergroups only.
437 pub can_pin_messages: Option<bool>,
438 /// Indicates whether the user is allowed to send audios.
439 pub can_send_audios: Option<bool>,
440 /// Indicates whether the user is allowed to send documents.
441 pub can_send_documents: Option<bool>,
442 /// Indicates whether the user can send text messages, contacts, locations and venues.
443 pub can_send_messages: bool,
444 /// Indicates whether the user can send animations, games, stickers and use inline bots.
445 pub can_send_other_messages: bool,
446 /// Indicates whether the user is allowed to send photos.
447 pub can_send_photos: Option<bool>,
448 /// Indicates whether the user is allowed to send polls.
449 pub can_send_polls: bool,
450 /// Indicates whether the user is allowed to send video notes.
451 pub can_send_video_notes: Option<bool>,
452 /// Indicates whether the user is allowed to send videos.
453 pub can_send_videos: Option<bool>,
454 /// Indicates whether the user is allowed to send voice notes.
455 pub can_send_voice_notes: Option<bool>,
456 /// Indicates whether the user is a member of the chat at the moment of the request.
457 pub is_member: bool,
458 /// Date when restrictions will be lifted for this user; unix time.
459 pub until_date: Integer,
460}
461
462impl ChatMemberRestricted {
463 /// Creates a new `ChatMemberRestricted`
464 ///
465 /// # Arguments
466 ///
467 /// * `user` - Information about the user.
468 /// * `until_date` - Date when restrictions will be lifted for this user; unix time.
469 pub fn new(user: User, until_date: Integer) -> Self {
470 Self {
471 user,
472 can_add_web_page_previews: false,
473 can_change_info: false,
474 can_invite_users: false,
475 can_manage_topics: false,
476 can_pin_messages: None,
477 can_send_audios: None,
478 can_send_documents: None,
479 can_send_messages: false,
480 can_send_other_messages: false,
481 can_send_photos: None,
482 can_send_polls: false,
483 can_send_video_notes: None,
484 can_send_videos: None,
485 can_send_voice_notes: None,
486 is_member: false,
487 until_date,
488 }
489 }
490
491 /// Sets a new value for the `can_add_web_page_previews` flag.
492 ///
493 /// # Arguments
494 ///
495 /// * `value` - Indicates whether the user may add web page previews to his messages.
496 pub fn with_can_add_web_page_previews(mut self, value: bool) -> Self {
497 self.can_add_web_page_previews = value;
498 self
499 }
500
501 /// Sets a new value for the `can_change_info` flag.
502 ///
503 /// # Arguments
504 ///
505 /// * `value` - Indicates whether the user allowed to change the chat title,
506 /// photo and other settings.
507 pub fn with_can_change_info(mut self, value: bool) -> Self {
508 self.can_change_info = value;
509 self
510 }
511
512 /// Sets a new value for the `can_invite_users` flag.
513 ///
514 /// # Arguments
515 ///
516 /// * `value` - Indicates whether the user allowed to invite new users to the chat.
517 pub fn with_can_invite_users(mut self, value: bool) -> Self {
518 self.can_invite_users = value;
519 self
520 }
521
522 /// Sets a new value for the `can_manage_topics` flag.
523 ///
524 /// # Arguments
525 ///
526 /// * `value` - Indicates whether the user is allowed to create forum topics.
527 pub fn with_can_manage_topics(mut self, value: bool) -> Self {
528 self.can_manage_topics = value;
529 self
530 }
531
532 /// Sets a new value for the `can_pin_messages` flag.
533 ///
534 /// # Arguments
535 ///
536 /// * `value` - Indicates whether the user allowed to pin messages;
537 /// groups and supergroups only.
538 pub fn with_can_pin_messages(mut self, value: bool) -> Self {
539 self.can_pin_messages = Some(value);
540 self
541 }
542
543 /// Sets a new value for the `can_send_audios` flag.
544 ///
545 /// # Arguments
546 ///
547 /// * `value` - Indicates whether the user is allowed to send audios.
548 pub fn with_can_send_audios(mut self, value: bool) -> Self {
549 self.can_send_audios = Some(value);
550 self
551 }
552
553 /// Sets a new value for the `can_send_documents` flag.
554 ///
555 /// # Arguments
556 ///
557 /// * `value` - Indicates whether the user is allowed to send documents.
558 pub fn with_can_send_documents(mut self, value: bool) -> Self {
559 self.can_send_documents = Some(value);
560 self
561 }
562
563 /// Sets a new value for the `can_send_messages` flag.
564 ///
565 /// # Arguments
566 ///
567 /// * `value` - Indicates whether the user can send text messages,
568 /// contacts, locations and venues.
569 pub fn with_can_send_messages(mut self, value: bool) -> Self {
570 self.can_send_messages = value;
571 self
572 }
573
574 /// Sets a new value for the `can_send_other_messages` flag.
575 ///
576 /// # Arguments
577 ///
578 /// * `value` - Indicates whether the user can send animations,
579 /// games, stickers and use inline bots.
580 pub fn with_can_send_other_messages(mut self, value: bool) -> Self {
581 self.can_send_other_messages = value;
582 self
583 }
584
585 /// Sets a new value for the `can_send_photos` flag.
586 ///
587 /// # Arguments
588 ///
589 /// * `value` - Indicates whether the user is allowed to send photos.
590 pub fn with_can_send_photos(mut self, value: bool) -> Self {
591 self.can_send_photos = Some(value);
592 self
593 }
594
595 /// Sets a new value for the `can_send_polls` flag.
596 ///
597 /// # Arguments
598 ///
599 /// * `value` - Indicates whether the user is allowed to send polls.
600 pub fn with_can_send_polls(mut self, value: bool) -> Self {
601 self.can_send_polls = value;
602 self
603 }
604
605 /// Sets a new value for the `can_send_video_notes` flag.
606 ///
607 /// # Arguments
608 ///
609 /// * `value` - Indicates whether the user is allowed to send video notes.
610 pub fn with_can_send_video_notes(mut self, value: bool) -> Self {
611 self.can_send_video_notes = Some(value);
612 self
613 }
614
615 /// Sets a new value for the `can_send_videos` flag.
616 ///
617 /// # Arguments
618 ///
619 /// * `value` - Indicates whether the user is allowed to send videos.
620 pub fn with_can_send_videos(mut self, value: bool) -> Self {
621 self.can_send_videos = Some(value);
622 self
623 }
624
625 /// Sets a new value for the `can_send_voice_notes` flag.
626 ///
627 /// # Arguments
628 ///
629 /// * `value` - Indicates whether the user is allowed to send voice notes.
630 pub fn with_can_send_voice_notes(mut self, value: bool) -> Self {
631 self.can_send_voice_notes = Some(value);
632 self
633 }
634
635 /// Sets a new value for the `is_member` flag.
636 ///
637 /// # Arguments
638 ///
639 /// * `value` - Indicates whether the user is a member of the chat at the moment of the request.
640 pub fn with_is_member(mut self, value: bool) -> Self {
641 self.is_member = value;
642 self
643 }
644}
645
646/// Represents changes in a status of a chat member.
647#[serde_with::skip_serializing_none]
648#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
649pub struct ChatMemberUpdated {
650 /// Chat the user belongs to.
651 pub chat: Chat,
652 /// Date the change was done in Unix time.
653 pub date: Integer,
654 /// Performer of the action, which resulted in the change.
655 pub from: User,
656 /// New information about the chat member.
657 pub new_chat_member: ChatMember,
658 /// Previous information about the chat member.
659 pub old_chat_member: ChatMember,
660 /// Chat invite link, which was used by the user to join the chat;
661 /// for joining by invite link events only.
662 pub invite_link: Option<ChatInviteLink>,
663 /// Indicates whether the user joined the chat via a chat folder invite link.
664 pub via_chat_folder_invite_link: Option<bool>,
665 /// Indicates whether the user joined the chat after sending a direct join request
666 /// and being approved by an administrator.
667 pub via_join_request: Option<bool>,
668}
669
670impl ChatMemberUpdated {
671 /// Creates a new `ChatMemberUpdated`.
672 ///
673 /// # Arguments
674 ///
675 /// * `chat` - Chat the user belongs to.
676 /// * `date` - Date the change was done in Unix time.
677 /// * `from` - Performer of the action, which resulted in the change.
678 /// * `new_chat_member` - New information about the chat member.
679 /// * `old_chat_member` - Previous information about the chat member.
680 pub fn new(
681 chat: Chat,
682 date: Integer,
683 from: User,
684 new_chat_member: ChatMember,
685 old_chat_member: ChatMember,
686 ) -> Self {
687 Self {
688 chat,
689 date,
690 from,
691 new_chat_member,
692 old_chat_member,
693 invite_link: None,
694 via_chat_folder_invite_link: None,
695 via_join_request: None,
696 }
697 }
698
699 /// Sets a new invite link
700 ///
701 /// # Arguments
702 ///
703 /// * `value` - The invite link, which was used by the user to join the chat.
704 pub fn with_invite_link(mut self, value: ChatInviteLink) -> Self {
705 self.invite_link = Some(value);
706 self
707 }
708
709 /// Sets a new value for the `via_chat_folder_invite_link` flag.
710 ///
711 /// # Arguments
712 ///
713 /// * `value` - Indicates whether the user joined the chat via a chat folder invite link.
714 pub fn with_via_chat_folder_invite_link(mut self, value: bool) -> Self {
715 self.via_chat_folder_invite_link = Some(value);
716 self
717 }
718
719 /// Sets a new value for the `via_join_request` flag.
720 ///
721 /// # Arguments
722 ///
723 /// * `value` - Indicates whether the user joined the chat after sending a direct join request
724 /// and being approved by an administrator.
725 pub fn with_via_join_request(mut self, value: bool) -> Self {
726 self.via_join_request = Some(value);
727 self
728 }
729}
730
731/// Bans a user in a chat.
732///
733/// In the case of supergroups and channels,
734/// the user will not be able to return to the chat on their own using invite links,
735/// etc., unless unbanned first.
736///
737/// The bot must be an administrator in the chat
738/// for this to work and must have the appropriate admin rights.
739#[serde_with::skip_serializing_none]
740#[derive(Clone, Debug, Serialize)]
741pub struct BanChatMember {
742 chat_id: ChatId,
743 user_id: Integer,
744 revoke_messages: Option<bool>,
745 until_date: Option<Integer>,
746}
747
748impl BanChatMember {
749 /// Creates a new `BanChatMember`.
750 ///
751 /// # Arguments
752 ///
753 /// * `chat_id` - Unique identifier of the target chat.
754 /// * `user_id` - Unique identifier of the target user.
755 pub fn new<T>(chat_id: T, user_id: Integer) -> Self
756 where
757 T: Into<ChatId>,
758 {
759 Self {
760 chat_id: chat_id.into(),
761 user_id,
762 until_date: None,
763 revoke_messages: None,
764 }
765 }
766
767 /// Sets a new value for the `revoke_messages` flag.
768 ///
769 /// # Arguments
770 ///
771 /// * `value` - Delete all messages from the chat for the user that is being removed;
772 /// if `false`, the user will be able to see messages in the group that were
773 /// sent before the user was removed;
774 /// always `true` for supergroups and channels.
775 pub fn with_revoke_messages(mut self, value: bool) -> Self {
776 self.revoke_messages = Some(value);
777 self
778 }
779
780 /// Sets a new until date.
781 ///
782 /// # Arguments
783 ///
784 /// * `value` - The date when the user will be unbanned, unix time.
785 ///
786 /// If user is banned for more than 366 days or less than 30 seconds
787 /// from the current time they are considered to be banned forever.
788 pub fn with_until_date(mut self, value: Integer) -> Self {
789 self.until_date = Some(value);
790 self
791 }
792}
793
794impl Method for BanChatMember {
795 type Response = bool;
796
797 fn into_payload(self) -> Payload {
798 Payload::json("banChatMember", self)
799 }
800}
801
802/// Returns a list of administrators in a chat.
803///
804/// On success, returns an array of [`ChatMember`] objects that contains
805/// information about all chat administrators except other bots.
806///
807/// If the chat is a group or a supergroup and no administrators
808/// were appointed, only the creator will be returned.
809#[derive(Clone, Debug, Serialize)]
810pub struct GetChatAdministrators {
811 chat_id: ChatId,
812}
813
814impl GetChatAdministrators {
815 /// Creates a new `GetChatAdministrators`.
816 ///
817 /// # Arguments
818 ///
819 /// * `chat_id` - Unique identifier of the target chat.
820 pub fn new<T>(chat_id: T) -> Self
821 where
822 T: Into<ChatId>,
823 {
824 Self {
825 chat_id: chat_id.into(),
826 }
827 }
828}
829
830impl Method for GetChatAdministrators {
831 type Response = Vec<ChatMember>;
832
833 fn into_payload(self) -> Payload {
834 Payload::json("getChatAdministrators", self)
835 }
836}
837
838/// Returns a member of a chat.
839#[derive(Clone, Debug, Serialize)]
840pub struct GetChatMember {
841 chat_id: ChatId,
842 user_id: Integer,
843}
844
845impl GetChatMember {
846 /// Creates a new `GetChatMember`
847 ///
848 /// # Arguments
849 ///
850 /// * `chat_id` - Unique identifier of the target chat.
851 /// * `user_id` - Unique identifier of the target user.
852 pub fn new<T>(chat_id: T, user_id: Integer) -> Self
853 where
854 T: Into<ChatId>,
855 {
856 Self {
857 chat_id: chat_id.into(),
858 user_id,
859 }
860 }
861}
862
863impl Method for GetChatMember {
864 type Response = ChatMember;
865
866 fn into_payload(self) -> Payload {
867 Payload::json("getChatMember", self)
868 }
869}
870
871/// Returns a number of members in a chat.
872#[derive(Clone, Debug, Serialize)]
873pub struct GetChatMemberCount {
874 chat_id: ChatId,
875}
876
877impl GetChatMemberCount {
878 /// Creates a new `GetChatMemberCount`.
879 ///
880 /// # Arguments
881 ///
882 /// * `chat_id` - Unique identifier of the target chat.
883 pub fn new<T>(chat_id: T) -> Self
884 where
885 T: Into<ChatId>,
886 {
887 Self {
888 chat_id: chat_id.into(),
889 }
890 }
891}
892
893impl Method for GetChatMemberCount {
894 type Response = Integer;
895
896 fn into_payload(self) -> Payload {
897 Payload::json("getChatMemberCount", self)
898 }
899}
900
901/// Promotes or demotes a user in a chat.
902///
903/// The bot must be an administrator in the chat
904/// for this to work and must have the appropriate admin rights.
905#[serde_with::skip_serializing_none]
906#[derive(Clone, Debug, Serialize)]
907pub struct PromoteChatMember {
908 chat_id: ChatId,
909 user_id: Integer,
910 can_change_info: Option<bool>,
911 can_delete_messages: Option<bool>,
912 can_delete_stories: Option<bool>,
913 can_edit_messages: Option<bool>,
914 can_edit_stories: Option<bool>,
915 can_invite_users: Option<bool>,
916 can_manage_chat: Option<bool>,
917 can_manage_topics: Option<bool>,
918 can_manage_video_chats: Option<bool>,
919 can_pin_messages: Option<bool>,
920 can_post_messages: Option<bool>,
921 can_post_stories: Option<bool>,
922 can_promote_members: Option<bool>,
923 can_restrict_members: Option<bool>,
924 is_anonymous: Option<bool>,
925}
926
927impl PromoteChatMember {
928 /// Creates a new `PromoteChatMember`
929 ///
930 /// # Arguments
931 ///
932 /// * `chat_id` - Unique identifier of the target chat.
933 /// * `user_id` - Unique identifier of the target user.
934 pub fn new<T>(chat_id: T, user_id: Integer) -> Self
935 where
936 T: Into<ChatId>,
937 {
938 Self {
939 chat_id: chat_id.into(),
940 user_id,
941 can_change_info: None,
942 can_delete_messages: None,
943 can_delete_stories: None,
944 can_edit_messages: None,
945 can_edit_stories: None,
946 can_invite_users: None,
947 can_manage_chat: None,
948 can_manage_topics: None,
949 can_manage_video_chats: None,
950 can_pin_messages: None,
951 can_post_messages: None,
952 can_post_stories: None,
953 can_promote_members: None,
954 can_restrict_members: None,
955 is_anonymous: None,
956 }
957 }
958
959 /// Promotes all privileges.
960 pub fn promote_all(mut self) -> Self {
961 self.is_anonymous = Some(true);
962 self.can_change_info = Some(true);
963 self.can_delete_messages = Some(true);
964 self.can_delete_stories = Some(true);
965 self.can_edit_messages = Some(true);
966 self.can_edit_stories = Some(true);
967 self.can_invite_users = Some(true);
968 self.can_manage_chat = Some(true);
969 self.can_manage_topics = Some(true);
970 self.can_manage_video_chats = Some(true);
971 self.can_pin_messages = Some(true);
972 self.can_post_messages = Some(true);
973 self.can_post_stories = Some(true);
974 self.can_promote_members = Some(true);
975 self.can_restrict_members = Some(true);
976 self
977 }
978
979 /// Demotes all privileges.
980 pub fn demote_all(mut self) -> Self {
981 self.is_anonymous = Some(false);
982 self.can_change_info = Some(false);
983 self.can_delete_messages = Some(false);
984 self.can_delete_stories = Some(false);
985 self.can_edit_messages = Some(false);
986 self.can_edit_stories = Some(false);
987 self.can_invite_users = Some(false);
988 self.can_manage_chat = Some(false);
989 self.can_manage_topics = Some(false);
990 self.can_manage_video_chats = Some(false);
991 self.can_pin_messages = Some(false);
992 self.can_post_messages = Some(false);
993 self.can_post_stories = Some(false);
994 self.can_promote_members = Some(false);
995 self.can_restrict_members = Some(false);
996 self
997 }
998
999 /// Sets a new value for the `can_change_info` flag.
1000 ///
1001 /// # Arguments
1002 ///
1003 /// * `value` - Indicates whether the administrator can change chat title,
1004 /// photo and other settings.
1005 pub fn with_can_change_info(mut self, value: bool) -> Self {
1006 self.can_change_info = Some(value);
1007 self
1008 }
1009
1010 /// Sets a new value for the `can_delete_messages` flag.
1011 ///
1012 /// # Arguments
1013 ///
1014 /// * `value` - Indicates whether the administrator can delete messages of other users.
1015 pub fn with_can_delete_messages(mut self, value: bool) -> Self {
1016 self.can_delete_messages = Some(value);
1017 self
1018 }
1019
1020 /// Sets a new value for the `can_delete_stories` flag.
1021 ///
1022 /// # Arguments
1023 ///
1024 /// * `value` - Indicates whether the administrator can delete stories posted by other users; channels only.
1025 pub fn with_can_delete_stories(mut self, value: bool) -> Self {
1026 self.can_delete_stories = Some(value);
1027 self
1028 }
1029
1030 /// Sets a new value for the `can_edit_messages` flag.
1031 ///
1032 /// # Arguments
1033 ///
1034 /// * `value` - Indicates whether the administrator can edit messages
1035 /// of other users and can pin messages; channels only.
1036 pub fn with_can_edit_messages(mut self, value: bool) -> Self {
1037 self.can_edit_messages = Some(value);
1038 self
1039 }
1040
1041 /// Sets a new value for the `can_edit_stories` flag.
1042 ///
1043 /// # Arguments
1044 ///
1045 /// * `value` - Indicates whether the administrator can
1046 /// edit stories posted by other users; channels only.
1047 pub fn with_can_edit_stories(mut self, value: bool) -> Self {
1048 self.can_edit_stories = Some(value);
1049 self
1050 }
1051
1052 /// Sets a new value for the `can_invite_users` flag.
1053 ///
1054 /// # Arguments
1055 ///
1056 /// * `value` - Indicates whether the administrator can invite new users to the chat.
1057 pub fn with_can_invite_users(mut self, value: bool) -> Self {
1058 self.can_invite_users = Some(value);
1059 self
1060 }
1061
1062 /// Sets a new value for the `can_manage_chat` flag.
1063 ///
1064 /// # Arguments
1065 ///
1066 /// * `value` - Indicates whether the administrator can access the chat event log,
1067 /// chat statistics, message statistics in channels, see channel members,
1068 /// see anonymous administrators in supergroups and ignore slow mode;
1069 /// implied by any other administrator privilege.
1070 pub fn with_can_manage_chat(mut self, value: bool) -> Self {
1071 self.can_manage_chat = Some(value);
1072 self
1073 }
1074
1075 /// Sets a new value for the `can_manage_topics` flag.
1076 ///
1077 /// # Arguments
1078 ///
1079 /// * `value` - User is allowed to create, rename, close, and reopen forum topics; supergroups only.
1080 pub fn with_can_manage_topics(mut self, value: bool) -> Self {
1081 self.can_manage_topics = Some(value);
1082 self
1083 }
1084
1085 /// Sets a new value for the `can_manage_video_chats` flag.
1086 ///
1087 /// # Arguments
1088 ///
1089 /// * `value` - Indicates whether the administrator can manage video chats; supergroups only.
1090 pub fn with_can_manage_video_chats(mut self, value: bool) -> Self {
1091 self.can_manage_video_chats = Some(value);
1092 self
1093 }
1094
1095 /// Sets a new value for the `can_pin_messages` flag.
1096 ///
1097 /// # Arguments
1098 ///
1099 /// * `value` - Indicates whether the administrator can pin messages; supergroups only.
1100 pub fn with_can_pin_messages(mut self, value: bool) -> Self {
1101 self.can_pin_messages = Some(value);
1102 self
1103 }
1104
1105 /// Sets a new value for the `can_post_messages` flag.
1106 ///
1107 /// # Arguments
1108 ///
1109 /// * `value` - Indicates whether the administrator can create channel posts; channels only.
1110 pub fn with_can_post_messages(mut self, value: bool) -> Self {
1111 self.can_post_messages = Some(value);
1112 self
1113 }
1114
1115 /// Sets a new value for the `can_post_stories` flag.
1116 ///
1117 /// # Arguments
1118 ///
1119 /// * `value` - Indicates whether the administrator can post stories in the channel; channels only.
1120 pub fn with_can_post_stories(mut self, value: bool) -> Self {
1121 self.can_post_stories = Some(value);
1122 self
1123 }
1124
1125 /// Sets a new value for the `can_promote_members` flag.
1126 ///
1127 /// # Arguments
1128 ///
1129 /// * `value` - Indicates whether the administrator can add new administrators with a subset
1130 /// of his own privileges or demote administrators that he has promoted,
1131 /// directly or indirectly (promoted by administrators that were appointed by him).
1132 pub fn with_can_promote_members(mut self, value: bool) -> Self {
1133 self.can_promote_members = Some(value);
1134 self
1135 }
1136
1137 /// Sets a new value for the `can_restrict_members` flag.
1138 ///
1139 /// # Arguments
1140 ///
1141 /// * `value` - Indicates whether the administrator can restrict, ban or unban chat members.
1142 pub fn with_can_restrict_members(mut self, value: bool) -> Self {
1143 self.can_restrict_members = Some(value);
1144 self
1145 }
1146
1147 /// Sets a new value for the `is_anonymous` flag.
1148 ///
1149 /// # Arguments
1150 ///
1151 /// * `value` - Indicates whether the administrator's presence in the chat is hidden.
1152 pub fn with_is_anonymous(mut self, value: bool) -> Self {
1153 self.is_anonymous = Some(value);
1154 self
1155 }
1156}
1157
1158impl Method for PromoteChatMember {
1159 type Response = bool;
1160
1161 fn into_payload(self) -> Payload {
1162 Payload::json("promoteChatMember", self)
1163 }
1164}
1165
1166/// Restricts a user in a supergroup.
1167///
1168/// The bot must be an administrator in the supergroup
1169/// for this to work and must have the appropriate admin rights.
1170#[serde_with::skip_serializing_none]
1171#[derive(Clone, Debug, Serialize)]
1172pub struct RestrictChatMember {
1173 chat_id: ChatId,
1174 permissions: ChatPermissions,
1175 user_id: Integer,
1176 until_date: Option<Integer>,
1177 use_independent_chat_permissions: Option<bool>,
1178}
1179
1180impl RestrictChatMember {
1181 /// Creates a new `RestrictChatMember`.
1182 ///
1183 /// # Arguments
1184 ///
1185 /// * `chat_id` - Unique identifier for the target chat.
1186 /// * `user_id` - Unique identifier of the target user.
1187 pub fn new<T>(chat_id: T, user_id: Integer) -> Self
1188 where
1189 T: Into<ChatId>,
1190 {
1191 RestrictChatMember {
1192 chat_id: chat_id.into(),
1193 permissions: ChatPermissions::default(),
1194 user_id,
1195 until_date: None,
1196 use_independent_chat_permissions: None,
1197 }
1198 }
1199
1200 /// Allows everything.
1201 pub fn allow_all(mut self) -> Self {
1202 self.permissions = ChatPermissions::allowed();
1203 self
1204 }
1205
1206 /// Restricts everything.
1207 pub fn restrict_all(mut self) -> Self {
1208 self.permissions = ChatPermissions::restricted();
1209 self
1210 }
1211
1212 /// Replaces current permissions with the new one.
1213 ///
1214 /// # Arguments
1215 ///
1216 /// * `value` - The new permissions.
1217 pub fn with_permissions(mut self, value: ChatPermissions) -> Self {
1218 self.permissions = value;
1219 self
1220 }
1221
1222 /// Sets a new until date.
1223 ///
1224 /// # Arguments
1225 ///
1226 /// * `value` - The date when restrictions will be lifted for the user, unix time.
1227 ///
1228 /// If user is restricted for more than 366 days or less than 30 seconds
1229 /// from the current time, they are considered to be restricted forever.
1230 pub fn with_until_date(mut self, value: Integer) -> Self {
1231 self.until_date = Some(value);
1232 self
1233 }
1234
1235 /// Sets a new value for the `use_independent_chat_permissions` flag.
1236 ///
1237 /// # Arguments
1238 ///
1239 /// * `value` - Indicates whether the chat permissions are set independently.
1240 ///
1241 /// If `false`, the `can_send_other_messages` and `can_add_web_page_previews` permissions
1242 /// will imply the `can_send_messages`, `can_send_audios`, `can_send_documents`,
1243 /// `can_send_photos`, `can_send_videos`, `can_send_video_notes`,
1244 /// and `can_send_voice_notes` permissions; the `can_send_polls` permission
1245 /// will imply the `can_send_messages` permission.
1246 pub fn with_use_independent_chat_permissions(mut self, value: bool) -> Self {
1247 self.use_independent_chat_permissions = Some(value);
1248 self
1249 }
1250}
1251
1252impl Method for RestrictChatMember {
1253 type Response = bool;
1254
1255 fn into_payload(self) -> Payload {
1256 Payload::json("restrictChatMember", self)
1257 }
1258}
1259
1260/// Sets a custom title for an administrator in a supergroup promoted by the bot.
1261#[derive(Clone, Debug, Serialize)]
1262pub struct SetChatAdministratorCustomTitle {
1263 chat_id: ChatId,
1264 user_id: Integer,
1265 custom_title: String,
1266}
1267
1268impl SetChatAdministratorCustomTitle {
1269 /// Creates a new `SetChatAdministratorCustomTitle`.
1270 ///
1271 /// # Arguments
1272 ///
1273 /// * `chat_id` - Unique identifier of the target chat.
1274 /// * `custom_title` - New custom title for the administrator; 0-16 characters; emoji are not allowed.
1275 /// * `user_id` - Unique identifier of the target user.
1276 pub fn new<A, B>(chat_id: A, custom_title: B, user_id: Integer) -> Self
1277 where
1278 A: Into<ChatId>,
1279 B: Into<String>,
1280 {
1281 Self {
1282 chat_id: chat_id.into(),
1283 custom_title: custom_title.into(),
1284 user_id,
1285 }
1286 }
1287}
1288
1289impl Method for SetChatAdministratorCustomTitle {
1290 type Response = bool;
1291
1292 fn into_payload(self) -> Payload {
1293 Payload::json("setChatAdministratorCustomTitle", self)
1294 }
1295}
1296
1297/// Unbans a previously kicked user in a supergroup or channel.
1298///
1299/// The user will not return to the group or channel
1300/// automatically, but will be able to join via link, etc.
1301///
1302/// The bot must be an administrator for this to work
1303#[serde_with::skip_serializing_none]
1304#[derive(Clone, Debug, Serialize)]
1305pub struct UnbanChatMember {
1306 chat_id: ChatId,
1307 user_id: Integer,
1308 only_if_banned: Option<bool>,
1309}
1310
1311impl UnbanChatMember {
1312 /// Creates a new `UnbanChatMember`.
1313 ///
1314 /// # Arguments
1315 ///
1316 /// * `chat_id` - Unique identifier of the target chat.
1317 /// * `user_id` - Unique identifier of the target user.
1318 pub fn new<T>(chat_id: T, user_id: Integer) -> Self
1319 where
1320 T: Into<ChatId>,
1321 {
1322 UnbanChatMember {
1323 chat_id: chat_id.into(),
1324 user_id,
1325 only_if_banned: None,
1326 }
1327 }
1328
1329 /// Sets a new value for the `only_if_banned` flag.
1330 ///
1331 /// # Arguments
1332 ///
1333 /// * `value` - If `true` - do nothing if the user is not banned.
1334 pub fn with_only_if_banned(mut self, only_if_banned: bool) -> Self {
1335 self.only_if_banned = Some(only_if_banned);
1336 self
1337 }
1338}
1339
1340impl Method for UnbanChatMember {
1341 type Response = bool;
1342
1343 fn into_payload(self) -> Payload {
1344 Payload::json("unbanChatMember", self)
1345 }
1346}