telegram_bot_async_raw/requests/
send_contact.rs

1use std::{borrow::Cow, ops::Not};
2
3use crate::{requests::*, types::*};
4
5/// Use this method to send phone contacts.
6#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize)]
7#[must_use = "requests do nothing unless sent"]
8pub struct SendContact<'p, 'f, 'l> {
9    chat_id: ChatRef,
10    phone_number: Cow<'p, str>,
11    first_name: Cow<'f, str>,
12    #[serde(skip_serializing_if = "Option::is_none")]
13    last_name: Option<Cow<'l, str>>,
14    #[serde(skip_serializing_if = "Not::not")]
15    disable_notification: bool,
16    #[serde(skip_serializing_if = "Option::is_none")]
17    reply_to_message_id: Option<MessageId>,
18    #[serde(skip_serializing_if = "Option::is_none")]
19    reply_markup: Option<ReplyMarkup>,
20}
21
22impl<'p, 'f, 'l> Request for SendContact<'p, 'f, 'l> {
23    type Type = JsonRequestType<Self>;
24    type Response = JsonIdResponse<Message>;
25
26    fn serialize(&self) -> Result<HttpRequest, Error> {
27        Self::Type::serialize(RequestUrl::method("sendContact"), self)
28    }
29}
30
31impl<'p, 'f, 'l> SendContact<'p, 'f, 'l> {
32    pub fn new<C, P, F>(chat: C, phone_number: P, first_name: F) -> Self
33    where
34        C: ToChatRef,
35        P: Into<Cow<'p, str>>,
36        F: Into<Cow<'f, str>>,
37    {
38        SendContact {
39            chat_id: chat.to_chat_ref(),
40            phone_number: phone_number.into(),
41            first_name: first_name.into(),
42            last_name: None,
43            disable_notification: false,
44            reply_to_message_id: None,
45            reply_markup: None,
46        }
47    }
48
49    pub fn last_name<F>(mut self, last_name: F) -> Self
50    where
51        F: Into<Cow<'l, str>>,
52    {
53        self.last_name = Some(last_name.into());
54        self
55    }
56
57    pub fn disable_notification(mut self) -> Self {
58        self.disable_notification = true;
59        self
60    }
61
62    pub fn reply_to<R>(mut self, to: R) -> Self
63    where
64        R: ToMessageId,
65    {
66        self.reply_to_message_id = Some(to.to_message_id());
67        self
68    }
69
70    pub fn reply_markup<R>(mut self, reply_markup: R) -> Self
71    where
72        R: Into<ReplyMarkup>,
73    {
74        self.reply_markup = Some(reply_markup.into());
75        self
76    }
77}
78
79/// Send phone contact.
80pub trait CanSendContact<'p, 'f, 'l> {
81    fn contact<P, F>(&self, phone_number: P, first_name: F) -> SendContact<'p, 'f, 'l>
82    where
83        P: Into<Cow<'p, str>>,
84        F: Into<Cow<'f, str>>;
85}
86
87impl<'p, 'f, 'l, C> CanSendContact<'p, 'f, 'l> for C
88where
89    C: ToChatRef,
90{
91    fn contact<P, F>(&self, phone_number: P, first_name: F) -> SendContact<'p, 'f, 'l>
92    where
93        P: Into<Cow<'p, str>>,
94        F: Into<Cow<'f, str>>,
95    {
96        SendContact::new(self, phone_number, first_name)
97    }
98}
99
100/// Reply with phone contact.
101pub trait CanReplySendContact {
102    fn contact_reply<'p, 'f, 'l, P: 'p, F: 'f>(
103        &self,
104        phone_number: P,
105        first_name: F,
106    ) -> SendContact<'p, 'f, 'l>
107    where
108        P: Into<Cow<'p, str>>,
109        F: Into<Cow<'f, str>>;
110}
111
112impl<M> CanReplySendContact for M
113where
114    M: ToMessageId + ToSourceChat,
115{
116    fn contact_reply<'p, 'f, 'l, P: 'p, F: 'f>(
117        &self,
118        phone_number: P,
119        first_name: F,
120    ) -> SendContact<'p, 'f, 'l>
121    where
122        P: Into<Cow<'p, str>>,
123        F: Into<Cow<'f, str>>,
124    {
125        let rq = self.to_source_chat().contact(phone_number, first_name);
126        rq.reply_to(self.to_message_id())
127    }
128}
129
130impl<'b> ToRequest<'b> for Contact {
131    type Request = SendContact<'b, 'b, 'b>;
132
133    fn to_request<C>(&'b self, chat: C) -> Self::Request
134    where
135        C: ToChatRef,
136    {
137        let mut rq = chat.contact(self.phone_number.as_str(), self.first_name.as_str());
138        if let Some(ref last_name) = self.last_name {
139            rq = rq.last_name(last_name.as_str());
140        }
141        rq
142    }
143}
144
145impl<'b> ToReplyRequest<'b> for Contact {
146    type Request = SendContact<'b, 'b, 'b>;
147
148    fn to_reply_request<M>(&'b self, message: M) -> Self::Request
149    where
150        M: ToMessageId + ToSourceChat,
151    {
152        let mut rq = message.contact_reply(self.phone_number.as_str(), self.first_name.as_str());
153        if let Some(ref last_name) = self.last_name {
154            rq = rq.last_name(last_name.as_str());
155        }
156        rq
157    }
158}