telbot_types/bot.rs
1use crate::chat::ChatId;
2use crate::user::User;
3use crate::{JsonMethod, TelegramMethod};
4use serde::{Deserialize, Serialize};
5
6/// This object represents a bot command.
7#[derive(Serialize, Deserialize, Clone)]
8pub struct BotCommand {
9 /// Text of the command, 1-32 characters.
10 /// Can contain only lowercase English letters, digits and underscores.
11 pub command: String,
12 /// Description of the command, 3-256 characters.
13 pub description: String,
14}
15
16/// This object represents the scope to which bot commands are applied.
17///
18/// Currently, the following 7 scopes are supported:
19/// - BotCommandScopeDefault
20/// - BotCommandScopeAllPrivateChats
21/// - BotCommandScopeAllGroupChats
22/// - BotCommandScopeAllChatAdministrators
23/// - BotCommandScopeChat
24/// - BotCommandScopeChatAdministrators
25/// - BotCommandScopeChatMember
26///
27/// # Determining list of commands
28///
29/// The following algorithm is used to determine the list of commands for a particular user viewing the bot menu. The first list of commands which is set is returned:
30///
31/// ## Commands in the chat with the bot
32///
33/// - botCommandScopeChat + language_code
34/// - botCommandScopeChat
35/// - botCommandScopeAllPrivateChats + language_code
36/// - botCommandScopeAllPrivateChats
37/// - botCommandScopeDefault + language_code
38/// - botCommandScopeDefault
39///
40/// ## Commands in group and supergroup chats
41///
42/// - botCommandScopeChatMember + language_code
43/// - botCommandScopeChatMember
44/// - botCommandScopeChatAdministrators + language_code (admins only)
45/// - botCommandScopeChatAdministrators (admins only)
46/// - botCommandScopeChat + language_code
47/// - botCommandScopeChat
48/// - botCommandScopeAllChatAdministrators + language_code (admins only)
49/// - botCommandScopeAllChatAdministrators (admins only)
50/// - botCommandScopeAllGroupChats + language_code
51/// - botCommandScopeAllGroupChats
52/// - botCommandScopeDefault + language_code
53/// - botCommandScopeDefault
54#[derive(Clone, Serialize)]
55pub enum BotCommandScope {
56 /// Default commands are used if no commands with a narrower scope are specified for the user.
57 Default,
58 /// Covers all private chats.
59 AllPrivateChats,
60 /// Covers all group and supergroup chats.
61 AllGroupChats,
62 /// Cvoers all group and supergroup chat administrators.
63 AllChatAdministrators,
64 /// Covers a specific chat.
65 Chat {
66 /// Unique identifier for the target chat or username of the target supergroup (in the format `@supergroupusername`)
67 chat_id: ChatId,
68 },
69 /// Covers all administrators of a specific group or supergroup chat.
70 ChatAdministrators {
71 /// Unique identifier for the target chat or username of the target supergroup (in the format `@supergroupusername`)
72 chat_id: ChatId,
73 },
74 /// Covers a specific member of a group or supergroup chat.
75 ChatMember {
76 /// Unique identifier for the target chat or username of the target supergroup (in the format `@supergroupusername`)
77 chat_id: ChatId,
78 /// Unique identifier of the target user
79 user_id: i64,
80 },
81}
82
83/// A simple method for testing your bot's auth token. Requires no parameters.
84///
85/// Returns basic information about the bot in form of a User object.
86#[derive(Clone, Serialize)]
87pub struct GetMe;
88
89impl TelegramMethod for GetMe {
90 type Response = User;
91
92 fn name() -> &'static str {
93 "getMe"
94 }
95}
96
97impl JsonMethod for GetMe {}
98
99/// Use this method to log out from the cloud Bot API server before launching the bot locally.
100///
101/// You **must** log out the bot before running it locally, otherwise there is no guarantee that the bot will receive updates.
102/// After a successful call, you can immediately log in on a local server, but will not be able to log in back to the cloud Bot API server for 10 minutes.
103/// Returns *True* on success. Requires no parameters.
104#[derive(Clone, Serialize)]
105pub struct LogOut;
106
107impl TelegramMethod for LogOut {
108 type Response = bool;
109
110 fn name() -> &'static str {
111 "logOut"
112 }
113}
114
115impl JsonMethod for LogOut {}
116
117/// Use this method to close the bot instance before moving it from one local server to another.
118/// You need to delete the webhook before calling this method to ensure that the bot isn't launched again after server restart.
119/// The method will return error 429 in the first 10 minutes after the bot is launched.
120/// Returns True on success. Requires no parameters.
121#[derive(Clone, Serialize)]
122pub struct Close;
123
124impl TelegramMethod for Close {
125 type Response = bool;
126
127 fn name() -> &'static str {
128 "close"
129 }
130}
131
132impl JsonMethod for Close {}
133
134/// Use this method to change the list of the bot's commands. See https://core.telegram.org/bots#commands for more details about bot commands.
135/// Returns _True_ on success.
136#[derive(Clone, Serialize)]
137pub struct SetMyCommands {
138 /// A JSON-serialized list of bot commands to be set as the list of the bot's commands.
139 /// At most 100 commands can be specified.
140 pub commands: Vec<BotCommand>,
141 /// A JSON-serialized object, describing scope of users for which the commands are relevant.
142 /// Defaults to [BotCommandScopeDefault](https://core.telegram.org/bots/api#botcommandscopedefault).
143 #[serde(skip_serializing_if = "Option::is_none")]
144 pub scope: Option<BotCommandScope>,
145 /// A two-letter ISO 639-1 language code.
146 /// If empty, commands will be applied to all users from the given scope,
147 /// for whose language there are no dedicated commands
148 #[serde(skip_serializing_if = "Option::is_none")]
149 pub language_code: Option<String>,
150}
151
152impl SetMyCommands {
153 /// Create a new setMyCommands request
154 pub fn new(commands: impl Into<Vec<BotCommand>>) -> Self {
155 Self {
156 commands: commands.into(),
157 scope: None,
158 language_code: None,
159 }
160 }
161
162 /// Set command scope
163 pub fn with_scope(self, scope: BotCommandScope) -> Self {
164 Self {
165 scope: Some(scope),
166 ..self
167 }
168 }
169
170 /// Set language code
171 pub fn with_language_code(self, language_code: impl Into<String>) -> Self {
172 Self {
173 language_code: Some(language_code.into()),
174 ..self
175 }
176 }
177}
178
179impl TelegramMethod for SetMyCommands {
180 type Response = bool;
181
182 fn name() -> &'static str {
183 "setMyCommands"
184 }
185}
186
187impl JsonMethod for SetMyCommands {}
188
189/// Use this method to delete the list of the bot's commands for the given scope and user language.
190/// After deletion, higher level commands will be shown to affected users.
191/// Returns _True_ on success.
192#[derive(Clone, Serialize)]
193pub struct DeleteMyCommands {
194 /// A JSON-serialized object, describing scope of users for which the commands are relevant.
195 /// Defaults to [BotCommandScopeDefault](https://core.telegram.org/bots/api#botcommandscopedefault).
196 #[serde(skip_serializing_if = "Option::is_none")]
197 pub scope: Option<BotCommandScope>,
198 /// A two-letter ISO 639-1 language code.
199 /// If empty, commands will be applied to all users from the given scope,
200 /// for whose language there are no dedicated commands
201 #[serde(skip_serializing_if = "Option::is_none")]
202 pub language_code: Option<String>,
203}
204
205impl DeleteMyCommands {
206 /// Create a new deleteMyCommands request
207 pub fn new() -> Self {
208 Self {
209 scope: None,
210 language_code: None,
211 }
212 }
213 /// Set command scope
214 pub fn with_scope(self, scope: BotCommandScope) -> Self {
215 Self {
216 scope: Some(scope),
217 ..self
218 }
219 }
220
221 /// Set language code
222 pub fn with_language_code(self, language_code: impl Into<String>) -> Self {
223 Self {
224 language_code: Some(language_code.into()),
225 ..self
226 }
227 }
228}
229
230impl TelegramMethod for DeleteMyCommands {
231 type Response = bool;
232
233 fn name() -> &'static str {
234 "deleteMyCommands"
235 }
236}
237
238/// Use this method to get the current list of the bot's commands for the given scope and user language.
239/// Returns Array of [BotCommand](https://core.telegram.org/bots/api#botcommand) on success.
240/// If commands aren't set, an empty list is returned.
241#[derive(Clone, Serialize)]
242pub struct GetMyCommands {
243 /// A JSON-serialized object, describing scope of users for which the commands are relevant.
244 /// Defaults to [BotCommandScopeDefault](https://core.telegram.org/bots/api#botcommandscopedefault).
245 #[serde(skip_serializing_if = "Option::is_none")]
246 pub scope: Option<BotCommandScope>,
247 /// A two-letter ISO 639-1 language code.
248 /// If empty, commands will be applied to all users from the given scope,
249 /// for whose language there are no dedicated commands
250 #[serde(skip_serializing_if = "Option::is_none")]
251 pub language_code: Option<String>,
252}
253
254impl GetMyCommands {
255 /// Create a new getMyCommands request
256 pub fn new() -> Self {
257 Self {
258 scope: None,
259 language_code: None,
260 }
261 }
262 /// Set command scope
263 pub fn with_scope(self, scope: BotCommandScope) -> Self {
264 Self {
265 scope: Some(scope),
266 ..self
267 }
268 }
269
270 /// Set language code
271 pub fn with_language_code(self, language_code: impl Into<String>) -> Self {
272 Self {
273 language_code: Some(language_code.into()),
274 ..self
275 }
276 }
277}
278
279impl TelegramMethod for GetMyCommands {
280 type Response = Vec<BotCommand>;
281
282 fn name() -> &'static str {
283 "getMyCommands"
284 }
285}