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