telegram_bot_async_raw/requests/
send_location.rs

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