tgbot/types/chat/join_request/mod.rs
1use serde::{Deserialize, Serialize};
2
3use crate::{
4 api::{Method, Payload},
5 types::{Chat, ChatId, ChatInviteLink, Integer, User},
6};
7
8#[cfg(test)]
9mod tests;
10
11/// Represents a join request sent to a chat.
12#[serde_with::skip_serializing_none]
13#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
14pub struct ChatJoinRequest {
15 /// Chat to which the request was sent.
16 pub chat: Chat,
17 /// Date the request was sent in Unix time.
18 pub date: Integer,
19 /// User that sent the join request.
20 pub from: User,
21 /// Bio of the user.
22 pub bio: Option<String>,
23 /// Chat invite link that was used by the user to send the join request.
24 pub invite_link: Option<ChatInviteLink>,
25 /// Identifier of a private chat with the user who sent the join request.
26 ///
27 /// A bot can use this identifier for 5 minutes to
28 /// send messages until the join request is processed,
29 /// assuming no other administrator contacted the user.
30 pub user_chat_id: Option<Integer>,
31}
32
33impl ChatJoinRequest {
34 /// Creates a new `ChatJoinRequest`.
35 ///
36 /// # Arguments
37 ///
38 /// * `chat` - Chat to which the request was sent.
39 /// * `date` - Date the request was sent in Unix time.
40 /// * `from` - User that sent the join request.
41 pub fn new(chat: Chat, date: Integer, from: User) -> Self {
42 Self {
43 chat,
44 date,
45 from,
46 bio: None,
47 invite_link: None,
48 user_chat_id: None,
49 }
50 }
51
52 /// Sets a new bio.
53 ///
54 /// # Arguments
55 ///
56 /// * `value` - The bio of the user.
57 pub fn with_bio<T>(mut self, value: T) -> Self
58 where
59 T: Into<String>,
60 {
61 self.bio = Some(value.into());
62 self
63 }
64
65 /// Sets a new invite link.
66 ///
67 /// # Arguments
68 ///
69 /// * `value` - The chat invite link that was used by the user to send the join request.
70 pub fn with_invite_link(mut self, value: ChatInviteLink) -> Self {
71 self.invite_link = Some(value);
72 self
73 }
74
75 /// Sets a new user chat ID.
76 ///
77 /// # Arguments
78 ///
79 /// * `value` - The identifier of a private chat with the user who sent the join request.
80 pub fn with_user_chat_id(mut self, value: Integer) -> Self {
81 self.user_chat_id = Some(value);
82 self
83 }
84}
85
86/// Approves a chat join request.
87///
88/// The bot must be an administrator in the chat for this to work
89/// and must have the `can_invite_users` administrator right.
90#[derive(Clone, Debug, Serialize)]
91pub struct ApproveChatJoinRequest {
92 chat_id: ChatId,
93 user_id: Integer,
94}
95
96impl ApproveChatJoinRequest {
97 /// Creates a new `ApproveChatJoinRequest`.
98 ///
99 /// # Arguments
100 ///
101 /// * `chat_id` - Unique identifier of the target chat.
102 /// * `user_id` - Unique identifier of the target user.
103 pub fn new<T>(chat_id: T, user_id: Integer) -> Self
104 where
105 T: Into<ChatId>,
106 {
107 Self {
108 chat_id: chat_id.into(),
109 user_id,
110 }
111 }
112}
113
114impl Method for ApproveChatJoinRequest {
115 type Response = bool;
116
117 fn into_payload(self) -> Payload {
118 Payload::json("approveChatJoinRequest", self)
119 }
120}
121
122/// Declines a chat join request.
123///
124/// The bot must be an administrator in the chat for this to work
125/// and must have the `can_invite_users` administrator right.
126#[derive(Clone, Debug, Serialize)]
127pub struct DeclineChatJoinRequest {
128 chat_id: ChatId,
129 user_id: Integer,
130}
131
132impl DeclineChatJoinRequest {
133 /// Creates a new `DeclineChatJoinRequest`.
134 ///
135 /// # Arguments
136 ///
137 /// * `chat_id` - Unique identifier of the target chat.
138 /// * `user_id` - Unique identifier of the target user.
139 pub fn new<T>(chat_id: T, user_id: Integer) -> Self
140 where
141 T: Into<ChatId>,
142 {
143 Self {
144 chat_id: chat_id.into(),
145 user_id,
146 }
147 }
148}
149
150impl Method for DeclineChatJoinRequest {
151 type Response = bool;
152
153 fn into_payload(self) -> Payload {
154 Payload::json("declineChatJoinRequest", self)
155 }
156}