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    /// Allows reacting to messages.
272    ///
273    /// If omitted, defaults to the value of `can_send_messages`.
274    #[serde(skip_serializing_if = "Option::is_none")]
275    pub can_react_to_messages: Option<bool>,
276}
277
278/// Location to which the supergroup is connected.
279#[derive(Debug, Clone, Serialize, Deserialize)]
280pub struct ChatLocation {
281    /// The location to which the supergroup is connected.
282    pub location: Location,
283    /// Location address; 1-64 characters.
284    pub address: String,
285}
286
287/// Geographic point on the map.
288#[derive(Debug, Clone, Serialize, Deserialize)]
289pub struct Location {
290    /// Latitude as defined by the sender.
291    pub latitude: f64,
292    /// Longitude as defined by the sender.
293    pub longitude: f64,
294    /// Radius of uncertainty for the location, in metres (0–1500).
295    #[serde(skip_serializing_if = "Option::is_none")]
296    pub horizontal_accuracy: Option<f64>,
297    /// Time relative to the message sending date, during which the location can
298    /// be updated; in seconds.
299    #[serde(skip_serializing_if = "Option::is_none")]
300    pub live_period: Option<u32>,
301    /// Direction of movement in degrees (1–360).
302    #[serde(skip_serializing_if = "Option::is_none")]
303    pub heading: Option<u16>,
304    /// Maximum distance in metres for proximity alerts.
305    #[serde(skip_serializing_if = "Option::is_none")]
306    pub proximity_alert_radius: Option<u32>,
307}
308
309/// Represents a venue.
310#[derive(Debug, Clone, Serialize, Deserialize)]
311pub struct Venue {
312    /// Venue location.
313    pub location: Location,
314    /// Name of the venue.
315    pub title: String,
316    /// Address of the venue.
317    pub address: String,
318    /// Foursquare identifier of the venue.
319    #[serde(skip_serializing_if = "Option::is_none")]
320    pub foursquare_id: Option<String>,
321    /// Foursquare type of the venue.
322    #[serde(skip_serializing_if = "Option::is_none")]
323    pub foursquare_type: Option<String>,
324    /// Google Places identifier of the venue.
325    #[serde(skip_serializing_if = "Option::is_none")]
326    pub google_place_id: Option<String>,
327    /// Google Places type of the venue.
328    #[serde(skip_serializing_if = "Option::is_none")]
329    pub google_place_type: Option<String>,
330}
331
332#[derive(Debug, Clone, Serialize, Deserialize)]
333/// Invite link for a chat.
334pub struct ChatInviteLink {
335    /// The invite link.
336    pub invite_link: String,
337    /// Creator of the link.
338    pub creator: User,
339    /// `true` if users joining the chat via the link need to be approved by chat admins.
340    pub creates_join_request: bool,
341    /// `true` if the link is primary.
342    pub is_primary: bool,
343    /// `true` if the link is revoked.
344    pub is_revoked: bool,
345    /// Invite link name.
346    #[serde(skip_serializing_if = "Option::is_none")]
347    pub name: Option<String>,
348    /// Point in time (Unix) when the link will expire or has been expired.
349    #[serde(skip_serializing_if = "Option::is_none")]
350    pub expire_date: Option<i64>,
351    /// Maximum number of users that can be members of the chat simultaneously.
352    #[serde(skip_serializing_if = "Option::is_none")]
353    pub member_limit: Option<u32>,
354    /// Number of pending join requests created using this link.
355    #[serde(skip_serializing_if = "Option::is_none")]
356    pub pending_join_request_count: Option<u32>,
357    /// Number of seconds the subscription created by this link will be active.
358    #[serde(skip_serializing_if = "Option::is_none")]
359    pub subscription_period: Option<u32>,
360    /// Number of Telegram Stars a user must pay for a subscription.
361    #[serde(skip_serializing_if = "Option::is_none")]
362    pub subscription_price: Option<u32>,
363}
364
365#[derive(Debug, Clone, Serialize, Deserialize)]
366/// Represents a join request sent to a chat.
367pub struct ChatJoinRequest {
368    /// The chat the join request was sent to.
369    pub chat: Chat,
370    /// The user that sent the join request.
371    pub from: User,
372    /// Identifier of a private chat with the user.
373    pub user_chat_id: i64,
374    /// Date the request was sent as Unix time.
375    pub date: i64,
376    /// Bio of the user, if available.
377    #[serde(skip_serializing_if = "Option::is_none")]
378    pub bio: Option<String>,
379    /// The invite link used to send the request, if any.
380    #[serde(skip_serializing_if = "Option::is_none")]
381    pub invite_link: Option<ChatInviteLink>,
382}