diff --git a/src/core/requests/mod.rs b/src/core/requests/mod.rs index 3144daad..25325116 100644 --- a/src/core/requests/mod.rs +++ b/src/core/requests/mod.rs @@ -88,3 +88,5 @@ mod tests { pub mod get_me; pub mod send_message; pub mod forward_message; +pub mod send_photo; +mod utils; diff --git a/src/core/requests/send_photo.rs b/src/core/requests/send_photo.rs new file mode 100644 index 00000000..c0bc5490 --- /dev/null +++ b/src/core/requests/send_photo.rs @@ -0,0 +1,129 @@ +use std::path::Path; + +use crate::core::requests::{RequestContext, ChatId, Request, RequestFuture, ResponseResult}; +use crate::core::types::{ParseMode, Message}; +use crate::core::requests::form_builder::FormBuilder; +use crate::core::network; + +#[derive(Debug, Clone)] +pub struct SendPhoto<'a> { + ctx: RequestContext<'a>, + + pub chat_id: ChatId, + // TODO: add enum Photo + pub photo: String, + pub caption: Option, + pub parse_mode: Option, + pub disable_notification: Option, + pub reply_to_message_id: Option, + + // TODO: add reply_markup +} + +impl<'a> Request<'a> for SendPhoto<'a> { + type ReturnValue = Message; + + fn send(self) -> RequestFuture<'a, ResponseResult> { + Box::pin(async move { + let params = FormBuilder::new() + .add("chat_id", &self.chat_id) + .add_file("photo", &self.photo) + .add_if_some("caption", self.caption.as_ref()) + .add_if_some("parse_mode", self.parse_mode.as_ref()) + .add_if_some( + "disable_notification", + self.disable_notification.as_ref() + ) + .add_if_some( + "reply_to_message_id", + self.reply_to_message_id.as_ref() + ) + .build(); + + network::request( + &self.ctx.client, + &self.ctx.token, + "sendPhoto", + Some(params) + ).await + }) + } +} + +impl<'a> SendPhoto<'a> { + pub(crate) fn new( + ctx: RequestContext<'a>, + chat_id: ChatId, + photo: String + ) -> Self { + Self { + ctx, + chat_id, + photo, + caption: None, + parse_mode: None, + disable_notification: None, + reply_to_message_id: None + } + } + + pub fn chat_id>(mut self, chat_id: T) -> Self { + self.chat_id = chat_id.into(); + self + } + + pub fn photo>(mut self, photo: T) -> Self { + self.photo = photo.into(); + self + } + + pub fn caption>(mut self, caption: T) -> Self { + self.caption = Some(caption.into()); + self + } + + pub fn parse_mode>(mut self, parse_mode: T) -> Self { + self.parse_mode = Some(parse_mode.into()); + self + } + + pub fn disable_notification>(mut self, disable_notification: T) -> Self { + self.disable_notification = Some(disable_notification.into()); + self + } + + pub fn reply_to_message_id>(mut self, reply_to_message_id: T) -> Self { + self.reply_to_message_id = Some(reply_to_message_id.into()); + self + } +} + +#[cfg(test)] +mod tests { + use super::*; + use reqwest::r#async::Client; + + const TOKEN: &str = "882997251:AAGImZKe4cO6vDzluWzCgYqebziIMroN7uU"; + const USER_ID: i64 = 268486177; + + #[test] + fn send_photo() { + use futures::FutureExt; + use futures::TryFutureExt; + tokio::run(async_send_photo().boxed().unit_error().compat()) + } + + async fn async_send_photo() { + let client = Client::new(); + let req = SendPhoto::new( + RequestContext { + client: &client, + token: TOKEN, + }, + ChatId::Id(USER_ID), + "D:\\Снимок.png".to_string(), + ); + + println!("{:?}", req.send().await); + } +}