Skip to main content

rustigram_types/
chat.rs

1use serde::{Deserialize, Serialize};
2
3use crate::message::Message;
4use crate::user::User;
5
6#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
7/// Type of a Telegram chat.
8#[serde(rename_all = "snake_case")]
9pub enum ChatType {
10    /// One-on-one conversation.
11    Private,
12    /// Group chat.
13    Group,
14    /// Supergroup.
15    Supergroup,
16    /// Broadcast channel.
17    Channel,
18}
19
20#[derive(Debug, Clone, Serialize, Deserialize)]
21/// Minimal chat descriptor — used in messages and updates.
22pub struct Chat {
23    /// Unique identifier for this chat.
24    pub id: i64,
25
26    /// Type of chat.
27    #[serde(rename = "type")]
28    pub kind: ChatType,
29
30    /// Title, for supergroups, channels and group chats.
31    #[serde(skip_serializing_if = "Option::is_none")]
32    pub title: Option<String>,
33
34    /// Username, for private chats, supergroups and channels.
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub username: Option<String>,
37
38    /// First name of the other party in a private chat.
39    #[serde(skip_serializing_if = "Option::is_none")]
40    pub first_name: Option<String>,
41
42    /// Last name of the other party in a private chat.
43    #[serde(skip_serializing_if = "Option::is_none")]
44    pub last_name: Option<String>,
45
46    /// `true` if the supergroup chat is a forum (has topics enabled).
47    #[serde(skip_serializing_if = "Option::is_none")]
48    pub is_forum: Option<bool>,
49}
50
51impl Chat {
52    /// Returns the chat's display name.
53    #[must_use]
54    pub fn display_name(&self) -> String {
55        self.title
56            .clone()
57            .or_else(|| {
58                self.first_name.as_ref().map(|f| {
59                    self.last_name
60                        .as_ref()
61                        .map_or(f.clone(), |l| format!("{f} {l}"))
62                })
63            })
64            .or_else(|| self.username.clone())
65            .unwrap_or_else(|| self.id.to_string())
66    }
67}
68
69#[derive(Debug, Clone, Serialize, Deserialize)]
70/// Full chat information — returned by `getChat`.
71pub struct ChatFullInfo {
72    /// Unique identifier for this chat.
73    pub id: i64,
74
75    /// Type of chat.
76    #[serde(rename = "type")]
77    pub kind: ChatType,
78
79    /// Title of the chat (groups, supergroups, and channels).
80    #[serde(skip_serializing_if = "Option::is_none")]
81    pub title: Option<String>,
82
83    /// Username of the chat.
84    #[serde(skip_serializing_if = "Option::is_none")]
85    pub username: Option<String>,
86
87    /// First name of the other party in a private chat.
88    #[serde(skip_serializing_if = "Option::is_none")]
89    pub first_name: Option<String>,
90
91    /// Last name of the other party in a private chat.
92    #[serde(skip_serializing_if = "Option::is_none")]
93    pub last_name: Option<String>,
94
95    /// `true` if the supergroup chat is a forum (has topics enabled).
96    #[serde(skip_serializing_if = "Option::is_none")]
97    pub is_forum: Option<bool>,
98
99    /// Identifier of the accent color for the chat name and backgrounds.
100    #[serde(skip_serializing_if = "Option::is_none")]
101    pub accent_color_id: Option<u32>,
102
103    /// Maximum number of reactions that can be set on a message.
104    #[serde(skip_serializing_if = "Option::is_none")]
105    pub max_reaction_count: Option<u32>,
106
107    /// Chat photo.
108    #[serde(skip_serializing_if = "Option::is_none")]
109    pub photo: Option<ChatPhoto>,
110
111    /// Active usernames of a channel, supergroup, or user.
112    #[serde(skip_serializing_if = "Option::is_none")]
113    pub active_usernames: Option<Vec<String>>,
114
115    /// Bio of the other party in a private chat.
116    #[serde(skip_serializing_if = "Option::is_none")]
117    pub bio: Option<String>,
118
119    /// `true` if privacy settings prevent viewing the other party's bio.
120    #[serde(skip_serializing_if = "Option::is_none")]
121    pub has_private_forwards: Option<bool>,
122
123    /// `true` if the privacy settings prevent sending voice and video note messages.
124    #[serde(skip_serializing_if = "Option::is_none")]
125    pub has_restricted_voice_and_video_messages: Option<bool>,
126
127    /// `true` if users need to join in order to send messages.
128    #[serde(skip_serializing_if = "Option::is_none")]
129    pub join_to_send_messages: Option<bool>,
130
131    /// `true` if all users directly joining the supergroup need to be approved.
132    #[serde(skip_serializing_if = "Option::is_none")]
133    pub join_by_request: Option<bool>,
134
135    /// Description, for groups, supergroups and channel chats.
136    #[serde(skip_serializing_if = "Option::is_none")]
137    pub description: Option<String>,
138
139    /// Primary invite link for the chat.
140    #[serde(skip_serializing_if = "Option::is_none")]
141    pub invite_link: Option<String>,
142
143    /// Latest pinned message.
144    #[serde(skip_serializing_if = "Option::is_none")]
145    pub pinned_message: Option<Box<Message>>,
146
147    /// Default chat member permissions, for groups and supergroups.
148    #[serde(skip_serializing_if = "Option::is_none")]
149    pub permissions: Option<ChatPermissions>,
150
151    /// `true` if paid media messages can be sent or forwarded to the channel chat.
152    #[serde(skip_serializing_if = "Option::is_none")]
153    pub can_send_paid_media: Option<bool>,
154
155    /// Delay in seconds between consecutive messages from a non-administrator.
156    #[serde(skip_serializing_if = "Option::is_none")]
157    pub slow_mode_delay: Option<u32>,
158
159    /// Delay in seconds after which all messages sent by the user will be automatically
160    /// deleted.
161    #[serde(skip_serializing_if = "Option::is_none")]
162    pub unrestrict_boost_count: Option<u32>,
163
164    /// Message auto-delete timer setting for new messages.
165    #[serde(skip_serializing_if = "Option::is_none")]
166    pub message_auto_delete_time: Option<u32>,
167
168    /// `true` if aggressive anti-spam checks are enabled in the supergroup.
169    #[serde(skip_serializing_if = "Option::is_none")]
170    pub has_aggressive_anti_spam_enabled: Option<bool>,
171
172    /// `true` if non-administrators can only get the list of bots and administrators.
173    #[serde(skip_serializing_if = "Option::is_none")]
174    pub has_hidden_members: Option<bool>,
175
176    /// `true` if messages from the chat can't be forwarded to other chats.
177    #[serde(skip_serializing_if = "Option::is_none")]
178    pub has_protected_content: Option<bool>,
179
180    /// `true` if new chat members will have access to old messages.
181    #[serde(skip_serializing_if = "Option::is_none")]
182    pub has_visible_history: Option<bool>,
183
184    /// Name of the group sticker set.
185    #[serde(skip_serializing_if = "Option::is_none")]
186    pub sticker_set_name: Option<String>,
187
188    /// `true` if the bot can change the group sticker set.
189    #[serde(skip_serializing_if = "Option::is_none")]
190    pub can_set_sticker_set: Option<bool>,
191
192    /// Custom emoji identifier of the emoji chosen by the chat for the reply header.
193    #[serde(skip_serializing_if = "Option::is_none")]
194    pub custom_emoji_sticker_set_name: Option<String>,
195
196    /// Unique identifier for the linked chat.
197    #[serde(skip_serializing_if = "Option::is_none")]
198    pub linked_chat_id: Option<i64>,
199
200    /// The location to which the supergroup is connected.
201    #[serde(skip_serializing_if = "Option::is_none")]
202    pub location: Option<ChatLocation>,
203}
204
205#[derive(Debug, Clone, Serialize, Deserialize)]
206/// Chat photo information.
207pub struct ChatPhoto {
208    /// File identifier of small (160x160) chat photo.
209    pub small_file_id: String,
210    /// Unique file identifier of small chat photo.
211    pub small_file_unique_id: String,
212    /// File identifier of big (640x640) chat photo.
213    pub big_file_id: String,
214    /// Unique file identifier of big chat photo.
215    pub big_file_unique_id: String,
216}
217
218#[derive(Debug, Clone, Default, Serialize, Deserialize)]
219/// Defines chat permissions for regular members.
220pub struct ChatPermissions {
221    /// Allows sending text messages.
222    #[serde(skip_serializing_if = "Option::is_none")]
223    pub can_send_messages: Option<bool>,
224    /// Allows sending audio files.
225    #[serde(skip_serializing_if = "Option::is_none")]
226    pub can_send_audios: Option<bool>,
227    /// Allows sending documents.
228    #[serde(skip_serializing_if = "Option::is_none")]
229    pub can_send_documents: Option<bool>,
230    /// Allows sending photos.
231    #[serde(skip_serializing_if = "Option::is_none")]
232    pub can_send_photos: Option<bool>,
233    /// Allows sending videos.
234    #[serde(skip_serializing_if = "Option::is_none")]
235    pub can_send_videos: Option<bool>,
236    /// Allows sending video notes.
237    #[serde(skip_serializing_if = "Option::is_none")]
238    pub can_send_video_notes: Option<bool>,
239    /// Allows sending voice notes.
240    #[serde(skip_serializing_if = "Option::is_none")]
241    pub can_send_voice_notes: Option<bool>,
242    /// Allows sending polls.
243    #[serde(skip_serializing_if = "Option::is_none")]
244    pub can_send_polls: Option<bool>,
245    /// Allows sending other types of messages (stickers, GIFs, games, etc.).
246    #[serde(skip_serializing_if = "Option::is_none")]
247    pub can_send_other_messages: Option<bool>,
248    /// Allows adding web page previews to messages.
249    #[serde(skip_serializing_if = "Option::is_none")]
250    pub can_add_web_page_previews: Option<bool>,
251    /// Allows changing the chat title, photo, and other settings.
252    #[serde(skip_serializing_if = "Option::is_none")]
253    pub can_change_info: Option<bool>,
254    /// Allows inviting new users to the chat.
255    #[serde(skip_serializing_if = "Option::is_none")]
256    pub can_invite_users: Option<bool>,
257    /// Allows pinning messages.
258    #[serde(skip_serializing_if = "Option::is_none")]
259    pub can_pin_messages: Option<bool>,
260    /// Allows managing forum topics (supergroups only).
261    #[serde(skip_serializing_if = "Option::is_none")]
262    pub can_manage_topics: Option<bool>,
263    /// Allows editing the chat tag (supergroups only).
264    #[serde(skip_serializing_if = "Option::is_none")]
265    pub can_edit_tag: Option<bool>,
266}
267
268/// Location to which the supergroup is connected.
269#[derive(Debug, Clone, Serialize, Deserialize)]
270pub struct ChatLocation {
271    /// The location to which the supergroup is connected.
272    pub location: Location,
273    /// Location address; 1-64 characters.
274    pub address: String,
275}
276
277/// Geographic point on the map.
278#[derive(Debug, Clone, Serialize, Deserialize)]
279pub struct Location {
280    /// Latitude as defined by the sender.
281    pub latitude: f64,
282    /// Longitude as defined by the sender.
283    pub longitude: f64,
284    /// Radius of uncertainty for the location, in metres (0–1500).
285    #[serde(skip_serializing_if = "Option::is_none")]
286    pub horizontal_accuracy: Option<f64>,
287    /// Time relative to the message sending date, during which the location can
288    /// be updated; in seconds.
289    #[serde(skip_serializing_if = "Option::is_none")]
290    pub live_period: Option<u32>,
291    /// Direction of movement in degrees (1–360).
292    #[serde(skip_serializing_if = "Option::is_none")]
293    pub heading: Option<u16>,
294    /// Maximum distance in metres for proximity alerts.
295    #[serde(skip_serializing_if = "Option::is_none")]
296    pub proximity_alert_radius: Option<u32>,
297}
298
299/// Represents a venue.
300#[derive(Debug, Clone, Serialize, Deserialize)]
301pub struct Venue {
302    /// Venue location.
303    pub location: Location,
304    /// Name of the venue.
305    pub title: String,
306    /// Address of the venue.
307    pub address: String,
308    /// Foursquare identifier of the venue.
309    #[serde(skip_serializing_if = "Option::is_none")]
310    pub foursquare_id: Option<String>,
311    /// Foursquare type of the venue.
312    #[serde(skip_serializing_if = "Option::is_none")]
313    pub foursquare_type: Option<String>,
314    /// Google Places identifier of the venue.
315    #[serde(skip_serializing_if = "Option::is_none")]
316    pub google_place_id: Option<String>,
317    /// Google Places type of the venue.
318    #[serde(skip_serializing_if = "Option::is_none")]
319    pub google_place_type: Option<String>,
320}
321
322#[derive(Debug, Clone, Serialize, Deserialize)]
323/// Invite link for a chat.
324pub struct ChatInviteLink {
325    /// The invite link.
326    pub invite_link: String,
327    /// Creator of the link.
328    pub creator: User,
329    /// `true` if users joining the chat via the link need to be approved by chat admins.
330    pub creates_join_request: bool,
331    /// `true` if the link is primary.
332    pub is_primary: bool,
333    /// `true` if the link is revoked.
334    pub is_revoked: bool,
335    /// Invite link name.
336    #[serde(skip_serializing_if = "Option::is_none")]
337    pub name: Option<String>,
338    /// Point in time (Unix) when the link will expire or has been expired.
339    #[serde(skip_serializing_if = "Option::is_none")]
340    pub expire_date: Option<i64>,
341    /// Maximum number of users that can be members of the chat simultaneously.
342    #[serde(skip_serializing_if = "Option::is_none")]
343    pub member_limit: Option<u32>,
344    /// Number of pending join requests created using this link.
345    #[serde(skip_serializing_if = "Option::is_none")]
346    pub pending_join_request_count: Option<u32>,
347    /// Number of seconds the subscription created by this link will be active.
348    #[serde(skip_serializing_if = "Option::is_none")]
349    pub subscription_period: Option<u32>,
350    /// Number of Telegram Stars a user must pay for a subscription.
351    #[serde(skip_serializing_if = "Option::is_none")]
352    pub subscription_price: Option<u32>,
353}
354
355#[derive(Debug, Clone, Serialize, Deserialize)]
356/// Represents a join request sent to a chat.
357pub struct ChatJoinRequest {
358    /// The chat the join request was sent to.
359    pub chat: Chat,
360    /// The user that sent the join request.
361    pub from: User,
362    /// Identifier of a private chat with the user.
363    pub user_chat_id: i64,
364    /// Date the request was sent as Unix time.
365    pub date: i64,
366    /// Bio of the user, if available.
367    #[serde(skip_serializing_if = "Option::is_none")]
368    pub bio: Option<String>,
369    /// The invite link used to send the request, if any.
370    #[serde(skip_serializing_if = "Option::is_none")]
371    pub invite_link: Option<ChatInviteLink>,
372}