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
73impl User {
74    /// Returns a human-readable display name, preferring `first_name + last_name`.
75    #[must_use]
76    pub fn full_name(&self) -> String {
77        match &self.last_name {
78            Some(last) => format!("{} {}", self.first_name, last),
79            None => self.first_name.clone(),
80        }
81    }
82
83    /// Returns a `@username` mention string if available.
84    #[must_use]
85    pub fn mention(&self) -> Option<String> {
86        self.username.as_ref().map(|u| format!("@{u}"))
87    }
88}
89
90/// Container for a user's profile photos.
91#[derive(Debug, Clone, Serialize, Deserialize)]
92pub struct UserProfilePhotos {
93    /// Total number of profile pictures the target user has.
94    pub total_count: u32,
95
96    /// Requested profile pictures (in up to 4 sizes each).
97    pub photos: Vec<Vec<crate::file::PhotoSize>>,
98}
99
100/// Container for a user's profile audios.
101#[derive(Debug, Clone, Serialize, Deserialize)]
102pub struct UserProfileAudios {
103    /// Total number of profile audios.
104    pub total_count: u32,
105
106    /// Requested profile audios.
107    pub audios: Vec<crate::file::Audio>,
108}
109
110/// Bot command scope — defines where a specific list of commands applies.
111#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
112#[serde(tag = "type", rename_all = "snake_case")]
113pub enum BotCommandScope {
114    /// Default scope — used when no more specific scope is applicable.
115    Default,
116    /// Covers all private chats.
117    AllPrivateChats,
118    /// Covers all group and supergroup chats.
119    AllGroupChats,
120    /// Covers all group and supergroup chat administrators.
121    AllChatAdministrators,
122    /// Covers a specific chat.
123    Chat {
124        /// Unique identifier or username of the target chat.
125        chat_id: ChatId,
126    },
127    /// Covers all administrators of a specific group or supergroup.
128    ChatAdministrators {
129        /// Unique identifier or username of the target chat.
130        chat_id: ChatId,
131    },
132    /// Covers a specific member of a group or supergroup.
133    ChatMember {
134        /// Unique identifier or username of the target chat.
135        chat_id: ChatId,
136        /// Unique identifier of the target user.
137        user_id: i64,
138    },
139}
140
141/// Represents a bot command.
142#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
143pub struct BotCommand {
144    /// Text of the command (1–32 characters, lowercase, alphanumeric + underscore).
145    pub command: String,
146    /// Description of the command (3–256 characters).
147    pub description: String,
148}
149
150/// Flexible chat identifier — either a numeric ID or a `@username`.
151#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
152#[serde(untagged)]
153pub enum ChatId {
154    /// Numeric chat ID (can be negative for groups/channels).
155    Id(i64),
156    /// Chat `@username`.
157    Username(String),
158}
159
160impl From<i64> for ChatId {
161    fn from(id: i64) -> Self {
162        Self::Id(id)
163    }
164}
165
166impl From<&str> for ChatId {
167    fn from(username: &str) -> Self {
168        Self::Username(username.to_owned())
169    }
170}
171
172impl From<String> for ChatId {
173    fn from(username: String) -> Self {
174        Self::Username(username)
175    }
176}
177
178impl std::fmt::Display for ChatId {
179    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
180        match self {
181            Self::Id(id) => write!(f, "{id}"),
182            Self::Username(u) => write!(f, "{u}"),
183        }
184    }
185}
186
187/// The bot's display name.
188///
189/// Returned by [`getMyName`](https://core.telegram.org/bots/api#getmyname).
190#[derive(Debug, Clone, Serialize, Deserialize)]
191pub struct BotName {
192    /// The bot's name.
193    pub name: String,
194}
195
196/// The bot's description shown in the chat when the chat is empty.
197///
198/// Returned by [`getMyDescription`](https://core.telegram.org/bots/api#getmydescription).
199#[derive(Debug, Clone, Serialize, Deserialize)]
200pub struct BotDescription {
201    /// The bot's description.
202    pub description: String,
203}
204
205/// The bot's short description shown on the profile page and in sharing links.
206///
207/// Returned by [`getMyShortDescription`](https://core.telegram.org/bots/api#getmyshortdescription).
208#[derive(Debug, Clone, Serialize, Deserialize)]
209pub struct BotShortDescription {
210    /// The bot's short description.
211    pub short_description: String,
212}
213
214/// Represents the rights of an administrator in a chat.
215///
216/// Used by [`setMyDefaultAdministratorRights`](https://core.telegram.org/bots/api#setmydefaultadministratorrights)
217/// and [`getMyDefaultAdministratorRights`](https://core.telegram.org/bots/api#getmydefaultadministratorrights).
218#[derive(Debug, Clone, Default, Serialize, Deserialize)]
219pub struct ChatAdministratorRights {
220    /// `true` if the user's presence in the chat is hidden.
221    pub is_anonymous: bool,
222    /// `true` if the administrator can access the chat event log, boost list, etc.
223    pub can_manage_chat: bool,
224    /// `true` if the administrator can delete messages of other users.
225    pub can_delete_messages: bool,
226    /// `true` if the administrator can manage video chats.
227    pub can_manage_video_chats: bool,
228    /// `true` if the administrator can restrict, ban, or unban chat members.
229    pub can_restrict_members: bool,
230    /// `true` if the administrator can promote members to administrators.
231    pub can_promote_members: bool,
232    /// `true` if the user is allowed to change the chat title, photo, and other settings.
233    pub can_change_info: bool,
234    /// `true` if the user is allowed to invite new users to the chat.
235    pub can_invite_users: bool,
236    /// `true` if the administrator can post stories to the chat.
237    pub can_post_stories: bool,
238    /// `true` if the administrator can edit stories posted by other users.
239    pub can_edit_stories: bool,
240    /// `true` if the administrator can delete stories posted by other users.
241    pub can_delete_stories: bool,
242    /// `true` if the administrator can post messages in the channel; channels only.
243    #[serde(skip_serializing_if = "Option::is_none")]
244    pub can_post_messages: Option<bool>,
245    /// `true` if the administrator can edit messages of other users and pin messages; channels only.
246    #[serde(skip_serializing_if = "Option::is_none")]
247    pub can_edit_messages: Option<bool>,
248    /// `true` if the user is allowed to pin messages; groups and supergroups only.
249    #[serde(skip_serializing_if = "Option::is_none")]
250    pub can_pin_messages: Option<bool>,
251    /// `true` if the user is allowed to create, rename, close, and reopen forum topics; supergroups only.
252    #[serde(skip_serializing_if = "Option::is_none")]
253    pub can_manage_topics: Option<bool>,
254    /// `true` if the administrator can manage direct messages and decline suggested posts; channels only.
255    #[serde(skip_serializing_if = "Option::is_none")]
256    pub can_manage_direct_messages: Option<bool>,
257    /// `true` if the administrator can edit the tags of regular members; groups and supergroups only.
258    #[serde(skip_serializing_if = "Option::is_none")]
259    pub can_manage_tags: Option<bool>,
260}
261
262/// Describes the access settings of a bot.
263///
264/// Returned by [`getManagedBotAccessSettings`](https://core.telegram.org/bots/api#getmanagedbotaccesssettings).
265#[derive(Debug, Clone, Serialize, Deserialize)]
266pub struct BotAccessSettings {
267    /// `true` if only selected users can access the bot. The bot's owner can always access it.
268    pub is_access_restricted: bool,
269
270    /// The list of other users who have access to the bot if the access is restricted.
271    #[serde(skip_serializing_if = "Option::is_none")]
272    pub added_users: Option<Vec<User>>,
273}