tgbot/types/definitions/chat/
invite_link.rs

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