tgbot/types/chat/invite_link/
mod.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{
4    api::{Method, Payload},
5    types::{ChatId, Integer, User},
6};
7
8#[cfg(test)]
9mod tests;
10
11/// Represents an invite link for a chat.
12#[serde_with::skip_serializing_none]
13#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
14pub struct ChatInviteLink {
15    /// Indicates whether users joining the chat via the link
16    /// need to be approved by chat administrators.
17    pub creates_join_request: bool,
18    /// The creator of the link.
19    pub creator: User,
20    /// The invite link.
21    ///
22    /// If the link was created by another chat administrator,
23    /// then the second part of the link will be replaced with “…”.
24    pub invite_link: String,
25    /// Indicates whether the link is primary.
26    pub is_primary: bool,
27    /// Indicates whether the link is revoked.
28    pub is_revoked: bool,
29    /// The point in time (Unix timestamp) when the link will expire or has been expired.
30    pub expire_date: Option<Integer>,
31    /// The maximum number of users that can be members
32    /// of the chat simultaneously after joining
33    /// the chat via this invite link; 1-99999.
34    pub member_limit: Option<Integer>,
35    /// The name of the invite link.
36    pub name: Option<String>,
37    /// The number of pending join requests created using this link.
38    pub pending_join_request_count: Option<Integer>,
39}
40
41impl ChatInviteLink {
42    /// Creates a new `ChatInviteLink`.
43    ///
44    /// # Arguments
45    ///
46    /// * `invite_link` - Invite link.
47    /// * `creator` - Creator of the link.
48    pub fn new<T>(invite_link: T, creator: User) -> Self
49    where
50        T: Into<String>,
51    {
52        Self {
53            invite_link: invite_link.into(),
54            creator,
55            creates_join_request: false,
56            is_primary: false,
57            is_revoked: false,
58            name: None,
59            expire_date: None,
60            member_limit: None,
61            pending_join_request_count: None,
62        }
63    }
64
65    /// Sets a new value for the `creates_join_request` flag.
66    ///
67    /// # Arguments
68    ///
69    /// * `value` - Indicates whether users joining the chat via the link need
70    ///   to be approved by chat administrators.
71    pub fn with_creates_join_request(mut self, value: bool) -> Self {
72        self.creates_join_request = value;
73        self
74    }
75
76    /// Sets a new expiration date.
77    ///
78    /// # Arguments
79    ///
80    /// * `value` - The point in time (Unix timestamp) when the link will expire or has been expired.
81    pub fn with_expire_date(mut self, value: Integer) -> Self {
82        self.expire_date = Some(value);
83        self
84    }
85
86    /// Sets a new value for the `is_primary` flag.
87    ///
88    /// # Arguments
89    ///
90    /// * `value` - Indicates whether the link is primary.
91    pub fn with_is_primary(mut self, value: bool) -> Self {
92        self.is_primary = value;
93        self
94    }
95
96    /// Sets a new value for the `is_revoked` flag.
97    ///
98    /// # Arguments
99    ///
100    /// * `value` - Indicates whether the link is revoked.
101    pub fn with_is_revoked(mut self, value: bool) -> Self {
102        self.is_revoked = value;
103        self
104    }
105
106    /// Sets a new member limit
107    ///
108    /// # Arguments
109    ///
110    /// * `value` - The maximum number of users that can be members
111    ///   of the chat simultaneously after joining
112    ///   the chat via this invite link; 1-99999.
113    pub fn with_member_limit(mut self, value: Integer) -> Self {
114        self.member_limit = Some(value);
115        self
116    }
117
118    /// Sets a new name of the invite link.
119    ///
120    /// # Arguments
121    ///
122    /// * `value` - The name of the invite link.
123    pub fn with_name<T>(mut self, value: T) -> Self
124    where
125        T: Into<String>,
126    {
127        self.name = Some(value.into());
128        self
129    }
130
131    /// Sets a new pending join requests count.
132    ///
133    /// # Arguments
134    ///
135    /// * `value` - The number of pending join requests created using this link.
136    pub fn with_pending_join_request_count(mut self, value: Integer) -> Self {
137        self.pending_join_request_count = Some(value);
138        self
139    }
140}
141
142/// Creates an additional invite link for a chat.
143///
144/// The bot must be an administrator in the chat for this to work
145/// and must have the appropriate admin rights.
146/// The link can be revoked using the method [`RevokeChatInviteLink`].
147/// Returns the new invite link as [`ChatInviteLink`] object.
148#[serde_with::skip_serializing_none]
149#[derive(Clone, Debug, Serialize)]
150pub struct CreateChatInviteLink {
151    chat_id: ChatId,
152    creates_join_request: Option<bool>,
153    expire_date: Option<Integer>,
154    member_limit: Option<Integer>,
155    name: Option<String>,
156}
157
158impl CreateChatInviteLink {
159    /// Creates a new `CreateChatInviteLink`.
160    ///
161    /// # Arguments
162    ///
163    /// * `chat_id` - Unique identifier of the target chat.
164    pub fn new<T>(chat_id: T) -> Self
165    where
166        T: Into<ChatId>,
167    {
168        Self {
169            chat_id: chat_id.into(),
170            creates_join_request: None,
171            expire_date: None,
172            member_limit: None,
173            name: None,
174        }
175    }
176
177    /// Sets a new value for the `creates_join_request` flag.
178    ///
179    /// * `value` - Indicates whether users joining the chat via the link need
180    ///   to be approved by chat administrators;
181    ///   if `true`, member_limit can't be specified.
182    pub fn with_creates_join_request(mut self, value: bool) -> Self {
183        self.creates_join_request = Some(value);
184        self
185    }
186
187    /// Sets a new expiration date.
188    ///
189    /// # Arguments
190    ///
191    /// * `value` - The point in time (Unix timestamp) when the link will expire.
192    pub fn with_expire_date(mut self, value: Integer) -> Self {
193        self.expire_date = Some(value);
194        self
195    }
196
197    /// Sets a new member limit.
198    ///
199    /// # Arguments
200    ///
201    /// * `value` - The maximum number of users that can be members of the chat simultaneously
202    ///   after joining the chat via this invite link; 1-99999.
203    pub fn with_member_limit(mut self, value: Integer) -> Self {
204        self.member_limit = Some(value);
205        self
206    }
207
208    /// Sets a new invite link name.
209    ///
210    /// # Arguments
211    ///
212    /// * `value` - The name of the invite link; 0-32 characters.
213    pub fn with_name<T>(mut self, value: T) -> Self
214    where
215        T: Into<String>,
216    {
217        self.name = Some(value.into());
218        self
219    }
220}
221
222impl Method for CreateChatInviteLink {
223    type Response = ChatInviteLink;
224
225    fn into_payload(self) -> Payload {
226        Payload::json("createChatInviteLink", self)
227    }
228}
229
230/// Creates a subscription invite link for a channel chat.
231///
232/// The bot must have the `can_invite_users` administrator rights.
233/// The link can be edited using the method [`crate::types::EditChatSubscriptionInviteLink`]
234/// or revoked using the method [`crate::types::RevokeChatInviteLink`].
235#[serde_with::skip_serializing_none]
236#[derive(Clone, Debug, Serialize)]
237pub struct CreateChatSubscriptionInviteLink {
238    chat_id: ChatId,
239    subscription_period: Integer,
240    subscription_price: Integer,
241    name: Option<String>,
242}
243
244impl CreateChatSubscriptionInviteLink {
245    /// Creates a new `CreateChatSubscriptionInviteLink`.
246    ///
247    /// # Arguments
248    ///
249    /// * `chat_id` - Unique identifier for the target channel chat.
250    /// * `subscription_period` - The number of seconds the subscription will be active for before the next payment.
251    ///   Currently, it must always be 2592000 (30 days).
252    /// * `subscription_price` - The amount of Telegram Stars a user must pay initially
253    ///   and after each subsequent subscription period to be a member of the chat; 1-10000.
254    pub fn new<T>(chat_id: T, subscription_period: Integer, subscription_price: Integer) -> Self
255    where
256        T: Into<ChatId>,
257    {
258        Self {
259            chat_id: chat_id.into(),
260            name: None,
261            subscription_period,
262            subscription_price,
263        }
264    }
265
266    /// Sets a new name.
267    ///
268    /// # Arguments
269    ///
270    /// * `value` - Invite link name; 0-32 characters.
271    pub fn with_name<T>(mut self, value: T) -> Self
272    where
273        T: Into<String>,
274    {
275        self.name = Some(value.into());
276        self
277    }
278}
279
280impl Method for CreateChatSubscriptionInviteLink {
281    type Response = ChatInviteLink;
282
283    fn into_payload(self) -> Payload {
284        Payload::json("createChatSubscriptionInviteLink", self)
285    }
286}
287
288/// Changes a non-primary invite link created by a bot.
289///
290/// The bot must be an administrator in the chat for this to work
291/// and must have the appropriate admin rights.
292/// Returns the edited invite link as a [`ChatInviteLink`] object.
293#[serde_with::skip_serializing_none]
294#[derive(Clone, Debug, Serialize)]
295pub struct EditChatInviteLink {
296    chat_id: ChatId,
297    invite_link: String,
298    creates_join_request: Option<bool>,
299    expire_date: Option<Integer>,
300    member_limit: Option<Integer>,
301    name: Option<String>,
302}
303
304impl EditChatInviteLink {
305    /// Creates a new `EditChatInviteLink`.
306    ///
307    /// # Arguments
308    ///
309    /// * `chat_id` - Unique identifier of the target chat.
310    /// * `invite_link` - The invite link to edit.
311    pub fn new<A, B>(chat_id: A, invite_link: B) -> Self
312    where
313        A: Into<ChatId>,
314        B: Into<String>,
315    {
316        Self {
317            chat_id: chat_id.into(),
318            invite_link: invite_link.into(),
319            creates_join_request: None,
320            expire_date: None,
321            member_limit: None,
322            name: None,
323        }
324    }
325
326    /// Sets a new value for the `creates_join_request` flag.
327    ///
328    /// # Arguments
329    ///
330    /// * `value` - Indicates whether users joining the chat via the link need
331    ///   to be approved by chat administrators;
332    ///   if `true`, `member_limit` can't be specified.
333    pub fn with_creates_join_request(mut self, value: bool) -> Self {
334        self.creates_join_request = Some(value);
335        self
336    }
337
338    /// Sets a new expiration date.
339    ///
340    /// # Arguments
341    ///
342    /// * `value` - The point in time (Unix timestamp) when the link will expire.
343    pub fn with_expire_date(mut self, value: Integer) -> Self {
344        self.expire_date = Some(value);
345        self
346    }
347
348    /// Sets a new member limit.
349    ///
350    /// # Arguments
351    ///
352    /// * `value` - The maximum number of users that can be members of the chat simultaneously
353    ///   after joining the chat via this invite link; 1-99999.
354    pub fn with_member_limit(mut self, value: Integer) -> Self {
355        self.member_limit = Some(value);
356        self
357    }
358
359    /// Sets a new name of the invite link.
360    ///
361    /// # Arguments
362    ///
363    /// * `value` - The name of the invite link; 0-32 characters.
364    pub fn with_name<T>(mut self, value: T) -> Self
365    where
366        T: Into<String>,
367    {
368        self.name = Some(value.into());
369        self
370    }
371}
372
373impl Method for EditChatInviteLink {
374    type Response = ChatInviteLink;
375
376    fn into_payload(self) -> Payload {
377        Payload::json("editChatInviteLink", self)
378    }
379}
380
381/// Allows to edit a subscription invite link created by the bot.
382///
383/// The bot must have the `can_invite_users` administrator rights.
384#[serde_with::skip_serializing_none]
385#[derive(Clone, Debug, Serialize)]
386pub struct EditChatSubscriptionInviteLink {
387    chat_id: ChatId,
388    invite_link: String,
389    name: Option<String>,
390}
391
392impl EditChatSubscriptionInviteLink {
393    /// Creates a new `EditChatSubscriptionInviteLink`.
394    ///
395    /// # Arguments
396    ///
397    /// * `chat_id` - Unique identifier for the target chat.
398    /// * `invite_link` - The invite link to edit
399    pub fn new<A, B>(chat_id: A, invite_link: B) -> Self
400    where
401        A: Into<ChatId>,
402        B: Into<String>,
403    {
404        Self {
405            chat_id: chat_id.into(),
406            invite_link: invite_link.into(),
407            name: None,
408        }
409    }
410
411    /// Sets a new name of the invite link.
412    ///
413    /// # Arguments
414    ///
415    /// * `value` - The name of the invite link; 0-32 characters.
416    pub fn with_name<T>(mut self, value: T) -> Self
417    where
418        T: Into<String>,
419    {
420        self.name = Some(value.into());
421        self
422    }
423}
424
425impl Method for EditChatSubscriptionInviteLink {
426    type Response = ChatInviteLink;
427
428    fn into_payload(self) -> Payload {
429        Payload::json("editChatSubscriptionInviteLink", self)
430    }
431}
432
433/// Generates a new invite link for a chat.
434///
435/// Any previously generated link is revoked.
436/// The bot must be an administrator in the chat for this to work
437/// and must have the appropriate admin rights.
438/// Returns the new invite link as String on success.
439///
440/// # Notes
441///
442/// Each administrator in a chat generates their own invite links.
443/// Bots can't use invite links generated by other administrators.
444/// If you want your bot to work with invite links, it will need to generate
445/// its own link using [`ExportChatInviteLink`] or by calling the [`crate::types::GetChat`] method.
446/// If your bot needs to generate a new primary invite link replacing its previous one,
447/// use [`ExportChatInviteLink`] again.
448#[derive(Clone, Debug, Serialize)]
449pub struct ExportChatInviteLink {
450    chat_id: ChatId,
451}
452
453impl ExportChatInviteLink {
454    /// Creates a new `ExportChatInviteLink`.
455    ///
456    /// # Arguments
457    ///
458    /// * `chat_id` - Unique identifier of the target chat.
459    pub fn new<T>(chat_id: T) -> Self
460    where
461        T: Into<ChatId>,
462    {
463        Self {
464            chat_id: chat_id.into(),
465        }
466    }
467}
468
469impl Method for ExportChatInviteLink {
470    type Response = String;
471
472    fn into_payload(self) -> Payload {
473        Payload::json("exportChatInviteLink", self)
474    }
475}
476
477/// Revokes an invite link created by a bot.
478///
479/// If the primary link is revoked, a new link is automatically generated.
480/// The bot must be an administrator in the chat for this to work and
481/// must have the appropriate admin rights.
482/// Returns the revoked invite link as [`ChatInviteLink`] object.
483#[derive(Clone, Debug, Serialize)]
484pub struct RevokeChatInviteLink {
485    chat_id: ChatId,
486    invite_link: String,
487}
488
489impl RevokeChatInviteLink {
490    /// Creates a new `RevokeChatInviteLink`.
491    ///
492    /// # Arguments
493    ///
494    /// * `chat_id` - Unique identifier of the target chat.
495    /// * `invite_link` - The invite link to revoke.
496    pub fn new<A, B>(chat_id: A, invite_link: B) -> Self
497    where
498        A: Into<ChatId>,
499        B: Into<String>,
500    {
501        Self {
502            chat_id: chat_id.into(),
503            invite_link: invite_link.into(),
504        }
505    }
506}
507
508impl Method for RevokeChatInviteLink {
509    type Response = ChatInviteLink;
510
511    fn into_payload(self) -> Payload {
512        Payload::json("revokeChatInviteLink", self)
513    }
514}