From 1b52a8cdd3da2b039f66309e3e6b0151ad70480f Mon Sep 17 00:00:00 2001 From: Waffle Date: Sat, 21 Sep 2019 00:20:15 +0300 Subject: [PATCH] Split bot into different files --- src/bot/api.rs | 117 ++++++++++++++++ src/bot/download.rs | 63 +++++++++ src/bot/mod.rs | 185 +------------------------- src/network/mod.rs | 4 +- src/requests/answer_shipping_query.rs | 4 +- src/requests/get_file.rs | 4 +- src/types/user.rs | 2 +- 7 files changed, 191 insertions(+), 188 deletions(-) create mode 100644 src/bot/api.rs create mode 100644 src/bot/download.rs diff --git a/src/bot/api.rs b/src/bot/api.rs new file mode 100644 index 00000000..e9af235e --- /dev/null +++ b/src/bot/api.rs @@ -0,0 +1,117 @@ +use crate::bot::Bot; +use crate::requests::edit_message_live_location::EditMessageLiveLocation; +use crate::requests::forward_message::ForwardMessage; +use crate::requests::get_file::GetFile; +use crate::requests::get_me::GetMe; +use crate::requests::send_audio::SendAudio; +use crate::requests::send_location::SendLocation; +use crate::requests::send_media_group::SendMediaGroup; +use crate::requests::send_message::SendMessage; +use crate::requests::send_photo::SendPhoto; +use crate::requests::stop_message_live_location::StopMessageLiveLocation; +use crate::requests::ChatId; +use crate::types::{InputFile, InputMedia}; + +/// Telegram functions +impl Bot { + pub fn get_me(&self) -> GetMe { + GetMe::new(self.ctx()) + } + + pub fn send_message(&self, chat_id: C, text: T) -> SendMessage + where + C: Into, + T: Into, + { + SendMessage::new(self.ctx(), chat_id.into(), text.into()) + } + + pub fn edit_message_live_location( + &self, + latitude: Lt, + longitude: Lg, + ) -> EditMessageLiveLocation + where + Lt: Into, + Lg: Into, + { + EditMessageLiveLocation::new( + self.ctx(), + latitude.into(), + longitude.into(), + ) + } + + pub fn forward_message( + &self, + chat_id: C, + from_chat_id: F, + message_id: M, + ) -> ForwardMessage + where + C: Into, + F: Into, + M: Into, + { + ForwardMessage::new( + self.ctx(), + chat_id.into(), + from_chat_id.into(), + message_id.into(), + ) + } + + pub fn send_audio(&self, chat_id: C, audio: A) -> SendAudio + where + C: Into, + A: Into, + { + SendAudio::new(self.ctx(), chat_id.into(), audio.into()) + } + + pub fn send_location( + &self, + chat_id: C, + latitude: Lt, + longitude: Lg, + ) -> SendLocation + where + C: Into, + Lt: Into, + Lg: Into, + { + SendLocation::new( + self.ctx(), + chat_id.into(), + latitude.into(), + longitude.into(), + ) + } + + pub fn send_media_group(&self, chat_id: C, media: M) -> SendMediaGroup + where + C: Into, + M: Into>, + { + SendMediaGroup::new(self.ctx(), chat_id.into(), media.into()) + } + + pub fn send_photo(&self, chat_id: C, photo: P) -> SendPhoto + where + C: Into, + P: Into, + { + SendPhoto::new(self.ctx(), chat_id.into(), photo.into()) + } + + pub fn stop_message_live_location(&self) -> StopMessageLiveLocation { + StopMessageLiveLocation::new(self.ctx()) + } + + pub fn get_file(&self, file_id: F) -> GetFile + where + F: Into, + { + GetFile::new(self.ctx(), file_id.into()) + } +} diff --git a/src/bot/download.rs b/src/bot/download.rs new file mode 100644 index 00000000..08bb988d --- /dev/null +++ b/src/bot/download.rs @@ -0,0 +1,63 @@ +use super::Bot; +use crate::network::{download_file, download_file_stream}; +use crate::DownloadError; +use reqwest::r#async::Chunk; +use tokio::prelude::AsyncWrite; +use tokio::stream::Stream; + +impl Bot { + /// Download file from telegram into `destination`. + /// `path` can be obtained from [`get_file`] method. + /// + /// For downloading as Stream of Chunks see [`download_file_stream`]. + /// + /// ## Examples + /// + /// ```no_run + /// use async_telegram_bot::{ + /// bot::Bot, requests::Request, types::File as TgFile, + /// }; + /// use tokio::fs::File; + /// # use async_telegram_bot::RequestError; + /// + /// # async fn run() -> Result<(), Box> { + /// let bot = Bot::new("TOKEN"); + /// let mut file = File::create("/home/waffle/Pictures/test.png").await?; + /// + /// let TgFile { file_path, .. } = bot.get_file("*file_id*").send().await?; + /// bot.download_file(&file_path, &mut file).await?; + /// # Ok(()) } + /// ``` + /// + /// [`get_file`]: crate::bot::Bot::get_file + /// [`download_file_stream`]: crate::bot::Bot::download_file_stream + pub async fn download_file( + &self, + path: &str, + destination: &mut D, + ) -> Result<(), DownloadError> + where + D: AsyncWrite + Unpin, + { + download_file(&self.client, &self.token, path, destination).await + } + + /// Download file from telegram. + /// + /// `path` can be obtained from [`get_file`] method. + /// + /// For downloading into [`AsyncWrite`] (e.g. [`tokio::fs::File`]) + /// see [`download_file`]. + /// + /// [`get_file`]: crate::bot::Bot::get_file + /// [`AsyncWrite`]: tokio::io::AsyncWrite + /// [`tokio::fs::File`]: tokio::fs::File + /// [`download_file`]: crate::bot::Bot::download_file + pub async fn download_file_stream( + &self, + path: &str, + ) -> Result>, reqwest::Error> + { + download_file_stream(&self.client, &self.token, path).await + } +} diff --git a/src/bot/mod.rs b/src/bot/mod.rs index 746c6503..c346a1f1 100644 --- a/src/bot/mod.rs +++ b/src/bot/mod.rs @@ -1,28 +1,16 @@ +mod api; +mod download; + use reqwest::r#async::Client; -use crate::network::{download_file, download_file_stream}; -use crate::requests::get_file::GetFile; -use crate::{ - requests::{ - edit_message_live_location::EditMessageLiveLocation, - forward_message::ForwardMessage, get_me::GetMe, send_audio::SendAudio, - send_location::SendLocation, send_media_group::SendMediaGroup, - send_message::SendMessage, send_photo::SendPhoto, - stop_message_live_location::StopMessageLiveLocation, ChatId, - RequestContext, - }, - types::{InputFile, InputMedia}, -}; -use crate::DownloadError; -use reqwest::r#async::Chunk; -use tokio::io::AsyncWrite; -use tokio::stream::Stream; +use crate::requests::RequestContext; pub struct Bot { token: String, client: Client, } +/// Constructors impl Bot { pub fn new(token: &str) -> Self { Bot { @@ -37,7 +25,9 @@ impl Bot { client, } } +} +impl Bot { fn ctx(&self) -> RequestContext { RequestContext { token: &self.token, @@ -45,164 +35,3 @@ impl Bot { } } } - -/// Telegram functions -impl Bot { - /// Download file from telegram into `destination`. - /// `path` can be obtained from [`get_file`] method. - /// - /// For downloading as Stream of Chunks see [`download_file_stream`]. - /// - /// ## Examples - /// - /// ```no_run - /// use async_telegram_bot::{ - /// bot::Bot, - /// requests::Request, - /// types::File as TgFile, - /// }; - /// use tokio::fs::File; - /// # use async_telegram_bot::RequestError; - /// - /// # async fn run() -> Result<(), Box> { - /// let bot = Bot::new("TOKEN"); - /// let mut file = File::create("/home/waffle/Pictures/test.png").await?; - /// - /// let TgFile { file_path, .. } = bot.get_file("*file_id*").send().await?; - /// bot.download_file(&file_path, &mut file).await?; - /// # Ok(()) } - /// ``` - /// - /// [`get_file`]: crate::bot::Bot::get_file - /// [`download_file_stream`]: crate::bot::Bot::download_file_stream - pub async fn download_file( - &self, - path: &str, - destination: &mut D, - ) -> Result<(), DownloadError> - where - D: AsyncWrite + Unpin, - { - download_file(&self.client, &self.token, path, destination).await - } - - /// Download file from telegram. - /// - /// `path` can be obtained from [`get_file`] method. - /// - /// For downloading into [`AsyncWrite`] (e.g. [`tokio::fs::File`]) - /// see [`download_file`]. - /// - /// [`get_file`]: crate::bot::Bot::get_file - /// [`AsyncWrite`]: tokio::io::AsyncWrite - /// [`tokio::fs::File`]: tokio::fs::File - /// [`download_file`]: crate::bot::Bot::download_file - pub async fn download_file_stream( - &self, - path: &str, - ) -> Result>, reqwest::Error> - { - download_file_stream(&self.client, &self.token, path).await - } - - pub fn get_me(&self) -> GetMe { - GetMe::new(self.ctx()) - } - - pub fn send_message(&self, chat_id: C, text: T) -> SendMessage - where - C: Into, - T: Into, - { - SendMessage::new(self.ctx(), chat_id.into(), text.into()) - } - - pub fn edit_message_live_location( - &self, - latitude: Lt, - longitude: Lg, - ) -> EditMessageLiveLocation - where - Lt: Into, - Lg: Into, - { - EditMessageLiveLocation::new( - self.ctx(), - latitude.into(), - longitude.into(), - ) - } - - pub fn forward_message( - &self, - chat_id: C, - from_chat_id: F, - message_id: M, - ) -> ForwardMessage - where - C: Into, - F: Into, - M: Into, - { - ForwardMessage::new( - self.ctx(), - chat_id.into(), - from_chat_id.into(), - message_id.into(), - ) - } - - pub fn send_audio(&self, chat_id: C, audio: A) -> SendAudio - where - C: Into, - A: Into, - { - SendAudio::new(self.ctx(), chat_id.into(), audio.into()) - } - - pub fn send_location( - &self, - chat_id: C, - latitude: Lt, - longitude: Lg, - ) -> SendLocation - where - C: Into, - Lt: Into, - Lg: Into, - { - SendLocation::new( - self.ctx(), - chat_id.into(), - latitude.into(), - longitude.into(), - ) - } - - pub fn send_media_group(&self, chat_id: C, media: M) -> SendMediaGroup - where - C: Into, - M: Into>, - { - SendMediaGroup::new(self.ctx(), chat_id.into(), media.into()) - } - - pub fn send_photo(&self, chat_id: C, photo: P) -> SendPhoto - where - C: Into, - P: Into, - { - SendPhoto::new(self.ctx(), chat_id.into(), photo.into()) - } - - pub fn stop_message_live_location(&self) -> StopMessageLiveLocation { - StopMessageLiveLocation::new(self.ctx()) - } - - pub fn get_file(&self, file_id: F) -> GetFile - where - F: Into, - { - GetFile::new(self.ctx(), file_id.into()) - } -} diff --git a/src/network/mod.rs b/src/network/mod.rs index 26d5c670..71efb563 100644 --- a/src/network/mod.rs +++ b/src/network/mod.rs @@ -1,7 +1,5 @@ use crate::{ - RequestError, - requests::ResponseResult, - types::ResponseParameters, + requests::ResponseResult, types::ResponseParameters, RequestError, }; use crate::DownloadError; diff --git a/src/requests/answer_shipping_query.rs b/src/requests/answer_shipping_query.rs index b0c26841..47c0a25b 100644 --- a/src/requests/answer_shipping_query.rs +++ b/src/requests/answer_shipping_query.rs @@ -1,7 +1,5 @@ use crate::network; -use crate::requests::{ - Request, RequestContext, RequestFuture, ResponseResult, -}; +use crate::requests::{Request, RequestContext, RequestFuture, ResponseResult}; use crate::types::ShippingOption; #[derive(Debug, Clone, Serialize)] diff --git a/src/requests/get_file.rs b/src/requests/get_file.rs index 79995532..aa6df1f3 100644 --- a/src/requests/get_file.rs +++ b/src/requests/get_file.rs @@ -1,7 +1,5 @@ use crate::network; -use crate::requests::{ - Request, RequestContext, RequestFuture, ResponseResult, -}; +use crate::requests::{Request, RequestContext, RequestFuture, ResponseResult}; use crate::types::File; /// Use this method to get basic info about a file and prepare it for diff --git a/src/types/user.rs b/src/types/user.rs index a163b059..680b6a12 100644 --- a/src/types/user.rs +++ b/src/types/user.rs @@ -28,7 +28,7 @@ mod tests { first_name: "firstName".to_string(), last_name: Some("lastName".to_string()), username: Some("Username".to_string()), - language_code: Some("languageCode".to_string()) + language_code: Some("languageCode".to_string()), }; let actual = serde_json::from_str::(&json).unwrap(); assert_eq!(actual, expected)