tgbot/types/menu/
mod.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{
4    api::{Method, Payload},
5    types::{Integer, WebAppInfo},
6};
7
8#[cfg(test)]
9mod tests;
10
11/// Represents a menu button of the bot in a private chat.
12///
13/// If a menu button other than default is set for a private chat, then it is applied in the chat.
14/// Otherwise the default menu button is applied.
15/// By default, the menu button opens the list of bot commands.
16#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
17#[serde(from = "MenuButtonRaw", into = "MenuButtonRaw")]
18pub enum MenuButton {
19    /// Opens the list of bot commands.
20    Commands,
21    /// Default behaviour.
22    Default,
23    /// Launches a web app.
24    WebApp(MenuButtonWebApp),
25}
26
27/// Represents a menu button, which launches a Web App.
28#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
29pub struct MenuButtonWebApp {
30    /// Text on the button.
31    pub text: String,
32    /// Description of the Web App that will be launched when the user presses the button.
33    ///
34    /// The Web App will be able to send an arbitrary message on behalf
35    /// of the user using the method `answerWebAppQuery`.
36    /// Alternatively, a t.me link to a Web App of the bot can be specified in the object instead of the Web App's URL,
37    /// in which case the Web App will be opened as if the user pressed the link.
38    pub web_app: WebAppInfo,
39}
40
41impl MenuButtonWebApp {
42    /// Creates a new `MenuButtonWebApp`.
43    ///
44    /// # Arguments
45    ///
46    /// * `text` - Text of the button.
47    /// * `web_app` - Web app to launch.
48    pub fn new<T>(text: T, web_app: WebAppInfo) -> Self
49    where
50        T: Into<String>,
51    {
52        Self {
53            text: text.into(),
54            web_app,
55        }
56    }
57}
58
59#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
60#[serde(rename_all = "snake_case", tag = "type")]
61enum MenuButtonRaw {
62    Commands {},
63    Default {},
64    WebApp(MenuButtonWebApp),
65}
66
67impl From<MenuButtonRaw> for MenuButton {
68    fn from(value: MenuButtonRaw) -> Self {
69        match value {
70            MenuButtonRaw::Commands {} => Self::Commands,
71            MenuButtonRaw::Default {} => Self::Default,
72            MenuButtonRaw::WebApp(value) => Self::WebApp(value),
73        }
74    }
75}
76
77impl From<MenuButton> for MenuButtonRaw {
78    fn from(value: MenuButton) -> Self {
79        match value {
80            MenuButton::Commands => Self::Commands {},
81            MenuButton::Default => Self::Default {},
82            MenuButton::WebApp(value) => Self::WebApp(value),
83        }
84    }
85}
86
87/// Returns the current value of the bot menu button in a private chat, or the default menu button.
88#[serde_with::skip_serializing_none]
89#[derive(Clone, Debug, Default, Serialize)]
90pub struct GetChatMenuButton {
91    chat_id: Option<Integer>,
92}
93
94impl GetChatMenuButton {
95    /// Sets a new chat ID.
96    ///
97    /// # Arguments
98    ///
99    /// * `value` - Unique identifier of the target private chat.
100    ///
101    /// If not specified, default bot menu button will be returned.
102    pub fn with_chat_id(mut self, value: Integer) -> Self {
103        self.chat_id = Some(value);
104        self
105    }
106}
107
108impl Method for GetChatMenuButton {
109    type Response = MenuButton;
110
111    fn into_payload(self) -> Payload {
112        Payload::json("getChatMenuButton", self)
113    }
114}
115
116/// Changes a button of a menu of a bot in a private chat, or a default menu button.
117#[serde_with::skip_serializing_none]
118#[derive(Clone, Debug, Default, Serialize)]
119pub struct SetChatMenuButton {
120    chat_id: Option<Integer>,
121    menu_button: Option<MenuButton>,
122}
123
124impl SetChatMenuButton {
125    /// Sets a new chat ID.
126    ///
127    /// # Arguments
128    ///
129    /// * `value` - Unique identifier of the target private chat.
130    ///
131    /// If not specified, default bot menu button will be changed.
132    pub fn chat_id(mut self, chat_id: Integer) -> Self {
133        self.chat_id = Some(chat_id);
134        self
135    }
136
137    /// Sets a new menu button.
138    ///
139    /// # Arguments
140    ///
141    /// * `value` - An object for the new bot menu button; default - [`MenuButton::Default`].
142    pub fn menu_button(mut self, value: MenuButton) -> Self {
143        self.menu_button = Some(value);
144        self
145    }
146}
147
148impl Method for SetChatMenuButton {
149    type Response = bool;
150
151    fn into_payload(self) -> Payload {
152        Payload::json("setChatMenuButton", self)
153    }
154}