mirror of
https://github.com/teloxide/teloxide.git
synced 2025-01-03 17:52:12 +01:00
Split bot into different files
This commit is contained in:
parent
fb0daffbd5
commit
1b52a8cdd3
7 changed files with 191 additions and 188 deletions
117
src/bot/api.rs
Normal file
117
src/bot/api.rs
Normal file
|
@ -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<C, T>(&self, chat_id: C, text: T) -> SendMessage
|
||||
where
|
||||
C: Into<ChatId>,
|
||||
T: Into<String>,
|
||||
{
|
||||
SendMessage::new(self.ctx(), chat_id.into(), text.into())
|
||||
}
|
||||
|
||||
pub fn edit_message_live_location<Lt, Lg>(
|
||||
&self,
|
||||
latitude: Lt,
|
||||
longitude: Lg,
|
||||
) -> EditMessageLiveLocation
|
||||
where
|
||||
Lt: Into<f64>,
|
||||
Lg: Into<f64>,
|
||||
{
|
||||
EditMessageLiveLocation::new(
|
||||
self.ctx(),
|
||||
latitude.into(),
|
||||
longitude.into(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn forward_message<C, F, M>(
|
||||
&self,
|
||||
chat_id: C,
|
||||
from_chat_id: F,
|
||||
message_id: M,
|
||||
) -> ForwardMessage
|
||||
where
|
||||
C: Into<ChatId>,
|
||||
F: Into<ChatId>,
|
||||
M: Into<i32>,
|
||||
{
|
||||
ForwardMessage::new(
|
||||
self.ctx(),
|
||||
chat_id.into(),
|
||||
from_chat_id.into(),
|
||||
message_id.into(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn send_audio<C, A>(&self, chat_id: C, audio: A) -> SendAudio
|
||||
where
|
||||
C: Into<ChatId>,
|
||||
A: Into<InputFile>,
|
||||
{
|
||||
SendAudio::new(self.ctx(), chat_id.into(), audio.into())
|
||||
}
|
||||
|
||||
pub fn send_location<C, Lt, Lg>(
|
||||
&self,
|
||||
chat_id: C,
|
||||
latitude: Lt,
|
||||
longitude: Lg,
|
||||
) -> SendLocation
|
||||
where
|
||||
C: Into<ChatId>,
|
||||
Lt: Into<f64>,
|
||||
Lg: Into<f64>,
|
||||
{
|
||||
SendLocation::new(
|
||||
self.ctx(),
|
||||
chat_id.into(),
|
||||
latitude.into(),
|
||||
longitude.into(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn send_media_group<C, M>(&self, chat_id: C, media: M) -> SendMediaGroup
|
||||
where
|
||||
C: Into<ChatId>,
|
||||
M: Into<Vec<InputMedia>>,
|
||||
{
|
||||
SendMediaGroup::new(self.ctx(), chat_id.into(), media.into())
|
||||
}
|
||||
|
||||
pub fn send_photo<C, P>(&self, chat_id: C, photo: P) -> SendPhoto
|
||||
where
|
||||
C: Into<ChatId>,
|
||||
P: Into<InputFile>,
|
||||
{
|
||||
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<F>(&self, file_id: F) -> GetFile
|
||||
where
|
||||
F: Into<String>,
|
||||
{
|
||||
GetFile::new(self.ctx(), file_id.into())
|
||||
}
|
||||
}
|
63
src/bot/download.rs
Normal file
63
src/bot/download.rs
Normal file
|
@ -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<dyn std::error::Error>> {
|
||||
/// 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<D>(
|
||||
&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<impl Stream<Item = Result<Chunk, reqwest::Error>>, reqwest::Error>
|
||||
{
|
||||
download_file_stream(&self.client, &self.token, path).await
|
||||
}
|
||||
}
|
185
src/bot/mod.rs
185
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<dyn std::error::Error>> {
|
||||
/// 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<D>(
|
||||
&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<impl Stream<Item = Result<Chunk, reqwest::Error>>, 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<C, T>(&self, chat_id: C, text: T) -> SendMessage
|
||||
where
|
||||
C: Into<ChatId>,
|
||||
T: Into<String>,
|
||||
{
|
||||
SendMessage::new(self.ctx(), chat_id.into(), text.into())
|
||||
}
|
||||
|
||||
pub fn edit_message_live_location<Lt, Lg>(
|
||||
&self,
|
||||
latitude: Lt,
|
||||
longitude: Lg,
|
||||
) -> EditMessageLiveLocation
|
||||
where
|
||||
Lt: Into<f64>,
|
||||
Lg: Into<f64>,
|
||||
{
|
||||
EditMessageLiveLocation::new(
|
||||
self.ctx(),
|
||||
latitude.into(),
|
||||
longitude.into(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn forward_message<C, F, M>(
|
||||
&self,
|
||||
chat_id: C,
|
||||
from_chat_id: F,
|
||||
message_id: M,
|
||||
) -> ForwardMessage
|
||||
where
|
||||
C: Into<ChatId>,
|
||||
F: Into<ChatId>,
|
||||
M: Into<i32>,
|
||||
{
|
||||
ForwardMessage::new(
|
||||
self.ctx(),
|
||||
chat_id.into(),
|
||||
from_chat_id.into(),
|
||||
message_id.into(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn send_audio<C, A>(&self, chat_id: C, audio: A) -> SendAudio
|
||||
where
|
||||
C: Into<ChatId>,
|
||||
A: Into<InputFile>,
|
||||
{
|
||||
SendAudio::new(self.ctx(), chat_id.into(), audio.into())
|
||||
}
|
||||
|
||||
pub fn send_location<C, Lt, Lg>(
|
||||
&self,
|
||||
chat_id: C,
|
||||
latitude: Lt,
|
||||
longitude: Lg,
|
||||
) -> SendLocation
|
||||
where
|
||||
C: Into<ChatId>,
|
||||
Lt: Into<f64>,
|
||||
Lg: Into<f64>,
|
||||
{
|
||||
SendLocation::new(
|
||||
self.ctx(),
|
||||
chat_id.into(),
|
||||
latitude.into(),
|
||||
longitude.into(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn send_media_group<C, M>(&self, chat_id: C, media: M) -> SendMediaGroup
|
||||
where
|
||||
C: Into<ChatId>,
|
||||
M: Into<Vec<InputMedia>>,
|
||||
{
|
||||
SendMediaGroup::new(self.ctx(), chat_id.into(), media.into())
|
||||
}
|
||||
|
||||
pub fn send_photo<C, P>(&self, chat_id: C, photo: P) -> SendPhoto
|
||||
where
|
||||
C: Into<ChatId>,
|
||||
P: Into<InputFile>,
|
||||
{
|
||||
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<F>(&self, file_id: F) -> GetFile
|
||||
where
|
||||
F: Into<String>,
|
||||
{
|
||||
GetFile::new(self.ctx(), file_id.into())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
use crate::{
|
||||
RequestError,
|
||||
requests::ResponseResult,
|
||||
types::ResponseParameters,
|
||||
requests::ResponseResult, types::ResponseParameters, RequestError,
|
||||
};
|
||||
|
||||
use crate::DownloadError;
|
||||
|
|
|
@ -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)]
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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::<User>(&json).unwrap();
|
||||
assert_eq!(actual, expected)
|
||||
|
|
Loading…
Reference in a new issue