telbot_types/
user.rs

1use serde::{Deserialize, Serialize};
2
3use crate::chat::{
4    ApproveChatJoinRequest, BanChatMember, ChatId, ChatPermissions, DeclineChatJoinRequest,
5    GetChatMember, PromoteChatMember, RestrictChatMember, SetChatAdministratorCustomTitle,
6    UnbanChatMember,
7};
8use crate::file::PhotoSize;
9use crate::{JsonMethod, TelegramMethod};
10
11/// This object represents a Telegram user or bot.
12#[derive(Debug, Clone, Serialize, Deserialize)]
13pub struct User {
14    /// Unique identifier for this user or bot.
15    pub id: i64,
16    /// True, if this user is a bot
17    pub is_bot: bool,
18    /// User's or bot's first name
19    pub first_name: String,
20    /// User's or bot's last name
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub last_name: Option<String>,
23    /// User's or bot's username
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub username: Option<String>,
26    /// [IETF language tag](https://en.wikipedia.org/wiki/IETF_language_tag) of the user's language
27    #[serde(skip_serializing_if = "Option::is_none")]
28    pub language_code: Option<String>,
29    /// True, if the bot can be invited to groups.
30    /// Returned only in getMe.
31    #[serde(skip_serializing_if = "Option::is_none")]
32    pub can_join_groups: Option<bool>,
33    /// True, if [privacy mode](https://core.telegram.org/bots#privacy-mode) is disabled for the bot.
34    /// Returned only in getMe.
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub can_read_all_group_messages: Option<bool>,
37    /// True, if the bot supports inline queries.
38    /// Returned only in getMe.
39    #[serde(skip_serializing_if = "Option::is_none")]
40    pub supports_inline_queries: Option<bool>,
41}
42
43impl User {
44    pub fn get_profile_photos(&self) -> GetUserProfilePhotos {
45        GetUserProfilePhotos::new(self.id)
46    }
47
48    pub fn ban_from(&self, chat_id: impl Into<ChatId>) -> BanChatMember {
49        BanChatMember::new(chat_id, self.id)
50    }
51
52    pub fn unban_from(&self, chat_id: impl Into<ChatId>) -> UnbanChatMember {
53        UnbanChatMember::new(chat_id, self.id)
54    }
55
56    pub fn restrict_from(
57        &self,
58        chat_id: impl Into<ChatId>,
59        permissions: ChatPermissions,
60    ) -> RestrictChatMember {
61        RestrictChatMember::new(chat_id, self.id, permissions)
62    }
63
64    pub fn promote_from(&self, chat_id: impl Into<ChatId>) -> PromoteChatMember {
65        PromoteChatMember::new(chat_id, self.id)
66    }
67
68    pub fn set_administrator_title_from(
69        &self,
70        chat_id: impl Into<ChatId>,
71        custom_title: impl Into<String>,
72    ) -> SetChatAdministratorCustomTitle {
73        SetChatAdministratorCustomTitle::new(chat_id, self.id, custom_title)
74    }
75
76    pub fn approve_join_to(&self, chat_id: impl Into<ChatId>) -> ApproveChatJoinRequest {
77        ApproveChatJoinRequest::new(chat_id, self.id)
78    }
79
80    pub fn decline_join_to(&self, chat_id: impl Into<ChatId>) -> DeclineChatJoinRequest {
81        DeclineChatJoinRequest::new(chat_id, self.id)
82    }
83
84    pub fn get_member_from(&self, chat_id: impl Into<ChatId>) -> GetChatMember {
85        GetChatMember::new(chat_id, self.id)
86    }
87}
88
89/// This object represent a user's profile pictures.
90#[derive(Debug, Deserialize)]
91pub struct UserProfilePhotos {
92    /// Total number of profile pictures the target user has
93    pub total_count: usize,
94    /// Requested profile pictures (in up to 4 sizes each)
95    pub photos: Vec<Vec<PhotoSize>>,
96}
97
98/// Use this method to get a list of profile pictures for a user.
99/// Returns a [UserProfilePhotos] object.
100#[derive(Clone, Serialize)]
101pub struct GetUserProfilePhotos {
102    /// Unique identifier of the target user
103    user_id: i64,
104    /// Sequential number of the first photo to be returned. By default, all photos are returned.
105    #[serde(skip_serializing_if = "Option::is_none")]
106    offset: Option<u32>,
107    /// Limits the number of photos to be retrieved. Values between 1-100 are accepted. Defaults to 100.
108    #[serde(skip_serializing_if = "Option::is_none")]
109    limit: Option<u32>,
110}
111
112impl GetUserProfilePhotos {
113    /// Create a new getUserProfilePhotos request
114    pub fn new(user_id: i64) -> Self {
115        Self {
116            user_id,
117            offset: None,
118            limit: None,
119        }
120    }
121    /// Set offset
122    pub fn with_offset(self, offset: u32) -> Self {
123        Self {
124            offset: Some(offset),
125            ..self
126        }
127    }
128    /// Set limit
129    pub fn with_limit(self, limit: u32) -> Self {
130        Self {
131            limit: Some(limit),
132            ..self
133        }
134    }
135}
136
137impl TelegramMethod for GetUserProfilePhotos {
138    type Response = UserProfilePhotos;
139
140    fn name() -> &'static str {
141        "getUserProfilePhotos"
142    }
143}
144
145impl JsonMethod for GetUserProfilePhotos {}