tgbot/types/contact/
mod.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{
4    api::{Method, Payload},
5    types::{ChatId, Integer, Message, ReplyMarkup, ReplyParameters},
6};
7
8#[cfg(test)]
9mod tests;
10
11/// Represents a phone contact.
12#[serde_with::skip_serializing_none]
13#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
14pub struct Contact {
15    /// First name.
16    pub first_name: String,
17    /// Phone number.
18    pub phone_number: String,
19    /// Last name.
20    pub last_name: Option<String>,
21    /// Identifier in Telegram.
22    pub user_id: Option<Integer>,
23    /// Additional data in the form of a vCard.
24    pub vcard: Option<String>,
25}
26
27impl Contact {
28    /// Creates a new `Contact`.
29    ///
30    /// # Arguments
31    ///
32    /// * `first_name` - First name.
33    /// * `phone_number` - Phone number.
34    pub fn new<A, B>(first_name: A, phone_number: B) -> Self
35    where
36        A: Into<String>,
37        B: Into<String>,
38    {
39        Self {
40            first_name: first_name.into(),
41            phone_number: phone_number.into(),
42            last_name: None,
43            user_id: None,
44            vcard: None,
45        }
46    }
47
48    /// Sets a new last name.
49    ///
50    /// # Arguments
51    ///
52    /// * `value` - Last name.
53    pub fn with_last_name<T>(mut self, value: T) -> Self
54    where
55        T: Into<String>,
56    {
57        self.last_name = Some(value.into());
58        self
59    }
60
61    /// Sets a new user ID.
62    ///
63    /// # Arguments
64    ///
65    /// * `value` - User ID.
66    pub fn with_user_id(mut self, value: Integer) -> Self {
67        self.user_id = Some(value);
68        self
69    }
70
71    /// Sets a new vCard.
72    ///
73    /// # Arguments
74    ///
75    /// * `value` - Additional data about the contact in the form of a vCard; 0-2048 bytes.
76    pub fn with_vcard<T>(mut self, value: T) -> Self
77    where
78        T: Into<String>,
79    {
80        self.vcard = Some(value.into());
81        self
82    }
83}
84
85/// Sends a phone contact.
86#[serde_with::skip_serializing_none]
87#[derive(Clone, Debug, Serialize)]
88pub struct SendContact {
89    chat_id: ChatId,
90    first_name: String,
91    phone_number: String,
92    allow_paid_broadcast: Option<bool>,
93    business_connection_id: Option<String>,
94    disable_notification: Option<bool>,
95    last_name: Option<String>,
96    message_effect_id: Option<String>,
97    message_thread_id: Option<Integer>,
98    protect_content: Option<bool>,
99    reply_markup: Option<ReplyMarkup>,
100    reply_parameters: Option<ReplyParameters>,
101    vcard: Option<String>,
102}
103
104impl SendContact {
105    /// Creates a new `SendContact`.
106    ///
107    /// # Arguments
108    ///
109    /// * `chat_id` - Unique identifier of the target chat.
110    /// * `first_name` - First name.
111    /// * `phone_number` - Phone number.
112    pub fn new<A, B, C>(chat_id: A, first_name: B, phone_number: C) -> Self
113    where
114        A: Into<ChatId>,
115        B: Into<String>,
116        C: Into<String>,
117    {
118        SendContact {
119            chat_id: chat_id.into(),
120            first_name: first_name.into(),
121            phone_number: phone_number.into(),
122            allow_paid_broadcast: None,
123            business_connection_id: None,
124            disable_notification: None,
125            last_name: None,
126            message_effect_id: None,
127            message_thread_id: None,
128            protect_content: None,
129            reply_markup: None,
130            reply_parameters: None,
131            vcard: None,
132        }
133    }
134
135    /// Sets a new value for the `allow_paid_broadcast` flag.
136    ///
137    /// # Arguments
138    ///
139    /// * `value` - Whether to allow up to 1000 messages per second, ignoring broadcasting limits
140    ///   for a fee of 0.1 Telegram Stars per message.
141    ///   The relevant Stars will be withdrawn from the bot's balance.
142    pub fn with_allow_paid_broadcast(mut self, value: bool) -> Self {
143        self.allow_paid_broadcast = Some(value);
144        self
145    }
146
147    /// Sets a new business connection ID.
148    ///
149    /// # Arguments
150    ///
151    /// * `value` - Unique identifier of the business connection.
152    pub fn with_business_connection_id<T>(mut self, value: T) -> Self
153    where
154        T: Into<String>,
155    {
156        self.business_connection_id = Some(value.into());
157        self
158    }
159
160    /// Sets a new value for the `disable_notification` flag.
161    ///
162    /// # Arguments
163    ///
164    /// * `value` - Indicates whether to send the message silently or not;
165    ///   a user will receive a notification without sound.
166    pub fn with_disable_notification(mut self, value: bool) -> Self {
167        self.disable_notification = Some(value);
168        self
169    }
170
171    /// Sets a last name.
172    ///
173    /// # Arguments
174    ///
175    /// * `value` - Contact's last name.
176    pub fn with_last_name<T>(mut self, value: T) -> Self
177    where
178        T: Into<String>,
179    {
180        self.last_name = Some(value.into());
181        self
182    }
183
184    /// Sets a new message effect ID.
185    ///
186    /// # Arguments
187    ///
188    /// * `value` - Unique identifier of the message effect to be added to the message; for private chats only.
189    pub fn with_message_effect_id<T>(mut self, value: T) -> Self
190    where
191        T: Into<String>,
192    {
193        self.message_effect_id = Some(value.into());
194        self
195    }
196
197    /// Sets a new message thread ID.
198    ///
199    /// # Arguments
200    ///
201    /// * `value` - Unique identifier of the target message thread;
202    ///   supergroups only.
203    pub fn with_message_thread_id(mut self, value: Integer) -> Self {
204        self.message_thread_id = Some(value);
205        self
206    }
207
208    /// Sets a new value for the `protect_content` flag.
209    ///
210    /// # Arguments
211    ///
212    /// * `value` - Indicates whether to protect the contents
213    ///   of the sent message from forwarding and saving.
214    pub fn with_protect_content(mut self, value: bool) -> Self {
215        self.protect_content = Some(value);
216        self
217    }
218
219    /// Sets a new reply markup.
220    ///
221    /// # Arguments
222    ///
223    /// * `value` - Reply markup.
224    pub fn with_reply_markup<T>(mut self, value: T) -> Self
225    where
226        T: Into<ReplyMarkup>,
227    {
228        self.reply_markup = Some(value.into());
229        self
230    }
231
232    /// Sets new reply parameters.
233    ///
234    /// # Arguments
235    ///
236    /// * `value` - Description of the message to reply to.
237    pub fn with_reply_parameters(mut self, value: ReplyParameters) -> Self {
238        self.reply_parameters = Some(value);
239        self
240    }
241
242    /// Sets a new vCard.
243    ///
244    /// # Arguments
245    ///
246    /// * `value` - Additional data about the contact in the form of a vCard; 0-2048 bytes.
247    pub fn with_vcard<T>(mut self, value: T) -> Self
248    where
249        T: Into<String>,
250    {
251        self.vcard = Some(value.into());
252        self
253    }
254}
255
256impl Method for SendContact {
257    type Response = Message;
258
259    fn into_payload(self) -> Payload {
260        Payload::json("sendContact", self)
261    }
262}