telegram_bot_raw/types/
chat.rs

1use serde::de::{Deserialize, Deserializer, Error};
2
3use crate::types::*;
4
5/// This object represents a Telegram user or bot.
6#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize)]
7pub struct User {
8    /// Unique identifier for this user or bot.
9    pub id: UserId,
10    /// User‘s or bot’s first name.
11    pub first_name: String,
12    /// User‘s or bot’s last name.
13    pub last_name: Option<String>,
14    /// User‘s or bot’s username.
15    pub username: Option<String>,
16    /// True, if this user is a bot.
17    pub is_bot: bool,
18    /// IETF language tag of the user's language
19    pub language_code: Option<String>,
20}
21
22/// This object represents a group.
23#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize)]
24pub struct Group {
25    /// Unique identifier for this chat.
26    pub id: GroupId,
27    /// Title, for supergroups, channels and group chats.
28    pub title: String,
29    /// True if a group has ‘All Members Are Admins’ enabled.
30    pub all_members_are_administrators: bool,
31    /// Invite link for this group, specific to this bot.
32    /// You can generate a new invite link by using the
33    /// export_invite_link method.
34    pub invite_link: Option<String>,
35}
36
37/// This object represents a supergroup.
38#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize)]
39pub struct Supergroup {
40    /// Unique identifier for this chat.
41    pub id: SupergroupId,
42    /// Title, for supergroups, channels and group chats.
43    pub title: String,
44    /// Username for supergroup.
45    pub username: Option<String>,
46    /// Invite link for this supergroup, specific to this bot.
47    /// You can generate a new invite link by using the
48    /// export_invite_link method.
49    pub invite_link: Option<String>,
50}
51
52/// This object represents a channel.
53#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize)]
54pub struct Channel {
55    /// Unique identifier for this chat.
56    pub id: ChannelId,
57    /// Title, for supergroups, channels and group chats.
58    pub title: String,
59    /// Username for channel.
60    pub username: Option<String>,
61    /// Invite link for this channel, specific to this bot.
62    /// You can generate a new invite link by using the
63    /// export_invite_link method.
64    pub invite_link: Option<String>,
65}
66
67/// This object represents a private, group or supergroup.
68#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
69pub enum MessageChat {
70    Private(User),
71    Group(Group),
72    Supergroup(Supergroup),
73    #[doc(hidden)]
74    Unknown(RawChat),
75}
76
77impl MessageChat {
78    pub fn id(&self) -> ChatId {
79        match *self {
80            MessageChat::Private(ref x) => x.id.into(),
81            MessageChat::Group(ref x) => x.id.into(),
82            MessageChat::Supergroup(ref x) => x.id.into(),
83            MessageChat::Unknown(ref x) => x.id.into(),
84        }
85    }
86}
87
88/// This object represents a chat.
89#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
90pub enum Chat {
91    Private(User),
92    Group(Group),
93    Supergroup(Supergroup),
94    Channel(Channel),
95    #[doc(hidden)]
96    Unknown(RawChat),
97}
98
99impl Chat {
100    pub fn id(&self) -> ChatId {
101        match *self {
102            Chat::Private(ref x) => x.id.into(),
103            Chat::Group(ref x) => x.id.into(),
104            Chat::Supergroup(ref x) => x.id.into(),
105            Chat::Channel(ref x) => x.id.into(),
106            Chat::Unknown(ref x) => x.id.into(),
107        }
108    }
109}
110
111impl<'de> Deserialize<'de> for Chat {
112    fn deserialize<D>(deserializer: D) -> Result<Chat, D::Error>
113    where
114        D: Deserializer<'de>,
115    {
116        let raw: RawChat = Deserialize::deserialize(deserializer)?;
117
118        macro_rules! required_field {
119            ($name:ident) => {{
120                match raw.$name {
121                    Some(val) => val,
122                    None => return Err(D::Error::missing_field(stringify!($name))),
123                }
124            }};
125        }
126
127        Ok(match raw.type_.as_ref() {
128            "private" => Chat::Private(User {
129                id: raw.id.into(),
130                username: raw.username,
131                first_name: required_field!(first_name),
132                last_name: raw.last_name,
133                is_bot: false,
134                language_code: raw.language_code,
135            }),
136            "group" => Chat::Group(Group {
137                id: raw.id.into(),
138                title: required_field!(title),
139                all_members_are_administrators: required_field!(all_members_are_administrators),
140                invite_link: raw.invite_link,
141            }),
142            "supergroup" => Chat::Supergroup(Supergroup {
143                id: raw.id.into(),
144                title: required_field!(title),
145                username: raw.username,
146                invite_link: raw.invite_link,
147            }),
148            "channel" => Chat::Channel(Channel {
149                id: raw.id.into(),
150                title: required_field!(title),
151                username: raw.username,
152                invite_link: raw.invite_link,
153            }),
154            _ => Chat::Unknown(raw),
155        })
156    }
157}
158
159/// This object represents a chat, directly mapped.
160#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize)]
161pub struct RawChat {
162    /// Unique identifier for this chat.
163    pub id: Integer,
164    /// Type of chat, can be either “private”, “group”, “supergroup” or “channel”
165    #[serde(rename = "type")]
166    pub type_: String,
167    /// Title, for supergroups, channels and group chats
168    pub title: Option<String>,
169    /// Username, for private chats, supergroups and channels if available
170    pub username: Option<String>,
171    /// First name of the other party in a private chat
172    pub first_name: Option<String>,
173    /// Last name of the other party in a private chat
174    pub last_name: Option<String>,
175    /// Invite link for this chat, specific to this bot.
176    /// Does not apply to private chats.
177    pub invite_link: Option<String>,
178    /// IETF language tag of the other party in a private chat
179    pub language_code: Option<String>,
180    /// True if a group has ‘All Members Are Admins’ enabled.
181    pub all_members_are_administrators: Option<bool>,
182}