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