added method setChatTitle

This commit is contained in:
P0lunin 2019-10-17 19:48:48 +03:00
parent ceaf36b101
commit 351229a6c0
3 changed files with 101 additions and 1 deletions

View file

@ -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<C, T>(
&self,
chat_id: C,
title: T,
) -> SetChatTitle
where
C: Into<ChatId>,
T: Into<String>,
{
SetChatTitle::new(self, chat_id, title)
}
}

View file

@ -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;

View file

@ -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::Output> {
self.send().await
}
}
impl SetChatTitle<'_> {
async fn send(self) -> ResponseResult<True> {
network::request_json(
&self.bot.client(),
&self.bot.token(),
"SetChatTitle",
&self
).await
}
}
impl<'a> SetChatTitle<'a> {
pub(crate) fn new<C, T>(
bot: &'a Bot,
chat_id: C,
title: T
) -> Self
where
C: Into<ChatId>,
T: Into<String>,
{
Self {
bot,
chat_id: chat_id.into(),
title: title.into(),
}
}
pub fn chat_id<C>(mut self, chat_id: C) -> Self
where
C: Into<ChatId>,
{
self.chat_id = chat_id.into();
self
}
pub fn title<C>(mut self, title: C) -> Self
where
C: Into<String>,
{
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::<SetChatTitle>(&method).unwrap();
assert_eq!(actual, expected);
}
}