add SendMessage request

This commit is contained in:
Waffle 2020-09-29 00:45:04 +03:00
parent 745d4a1ffe
commit 8271150218
6 changed files with 154 additions and 2 deletions

View file

@ -1710,4 +1710,14 @@ impl Requester for Bot {
fn get_me(&self) -> JsonRequest<payloads::GetMe> {
Self::GetMe::new(self.clone(), payloads::GetMe::new())
}
type SendMessage = JsonRequest<payloads::SendMessage>;
fn send_message<C, T>(&self, chat_id: C, text: T) -> JsonRequest<payloads::SendMessage>
where
C: Into<ChatId>,
T: Into<String>
{
Self::SendMessage::new(self.clone(), payloads::SendMessage::new(chat_id, text))
}
}

View file

@ -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<B: Requester> Requester for CacheMe<B> {
),
}
}
type SendMessage = B::SendMessage;
fn send_message<C, T>(&self, chat_id: C, text: T) -> Self::SendMessage
where
C: Into<ChatId>,
T: Into<String>
{
self.bot.send_message(chat_id, text)
}
}
pub struct CachedMeRequest<R: Request<Payload = GetMe>>(Inner<R>, GetMe);

View file

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

View file

@ -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<ParseMode>,
/// Disables link previews for links in this message
pub disable_web_page_preview: Option<bool>,
/// Sends the message silently.
/// Users will receive a notification with no sound.
pub disable_notification: Option<bool>,
/// If the message is a reply, [id] of the original message
///
/// [id]: crate::types::Message::id
pub reply_to_message_id: Option<i32>,
/// Additional interface options.
pub reply_markup: Option<ReplyMarkup>,
}
impl Payload for SendMessage {
type Output = Message;
const NAME: &'static str = "sendMessage";
}
impl SendMessage {
pub fn new<C, T>(chat_id: C, text: T) -> Self
where
C: Into<ChatId>,
T: Into<String>,
{
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<Payload = SendMessage> + Sized {
fn chat_id<T>(mut self, value: T) -> Self
where
T: Into<ChatId>,
{
self.payload_mut().chat_id = value.into();
self
}
fn text<T>(mut self, value: T) -> Self
where
T: Into<String>, // 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<T>(mut self, value: T) -> Self
where
T: Into<ReplyMarkup>,
{
self.payload_mut().reply_markup = Some(value.into());
self
}
}
impl<P> SendMessageSetters for P where P: HasPayload<Payload = SendMessage> {}

View file

@ -1 +1,2 @@
pub use crate::payloads::GetMeSetters as _;
pub use crate::payloads::SendMessageSetters as _;

View file

@ -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<Payload = SendMessage>;
/// For telegram documentation of the method see [`SendMessage`].
fn send_message<C, T>(&self, chat_id: C, text: T) -> Self::SendMessage
where
C: Into<ChatId>,
T: Into<String>;
// FIXME(waffle): add remaining 68 methods
}