From 8271150218eb29e74614e52593356518442f0646 Mon Sep 17 00:00:00 2001 From: Waffle Date: Tue, 29 Sep 2020 00:45:04 +0300 Subject: [PATCH] add SendMessage request --- src/bot/api.rs | 10 ++++ src/bot/cache_me.rs | 12 ++++ src/payloads/mod.rs | 2 + src/payloads/send_message.rs | 112 +++++++++++++++++++++++++++++++++++ src/payloads/setters.rs | 1 + src/requests/requester.rs | 19 +++++- 6 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 src/payloads/send_message.rs diff --git a/src/bot/api.rs b/src/bot/api.rs index be6b21f5..721f8a71 100644 --- a/src/bot/api.rs +++ b/src/bot/api.rs @@ -1710,4 +1710,14 @@ impl Requester for Bot { fn get_me(&self) -> JsonRequest { Self::GetMe::new(self.clone(), payloads::GetMe::new()) } + + type SendMessage = JsonRequest; + + fn send_message(&self, chat_id: C, text: T) -> JsonRequest + where + C: Into, + T: Into + { + Self::SendMessage::new(self.clone(), payloads::SendMessage::new(chat_id, text)) + } } diff --git a/src/bot/cache_me.rs b/src/bot/cache_me.rs index e4a4623d..51f1ba76 100644 --- a/src/bot/cache_me.rs +++ b/src/bot/cache_me.rs @@ -13,6 +13,8 @@ use crate::{ requests::{HasPayload, Request, Requester}, types::User, }; +use crate::payloads::SendMessage; +use crate::types::ChatId; /// `get_me` cache. /// @@ -66,6 +68,16 @@ impl Requester for CacheMe { ), } } + + type SendMessage = B::SendMessage; + + fn send_message(&self, chat_id: C, text: T) -> Self::SendMessage + where + C: Into, + T: Into + { + self.bot.send_message(chat_id, text) + } } pub struct CachedMeRequest>(Inner, GetMe); diff --git a/src/payloads/mod.rs b/src/payloads/mod.rs index e01013c2..c468e562 100644 --- a/src/payloads/mod.rs +++ b/src/payloads/mod.rs @@ -1,5 +1,7 @@ pub mod setters; mod get_me; +mod send_message; pub use get_me::{GetMe, GetMeSetters}; +pub use send_message::{SendMessage, SendMessageSetters}; diff --git a/src/payloads/send_message.rs b/src/payloads/send_message.rs new file mode 100644 index 00000000..5d483525 --- /dev/null +++ b/src/payloads/send_message.rs @@ -0,0 +1,112 @@ +use serde::{Deserialize, Serialize}; + +use crate::{ + requests::{HasPayload, Payload}, + types::{ChatId, Message, ParseMode, ReplyMarkup} +}; + +/// Use this method to send text messages. +/// +/// On success, the sent [`Message`] is returned. +/// +/// [`Message`]: crate::types::Message +#[serde_with_macros::skip_serializing_none] +#[derive(Debug, PartialEq, Eq, Hash, Clone, Deserialize, Serialize)] +pub struct SendMessage { + /// Unique identifier for the target chat or username of the target channel + /// (in the format `@channelusername`) + pub chat_id: ChatId, + /// Text of the message to be sent + pub text: String, + /// Send [Markdown] or [HTML], if you want Telegram apps to show + /// [bold, italic, fixed-width text or inline URLs] in your bot's message. + /// + /// [Markdown]: crate::types::ParseMode::Markdown + /// [HTML]: crate::types::ParseMode::HTML + /// [bold, italic, fixed-width text or inline URLs]: + /// crate::types::ParseMode + pub parse_mode: Option, + /// Disables link previews for links in this message + pub disable_web_page_preview: Option, + /// Sends the message silently. + /// Users will receive a notification with no sound. + pub disable_notification: Option, + /// If the message is a reply, [id] of the original message + /// + /// [id]: crate::types::Message::id + pub reply_to_message_id: Option, + /// Additional interface options. + pub reply_markup: Option, +} + +impl Payload for SendMessage { + type Output = Message; + + const NAME: &'static str = "sendMessage"; +} + +impl SendMessage { + pub fn new(chat_id: C, text: T) -> Self + where + C: Into, + T: Into, + { + SendMessage { + chat_id: chat_id.into(), + text: text.into(), + parse_mode: None, + disable_web_page_preview: None, + disable_notification: None, + reply_to_message_id: None, + reply_markup: None, + } + } +} + +pub trait SendMessageSetters: HasPayload + Sized { + fn chat_id(mut self, value: T) -> Self + where + T: Into, + { + self.payload_mut().chat_id = value.into(); + self + } + + fn text(mut self, value: T) -> Self + where + T: Into, // TODO: into? + { + self.payload_mut().text = value.into(); + self + } + + fn parse_mode(mut self, value: ParseMode) -> Self { + self.payload_mut().parse_mode = Some(value); + self + } + + fn disable_web_page_preview(mut self, value: bool) -> Self { + self.payload_mut().disable_web_page_preview = Some(value); + self + } + + fn disable_notification(mut self, value: bool) -> Self { + self.payload_mut().disable_notification = Some(value); + self + } + + fn reply_to_message_id(mut self, value: i32) -> Self { + self.payload_mut().reply_to_message_id = Some(value); + self + } + + fn reply_markup(mut self, value: T) -> Self + where + T: Into, + { + self.payload_mut().reply_markup = Some(value.into()); + self + } +} + +impl

SendMessageSetters for P where P: HasPayload {} diff --git a/src/payloads/setters.rs b/src/payloads/setters.rs index c7cfd823..646f1a88 100644 --- a/src/payloads/setters.rs +++ b/src/payloads/setters.rs @@ -1 +1,2 @@ pub use crate::payloads::GetMeSetters as _; +pub use crate::payloads::SendMessageSetters as _; diff --git a/src/requests/requester.rs b/src/requests/requester.rs index 8390b786..69dd1d25 100644 --- a/src/requests/requester.rs +++ b/src/requests/requester.rs @@ -1,4 +1,11 @@ -use crate::{payloads::GetMe, requests::Request}; +use crate::{ + payloads::{ + GetMe, + SendMessage + }, + requests::Request, + types::ChatId, +}; /// The trait implemented by all bots & bot wrappers. /// Essentially a request builder factory (?). @@ -11,5 +18,13 @@ pub trait Requester { /// For telegram documentation of the method see [`GetMe`]. fn get_me(&self) -> Self::GetMe; - // FIXME(waffle): add remaining 69 methods + type SendMessage: Request; + + /// For telegram documentation of the method see [`SendMessage`]. + fn send_message(&self, chat_id: C, text: T) -> Self::SendMessage + where + C: Into, + T: Into; + + // FIXME(waffle): add remaining 68 methods }