rutebot/requests/
send_message.rs

1use std::ops::Not;
2
3use serde::Serialize;
4
5use crate::{
6    requests::{ChatId, ParseMode, ReplyMarkup, Request},
7    responses::Message,
8};
9
10/// Use this struct to send text messages. On success, the sent `Message` is returned.
11#[derive(Serialize, Debug, Clone)]
12pub struct SendMessage<'a> {
13    /// Identifier for the target chat
14    pub chat_id: ChatId<'a>,
15
16    /// Text of the message to be sent.
17    pub text: &'a str,
18
19    /// Sends the message [silently](https://telegram.org/blog/channels-2-0#silent-messages).
20    /// Users will receive a notification with no sound.
21    #[serde(skip_serializing_if = "Not::not")]
22    pub disable_notification: bool,
23
24    /// Send `ParseMode::Markdown` or `ParseMode::Html`,
25    /// if you want Telegram apps to show
26    /// [bold, italic, fixed-width text or inline URLs](https://core.telegram.org/bots/api#formatting-options) in your bot's message.
27    #[serde(skip_serializing_if = "Option::is_none")]
28    pub parse_mode: Option<ParseMode>,
29
30    /// Disables link previews for links in this message
31    #[serde(skip_serializing_if = "Not::not")]
32    pub disable_web_page_preview: bool,
33
34    /// If the message is a reply, ID of the original message
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub reply_to_message_id: Option<i64>,
37
38    /// Additional interface options.
39    #[serde(skip_serializing_if = "Option::is_none")]
40    pub reply_markup: Option<ReplyMarkup<'a>>,
41}
42
43impl<'a> Request for SendMessage<'a> {
44    type ResponseType = Message;
45
46    fn method(&self) -> &'static str {
47        "sendMessage"
48    }
49}
50
51impl<'a> SendMessage<'a> {
52    pub fn new(chat_id: impl Into<ChatId<'a>>, text: &'a str) -> Self {
53        Self {
54            chat_id: chat_id.into(),
55            text,
56            disable_notification: false,
57            parse_mode: None,
58            disable_web_page_preview: false,
59            reply_to_message_id: None,
60            reply_markup: None,
61        }
62    }
63
64    pub fn new_reply(
65        chat_id: impl Into<ChatId<'a>>,
66        text: &'a str,
67        reply_to_message_id: i64,
68    ) -> Self {
69        Self {
70            chat_id: chat_id.into(),
71            text,
72            disable_notification: false,
73            parse_mode: None,
74            disable_web_page_preview: false,
75            reply_to_message_id: Some(reply_to_message_id),
76            reply_markup: None,
77        }
78    }
79}