From 0c6246295b69f5d424320dcbb2c3024624bc5024 Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Wed, 29 Jul 2020 17:08:26 +0600 Subject: [PATCH] Add SetMyCommands + Bot::set_my_commands --- src/bot/api.rs | 22 ++++++++++--- src/requests/all/mod.rs | 2 ++ src/requests/all/set_my_commands.rs | 50 +++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 src/requests/all/set_my_commands.rs diff --git a/src/bot/api.rs b/src/bot/api.rs index 4c8180b9..32b568cd 100644 --- a/src/bot/api.rs +++ b/src/bot/api.rs @@ -12,12 +12,12 @@ use crate::{ SendInvoice, SendLocation, SendMediaGroup, SendMessage, SendPhoto, SendPoll, SendSticker, SendVenue, SendVideo, SendVideoNote, SendVoice, SetChatAdministratorCustomTitle, SetChatDescription, SetChatPermissions, SetChatPhoto, SetChatStickerSet, SetChatTitle, - SetGameScore, SetStickerPositionInSet, SetWebhook, StopMessageLiveLocation, StopPoll, - UnbanChatMember, UnpinChatMessage, UploadStickerFile, + SetGameScore, SetMyCommands, SetStickerPositionInSet, SetWebhook, StopMessageLiveLocation, + StopPoll, UnbanChatMember, UnpinChatMessage, UploadStickerFile, }, types::{ - ChatId, ChatOrInlineMessage, ChatPermissions, InlineQueryResult, InputFile, InputMedia, - LabeledPrice, + BotCommand, ChatId, ChatOrInlineMessage, ChatPermissions, InlineQueryResult, InputFile, + InputMedia, LabeledPrice, }, Bot, }; @@ -1523,4 +1523,18 @@ impl Bot { pub fn get_my_commands(&self) -> GetMyCommands { GetMyCommands::new(self.clone()) } + + /// Use this method to change the list of the bot's commands. + /// + /// [The official docs](https://core.telegram.org/bots/api#setmycommands). + /// + /// # Params + /// - `commands`: A JSON-serialized list of bot commands to be set as the + /// list of the bot's commands. At most 100 commands can be specified. + pub fn set_my_commands(&self, commands: C) -> SetMyCommands + where + C: Into>, + { + SetMyCommands::new(self.clone(), commands) + } } diff --git a/src/requests/all/mod.rs b/src/requests/all/mod.rs index 038531a3..f3d01763 100644 --- a/src/requests/all/mod.rs +++ b/src/requests/all/mod.rs @@ -58,6 +58,7 @@ mod set_chat_photo; mod set_chat_sticker_set; mod set_chat_title; mod set_game_score; +mod set_my_commands; mod set_sticker_position_in_set; mod set_webhook; mod stop_message_live_location; @@ -126,6 +127,7 @@ pub use set_chat_photo::*; pub use set_chat_sticker_set::*; pub use set_chat_title::*; pub use set_game_score::*; +pub use set_my_commands::*; pub use set_sticker_position_in_set::*; pub use set_webhook::*; pub use std::pin::Pin; diff --git a/src/requests/all/set_my_commands.rs b/src/requests/all/set_my_commands.rs new file mode 100644 index 00000000..831f9705 --- /dev/null +++ b/src/requests/all/set_my_commands.rs @@ -0,0 +1,50 @@ +use serde::Serialize; + +use crate::{ + net, + requests::{Request, ResponseResult}, + types::{BotCommand, True}, + Bot, +}; + +/// Use this method to change the list of the bot's commands. +/// +/// [The official docs](https://core.telegram.org/bots/api#setmycommands). +#[serde_with_macros::skip_serializing_none] +#[derive(Debug, Clone, Serialize)] +pub struct SetMyCommands { + #[serde(skip_serializing)] + bot: Bot, + + commands: Vec, +} + +#[async_trait::async_trait] +impl Request for SetMyCommands { + type Output = True; + + async fn send(&self) -> ResponseResult { + net::request_json(self.bot.client(), self.bot.token(), "setMyCommands", &self).await + } +} + +impl SetMyCommands { + pub(crate) fn new(bot: Bot, commands: C) -> Self + where + C: Into>, + { + Self { bot, commands: commands.into() } + } + + /// A JSON-serialized list of bot commands to be set as the list of the + /// bot's commands. + /// + /// At most 100 commands can be specified. + pub fn commands(mut self, commands: C) -> Self + where + C: Into>, + { + self.commands = commands.into(); + self + } +}