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