telegram_bot_raw/requests/
send_contact.rs

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