telegram_bot_raw/requests/
send_message.rs

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