From 351229a6c012b853a8b430aea92e182b7b934d58 Mon Sep 17 00:00:00 2001 From: P0lunin Date: Thu, 17 Oct 2019 19:48:48 +0300 Subject: [PATCH] added method setChatTitle --- src/bot/api.rs | 14 +++++- src/requests/mod.rs | 2 + src/requests/set_chat_title.rs | 86 ++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 src/requests/set_chat_title.rs diff --git a/src/bot/api.rs b/src/bot/api.rs index 924e7abc..a52fa3f6 100644 --- a/src/bot/api.rs +++ b/src/bot/api.rs @@ -9,7 +9,7 @@ use crate::{ SendLocation, SendMediaGroup, SendMessage, SendPhoto, SendPoll, SendVenue, SendVideo, SendVideoNote, SendVoice, SetChatDescription, SetChatStickerSet, StopMessageLiveLocation, UnbanChatMember, - UnpinChatMessage, + UnpinChatMessage, SetChatTitle }, types::{ChatAction, ChatId, ChatPermissions, InputFile, InputMedia}, }; @@ -356,4 +356,16 @@ impl Bot { { SendAnimation::new(self, chat_id, animation) } + + pub fn set_chat_title( + &self, + chat_id: C, + title: T, + ) -> SetChatTitle + where + C: Into, + T: Into, + { + SetChatTitle::new(self, chat_id, title) + } } diff --git a/src/requests/mod.rs b/src/requests/mod.rs index d2ee64f9..496d71ef 100644 --- a/src/requests/mod.rs +++ b/src/requests/mod.rs @@ -36,6 +36,7 @@ pub use send_video_note::*; pub use send_voice::*; pub use set_chat_description::*; pub use set_chat_sticker_set::*; +pub use set_chat_title::*; pub use stop_message_live_location::*; pub use unban_chat_member::*; pub use unpin_chat_message::*; @@ -76,6 +77,7 @@ mod send_video_note; mod send_voice; mod set_chat_description; mod set_chat_sticker_set; +mod set_chat_title; mod stop_message_live_location; mod unban_chat_member; mod unpin_chat_message; diff --git a/src/requests/set_chat_title.rs b/src/requests/set_chat_title.rs new file mode 100644 index 00000000..bdf21dc9 --- /dev/null +++ b/src/requests/set_chat_title.rs @@ -0,0 +1,86 @@ +use async_trait::async_trait; + +use crate::bot::Bot; +use crate::types::{ChatId, True}; +use crate::requests::{Request, ResponseResult}; +use crate::network; + +#[derive(Debug, Clone, Serialize)] +pub struct SetChatTitle<'a> { + #[serde(skip_serializing)] + bot: &'a Bot, + + chat_id: ChatId, + title: String, +} + +#[async_trait] +impl Request for SetChatTitle<'_> { + type Output = True; + + async fn send_boxed(self) -> ResponseResult { + self.send().await + } +} + +impl SetChatTitle<'_> { + async fn send(self) -> ResponseResult { + network::request_json( + &self.bot.client(), + &self.bot.token(), + "SetChatTitle", + &self + ).await + } +} + +impl<'a> SetChatTitle<'a> { + pub(crate) fn new( + bot: &'a Bot, + chat_id: C, + title: T + ) -> Self + where + C: Into, + T: Into, + { + Self { + bot, + chat_id: chat_id.into(), + title: title.into(), + } + } + + pub fn chat_id(mut self, chat_id: C) -> Self + where + C: Into, + { + self.chat_id = chat_id.into(); + self + } + + pub fn title(mut self, title: C) -> Self + where + C: Into, + { + self.title = title.into(); + self + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn serialize() { + let bot = Bot::new("token"); + let chat_id = 123; + let title = "title"; + let method = SetChatTitle::new(&bot, chat_id, title); + + let expected = r#"{"chat_id":123,"title":"title"}"#; + let actual = serde_json::to_string::(&method).unwrap(); + assert_eq!(actual, expected); + } +}