telegram_bot_fork_raw/requests/
send_contact.rs

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