tgbot/types/definitions/chat/
mod.rs

1use serde::{Deserialize, Serialize};
2
3pub use self::{
4    action::*,
5    boost::*,
6    full_info::*,
7    id::*,
8    invite_link::*,
9    join_request::*,
10    location::*,
11    member::*,
12    message::*,
13    permissions::*,
14    photo::*,
15    sender_chat::*,
16    sticker_set::*,
17};
18use crate::{
19    api::{Method, Payload},
20    types::BackgroundType,
21};
22
23mod action;
24mod boost;
25mod full_info;
26mod id;
27mod invite_link;
28mod join_request;
29mod location;
30mod member;
31mod message;
32mod permissions;
33mod photo;
34mod sender_chat;
35mod sticker_set;
36
37/// Represents a chat.
38#[derive(Clone, Debug, derive_more::From, Deserialize, PartialEq, Serialize)]
39#[serde(tag = "type")]
40#[serde(rename_all = "snake_case")]
41pub enum Chat {
42    /// Represents a channel chat.
43    Channel(ChannelChat),
44    /// Represents a group chat.
45    Group(GroupChat),
46    /// Represents a private chat.
47    Private(PrivateChat),
48    /// Represents a supergroup chat.
49    Supergroup(SupergroupChat),
50}
51
52impl Chat {
53    /// Returns an ID of the chat.
54    pub fn get_id(&self) -> ChatPeerId {
55        match self {
56            Chat::Channel(chat) => chat.id,
57            Chat::Group(chat) => chat.id,
58            Chat::Private(chat) => chat.id,
59            Chat::Supergroup(chat) => chat.id,
60        }
61    }
62
63    /// Returns a username of the chat.
64    pub fn get_username(&self) -> Option<&ChatUsername> {
65        match &self {
66            Chat::Channel(chat) => chat.username.as_ref(),
67            Chat::Group(_) => None,
68            Chat::Private(chat) => chat.username.as_ref(),
69            Chat::Supergroup(chat) => chat.username.as_ref(),
70        }
71    }
72}
73
74/// Represents a channel chat.
75#[serde_with::skip_serializing_none]
76#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
77pub struct ChannelChat {
78    /// Unique identifier of the channel.
79    pub id: ChatPeerId,
80    /// Title of the channel.
81    pub title: String,
82    /// Username of the channel.
83    pub username: Option<ChatUsername>,
84}
85
86impl ChannelChat {
87    /// Creates a new `ChannelChat`.
88    ///
89    /// # Arguments
90    ///
91    /// * `id` - Unique identifier of the channel.
92    /// * `title` - Title of the channel.
93    pub fn new<A, B>(id: A, title: B) -> Self
94    where
95        A: Into<ChatPeerId>,
96        B: Into<String>,
97    {
98        Self {
99            id: id.into(),
100            title: title.into(),
101            username: None,
102        }
103    }
104
105    /// Sets a new username.
106    ///
107    /// # Arguments
108    ///
109    /// * `value` - Username of the channel.
110    pub fn with_username<T>(mut self, value: T) -> Self
111    where
112        T: Into<ChatUsername>,
113    {
114        self.username = Some(value.into());
115        self
116    }
117}
118
119/// Represents a group chat.
120#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
121pub struct GroupChat {
122    /// Unique identifier of the group.
123    pub id: ChatPeerId,
124    /// Title of the group.
125    pub title: String,
126}
127
128impl GroupChat {
129    /// Creates a new `GroupChat`.
130    ///
131    /// # Arguments
132    ///
133    /// * `id` - Unique identifier of the group.
134    /// * `title` - Title of the group.
135    pub fn new<A, B>(id: A, title: B) -> Self
136    where
137        A: Into<ChatPeerId>,
138        B: Into<String>,
139    {
140        Self {
141            id: id.into(),
142            title: title.into(),
143        }
144    }
145}
146
147/// Represents a private chat.
148#[serde_with::skip_serializing_none]
149#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
150pub struct PrivateChat {
151    /// Unique identifier of the chat.
152    pub id: ChatPeerId,
153    /// First name of the other party.
154    pub first_name: String,
155    /// Last name of the other party.
156    pub last_name: Option<String>,
157    /// Username of the target chat.
158    pub username: Option<ChatUsername>,
159}
160
161impl PrivateChat {
162    /// Creates a new `PrivateChat`.
163    ///
164    /// # Arguments
165    ///
166    /// * `id` - Unique identifier of the target chat.
167    /// * `first_name` - First name of the other party.
168    pub fn new<A, B>(id: A, first_name: B) -> Self
169    where
170        A: Into<ChatPeerId>,
171        B: Into<String>,
172    {
173        Self {
174            id: id.into(),
175            first_name: first_name.into(),
176            last_name: None,
177            username: None,
178        }
179    }
180
181    /// Sets a new last name.
182    ///
183    /// # Arguments
184    ///
185    /// * `value` - Last name.
186    pub fn with_last_name<T>(mut self, value: T) -> Self
187    where
188        T: Into<String>,
189    {
190        self.last_name = Some(value.into());
191        self
192    }
193
194    /// Sets a new username.
195    ///
196    /// # Arguments
197    ///
198    /// * `value` - Username.
199    pub fn with_username<T>(mut self, value: T) -> Self
200    where
201        T: Into<ChatUsername>,
202    {
203        self.username = Some(value.into());
204        self
205    }
206}
207
208/// Represents a supergroup chat.
209#[serde_with::skip_serializing_none]
210#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
211pub struct SupergroupChat {
212    /// Unique identifier of the supergroup.
213    pub id: ChatPeerId,
214    /// Title of the supergroup.
215    pub title: String,
216    /// Whether the chat is the direct messages chat of a channel.
217    pub is_direct_messages: Option<bool>,
218    /// Whether the supergroup has topic enabled.
219    pub is_forum: Option<bool>,
220    /// Username of the supergroup.
221    pub username: Option<ChatUsername>,
222}
223
224impl SupergroupChat {
225    /// Creates a new `SupergroupChat`.
226    ///
227    /// # Arguments
228    ///
229    /// * `id` - Unique identifier of the supergroup.
230    /// * `title` - Title of the supergroup.
231    pub fn new<A, B>(id: A, title: B) -> Self
232    where
233        A: Into<ChatPeerId>,
234        B: Into<String>,
235    {
236        Self {
237            id: id.into(),
238            title: title.into(),
239            is_direct_messages: None,
240            is_forum: None,
241            username: None,
242        }
243    }
244
245    /// Sets a new value for the `is_direct_messages` flag.
246    ///
247    /// # Arguments
248    ///
249    /// `value` - Whether the chat is the direct messages chat of a channel.
250    pub fn with_is_direct_messages(mut self, value: bool) -> Self {
251        self.is_direct_messages = Some(value);
252        self
253    }
254
255    /// Sets a new value for the `is_forum` flag.
256    ///
257    /// # Arguments
258    ///
259    /// `value` - Whether the supergroup has topics enabled.
260    pub fn with_is_forum(mut self, value: bool) -> Self {
261        self.is_forum = Some(value);
262        self
263    }
264
265    /// Sets a new username.
266    ///
267    /// # Arguments
268    ///
269    /// * `value` - Username of the supergroup.
270    pub fn with_username<T>(mut self, value: T) -> Self
271    where
272        T: Into<ChatUsername>,
273    {
274        self.username = Some(value.into());
275        self
276    }
277}
278
279/// Represents a chat background.
280#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
281pub struct ChatBackground {
282    /// Type of the background
283    #[serde(rename = "type")]
284    pub background_type: BackgroundType,
285}
286
287impl From<BackgroundType> for ChatBackground {
288    fn from(value: BackgroundType) -> Self {
289        Self { background_type: value }
290    }
291}
292
293/// Returns up to date information about the chat.
294#[derive(Clone, Debug, Serialize)]
295pub struct GetChat {
296    chat_id: ChatId,
297}
298
299impl GetChat {
300    /// Creates a new `GetChat`.
301    ///
302    /// # Arguments
303    ///
304    /// * `chat_id` - Unique identifier of the target chat.
305    pub fn new<T>(chat_id: T) -> Self
306    where
307        T: Into<ChatId>,
308    {
309        GetChat {
310            chat_id: chat_id.into(),
311        }
312    }
313}
314
315impl Method for GetChat {
316    type Response = ChatFullInfo;
317
318    fn into_payload(self) -> Payload {
319        Payload::json("getChat", self)
320    }
321}
322
323/// Leaves a group, supergroup or channel.
324#[derive(Clone, Debug, Serialize)]
325pub struct LeaveChat {
326    chat_id: ChatId,
327}
328
329impl LeaveChat {
330    /// Creates a new `LeaveChat`.
331    ///
332    /// # Arguments
333    ///
334    /// * `chat_id` - Unique identifier of the target chat.
335    pub fn new<T>(chat_id: T) -> Self
336    where
337        T: Into<ChatId>,
338    {
339        LeaveChat {
340            chat_id: chat_id.into(),
341        }
342    }
343}
344
345impl Method for LeaveChat {
346    type Response = bool;
347
348    fn into_payload(self) -> Payload {
349        Payload::json("leaveChat", self)
350    }
351}
352
353/// Changes a description of a chat.
354///
355/// The bot must be an administrator in the chat for this to work
356/// and must have the appropriate admin rights.
357#[serde_with::skip_serializing_none]
358#[derive(Clone, Debug, Serialize)]
359pub struct SetChatDescription {
360    chat_id: ChatId,
361    description: Option<String>,
362}
363
364impl SetChatDescription {
365    /// Creates a new `SetChatDescription`.
366    ///
367    /// # Arguments
368    ///
369    /// * `chat_id` - Unique identifier of the target chat.
370    pub fn new<T>(chat_id: T) -> Self
371    where
372        T: Into<ChatId>,
373    {
374        SetChatDescription {
375            chat_id: chat_id.into(),
376            description: None,
377        }
378    }
379
380    /// Sets a new chat description.
381    ///
382    /// # Arguments
383    ///
384    /// * `value` - Description; 0-255 characters.
385    pub fn with_description<T>(mut self, value: T) -> Self
386    where
387        T: Into<String>,
388    {
389        self.description = Some(value.into());
390        self
391    }
392}
393
394impl Method for SetChatDescription {
395    type Response = bool;
396
397    fn into_payload(self) -> Payload {
398        Payload::json("setChatDescription", self)
399    }
400}
401
402/// Changes a title of a chat.
403///
404/// Titles can't be changed for private chats.
405/// The bot must be an administrator in the chat for this to work
406/// and must have the appropriate admin rights.
407///
408/// # Notes
409///
410/// In regular groups (non-supergroups), this method will only work
411/// if the ‘All Members Are Admins’ setting is off in the target group.
412#[derive(Clone, Debug, Serialize)]
413pub struct SetChatTitle {
414    chat_id: ChatId,
415    title: String,
416}
417
418impl SetChatTitle {
419    /// Creates a new `SetChatTitle`.
420    ///
421    /// # Arguments
422    ///
423    /// * `chat_id` - Unique identifier of the target chat.
424    /// * `title` - New chat title; 1-255 characters.
425    pub fn new<A, B>(chat_id: A, title: B) -> Self
426    where
427        A: Into<ChatId>,
428        B: Into<String>,
429    {
430        SetChatTitle {
431            chat_id: chat_id.into(),
432            title: title.into(),
433        }
434    }
435}
436
437impl Method for SetChatTitle {
438    type Response = bool;
439
440    fn into_payload(self) -> Payload {
441        Payload::json("setChatTitle", self)
442    }
443}