Skip to main content

tgbot/types/definitions/chat/
join_request.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{
4    api::{Method, Payload},
5    types::{Chat, ChatId, ChatInviteLink, Integer, User},
6};
7
8/// Represents a join request sent to a chat.
9#[serde_with::skip_serializing_none]
10#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
11pub struct ChatJoinRequest {
12    /// Chat to which the request was sent.
13    pub chat: Chat,
14    /// Date the request was sent in Unix time.
15    pub date: Integer,
16    /// User that sent the join request.
17    pub from: User,
18    /// Bio of the user.
19    pub bio: Option<String>,
20    /// Chat invite link that was used by the user to send the join request.
21    pub invite_link: Option<ChatInviteLink>,
22    /// Identifier of the join request query.
23    ///
24    /// If present, then the bot must call [`crate::types::SendChatJoinRequestWebApp`]
25    /// or directly call [`crate::types::AnswerChatJoinRequestQuery`] within 10 seconds.
26    pub query_id: Option<String>,
27    /// Identifier of a private chat with the user who sent the join request.
28    ///
29    /// A bot can use this identifier for 5 minutes to
30    /// send messages until the join request is processed,
31    /// assuming no other administrator contacted the user.
32    pub user_chat_id: Option<Integer>,
33}
34
35impl ChatJoinRequest {
36    /// Creates a new `ChatJoinRequest`.
37    ///
38    /// # Arguments
39    ///
40    /// * `chat` - Chat to which the request was sent.
41    /// * `date` - Date the request was sent in Unix time.
42    /// * `from` - User that sent the join request.
43    pub fn new(chat: Chat, date: Integer, from: User) -> Self {
44        Self {
45            chat,
46            date,
47            from,
48            bio: None,
49            invite_link: None,
50            query_id: None,
51            user_chat_id: None,
52        }
53    }
54
55    /// Sets a new bio.
56    ///
57    /// # Arguments
58    ///
59    /// * `value` - The bio of the user.
60    pub fn with_bio<T>(mut self, value: T) -> Self
61    where
62        T: Into<String>,
63    {
64        self.bio = Some(value.into());
65        self
66    }
67
68    /// Sets a new invite link.
69    ///
70    /// # Arguments
71    ///
72    /// * `value` - The chat invite link that was used by the user to send the join request.
73    pub fn with_invite_link(mut self, value: ChatInviteLink) -> Self {
74        self.invite_link = Some(value);
75        self
76    }
77
78    /// Sets a new query ID.
79    ///
80    /// # Arguments
81    ///
82    /// * `value` - Identifier of the join request query.
83    pub fn with_query_id<T>(mut self, value: T) -> Self
84    where
85        T: Into<String>,
86    {
87        self.query_id = Some(value.into());
88        self
89    }
90
91    /// Sets a new user chat ID.
92    ///
93    /// # Arguments
94    ///
95    /// * `value` - The identifier of a private chat with the user who sent the join request.
96    pub fn with_user_chat_id(mut self, value: Integer) -> Self {
97        self.user_chat_id = Some(value);
98        self
99    }
100}
101
102#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
103#[serde(rename_all = "snake_case")]
104enum ChatJoinRequestQueryResult {
105    Approve,
106    Decline,
107    Queue,
108}
109
110/// Processes a received chat join request query.
111#[derive(Clone, Debug, Serialize)]
112pub struct AnswerChatJoinRequestQuery {
113    chat_join_request_query_id: String,
114    result: ChatJoinRequestQueryResult,
115}
116
117impl AnswerChatJoinRequestQuery {
118    fn new<T>(query_id: T, result: ChatJoinRequestQueryResult) -> Self
119    where
120        T: Into<String>,
121    {
122        Self {
123            chat_join_request_query_id: query_id.into(),
124            result,
125        }
126    }
127
128    /// Creates a new `AnswerChatJoinRequestQuery`.
129    ///
130    /// # Arguments
131    ///
132    /// * `query_id` - Unique identifier of the request query.
133    ///
134    /// Allows the user to join the chat.
135    pub fn approve<T>(query_id: T) -> Self
136    where
137        T: Into<String>,
138    {
139        Self::new(query_id, ChatJoinRequestQueryResult::Approve)
140    }
141
142    /// Creates a new `AnswerChatJoinRequestQuery`.
143    ///
144    /// # Arguments
145    ///
146    /// * `query_id` - Unique identifier of the request query.
147    ///
148    /// Disallows the user to join the chat.
149    pub fn decline<T>(query_id: T) -> Self
150    where
151        T: Into<String>,
152    {
153        Self::new(query_id, ChatJoinRequestQueryResult::Decline)
154    }
155
156    /// Creates a new `AnswerChatJoinRequestQuery`.
157    ///
158    /// # Arguments
159    ///
160    /// * `query_id` - Unique identifier of the request query.
161    ///
162    /// Leaves the decision to other administrators.
163    pub fn queue<T>(query_id: T) -> Self
164    where
165        T: Into<String>,
166    {
167        Self::new(query_id, ChatJoinRequestQueryResult::Queue)
168    }
169}
170
171impl Method for AnswerChatJoinRequestQuery {
172    type Response = bool;
173
174    fn into_payload(self) -> Payload {
175        Payload::json("answerChatJoinRequestQuery", self)
176    }
177}
178
179/// Approves a chat join request.
180///
181/// The bot must be an administrator in the chat for this to work
182/// and must have the `can_invite_users` administrator right.
183#[derive(Clone, Debug, Serialize)]
184pub struct ApproveChatJoinRequest {
185    chat_id: ChatId,
186    user_id: Integer,
187}
188
189impl ApproveChatJoinRequest {
190    /// Creates a new `ApproveChatJoinRequest`.
191    ///
192    /// # Arguments
193    ///
194    /// * `chat_id` - Unique identifier of the target chat.
195    /// * `user_id` - Unique identifier of the target user.
196    pub fn new<T>(chat_id: T, user_id: Integer) -> Self
197    where
198        T: Into<ChatId>,
199    {
200        Self {
201            chat_id: chat_id.into(),
202            user_id,
203        }
204    }
205}
206
207impl Method for ApproveChatJoinRequest {
208    type Response = bool;
209
210    fn into_payload(self) -> Payload {
211        Payload::json("approveChatJoinRequest", self)
212    }
213}
214
215/// Declines a chat join request.
216///
217/// The bot must be an administrator in the chat for this to work
218/// and must have the `can_invite_users` administrator right.
219#[derive(Clone, Debug, Serialize)]
220pub struct DeclineChatJoinRequest {
221    chat_id: ChatId,
222    user_id: Integer,
223}
224
225impl DeclineChatJoinRequest {
226    /// Creates a new `DeclineChatJoinRequest`.
227    ///
228    /// # Arguments
229    ///
230    /// * `chat_id` - Unique identifier of the target chat.
231    /// * `user_id` - Unique identifier of the target user.
232    pub fn new<T>(chat_id: T, user_id: Integer) -> Self
233    where
234        T: Into<ChatId>,
235    {
236        Self {
237            chat_id: chat_id.into(),
238            user_id,
239        }
240    }
241}
242
243impl Method for DeclineChatJoinRequest {
244    type Response = bool;
245
246    fn into_payload(self) -> Payload {
247        Payload::json("declineChatJoinRequest", self)
248    }
249}
250
251/// Processes a received chat join request query by
252/// showing a Mini App to the user before deciding the outcome.
253#[derive(Clone, Debug, Serialize)]
254pub struct SendChatJoinRequestWebApp {
255    chat_join_request_query_id: String,
256    web_app_url: String,
257}
258
259impl SendChatJoinRequestWebApp {
260    /// Creates a new `SendChatJoinRequestWebApp`.
261    ///
262    /// # Arguments
263    ///
264    /// * `query_id` - Unique identifier of the join request query.
265    /// * `url` - The URL of the Mini App to be opened.
266    pub fn new<A, B>(query_id: A, url: B) -> Self
267    where
268        A: Into<String>,
269        B: Into<String>,
270    {
271        Self {
272            chat_join_request_query_id: query_id.into(),
273            web_app_url: url.into(),
274        }
275    }
276}
277
278impl Method for SendChatJoinRequestWebApp {
279    type Response = bool;
280
281    fn into_payload(self) -> Payload {
282        Payload::json("sendChatJoinRequestWebApp", self)
283    }
284}