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