Make BotMessageExt for Requester instead of Bot

This commit is contained in:
LasterAlex 2024-12-07 00:04:21 +02:00
parent 94db1757dc
commit 6f8eaf803b
No known key found for this signature in database

View file

@ -2,21 +2,17 @@
//! //!
//! [`Bot`]: crate::Bot //! [`Bot`]: crate::Bot
use crate::{prelude::*, types::*}; use crate::{prelude::*, types::*};
use teloxide_core::{
payloads::*,
requests::{JsonRequest, MultipartRequest},
};
/// Syntax sugar for [`Message`] manipulations. /// Syntax sugar for [`Message`] manipulations.
/// ///
/// [`Message`]: crate::types::Message /// [`Message`]: crate::types::Message
pub trait BotMessagesExt { pub trait BotMessagesExt: Requester {
/// This function is the same as [`Bot::forward_message`], /// This function is the same as [`Bot::forward_message`],
/// but can take in [`Message`] to forward it. /// but can take in [`Message`] to forward it.
/// ///
/// [`Bot::forward_message`]: crate::Bot::forward_message /// [`Bot::forward_message`]: crate::Bot::forward_message
/// [`Message`]: crate::types::Message /// [`Message`]: crate::types::Message
fn forward<C>(&self, to_chat_id: C, message: &Message) -> JsonRequest<ForwardMessage> fn forward<C>(&self, to_chat_id: C, message: &Message) -> Self::ForwardMessage
where where
C: Into<Recipient>; C: Into<Recipient>;
@ -30,42 +26,42 @@ pub trait BotMessagesExt {
message: &Message, message: &Message,
latitude: f64, latitude: f64,
longitude: f64, longitude: f64,
) -> JsonRequest<EditMessageLiveLocation>; ) -> Self::EditMessageLiveLocation;
/// This function is the same as [`Bot::stop_message_live_location`], /// This function is the same as [`Bot::stop_message_live_location`],
/// but can take in [`Message`] to stop the live location in it. /// but can take in [`Message`] to stop the live location in it.
/// ///
/// [`Bot::stop_message_live_location`]: crate::Bot::stop_message_live_location /// [`Bot::stop_message_live_location`]: crate::Bot::stop_message_live_location
/// [`Message`]: crate::types::Message /// [`Message`]: crate::types::Message
fn stop_live_location(&self, message: &Message) -> JsonRequest<StopMessageLiveLocation>; fn stop_live_location(&self, message: &Message) -> Self::StopMessageLiveLocation;
/// This function is the same as [`Bot::set_message_reaction`], /// This function is the same as [`Bot::set_message_reaction`],
/// but can take in [`Message`] to set a reaction on it. /// but can take in [`Message`] to set a reaction on it.
/// ///
/// [`Bot::set_message_reaction`]: crate::Bot::set_message_reaction /// [`Bot::set_message_reaction`]: crate::Bot::set_message_reaction
/// [`Message`]: crate::types::Message /// [`Message`]: crate::types::Message
fn set_reaction(&self, message: &Message) -> JsonRequest<SetMessageReaction>; fn set_reaction(&self, message: &Message) -> Self::SetMessageReaction;
/// This function is the same as [`Bot::pin_chat_message`], /// This function is the same as [`Bot::pin_chat_message`],
/// but can take in [`Message`] to pin it. /// but can take in [`Message`] to pin it.
/// ///
/// [`Bot::pin_chat_message`]: crate::Bot::pin_chat_message /// [`Bot::pin_chat_message`]: crate::Bot::pin_chat_message
/// [`Message`]: crate::types::Message /// [`Message`]: crate::types::Message
fn pin(&self, message: &Message) -> JsonRequest<PinChatMessage>; fn pin(&self, message: &Message) -> Self::PinChatMessage;
/// This function is the same as [`Bot::unpin_chat_message`], /// This function is the same as [`Bot::unpin_chat_message`],
/// but can take in [`Message`] to unpin it. /// but can take in [`Message`] to unpin it.
/// ///
/// [`Bot::unpin_chat_message`]: crate::Bot::unpin_chat_message /// [`Bot::unpin_chat_message`]: crate::Bot::unpin_chat_message
/// [`Message`]: crate::types::Message /// [`Message`]: crate::types::Message
fn unpin(&self, message: &Message) -> JsonRequest<UnpinChatMessage>; fn unpin(&self, message: &Message) -> Self::UnpinChatMessage;
/// This function is the same as [`Bot::edit_message_text`], /// This function is the same as [`Bot::edit_message_text`],
/// but can take in [`Message`] to edit it. /// but can take in [`Message`] to edit it.
/// ///
/// [`Bot::edit_message_text`]: crate::Bot::edit_message_text /// [`Bot::edit_message_text`]: crate::Bot::edit_message_text
/// [`Message`]: crate::types::Message /// [`Message`]: crate::types::Message
fn edit_text<T>(&self, message: &Message, text: T) -> JsonRequest<EditMessageText> fn edit_text<T>(&self, message: &Message, text: T) -> Self::EditMessageText
where where
T: Into<String>; T: Into<String>;
@ -74,52 +70,51 @@ pub trait BotMessagesExt {
/// ///
/// [`Bot::edit_message_caption`]: crate::Bot::edit_message_caption /// [`Bot::edit_message_caption`]: crate::Bot::edit_message_caption
/// [`Message`]: crate::types::Message /// [`Message`]: crate::types::Message
fn edit_caption(&self, message: &Message) -> JsonRequest<EditMessageCaption>; fn edit_caption(&self, message: &Message) -> Self::EditMessageCaption;
/// This function is the same as [`Bot::edit_message_media`], /// This function is the same as [`Bot::edit_message_media`],
/// but can take in [`Message`] to edit it. /// but can take in [`Message`] to edit it.
/// ///
/// [`Bot::edit_message_media`]: crate::Bot::edit_message_media /// [`Bot::edit_message_media`]: crate::Bot::edit_message_media
/// [`Message`]: crate::types::Message /// [`Message`]: crate::types::Message
fn edit_media( fn edit_media(&self, message: &Message, media: InputMedia) -> Self::EditMessageMedia;
&self,
message: &Message,
media: InputMedia,
) -> MultipartRequest<EditMessageMedia>;
/// This function is the same as [`Bot::edit_message_reply_markup`], /// This function is the same as [`Bot::edit_message_reply_markup`],
/// but can take in [`Message`] to edit it. /// but can take in [`Message`] to edit it.
/// ///
/// [`Bot::edit_message_reply_markup`]: crate::Bot::edit_message_reply_markup /// [`Bot::edit_message_reply_markup`]: crate::Bot::edit_message_reply_markup
/// [`Message`]: crate::types::Message /// [`Message`]: crate::types::Message
fn edit_reply_markup(&self, message: &Message) -> JsonRequest<EditMessageReplyMarkup>; fn edit_reply_markup(&self, message: &Message) -> Self::EditMessageReplyMarkup;
/// This function is the same as [`Bot::stop_poll`], /// This function is the same as [`Bot::stop_poll`],
/// but can take in [`Message`] to stop the poll in it. /// but can take in [`Message`] to stop the poll in it.
/// ///
/// [`Bot::stop_poll`]: crate::Bot::stop_poll /// [`Bot::stop_poll`]: crate::Bot::stop_poll
/// [`Message`]: crate::types::Message /// [`Message`]: crate::types::Message
fn stop_poll_message(&self, message: &Message) -> JsonRequest<StopPoll>; fn stop_poll_message(&self, message: &Message) -> Self::StopPoll;
/// This function is the same as [`Bot::delete_message`], /// This function is the same as [`Bot::delete_message`],
/// but can take in [`Message`] to delete it. /// but can take in [`Message`] to delete it.
/// ///
/// [`Bot::delete_message`]: crate::Bot::delete_message /// [`Bot::delete_message`]: crate::Bot::delete_message
/// [`Message`]: crate::types::Message /// [`Message`]: crate::types::Message
fn delete(&self, message: &Message) -> JsonRequest<DeleteMessage>; fn delete(&self, message: &Message) -> Self::DeleteMessage;
/// This function is the same as [`Bot::copy_message`], /// This function is the same as [`Bot::copy_message`],
/// but can take in [`Message`] to copy it. /// but can take in [`Message`] to copy it.
/// ///
/// [`Bot::copy_messages`]: crate::Bot::copy_message /// [`Bot::copy_messages`]: crate::Bot::copy_message
/// [`Message`]: crate::types::Message /// [`Message`]: crate::types::Message
fn copy<C>(&self, to_chat_id: C, message: &Message) -> JsonRequest<CopyMessage> fn copy<C>(&self, to_chat_id: C, message: &Message) -> Self::CopyMessage
where where
C: Into<Recipient>; C: Into<Recipient>;
} }
impl BotMessagesExt for Bot { impl<R> BotMessagesExt for R
fn forward<C>(&self, to_chat_id: C, message: &Message) -> JsonRequest<ForwardMessage> where
R: Requester,
{
fn forward<C>(&self, to_chat_id: C, message: &Message) -> Self::ForwardMessage
where where
C: Into<Recipient>, C: Into<Recipient>,
{ {
@ -131,58 +126,54 @@ impl BotMessagesExt for Bot {
message: &Message, message: &Message,
latitude: f64, latitude: f64,
longitude: f64, longitude: f64,
) -> JsonRequest<EditMessageLiveLocation> { ) -> Self::EditMessageLiveLocation {
self.edit_message_live_location(message.chat.id, message.id, latitude, longitude) self.edit_message_live_location(message.chat.id, message.id, latitude, longitude)
} }
fn stop_live_location(&self, message: &Message) -> JsonRequest<StopMessageLiveLocation> { fn stop_live_location(&self, message: &Message) -> Self::StopMessageLiveLocation {
self.stop_message_live_location(message.chat.id, message.id) self.stop_message_live_location(message.chat.id, message.id)
} }
fn set_reaction(&self, message: &Message) -> JsonRequest<SetMessageReaction> { fn set_reaction(&self, message: &Message) -> Self::SetMessageReaction {
self.set_message_reaction(message.chat.id, message.id) self.set_message_reaction(message.chat.id, message.id)
} }
fn pin(&self, message: &Message) -> JsonRequest<PinChatMessage> { fn pin(&self, message: &Message) -> Self::PinChatMessage {
self.pin_chat_message(message.chat.id, message.id) self.pin_chat_message(message.chat.id, message.id)
} }
fn unpin(&self, message: &Message) -> JsonRequest<UnpinChatMessage> { fn unpin(&self, message: &Message) -> Self::UnpinChatMessage {
self.unpin_chat_message(message.chat.id).message_id(message.id) self.unpin_chat_message(message.chat.id).message_id(message.id)
} }
fn edit_text<T>(&self, message: &Message, text: T) -> JsonRequest<EditMessageText> fn edit_text<T>(&self, message: &Message, text: T) -> Self::EditMessageText
where where
T: Into<String>, T: Into<String>,
{ {
self.edit_message_text(message.chat.id, message.id, text) self.edit_message_text(message.chat.id, message.id, text)
} }
fn edit_caption(&self, message: &Message) -> JsonRequest<EditMessageCaption> { fn edit_caption(&self, message: &Message) -> Self::EditMessageCaption {
self.edit_message_caption(message.chat.id, message.id) self.edit_message_caption(message.chat.id, message.id)
} }
fn edit_media( fn edit_media(&self, message: &Message, media: InputMedia) -> Self::EditMessageMedia {
&self,
message: &Message,
media: InputMedia,
) -> MultipartRequest<EditMessageMedia> {
self.edit_message_media(message.chat.id, message.id, media) self.edit_message_media(message.chat.id, message.id, media)
} }
fn edit_reply_markup(&self, message: &Message) -> JsonRequest<EditMessageReplyMarkup> { fn edit_reply_markup(&self, message: &Message) -> Self::EditMessageReplyMarkup {
self.edit_message_reply_markup(message.chat.id, message.id) self.edit_message_reply_markup(message.chat.id, message.id)
} }
fn stop_poll_message(&self, message: &Message) -> JsonRequest<StopPoll> { fn stop_poll_message(&self, message: &Message) -> Self::StopPoll {
self.stop_poll(message.chat.id, message.id) self.stop_poll(message.chat.id, message.id)
} }
fn delete(&self, message: &Message) -> JsonRequest<DeleteMessage> { fn delete(&self, message: &Message) -> Self::DeleteMessage {
self.delete_message(message.chat.id, message.id) self.delete_message(message.chat.id, message.id)
} }
fn copy<C>(&self, to_chat_id: C, message: &Message) -> JsonRequest<CopyMessage> fn copy<C>(&self, to_chat_id: C, message: &Message) -> Self::CopyMessage
where where
C: Into<Recipient>, C: Into<Recipient>,
{ {