tgbot/types/definitions/chat/
mod.rs1use 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#[derive(Clone, Debug, derive_more::From, Deserialize, PartialEq, Serialize)]
39#[serde(tag = "type")]
40#[serde(rename_all = "snake_case")]
41pub enum Chat {
42 Channel(ChannelChat),
44 Group(GroupChat),
46 Private(PrivateChat),
48 Supergroup(SupergroupChat),
50}
51
52impl Chat {
53 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 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#[serde_with::skip_serializing_none]
76#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
77pub struct ChannelChat {
78 pub id: ChatPeerId,
80 pub title: String,
82 pub username: Option<ChatUsername>,
84}
85
86impl ChannelChat {
87 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 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#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
121pub struct GroupChat {
122 pub id: ChatPeerId,
124 pub title: String,
126}
127
128impl GroupChat {
129 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#[serde_with::skip_serializing_none]
149#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
150pub struct PrivateChat {
151 pub id: ChatPeerId,
153 pub first_name: String,
155 pub last_name: Option<String>,
157 pub username: Option<ChatUsername>,
159}
160
161impl PrivateChat {
162 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 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 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#[serde_with::skip_serializing_none]
210#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
211pub struct SupergroupChat {
212 pub id: ChatPeerId,
214 pub title: String,
216 pub is_direct_messages: Option<bool>,
218 pub is_forum: Option<bool>,
220 pub username: Option<ChatUsername>,
222}
223
224impl SupergroupChat {
225 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 pub fn with_is_direct_messages(mut self, value: bool) -> Self {
251 self.is_direct_messages = Some(value);
252 self
253 }
254
255 pub fn with_is_forum(mut self, value: bool) -> Self {
261 self.is_forum = Some(value);
262 self
263 }
264
265 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#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
281pub struct ChatBackground {
282 #[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#[derive(Clone, Debug, Serialize)]
295pub struct GetChat {
296 chat_id: ChatId,
297}
298
299impl GetChat {
300 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#[derive(Clone, Debug, Serialize)]
325pub struct LeaveChat {
326 chat_id: ChatId,
327}
328
329impl LeaveChat {
330 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#[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 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 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#[derive(Clone, Debug, Serialize)]
413pub struct SetChatTitle {
414 chat_id: ChatId,
415 title: String,
416}
417
418impl SetChatTitle {
419 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}