tgbot/types/definitions/
menu.rs

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