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    /// `true` if the command sends an ephemeral message, visible only to the
154    /// sender of the message and the bot.
155    #[serde(skip_serializing_if = "Option::is_none")]
156    pub is_ephemeral: Option<bool>,
157}
158
159/// Flexible chat identifier — either a numeric ID or a `@username`.
160#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
161#[serde(untagged)]
162pub enum ChatId {
163    /// Numeric chat ID (can be negative for groups/channels).
164    Id(i64),
165    /// Chat `@username`.
166    Username(String),
167}
168
169impl From<i64> for ChatId {
170    fn from(id: i64) -> Self {
171        Self::Id(id)
172    }
173}
174
175impl From<&str> for ChatId {
176    fn from(username: &str) -> Self {
177        Self::Username(username.to_owned())
178    }
179}
180
181impl From<String> for ChatId {
182    fn from(username: String) -> Self {
183        Self::Username(username)
184    }
185}
186
187impl std::fmt::Display for ChatId {
188    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
189        match self {
190            Self::Id(id) => write!(f, "{id}"),
191            Self::Username(u) => write!(f, "{u}"),
192        }
193    }
194}
195
196/// The bot's display name.
197///
198/// Returned by [`getMyName`](https://core.telegram.org/bots/api#getmyname).
199#[derive(Debug, Clone, Serialize, Deserialize)]
200pub struct BotName {
201    /// The bot's name.
202    pub name: String,
203}
204
205/// The bot's description shown in the chat when the chat is empty.
206///
207/// Returned by [`getMyDescription`](https://core.telegram.org/bots/api#getmydescription).
208#[derive(Debug, Clone, Serialize, Deserialize)]
209pub struct BotDescription {
210    /// The bot's description.
211    pub description: String,
212}
213
214/// The bot's short description shown on the profile page and in sharing links.
215///
216/// Returned by [`getMyShortDescription`](https://core.telegram.org/bots/api#getmyshortdescription).
217#[derive(Debug, Clone, Serialize, Deserialize)]
218pub struct BotShortDescription {
219    /// The bot's short description.
220    pub short_description: String,
221}
222
223/// Represents the rights of an administrator in a chat.
224///
225/// Used by [`setMyDefaultAdministratorRights`](https://core.telegram.org/bots/api#setmydefaultadministratorrights)
226/// and [`getMyDefaultAdministratorRights`](https://core.telegram.org/bots/api#getmydefaultadministratorrights).
227#[derive(Debug, Clone, Default, Serialize, Deserialize)]
228pub struct ChatAdministratorRights {
229    /// `true` if the user's presence in the chat is hidden.
230    pub is_anonymous: bool,
231    /// `true` if the administrator can access the chat event log, boost list, etc.
232    pub can_manage_chat: bool,
233    /// `true` if the administrator can delete messages of other users.
234    pub can_delete_messages: bool,
235    /// `true` if the administrator can manage video chats.
236    pub can_manage_video_chats: bool,
237    /// `true` if the administrator can restrict, ban, or unban chat members.
238    pub can_restrict_members: bool,
239    /// `true` if the administrator can promote members to administrators.
240    pub can_promote_members: bool,
241    /// `true` if the user is allowed to change the chat title, photo, and other settings.
242    pub can_change_info: bool,
243    /// `true` if the user is allowed to invite new users to the chat.
244    pub can_invite_users: bool,
245    /// `true` if the administrator can post stories to the chat.
246    pub can_post_stories: bool,
247    /// `true` if the administrator can edit stories posted by other users.
248    pub can_edit_stories: bool,
249    /// `true` if the administrator can delete stories posted by other users.
250    pub can_delete_stories: bool,
251    /// `true` if the administrator can post messages in the channel; channels only.
252    #[serde(skip_serializing_if = "Option::is_none")]
253    pub can_post_messages: Option<bool>,
254    /// `true` if the administrator can edit messages of other users and pin messages; channels only.
255    #[serde(skip_serializing_if = "Option::is_none")]
256    pub can_edit_messages: Option<bool>,
257    /// `true` if the user is allowed to pin messages; groups and supergroups only.
258    #[serde(skip_serializing_if = "Option::is_none")]
259    pub can_pin_messages: Option<bool>,
260    /// `true` if the user is allowed to create, rename, close, and reopen forum topics; supergroups only.
261    #[serde(skip_serializing_if = "Option::is_none")]
262    pub can_manage_topics: Option<bool>,
263    /// `true` if the administrator can manage direct messages and decline suggested posts; channels only.
264    #[serde(skip_serializing_if = "Option::is_none")]
265    pub can_manage_direct_messages: Option<bool>,
266    /// `true` if the administrator can edit the tags of regular members; groups and supergroups only.
267    #[serde(skip_serializing_if = "Option::is_none")]
268    pub can_manage_tags: Option<bool>,
269}
270
271/// Describes the access settings of a bot.
272///
273/// Returned by [`getManagedBotAccessSettings`](https://core.telegram.org/bots/api#getmanagedbotaccesssettings).
274#[derive(Debug, Clone, Serialize, Deserialize)]
275pub struct BotAccessSettings {
276    /// `true` if only selected users can access the bot. The bot's owner can always access it.
277    pub is_access_restricted: bool,
278
279    /// The list of other users who have access to the bot if the access is restricted.
280    #[serde(skip_serializing_if = "Option::is_none")]
281    pub added_users: Option<Vec<User>>,
282}