Skip to main content

tele/types/
command.rs

1use serde::{Deserialize, Serialize};
2
3use crate::types::common::{ChatId, UserId};
4use crate::{Error, Result};
5
6/// Telegram bot command object.
7#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
8#[non_exhaustive]
9pub struct BotCommand {
10    pub command: String,
11    pub description: String,
12}
13
14impl BotCommand {
15    pub fn new(command: impl Into<String>, description: impl Into<String>) -> Result<Self> {
16        let command = command.into();
17        let description = description.into();
18
19        if command.trim().is_empty() {
20            return Err(Error::InvalidRequest {
21                reason: "command cannot be empty".to_owned(),
22            });
23        }
24
25        if description.trim().is_empty() {
26            return Err(Error::InvalidRequest {
27                reason: "command description cannot be empty".to_owned(),
28            });
29        }
30
31        Ok(Self {
32            command,
33            description,
34        })
35    }
36}
37
38/// Scope for bot command settings.
39#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
40#[serde(tag = "type", rename_all = "snake_case")]
41pub enum BotCommandScope {
42    Default,
43    AllPrivateChats,
44    AllGroupChats,
45    AllChatAdministrators,
46    Chat { chat_id: ChatId },
47    ChatAdministrators { chat_id: ChatId },
48    ChatMember { chat_id: ChatId, user_id: UserId },
49}
50
51/// `setMyCommands` request.
52#[derive(Clone, Debug, Serialize)]
53pub struct SetMyCommandsRequest {
54    pub commands: Vec<BotCommand>,
55    #[serde(default, skip_serializing_if = "Option::is_none")]
56    pub scope: Option<BotCommandScope>,
57    #[serde(default, skip_serializing_if = "Option::is_none")]
58    pub language_code: Option<String>,
59}
60
61impl SetMyCommandsRequest {
62    pub fn new(commands: Vec<BotCommand>) -> Result<Self> {
63        if commands.is_empty() {
64            return Err(Error::InvalidRequest {
65                reason: "setMyCommands requires at least one command".to_owned(),
66            });
67        }
68
69        Ok(Self {
70            commands,
71            scope: None,
72            language_code: None,
73        })
74    }
75
76    pub fn scope(mut self, scope: BotCommandScope) -> Self {
77        self.scope = Some(scope);
78        self
79    }
80
81    pub fn language_code(mut self, language_code: impl Into<String>) -> Result<Self> {
82        let language_code = language_code.into();
83        if language_code.trim().is_empty() {
84            return Err(Error::InvalidRequest {
85                reason: "language_code cannot be empty".to_owned(),
86            });
87        }
88        self.language_code = Some(language_code);
89        Ok(self)
90    }
91}
92
93/// `getMyCommands` request.
94#[derive(Clone, Debug, Default, Serialize)]
95pub struct GetMyCommandsRequest {
96    #[serde(default, skip_serializing_if = "Option::is_none")]
97    pub scope: Option<BotCommandScope>,
98    #[serde(default, skip_serializing_if = "Option::is_none")]
99    pub language_code: Option<String>,
100}
101
102/// `deleteMyCommands` request.
103#[derive(Clone, Debug, Default, Serialize)]
104pub struct DeleteMyCommandsRequest {
105    #[serde(default, skip_serializing_if = "Option::is_none")]
106    pub scope: Option<BotCommandScope>,
107    #[serde(default, skip_serializing_if = "Option::is_none")]
108    pub language_code: Option<String>,
109}
110
111#[derive(Clone, Debug, Default, Serialize)]
112pub struct SetMyNameRequest {
113    #[serde(default, skip_serializing_if = "Option::is_none")]
114    pub name: Option<String>,
115    #[serde(default, skip_serializing_if = "Option::is_none")]
116    pub language_code: Option<String>,
117}
118
119#[derive(Clone, Debug, Default, Serialize)]
120pub struct GetMyNameRequest {
121    #[serde(default, skip_serializing_if = "Option::is_none")]
122    pub language_code: Option<String>,
123}
124
125#[derive(Clone, Debug, Default, Serialize)]
126pub struct SetMyDescriptionRequest {
127    #[serde(default, skip_serializing_if = "Option::is_none")]
128    pub description: Option<String>,
129    #[serde(default, skip_serializing_if = "Option::is_none")]
130    pub language_code: Option<String>,
131}
132
133#[derive(Clone, Debug, Default, Serialize)]
134pub struct GetMyDescriptionRequest {
135    #[serde(default, skip_serializing_if = "Option::is_none")]
136    pub language_code: Option<String>,
137}
138
139#[derive(Clone, Debug, Default, Serialize)]
140pub struct SetMyShortDescriptionRequest {
141    #[serde(default, skip_serializing_if = "Option::is_none")]
142    pub short_description: Option<String>,
143    #[serde(default, skip_serializing_if = "Option::is_none")]
144    pub language_code: Option<String>,
145}
146
147#[derive(Clone, Debug, Default, Serialize)]
148pub struct GetMyShortDescriptionRequest {
149    #[serde(default, skip_serializing_if = "Option::is_none")]
150    pub language_code: Option<String>,
151}
152
153#[derive(Clone, Debug, Deserialize, Serialize)]
154pub struct BotName {
155    pub name: String,
156}
157
158#[derive(Clone, Debug, Deserialize, Serialize)]
159pub struct BotDescription {
160    pub description: String,
161}
162
163#[derive(Clone, Debug, Deserialize, Serialize)]
164pub struct BotShortDescription {
165    pub short_description: String,
166}