Skip to main content

rustigram_types/
user.rs

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