telegram_bot_fork_raw/requests/
send_message.rs1use std::{borrow::Cow, ops::Not};
2
3use requests::*;
4use types::*;
5
6#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize)]
8#[must_use = "requests do nothing unless sent"]
9pub struct SendMessage<'s> {
10 chat_id: ChatRef,
11 text: Cow<'s, str>,
12 #[serde(skip_serializing_if = "Option::is_none")]
13 parse_mode: Option<ParseMode>,
14 #[serde(skip_serializing_if = "Not::not")]
15 disable_web_page_preview: bool,
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<'c, 's> Request for SendMessage<'s> {
25 type Type = JsonRequestType<Self>;
26 type Response = JsonIdResponse<Message>;
27
28 fn serialize(&self) -> Result<HttpRequest, Error> {
29 Self::Type::serialize(RequestUrl::method("sendMessage"), self)
30 }
31}
32
33impl<'s> SendMessage<'s> {
34 pub fn new<C, T>(chat: C, text: T) -> Self
35 where
36 C: ToChatRef,
37 T: Into<Cow<'s, str>>,
38 {
39 SendMessage {
40 chat_id: chat.to_chat_ref(),
41 text: text.into(),
42 parse_mode: None,
43 disable_web_page_preview: false,
44 disable_notification: false,
45 reply_to_message_id: None,
46 reply_markup: None,
47 }
48 }
49
50 pub fn parse_mode(&mut self, parse_mode: ParseMode) -> &mut Self {
51 self.parse_mode = Some(parse_mode);
52 self
53 }
54
55 pub fn disable_preview(&mut self) -> &mut Self {
56 self.disable_web_page_preview = true;
57 self
58 }
59
60 pub fn disable_notification(&mut self) -> &mut Self {
61 self.disable_notification = true;
62 self
63 }
64
65 pub fn reply_to<R>(&mut self, to: R) -> &mut Self
66 where
67 R: ToMessageId,
68 {
69 self.reply_to_message_id = Some(to.to_message_id());
70 self
71 }
72
73 pub fn reply_markup<R>(&mut self, reply_markup: R) -> &mut Self
74 where
75 R: Into<ReplyMarkup>,
76 {
77 self.reply_markup = Some(reply_markup.into());
78 self
79 }
80}
81
82pub trait CanSendMessage {
84 fn text<'s, T>(&self, text: T) -> SendMessage<'s>
85 where
86 T: Into<Cow<'s, str>>;
87}
88
89impl<C> CanSendMessage for C
90where
91 C: ToChatRef,
92{
93 fn text<'s, T>(&self, text: T) -> SendMessage<'s>
94 where
95 T: Into<Cow<'s, str>>,
96 {
97 SendMessage::new(self, text)
98 }
99}
100
101pub trait CanReplySendMessage {
103 fn text_reply<'c, 's, T>(&self, text: T) -> SendMessage<'s>
104 where
105 T: Into<Cow<'s, str>>;
106}
107
108impl<M> CanReplySendMessage for M
109where
110 M: ToMessageId + ToSourceChat,
111{
112 fn text_reply<'c, 's, T>(&self, text: T) -> SendMessage<'s>
113 where
114 T: Into<Cow<'s, str>>,
115 {
116 let mut rq = self.to_source_chat().text(text);
117 rq.reply_to(self.to_message_id());
118 rq
119 }
120}