Skip to main content

rustigram_types/
user.rs

1use serde::{Deserialize, Serialize};
2
3/// Represents a Telegram user or bot.
4///
5/// `#[non_exhaustive]`: build one with [`Default::default`] and assign the
6/// fields you need, so that Bot API field additions stay non-breaking.
7#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
8#[non_exhaustive]
9pub struct User {
10    /// Unique identifier for this user or bot.
11    pub id: i64,
12
13    /// `true` if this user is a bot.
14    pub is_bot: bool,
15
16    /// User's or bot's first name.
17    pub first_name: String,
18
19    /// User's or bot's last name.
20    #[serde(skip_serializing_if = "Option::is_none")]
21    pub last_name: Option<String>,
22
23    /// User's or bot's username.
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub username: Option<String>,
26
27    /// IETF language tag of the user's language.
28    #[serde(skip_serializing_if = "Option::is_none")]
29    pub language_code: Option<String>,
30
31    /// `true` if this user is a Telegram Premium user.
32    #[serde(skip_serializing_if = "Option::is_none")]
33    pub is_premium: Option<bool>,
34
35    /// `true` if this user added the bot to the attachment menu.
36    #[serde(skip_serializing_if = "Option::is_none")]
37    pub added_to_attachment_menu: Option<bool>,
38
39    /// Bots only — `true` if the bot can be invited to groups.
40    #[serde(skip_serializing_if = "Option::is_none")]
41    pub can_join_groups: Option<bool>,
42
43    /// Bots only — `true` if privacy mode is disabled for the bot.
44    #[serde(skip_serializing_if = "Option::is_none")]
45    pub can_read_all_group_messages: Option<bool>,
46
47    /// Bots only — `true` if the bot supports inline queries.
48    #[serde(skip_serializing_if = "Option::is_none")]
49    pub supports_inline_queries: Option<bool>,
50
51    /// Bots only — `true` if the bot can be connected to a Telegram Business account.
52    #[serde(skip_serializing_if = "Option::is_none")]
53    pub can_connect_to_business: Option<bool>,
54
55    /// Bots only — `true` if the bot has a main Web App.
56    #[serde(skip_serializing_if = "Option::is_none")]
57    pub has_main_web_app: Option<bool>,
58
59    /// Bots only — `true` if the bot has forum topic mode enabled in private chats.
60    #[serde(skip_serializing_if = "Option::is_none")]
61    pub has_topics_enabled: Option<bool>,
62
63    /// Bots only — `true` if the bot allows users to create and delete topics in private chats.
64    #[serde(skip_serializing_if = "Option::is_none")]
65    pub allows_users_to_create_topics: Option<bool>,
66
67    /// Bots only — `true` if other bots can be created to be controlled by this bot.
68    #[serde(skip_serializing_if = "Option::is_none")]
69    pub can_manage_bots: Option<bool>,
70
71    /// Bots only — `true` if the bot supports guest queries from chats it is not a member of.
72    /// Returned only in `getMe`.
73    #[serde(skip_serializing_if = "Option::is_none")]
74    pub supports_guest_queries: Option<bool>,
75
76    /// Bots only — `true` if the bot supports join request queries and can be assigned to
77    /// process them. Returned only in `getMe`.
78    #[serde(skip_serializing_if = "Option::is_none")]
79    pub supports_join_request_queries: Option<bool>,
80}
81
82impl User {
83    /// Returns a human-readable display name, preferring `first_name + last_name`.
84    #[must_use]
85    pub fn full_name(&self) -> String {
86        match &self.last_name {
87            Some(last) => format!("{} {}", self.first_name, last),
88            None => self.first_name.clone(),
89        }
90    }
91
92    /// Returns a `@username` mention string if available.
93    #[must_use]
94    pub fn mention(&self) -> Option<String> {
95        self.username.as_ref().map(|u| format!("@{u}"))
96    }
97}
98
99/// Container for a user's profile photos.
100#[derive(Debug, Clone, Serialize, Deserialize)]
101pub struct UserProfilePhotos {
102    /// Total number of profile pictures the target user has.
103    pub total_count: u32,
104
105    /// Requested profile pictures (in up to 4 sizes each).
106    pub photos: Vec<Vec<crate::file::PhotoSize>>,
107}
108
109/// Container for a user's profile audios.
110#[derive(Debug, Clone, Serialize, Deserialize)]
111pub struct UserProfileAudios {
112    /// Total number of profile audios.
113    pub total_count: u32,
114
115    /// Requested profile audios.
116    pub audios: Vec<crate::file::Audio>,
117}
118
119/// Bot command scope — defines where a specific list of commands applies.
120#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
121#[serde(tag = "type", rename_all = "snake_case")]
122pub enum BotCommandScope {
123    /// Default scope — used when no more specific scope is applicable.
124    Default,
125    /// Covers all private chats.
126    AllPrivateChats,
127    /// Covers all group and supergroup chats.
128    AllGroupChats,
129    /// Covers all group and supergroup chat administrators.
130    AllChatAdministrators,
131    /// Covers a specific chat.
132    Chat {
133        /// Unique identifier or username of the target chat.
134        chat_id: ChatId,
135    },
136    /// Covers all administrators of a specific group or supergroup.
137    ChatAdministrators {
138        /// Unique identifier or username of the target chat.
139        chat_id: ChatId,
140    },
141    /// Covers a specific member of a group or supergroup.
142    ChatMember {
143        /// Unique identifier or username of the target chat.
144        chat_id: ChatId,
145        /// Unique identifier of the target user.
146        user_id: i64,
147    },
148}
149
150/// Represents a bot command.
151#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
152pub struct BotCommand {
153    /// Text of the command (1–32 characters, lowercase, alphanumeric + underscore).
154    pub command: String,
155    /// Description of the command (3–256 characters).
156    pub description: String,
157    /// `true` if the command sends an ephemeral message, visible only to the
158    /// sender of the message and the bot.
159    #[serde(skip_serializing_if = "Option::is_none")]
160    pub is_ephemeral: Option<bool>,
161}
162
163/// Flexible chat identifier — either a numeric ID or a `@username`.
164#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
165#[serde(untagged)]
166pub enum ChatId {
167    /// Numeric chat ID (can be negative for groups/channels).
168    Id(i64),
169    /// Chat `@username`.
170    Username(String),
171}
172
173impl From<i64> for ChatId {
174    fn from(id: i64) -> Self {
175        Self::Id(id)
176    }
177}
178
179impl From<&str> for ChatId {
180    fn from(username: &str) -> Self {
181        Self::Username(username.to_owned())
182    }
183}
184
185impl From<String> for ChatId {
186    fn from(username: String) -> Self {
187        Self::Username(username)
188    }
189}
190
191impl std::fmt::Display for ChatId {
192    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
193        match self {
194            Self::Id(id) => write!(f, "{id}"),
195            Self::Username(u) => write!(f, "{u}"),
196        }
197    }
198}
199
200/// The bot's display name.
201///
202/// Returned by [`getMyName`](https://core.telegram.org/bots/api#getmyname).
203#[derive(Debug, Clone, Serialize, Deserialize)]
204pub struct BotName {
205    /// The bot's name.
206    pub name: String,
207}
208
209/// The bot's description shown in the chat when the chat is empty.
210///
211/// Returned by [`getMyDescription`](https://core.telegram.org/bots/api#getmydescription).
212#[derive(Debug, Clone, Serialize, Deserialize)]
213pub struct BotDescription {
214    /// The bot's description.
215    pub description: String,
216}
217
218/// The bot's short description shown on the profile page and in sharing links.
219///
220/// Returned by [`getMyShortDescription`](https://core.telegram.org/bots/api#getmyshortdescription).
221#[derive(Debug, Clone, Serialize, Deserialize)]
222pub struct BotShortDescription {
223    /// The bot's short description.
224    pub short_description: String,
225}
226
227/// Represents the rights of an administrator in a chat.
228///
229/// Used by [`setMyDefaultAdministratorRights`](https://core.telegram.org/bots/api#setmydefaultadministratorrights)
230/// and [`getMyDefaultAdministratorRights`](https://core.telegram.org/bots/api#getmydefaultadministratorrights).
231#[derive(Debug, Clone, Default, Serialize, Deserialize)]
232pub struct ChatAdministratorRights {
233    /// `true` if the user's presence in the chat is hidden.
234    pub is_anonymous: bool,
235    /// `true` if the administrator can access the chat event log, boost list, etc.
236    pub can_manage_chat: bool,
237    /// `true` if the administrator can delete messages of other users.
238    pub can_delete_messages: bool,
239    /// `true` if the administrator can manage video chats.
240    pub can_manage_video_chats: bool,
241    /// `true` if the administrator can restrict, ban, or unban chat members.
242    pub can_restrict_members: bool,
243    /// `true` if the administrator can promote members to administrators.
244    pub can_promote_members: bool,
245    /// `true` if the user is allowed to change the chat title, photo, and other settings.
246    pub can_change_info: bool,
247    /// `true` if the user is allowed to invite new users to the chat.
248    pub can_invite_users: bool,
249    /// `true` if the administrator can post stories to the chat.
250    pub can_post_stories: bool,
251    /// `true` if the administrator can edit stories posted by other users.
252    pub can_edit_stories: bool,
253    /// `true` if the administrator can delete stories posted by other users.
254    pub can_delete_stories: bool,
255    /// `true` if the administrator can post messages in the channel; channels only.
256    #[serde(skip_serializing_if = "Option::is_none")]
257    pub can_post_messages: Option<bool>,
258    /// `true` if the administrator can edit messages of other users and pin messages; channels only.
259    #[serde(skip_serializing_if = "Option::is_none")]
260    pub can_edit_messages: Option<bool>,
261    /// `true` if the user is allowed to pin messages; groups and supergroups only.
262    #[serde(skip_serializing_if = "Option::is_none")]
263    pub can_pin_messages: Option<bool>,
264    /// `true` if the user is allowed to create, rename, close, and reopen forum topics; supergroups only.
265    #[serde(skip_serializing_if = "Option::is_none")]
266    pub can_manage_topics: Option<bool>,
267    /// `true` if the administrator can manage direct messages and decline suggested posts; channels only.
268    #[serde(skip_serializing_if = "Option::is_none")]
269    pub can_manage_direct_messages: Option<bool>,
270    /// `true` if the administrator can edit the tags of regular members; groups and supergroups only.
271    #[serde(skip_serializing_if = "Option::is_none")]
272    pub can_manage_tags: Option<bool>,
273}
274
275/// Describes the access settings of a bot.
276///
277/// Returned by [`getManagedBotAccessSettings`](https://core.telegram.org/bots/api#getmanagedbotaccesssettings).
278#[derive(Debug, Clone, Serialize, Deserialize)]
279pub struct BotAccessSettings {
280    /// `true` if only selected users can access the bot. The bot's owner can always access it.
281    pub is_access_restricted: bool,
282
283    /// The list of other users who have access to the bot if the access is restricted.
284    #[serde(skip_serializing_if = "Option::is_none")]
285    pub added_users: Option<Vec<User>>,
286}