diff --git a/src/bot/api.rs b/src/bot/api.rs index ae1c26d9..6ffe460b 100644 --- a/src/bot/api.rs +++ b/src/bot/api.rs @@ -10,7 +10,7 @@ use crate::{ PinChatMessage, PromoteChatMember, RestrictChatMember, SendAudio, SendChatAction, SendContact, SendLocation, SendMediaGroup, SendMessage, SendPhoto, SendPoll, SendVenue, SendVideoNote, SendVoice, - StopMessageLiveLocation, UnbanChatMember, UnpinChatMessage, + SetChatDescription, StopMessageLiveLocation, UnbanChatMember, UnpinChatMessage, }, types::{ChatAction, ChatId, ChatPermissions, InputFile, InputMedia}, }; @@ -260,6 +260,13 @@ impl Bot { SendVoice::new(self, chat_id, voice) } + pub fn send_chat_description<C>(&self, chat_id: C) -> SetChatDescription + where + C: Into<ChatId>, + { + SetChatDescription::new(self, chat_id) + } + pub fn unban_chat_member<C, U>( &self, chat_id: C, diff --git a/src/requests/mod.rs b/src/requests/mod.rs index 4bef49d3..993200cf 100644 --- a/src/requests/mod.rs +++ b/src/requests/mod.rs @@ -35,6 +35,7 @@ pub use send_video::*; pub use send_video_note::*; pub use send_voice::*; pub use set_chat_sticker_set::*; +pub use set_chat_description::*; pub use stop_message_live_location::*; pub use unban_chat_member::*; pub use unpin_chat_message::*; @@ -74,6 +75,7 @@ mod send_video; mod send_video_note; mod send_voice; mod set_chat_sticker_set; +mod set_chat_description; mod stop_message_live_location; mod unban_chat_member; mod unpin_chat_message; diff --git a/src/requests/set_chat_description.rs b/src/requests/set_chat_description.rs new file mode 100644 index 00000000..8a087405 --- /dev/null +++ b/src/requests/set_chat_description.rs @@ -0,0 +1,96 @@ +use async_trait::async_trait; + +use crate::{ + network, + requests::{Request, ResponseResult}, + types::{True, ChatId}, + bot::Bot, +}; + +#[derive(Debug, Clone, Serialize)] +pub struct SetChatDescription<'a> { + #[serde(skip_serializing)] + bot: &'a Bot, + + chat_id: ChatId, + #[serde(skip_serializing_if = "Option::is_none")] + description: Option<String>, +} + +#[async_trait] +impl Request for SetChatDescription<'_> { + type Output = True; + + async fn send_boxed(self) -> ResponseResult<Self::Output> { + self.send().await + } +} + +impl SetChatDescription<'_> { + pub async fn send(self) -> ResponseResult<True> { + network::request_json( + &self.bot.client(), + &self.bot.token(), + "setChatDescription", + &self + ).await + } +} + +impl<'a> SetChatDescription<'a> { + pub(crate) fn new<C>( + bot: &'a Bot, + chat_id: C, + ) -> Self + where C: Into<ChatId> + { + Self { + bot, + chat_id: chat_id.into(), + description: None + } + } + + pub fn chat_id<T>(mut self, chat_id: T) -> Self + where T: Into<ChatId> + { + self.chat_id = chat_id.into(); + self + } + + pub fn description<T>(mut self, description: T) -> Self + where T: Into<String> + { + self.description = Some(description.into()); + self + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn serialize_new() { + let bot = Bot::new("token"); + let chat_id = 123; + let method = SetChatDescription::new(&bot, chat_id); + + let expected = r#"{"chat_id":123}"#; + let actual = serde_json::to_string::<SetChatDescription>(&method).unwrap(); + assert_eq!(actual, expected); + } + + #[test] + fn serialize_description() { + let bot = Bot::new("token"); + let chat_id = 123; + let description = "description"; + let method = SetChatDescription::new(&bot, chat_id) + .description(description); + + let expected = r#"{"chat_id":123,"description":"description"}"#; + let actual = serde_json::to_string::<SetChatDescription>(&method).unwrap(); + assert_eq!(actual, expected); + } +}