telegram_bot_raw/requests/
send_location.rs

1use std::ops::Not;
2
3use crate::requests::*;
4use crate::types::*;
5
6/// Use this method to send point on the map.
7#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize)]
8#[must_use = "requests do nothing unless sent"]
9pub struct SendLocation {
10    chat_id: ChatRef,
11    latitude: Float,
12    longitude: Float,
13    #[serde(skip_serializing_if = "Option::is_none")]
14    live_period: Option<Integer>,
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 Request for SendLocation {
24    type Type = JsonRequestType<Self>;
25    type Response = JsonIdResponse<Message>;
26
27    fn serialize(&self) -> Result<HttpRequest, Error> {
28        Self::Type::serialize(RequestUrl::method("sendLocation"), self)
29    }
30}
31
32impl SendLocation {
33    pub fn new<C>(chat: C, latitude: Float, longitude: Float) -> Self
34    where
35        C: ToChatRef,
36    {
37        SendLocation {
38            chat_id: chat.to_chat_ref(),
39            latitude: latitude,
40            longitude: longitude,
41            live_period: None,
42            disable_notification: false,
43            reply_to_message_id: None,
44            reply_markup: None,
45        }
46    }
47
48    /// Period in seconds for which the location will be updated, should be between 60 and 86400.
49    pub fn live_period(&mut self, period: Integer) -> &mut Self {
50        self.live_period = Some(period);
51        self
52    }
53
54    pub fn disable_notification(&mut self) -> &mut Self {
55        self.disable_notification = true;
56        self
57    }
58
59    pub fn reply_to<R>(&mut self, to: R) -> &mut Self
60    where
61        R: ToMessageId,
62    {
63        self.reply_to_message_id = Some(to.to_message_id());
64        self
65    }
66
67    pub fn reply_markup<R>(&mut self, reply_markup: R) -> &mut Self
68    where
69        R: Into<ReplyMarkup>,
70    {
71        self.reply_markup = Some(reply_markup.into());
72        self
73    }
74}
75
76/// Send point on the map.
77pub trait CanSendLocation {
78    fn location(&self, latitude: Float, longitude: Float) -> SendLocation;
79}
80
81impl<C> CanSendLocation for C
82where
83    C: ToChatRef,
84{
85    fn location(&self, latitude: Float, longitude: Float) -> SendLocation {
86        SendLocation::new(self, latitude, longitude)
87    }
88}
89
90/// Reply with point on the map.
91pub trait CanReplySendLocation {
92    fn location_reply(&self, latitude: Float, longitude: Float) -> SendLocation;
93}
94
95impl<M> CanReplySendLocation for M
96where
97    M: ToMessageId + ToSourceChat,
98{
99    fn location_reply(&self, latitude: Float, longitude: Float) -> SendLocation {
100        let mut rq = self.to_source_chat().location(latitude, longitude);
101        rq.reply_to(self.to_message_id());
102        rq
103    }
104}
105
106impl<'b> ToRequest<'b> for Location {
107    type Request = SendLocation;
108
109    fn to_request<C>(&'b self, chat: C) -> Self::Request
110    where
111        C: ToChatRef,
112    {
113        chat.location(self.latitude, self.longitude)
114    }
115}
116
117impl<'b> ToReplyRequest<'b> for Location {
118    type Request = SendLocation;
119
120    fn to_reply_request<M>(&'b self, message: M) -> Self::Request
121    where
122        M: ToMessageId + ToSourceChat,
123    {
124        message.location_reply(self.latitude, self.longitude)
125    }
126}