telegram_bot_async_raw/requests/
send_venue.rs

1use std::{borrow::Cow, ops::Not};
2
3use crate::{requests::*, types::*};
4
5/// Use this method to send information about a venue.
6#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize)]
7#[must_use = "requests do nothing unless sent"]
8pub struct SendVenue<'t, 'a, 'f> {
9    chat_id: ChatRef,
10    latitude: Float,
11    longitude: Float,
12    title: Cow<'t, str>,
13    address: Cow<'a, str>,
14    #[serde(skip_serializing_if = "Option::is_none")]
15    foursquare_id: Option<Cow<'f, 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<'t, 'a, 'f> Request for SendVenue<'t, 'a, 'f> {
25    type Type = JsonRequestType<Self>;
26    type Response = JsonIdResponse<Message>;
27
28    fn serialize(&self) -> Result<HttpRequest, Error> {
29        Self::Type::serialize(RequestUrl::method("sendVenue"), self)
30    }
31}
32
33impl<'t, 'a, 'f> SendVenue<'t, 'a, 'f> {
34    pub fn new<C, T, A>(chat: C, latitude: Float, longitude: Float, title: T, address: A) -> Self
35    where
36        C: ToChatRef,
37        T: Into<Cow<'t, str>>,
38        A: Into<Cow<'a, str>>,
39    {
40        SendVenue {
41            chat_id: chat.to_chat_ref(),
42            latitude,
43            longitude,
44            title: title.into(),
45            address: address.into(),
46            disable_notification: false,
47            foursquare_id: None,
48            reply_to_message_id: None,
49            reply_markup: None,
50        }
51    }
52
53    pub fn disable_notification(mut self) -> Self {
54        self.disable_notification = true;
55        self
56    }
57
58    pub fn foursquare_id<F>(mut self, id: F) -> Self
59    where
60        F: Into<Cow<'f, str>>,
61    {
62        self.foursquare_id = Some(id.into());
63        self
64    }
65
66    pub fn reply_to<R>(mut self, to: R) -> Self
67    where
68        R: ToMessageId,
69    {
70        self.reply_to_message_id = Some(to.to_message_id());
71        self
72    }
73
74    pub fn reply_markup<R>(mut self, reply_markup: R) -> Self
75    where
76        R: Into<ReplyMarkup>,
77    {
78        self.reply_markup = Some(reply_markup.into());
79        self
80    }
81}
82
83/// Send information about a venue.
84pub trait CanSendVenue<'t, 'a, 'f> {
85    fn venue<T, A>(
86        &self,
87        latitude: Float,
88        longitude: Float,
89        title: T,
90        address: A,
91    ) -> SendVenue<'t, 'a, 'f>
92    where
93        T: Into<Cow<'t, str>>,
94        A: Into<Cow<'a, str>>;
95}
96
97impl<'t, 'a, 'f, C> CanSendVenue<'t, 'a, 'f> for C
98where
99    C: ToChatRef,
100{
101    fn venue<T, A>(
102        &self,
103        latitude: Float,
104        longitude: Float,
105        title: T,
106        address: A,
107    ) -> SendVenue<'t, 'a, 'f>
108    where
109        T: Into<Cow<'t, str>>,
110        A: Into<Cow<'a, str>>,
111    {
112        SendVenue::new(self, latitude, longitude, title, address)
113    }
114}
115
116/// Reply with information about a venue.
117pub trait CanReplySendVenue {
118    fn venue_reply<'t, 'a, 'f, T, A>(
119        &self,
120        latitude: Float,
121        longitude: Float,
122        title: T,
123        address: A,
124    ) -> SendVenue<'t, 'a, 'f>
125    where
126        T: Into<Cow<'t, str>>,
127        A: Into<Cow<'a, str>>;
128}
129
130impl<M> CanReplySendVenue for M
131where
132    M: ToMessageId + ToSourceChat,
133{
134    fn venue_reply<'t, 'a, 'f, T, A>(
135        &self,
136        latitude: Float,
137        longitude: Float,
138        title: T,
139        address: A,
140    ) -> SendVenue<'t, 'a, 'f>
141    where
142        T: Into<Cow<'t, str>>,
143        A: Into<Cow<'a, str>>,
144    {
145        let rq = self
146            .to_source_chat()
147            .venue(latitude, longitude, title, address);
148        rq.reply_to(self.to_message_id())
149    }
150}
151
152impl<'b> ToRequest<'b> for Venue {
153    type Request = SendVenue<'b, 'b, 'b>;
154
155    fn to_request<C>(&'b self, chat: C) -> Self::Request
156    where
157        C: ToChatRef,
158    {
159        let mut rq = chat.venue(
160            self.location.latitude,
161            self.location.longitude,
162            self.title.as_str(),
163            self.address.as_str(),
164        );
165        if let Some(ref foursquare_id) = self.foursquare_id {
166            rq = rq.foursquare_id(foursquare_id.as_str());
167        }
168        rq
169    }
170}
171
172impl<'b> ToReplyRequest<'b> for Venue {
173    type Request = SendVenue<'b, 'b, 'b>;
174
175    fn to_reply_request<M>(&'b self, message: M) -> Self::Request
176    where
177        M: ToMessageId + ToSourceChat,
178    {
179        let mut rq = message.venue_reply(
180            self.location.latitude,
181            self.location.longitude,
182            self.title.as_str(),
183            self.address.as_str(),
184        );
185        if let Some(ref foursquare_id) = self.foursquare_id {
186            rq = rq.foursquare_id(foursquare_id.as_str());
187        }
188        rq
189    }
190}