From 08d11a85d4b1280958c4b01c879eb0d0c7fc88d9 Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Fri, 3 Jan 2020 15:40:43 +0600 Subject: [PATCH] Use ChatOrInlineMessage --- src/bot/api.rs | 204 ++++-------------- src/requests/all/edit_message_caption.rs | 34 +-- .../all/edit_message_caption_inline.rs | 83 ------- .../all/edit_message_live_location.rs | 36 +--- .../all/edit_message_live_location_inline.rs | 85 -------- src/requests/all/edit_message_media.rs | 56 +++-- src/requests/all/edit_message_media_inline.rs | 83 ------- src/requests/all/edit_message_reply_markup.rs | 34 +-- .../all/edit_message_reply_markup_inline.rs | 63 ------ src/requests/all/edit_message_text.rs | 35 +-- src/requests/all/edit_message_text_inline.rs | 93 -------- src/requests/all/get_game_high_scores.rs | 35 +-- .../all/get_game_high_scores_inline.rs | 70 ------ src/requests/all/mod.rs | 16 -- src/requests/all/set_game_score.rs | 35 +-- src/requests/all/set_game_score_inline.rs | 95 -------- .../all/stop_message_live_location.rs | 34 +-- .../all/stop_message_live_location_inline.rs | 63 ------ src/types/chat_or_inline_message.rs | 11 + src/types/mod.rs | 2 + 20 files changed, 153 insertions(+), 1014 deletions(-) delete mode 100644 src/requests/all/edit_message_caption_inline.rs delete mode 100644 src/requests/all/edit_message_live_location_inline.rs delete mode 100644 src/requests/all/edit_message_media_inline.rs delete mode 100644 src/requests/all/edit_message_reply_markup_inline.rs delete mode 100644 src/requests/all/edit_message_text_inline.rs delete mode 100644 src/requests/all/get_game_high_scores_inline.rs delete mode 100644 src/requests/all/set_game_score_inline.rs delete mode 100644 src/requests/all/stop_message_live_location_inline.rs create mode 100644 src/types/chat_or_inline_message.rs diff --git a/src/bot/api.rs b/src/bot/api.rs index bc8f04bd..73a31753 100644 --- a/src/bot/api.rs +++ b/src/bot/api.rs @@ -4,13 +4,10 @@ use crate::{ AnswerPreCheckoutQuery, AnswerShippingQuery, CreateNewStickerSet, DeleteChatPhoto, DeleteChatStickerSet, DeleteMessage, DeleteStickerFromSet, DeleteWebhook, EditMessageCaption, - EditMessageCaptionInline, EditMessageLiveLocation, - EditMessageLiveLocationInline, EditMessageMedia, - EditMessageMediaInline, EditMessageReplyMarkup, - EditMessageReplyMarkupInline, EditMessageText, EditMessageTextInline, - ExportChatInviteLink, ForwardMessage, GetChat, GetChatAdministrators, - GetChatMember, GetChatMembersCount, GetFile, GetGameHighScores, - GetGameHighScoresInline, GetMe, GetStickerSet, GetUpdates, + EditMessageLiveLocation, EditMessageMedia, EditMessageReplyMarkup, + EditMessageText, ExportChatInviteLink, ForwardMessage, GetChat, + GetChatAdministrators, GetChatMember, GetChatMembersCount, GetFile, + GetGameHighScores, GetMe, GetStickerSet, GetUpdates, GetUserProfilePhotos, GetWebhookInfo, KickChatMember, LeaveChat, PinChatMessage, PromoteChatMember, RestrictChatMember, SendAnimation, SendAudio, SendChatAction, SendChatActionKind, SendContact, @@ -18,14 +15,13 @@ use crate::{ SendMessage, SendPhoto, SendPoll, SendSticker, SendVenue, SendVideo, SendVideoNote, SendVoice, SetChatAdministratorCustomTitle, SetChatDescription, SetChatPermissions, SetChatPhoto, - SetChatStickerSet, SetChatTitle, SetGameScore, SetGameScoreInline, - SetStickerPositionInSet, SetWebhook, StopMessageLiveLocation, - StopMessageLiveLocationInline, StopPoll, UnbanChatMember, + SetChatStickerSet, SetChatTitle, SetGameScore, SetStickerPositionInSet, + SetWebhook, StopMessageLiveLocation, StopPoll, UnbanChatMember, UnpinChatMessage, UploadStickerFile, }, types::{ - ChatId, ChatPermissions, InlineQueryResult, InputFile, InputMedia, - LabeledPrice, + ChatId, ChatOrInlineMessage, ChatPermissions, InlineQueryResult, + InputFile, InputMedia, LabeledPrice, }, Bot, }; @@ -156,57 +152,25 @@ impl Bot { SendLocation::new(self, chat_id, latitude, longitude) } - pub fn edit_message_live_location_inline( + pub fn edit_message_live_location( &self, - inline_message_id: I, + chat_or_inline_message: ChatOrInlineMessage, latitude: f32, longitude: f32, - ) -> EditMessageLiveLocationInline - where - I: Into, - { - EditMessageLiveLocationInline::new( + ) -> EditMessageLiveLocation { + EditMessageLiveLocation::new( self, - inline_message_id, + chat_or_inline_message, latitude, longitude, ) } - pub fn edit_message_live_location( + pub fn stop_message_live_location( &self, - chat_id: C, - message_id: i32, - latitude: f32, - longitude: f32, - ) -> EditMessageLiveLocation - where - C: Into, - { - EditMessageLiveLocation::new( - self, chat_id, message_id, latitude, longitude, - ) - } - - pub fn stop_message_live_location_inline( - &self, - inline_message_id: I, - ) -> StopMessageLiveLocationInline - where - I: Into, - { - StopMessageLiveLocationInline::new(self, inline_message_id) - } - - pub fn stop_message_live_location( - &self, - chat_id: C, - message_id: i32, - ) -> StopMessageLiveLocation - where - C: Into, - { - StopMessageLiveLocation::new(self, chat_id, message_id) + chat_or_inline_message: ChatOrInlineMessage, + ) -> StopMessageLiveLocation { + StopMessageLiveLocation::new(self, chat_or_inline_message) } pub fn send_venue( @@ -459,94 +423,37 @@ impl Bot { AnswerCallbackQuery::new(self, callback_query_id) } - pub fn edit_message_text_inline( + pub fn edit_message_text( &self, - inline_message_id: I, - text: T, - ) -> EditMessageTextInline - where - I: Into, - T: Into, - { - EditMessageTextInline::new(self, inline_message_id, text) - } - - pub fn edit_message_text( - &self, - chat_id: C, - message_id: i32, + chat_or_inline_message: ChatOrInlineMessage, text: T, ) -> EditMessageText where - C: Into, T: Into, { - EditMessageText::new(self, chat_id, message_id, text) + EditMessageText::new(self, chat_or_inline_message, text) } - pub fn edit_message_caption_inline( + pub fn edit_message_caption( &self, - inline_message_id: I, - ) -> EditMessageCaptionInline - where - I: Into, - { - EditMessageCaptionInline::new(self, inline_message_id) + chat_or_inline_message: ChatOrInlineMessage, + ) -> EditMessageCaption { + EditMessageCaption::new(self, chat_or_inline_message) } - pub fn edit_message_caption( + pub fn edit_message_media( &self, - chat_id: C, - message_id: i32, - ) -> EditMessageCaption - where - C: Into, - { - EditMessageCaption::new(self, chat_id, message_id) - } - - pub fn edit_message_media_inline( - &self, - inline_message_id: I, + chat_or_inline_message: ChatOrInlineMessage, media: InputMedia, - ) -> EditMessageMediaInline - where - I: Into, - { - EditMessageMediaInline::new(self, inline_message_id, media) + ) -> EditMessageMedia { + EditMessageMedia::new(self, chat_or_inline_message, media) } - pub fn edit_message_media( + pub fn edit_message_reply_markup( &self, - chat_id: C, - message_id: i32, - media: InputMedia, - ) -> EditMessageMedia - where - C: Into, - { - EditMessageMedia::new(self, chat_id, message_id, media) - } - - pub fn edit_message_reply_markup_inline( - &self, - inline_message_id: I, - ) -> EditMessageReplyMarkupInline - where - I: Into, - { - EditMessageReplyMarkupInline::new(self, inline_message_id) - } - - pub fn edit_message_reply_markup( - &self, - chat_id: C, - message_id: i32, - ) -> EditMessageReplyMarkup - where - C: Into, - { - EditMessageReplyMarkup::new(self, chat_id, message_id) + chat_or_inline_message: ChatOrInlineMessage, + ) -> EditMessageReplyMarkup { + EditMessageReplyMarkup::new(self, chat_or_inline_message) } pub fn stop_poll(&self, chat_id: C, message_id: i32) -> StopPoll @@ -719,52 +626,21 @@ impl Bot { SendGame::new(self, chat_id, game_short_name) } - pub fn set_game_score_inline( + pub fn set_game_score( &self, - inline_message_id: I, + chat_or_inline_message: ChatOrInlineMessage, user_id: i32, score: i32, - ) -> SetGameScoreInline - where - I: Into, - { - SetGameScoreInline::new(self, inline_message_id, user_id, score) + ) -> SetGameScore { + SetGameScore::new(self, chat_or_inline_message, user_id, score) } - pub fn set_game_score( + pub fn get_game_high_scores( &self, - chat_id: C, - message_id: i32, + chat_or_inline_message: ChatOrInlineMessage, user_id: i32, - score: i32, - ) -> SetGameScore - where - C: Into, - { - SetGameScore::new(self, chat_id, message_id, user_id, score) - } - - pub fn get_game_high_scores_inline( - &self, - inline_message_id: I, - user_id: i32, - ) -> GetGameHighScoresInline - where - I: Into, - { - GetGameHighScoresInline::new(self, inline_message_id, user_id) - } - - pub fn get_game_high_scores( - &self, - chat_id: C, - message_id: i32, - user_id: i32, - ) -> GetGameHighScores - where - C: Into, - { - GetGameHighScores::new(self, chat_id, message_id, user_id) + ) -> GetGameHighScores { + GetGameHighScores::new(self, chat_or_inline_message, user_id) } pub fn set_chat_administrator_custom_title( diff --git a/src/requests/all/edit_message_caption.rs b/src/requests/all/edit_message_caption.rs index b44db5fd..229d2267 100644 --- a/src/requests/all/edit_message_caption.rs +++ b/src/requests/all/edit_message_caption.rs @@ -3,7 +3,7 @@ use serde::Serialize; use crate::{ network, requests::{Request, ResponseResult}, - types::{ChatId, InlineKeyboardMarkup, Message, ParseMode}, + types::{ChatOrInlineMessage, InlineKeyboardMarkup, Message, ParseMode}, Bot, }; @@ -16,11 +16,9 @@ pub struct EditMessageCaption<'a> { #[serde(skip_serializing)] bot: &'a Bot, - /// Unique identifier for the target chat or username of the target channel - /// (in the format @channelusername) - chat_id: ChatId, - /// Identifier of the message to edit - message_id: i32, + #[serde(flatten)] + chat_or_inline_message: ChatOrInlineMessage, + /// New caption of the message caption: Option, /// Send Markdown or HTML, if you want Telegram apps to show bold, italic, @@ -44,31 +42,21 @@ impl Request for EditMessageCaption<'_> { } impl<'a> EditMessageCaption<'a> { - pub(crate) fn new(bot: &'a Bot, chat_id: C, message_id: i32) -> Self - where - C: Into, - { - let chat_id = chat_id.into(); + pub(crate) fn new( + bot: &'a Bot, + chat_or_inline_message: ChatOrInlineMessage, + ) -> Self { Self { bot, - chat_id, - message_id, + chat_or_inline_message, caption: None, parse_mode: None, reply_markup: None, } } - pub fn chat_id(mut self, val: T) -> Self - where - T: Into, - { - self.chat_id = val.into(); - self - } - - pub fn message_id(mut self, val: i32) -> Self { - self.message_id = val; + pub fn chat_or_inline_message(mut self, val: ChatOrInlineMessage) -> Self { + self.chat_or_inline_message = val; self } diff --git a/src/requests/all/edit_message_caption_inline.rs b/src/requests/all/edit_message_caption_inline.rs deleted file mode 100644 index 962bcebb..00000000 --- a/src/requests/all/edit_message_caption_inline.rs +++ /dev/null @@ -1,83 +0,0 @@ -use serde::Serialize; - -use crate::{ - network, - requests::{Request, ResponseResult}, - types::{InlineKeyboardMarkup, Message, ParseMode}, - Bot, -}; - -/// Use this method to edit captions of messages. On success, if edited message -/// is sent by the bot, the edited Message is returned, otherwise True is -/// returned. -#[serde_with_macros::skip_serializing_none] -#[derive(Debug, Clone, Serialize)] -pub struct EditMessageCaptionInline<'a> { - #[serde(skip_serializing)] - bot: &'a Bot, - - /// Identifier of the inline message - inline_message_id: String, - /// New caption of the message - caption: Option, - /// Send Markdown or HTML, if you want Telegram apps to show bold, italic, - /// fixed-width text or inline URLs in the media caption. - parse_mode: Option, - /// A JSON-serialized object for an inline keyboard. - reply_markup: Option, -} - -#[async_trait::async_trait] -impl Request for EditMessageCaptionInline<'_> { - async fn send(&self) -> ResponseResult { - network::request_json( - self.bot.client(), - self.bot.token(), - "editMessageCaption", - &serde_json::to_string(self).unwrap(), - ) - .await - } -} - -impl<'a> EditMessageCaptionInline<'a> { - pub(crate) fn new(bot: &'a Bot, inline_message_id: I) -> Self - where - I: Into, - { - let inline_message_id = inline_message_id.into(); - Self { - bot, - inline_message_id, - caption: None, - parse_mode: None, - reply_markup: None, - } - } - - pub fn inline_message_id(mut self, val: T) -> Self - where - T: Into, - { - self.inline_message_id = val.into(); - self - } - - pub fn caption(mut self, val: T) -> Self - where - T: Into, - { - self.caption = Some(val.into()); - self - } - - pub fn parse_mode(mut self, val: ParseMode) -> Self { - self.parse_mode = Some(val); - self - } - - pub fn reply_markup(mut self, val: InlineKeyboardMarkup) -> Self { - self.reply_markup = Some(val); - self - } -} diff --git a/src/requests/all/edit_message_live_location.rs b/src/requests/all/edit_message_live_location.rs index 0c83ca00..7a72fd07 100644 --- a/src/requests/all/edit_message_live_location.rs +++ b/src/requests/all/edit_message_live_location.rs @@ -3,7 +3,7 @@ use serde::Serialize; use crate::{ network, requests::{Request, ResponseResult}, - types::{ChatId, InlineKeyboardMarkup, Message}, + types::{ChatOrInlineMessage, InlineKeyboardMarkup, Message}, Bot, }; @@ -17,11 +17,9 @@ pub struct EditMessageLiveLocation<'a> { #[serde(skip_serializing)] bot: &'a Bot, - /// Unique identifier for the target chat or username of the target channel - /// (in the format @channelusername) - chat_id: ChatId, - /// Identifier of the message to edit - message_id: i32, + #[serde(flatten)] + chat_or_inline_message: ChatOrInlineMessage, + /// Latitude of new location latitude: f32, /// Longitude of new location @@ -44,37 +42,23 @@ impl Request for EditMessageLiveLocation<'_> { } impl<'a> EditMessageLiveLocation<'a> { - pub(crate) fn new( + pub(crate) fn new( bot: &'a Bot, - chat_id: C, - message_id: i32, + chat_or_inline_message: ChatOrInlineMessage, latitude: f32, longitude: f32, - ) -> Self - where - C: Into, - { - let chat_id = chat_id.into(); + ) -> Self { Self { bot, - chat_id, - message_id, + chat_or_inline_message, latitude, longitude, reply_markup: None, } } - pub fn chat_id(mut self, val: T) -> Self - where - T: Into, - { - self.chat_id = val.into(); - self - } - - pub fn message_id(mut self, val: i32) -> Self { - self.message_id = val; + pub fn chat_or_inline_message(mut self, val: ChatOrInlineMessage) -> Self { + self.chat_or_inline_message = val; self } diff --git a/src/requests/all/edit_message_live_location_inline.rs b/src/requests/all/edit_message_live_location_inline.rs deleted file mode 100644 index 9b71e5bf..00000000 --- a/src/requests/all/edit_message_live_location_inline.rs +++ /dev/null @@ -1,85 +0,0 @@ -use serde::Serialize; - -use crate::{ - network, - requests::{Request, ResponseResult}, - types::{InlineKeyboardMarkup, Message}, - Bot, -}; - -/// Use this method to edit live location messages. A location can be edited -/// until its live_period expires or editing is explicitly disabled by a call to -/// stopMessageLiveLocation. On success, if the edited message was sent by the -/// bot, the edited Message is returned, otherwise True is returned. -#[serde_with_macros::skip_serializing_none] -#[derive(Debug, Clone, Serialize)] -pub struct EditMessageLiveLocationInline<'a> { - #[serde(skip_serializing)] - bot: &'a Bot, - - /// Identifier of the inline message - inline_message_id: String, - /// Latitude of new location - latitude: f32, - /// Longitude of new location - longitude: f32, - /// A JSON-serialized object for a new inline keyboard. - reply_markup: Option, -} - -#[async_trait::async_trait] -impl Request for EditMessageLiveLocationInline<'_> { - async fn send(&self) -> ResponseResult { - network::request_json( - self.bot.client(), - self.bot.token(), - "editMessageLiveLocation", - &serde_json::to_string(self).unwrap(), - ) - .await - } -} - -impl<'a> EditMessageLiveLocationInline<'a> { - pub(crate) fn new( - bot: &'a Bot, - inline_message_id: I, - latitude: f32, - longitude: f32, - ) -> Self - where - I: Into, - { - let inline_message_id = inline_message_id.into(); - Self { - bot, - inline_message_id, - latitude, - longitude, - reply_markup: None, - } - } - - pub fn inline_message_id(mut self, val: T) -> Self - where - T: Into, - { - self.inline_message_id = val.into(); - self - } - - pub fn latitude(mut self, val: f32) -> Self { - self.latitude = val; - self - } - - pub fn longitude(mut self, val: f32) -> Self { - self.longitude = val; - self - } - - pub fn reply_markup(mut self, val: InlineKeyboardMarkup) -> Self { - self.reply_markup = Some(val); - self - } -} diff --git a/src/requests/all/edit_message_media.rs b/src/requests/all/edit_message_media.rs index 7b2792d0..487b024a 100644 --- a/src/requests/all/edit_message_media.rs +++ b/src/requests/all/edit_message_media.rs @@ -3,7 +3,7 @@ use serde::Serialize; use crate::{ network, requests::{form_builder::FormBuilder, Request, ResponseResult}, - types::{ChatId, InlineKeyboardMarkup, InputMedia, Message}, + types::{ChatOrInlineMessage, InlineKeyboardMarkup, InputMedia, Message}, Bot, }; @@ -20,11 +20,9 @@ pub struct EditMessageMedia<'a> { #[serde(skip_serializing)] bot: &'a Bot, - /// Unique identifier for the target chat or username of the target channel - /// (in the format @channelusername) - chat_id: ChatId, - /// Identifier of the message to edit - message_id: i32, + #[serde(flatten)] + chat_or_inline_message: ChatOrInlineMessage, + /// A JSON-serialized object for a new media content of the message media: InputMedia, /// A JSON-serialized object for a new inline keyboard. @@ -34,13 +32,27 @@ pub struct EditMessageMedia<'a> { #[async_trait::async_trait] impl Request for EditMessageMedia<'_> { async fn send(&self) -> ResponseResult { + let mut params = FormBuilder::new(); + + match &self.chat_or_inline_message { + ChatOrInlineMessage::Chat { + chat_id, + message_id, + } => { + params = params + .add("chat_id", chat_id) + .add("message_id", message_id); + } + ChatOrInlineMessage::Inline { inline_message_id } => { + params = params.add("inline_message_id", inline_message_id); + } + } + network::request_multipart( self.bot.client(), self.bot.token(), "editMessageMedia", - FormBuilder::new() - .add("chat_id", &self.chat_id) - .add("message_id", &self.message_id) + params .add("media", &self.media) .add("reply_markup", &self.reply_markup) .build(), @@ -50,35 +62,21 @@ impl Request for EditMessageMedia<'_> { } impl<'a> EditMessageMedia<'a> { - pub(crate) fn new( + pub(crate) fn new( bot: &'a Bot, - chat_id: C, - message_id: i32, + chat_or_inline_message: ChatOrInlineMessage, media: InputMedia, - ) -> Self - where - C: Into, - { - let chat_id = chat_id.into(); + ) -> Self { Self { bot, - chat_id, - message_id, + chat_or_inline_message, media, reply_markup: None, } } - pub fn chat_id(mut self, val: T) -> Self - where - T: Into, - { - self.chat_id = val.into(); - self - } - - pub fn message_id(mut self, val: i32) -> Self { - self.message_id = val; + pub fn chat_or_inline_message(mut self, val: ChatOrInlineMessage) -> Self { + self.chat_or_inline_message = val; self } diff --git a/src/requests/all/edit_message_media_inline.rs b/src/requests/all/edit_message_media_inline.rs deleted file mode 100644 index ed96c6ac..00000000 --- a/src/requests/all/edit_message_media_inline.rs +++ /dev/null @@ -1,83 +0,0 @@ -use serde::Serialize; - -use crate::{ - network, - requests::{form_builder::FormBuilder, Request, ResponseResult}, - types::{InlineKeyboardMarkup, InputMedia, Message}, - Bot, -}; - -/// Use this method to edit animation, audio, document, photo, or video -/// messages. If a message is a part of a message album, then it can be edited -/// only to a photo or a video. Otherwise, message type can be changed -/// arbitrarily. When inline message is edited, new file can't be uploaded. Use -/// previously uploaded file via its file_id or specify a URL. On success, if -/// the edited message was sent by the bot, the edited Message is returned, -/// otherwise True is returned. -#[serde_with_macros::skip_serializing_none] -#[derive(Debug, Clone, Serialize)] -pub struct EditMessageMediaInline<'a> { - #[serde(skip_serializing)] - bot: &'a Bot, - - /// Identifier of the inline message - inline_message_id: String, - /// A JSON-serialized object for a new media content of the message - media: InputMedia, - /// A JSON-serialized object for a new inline keyboard. - reply_markup: Option, -} - -#[async_trait::async_trait] -impl Request for EditMessageMediaInline<'_> { - async fn send(&self) -> ResponseResult { - network::request_multipart( - self.bot.client(), - self.bot.token(), - "editMessageMedia", - FormBuilder::new() - .add("inline_message_id", &self.inline_message_id) - .add("media", &self.media) - .add("reply_markup", &self.reply_markup) - .build(), - ) - .await - } -} - -impl<'a> EditMessageMediaInline<'a> { - pub(crate) fn new( - bot: &'a Bot, - inline_message_id: I, - media: InputMedia, - ) -> Self - where - I: Into, - { - let inline_message_id = inline_message_id.into(); - Self { - bot, - inline_message_id, - media, - reply_markup: None, - } - } - - pub fn inline_message_id(mut self, val: T) -> Self - where - T: Into, - { - self.inline_message_id = val.into(); - self - } - - pub fn media(mut self, val: InputMedia) -> Self { - self.media = val; - self - } - - pub fn reply_markup(mut self, val: InlineKeyboardMarkup) -> Self { - self.reply_markup = Some(val); - self - } -} diff --git a/src/requests/all/edit_message_reply_markup.rs b/src/requests/all/edit_message_reply_markup.rs index 5c2c76ba..7878cdd1 100644 --- a/src/requests/all/edit_message_reply_markup.rs +++ b/src/requests/all/edit_message_reply_markup.rs @@ -3,7 +3,7 @@ use serde::Serialize; use crate::{ network, requests::{Request, ResponseResult}, - types::{ChatId, InlineKeyboardMarkup, Message}, + types::{ChatOrInlineMessage, InlineKeyboardMarkup, Message}, Bot, }; @@ -16,11 +16,9 @@ pub struct EditMessageReplyMarkup<'a> { #[serde(skip_serializing)] bot: &'a Bot, - /// Unique identifier for the target chat or username of the target channel - /// (in the format @channelusername) - chat_id: ChatId, - /// Identifier of the message to edit - message_id: i32, + #[serde(flatten)] + chat_or_inline_message: ChatOrInlineMessage, + /// A JSON-serialized object for an inline keyboard. reply_markup: Option, } @@ -39,29 +37,19 @@ impl Request for EditMessageReplyMarkup<'_> { } impl<'a> EditMessageReplyMarkup<'a> { - pub(crate) fn new(bot: &'a Bot, chat_id: C, message_id: i32) -> Self - where - C: Into, - { - let chat_id = chat_id.into(); + pub(crate) fn new( + bot: &'a Bot, + chat_or_inline_message: ChatOrInlineMessage, + ) -> Self { Self { bot, - chat_id, - message_id, + chat_or_inline_message, reply_markup: None, } } - pub fn chat_id(mut self, val: T) -> Self - where - T: Into, - { - self.chat_id = val.into(); - self - } - - pub fn message_id(mut self, val: i32) -> Self { - self.message_id = val; + pub fn chat_or_inline_message(mut self, val: ChatOrInlineMessage) -> Self { + self.chat_or_inline_message = val; self } diff --git a/src/requests/all/edit_message_reply_markup_inline.rs b/src/requests/all/edit_message_reply_markup_inline.rs deleted file mode 100644 index b143c8ac..00000000 --- a/src/requests/all/edit_message_reply_markup_inline.rs +++ /dev/null @@ -1,63 +0,0 @@ -use serde::Serialize; - -use crate::{ - network, - requests::{Request, ResponseResult}, - types::{InlineKeyboardMarkup, Message}, - Bot, -}; - -/// Use this method to edit only the reply markup of messages. On success, if -/// edited message is sent by the bot, the edited Message is returned, otherwise -/// True is returned. -#[serde_with_macros::skip_serializing_none] -#[derive(Debug, Clone, Serialize)] -pub struct EditMessageReplyMarkupInline<'a> { - #[serde(skip_serializing)] - bot: &'a Bot, - - /// Identifier of the inline message - inline_message_id: String, - /// A JSON-serialized object for an inline keyboard. - reply_markup: Option, -} - -#[async_trait::async_trait] -impl Request for EditMessageReplyMarkupInline<'_> { - async fn send(&self) -> ResponseResult { - network::request_json( - self.bot.client(), - self.bot.token(), - "editMessageReplyMarkup", - &serde_json::to_string(self).unwrap(), - ) - .await - } -} - -impl<'a> EditMessageReplyMarkupInline<'a> { - pub(crate) fn new(bot: &'a Bot, inline_message_id: I) -> Self - where - I: Into, - { - let inline_message_id = inline_message_id.into(); - Self { - bot, - inline_message_id, - reply_markup: None, - } - } - - pub fn inline_message_id(mut self, val: T) -> Self - where - T: Into, - { - self.inline_message_id = val.into(); - self - } - - pub fn reply_markup(mut self, val: InlineKeyboardMarkup) -> Self { - self.reply_markup = Some(val); - self - } -} diff --git a/src/requests/all/edit_message_text.rs b/src/requests/all/edit_message_text.rs index 988ac484..61b80659 100644 --- a/src/requests/all/edit_message_text.rs +++ b/src/requests/all/edit_message_text.rs @@ -3,7 +3,7 @@ use serde::Serialize; use crate::{ network, requests::{Request, ResponseResult}, - types::{ChatId, InlineKeyboardMarkup, Message, ParseMode}, + types::{ChatOrInlineMessage, InlineKeyboardMarkup, Message, ParseMode}, Bot, }; @@ -16,11 +16,9 @@ pub struct EditMessageText<'a> { #[serde(skip_serializing)] bot: &'a Bot, - /// Unique identifier for the target chat or username of the target channel - /// (in the format @channelusername) - chat_id: ChatId, - /// Identifier of the message to edit - message_id: i32, + #[serde(flatten)] + chat_or_inline_message: ChatOrInlineMessage, + /// New text of the message text: String, /// Send Markdown or HTML, if you want Telegram apps to show bold, italic, @@ -46,39 +44,26 @@ impl Request for EditMessageText<'_> { } impl<'a> EditMessageText<'a> { - pub(crate) fn new( + pub(crate) fn new( bot: &'a Bot, - chat_id: C, - message_id: i32, + chat_or_inline_message: ChatOrInlineMessage, text: T, ) -> Self where - C: Into, T: Into, { - let chat_id = chat_id.into(); - let text = text.into(); Self { bot, - chat_id, - message_id, - text, + chat_or_inline_message, + text: text.into(), parse_mode: None, disable_web_page_preview: None, reply_markup: None, } } - pub fn chat_id(mut self, val: T) -> Self - where - T: Into, - { - self.chat_id = val.into(); - self - } - - pub fn message_id(mut self, val: i32) -> Self { - self.message_id = val; + pub fn chat_or_inline_message(mut self, val: ChatOrInlineMessage) -> Self { + self.chat_or_inline_message = val; self } diff --git a/src/requests/all/edit_message_text_inline.rs b/src/requests/all/edit_message_text_inline.rs deleted file mode 100644 index 968da009..00000000 --- a/src/requests/all/edit_message_text_inline.rs +++ /dev/null @@ -1,93 +0,0 @@ -use serde::Serialize; - -use crate::{ - network, - requests::{Request, ResponseResult}, - types::{InlineKeyboardMarkup, Message, ParseMode}, - Bot, -}; - -/// Use this method to edit text and game messages. On success, if edited -/// message is sent by the bot, the edited Message is returned, otherwise True -/// is returned. -#[serde_with_macros::skip_serializing_none] -#[derive(Debug, Clone, Serialize)] -pub struct EditMessageTextInline<'a> { - #[serde(skip_serializing)] - bot: &'a Bot, - - /// Identifier of the inline message - inline_message_id: String, - /// New text of the message - 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. - parse_mode: Option, - /// Disables link previews for links in this message - disable_web_page_preview: Option, - /// A JSON-serialized object for an inline keyboard. - reply_markup: Option, -} - -#[async_trait::async_trait] -impl Request for EditMessageTextInline<'_> { - async fn send(&self) -> ResponseResult { - network::request_json( - self.bot.client(), - self.bot.token(), - "editMessageText", - &serde_json::to_string(self).unwrap(), - ) - .await - } -} - -impl<'a> EditMessageTextInline<'a> { - pub(crate) fn new(bot: &'a Bot, inline_message_id: I, text: T) -> Self - where - I: Into, - T: Into, - { - let inline_message_id = inline_message_id.into(); - let text = text.into(); - Self { - bot, - inline_message_id, - text, - parse_mode: None, - disable_web_page_preview: None, - reply_markup: None, - } - } - - pub fn inline_message_id(mut self, val: T) -> Self - where - T: Into, - { - self.inline_message_id = val.into(); - self - } - - pub fn text(mut self, val: T) -> Self - where - T: Into, - { - self.text = val.into(); - self - } - - pub fn parse_mode(mut self, val: ParseMode) -> Self { - self.parse_mode = Some(val); - self - } - - pub fn disable_web_page_preview(mut self, val: bool) -> Self { - self.disable_web_page_preview = Some(val); - self - } - - pub fn reply_markup(mut self, val: InlineKeyboardMarkup) -> Self { - self.reply_markup = Some(val); - self - } -} diff --git a/src/requests/all/get_game_high_scores.rs b/src/requests/all/get_game_high_scores.rs index fe004f0c..02e727bb 100644 --- a/src/requests/all/get_game_high_scores.rs +++ b/src/requests/all/get_game_high_scores.rs @@ -3,7 +3,7 @@ use serde::Serialize; use crate::{ network, requests::{Request, ResponseResult}, - types::{ChatId, GameHighScore}, + types::{ChatOrInlineMessage, GameHighScore}, Bot, }; @@ -19,12 +19,11 @@ pub struct GetGameHighScores<'a> { #[serde(skip_serializing)] bot: &'a Bot, + #[serde(flatten)] + chat_or_inline_message: ChatOrInlineMessage, + /// Target user id user_id: i32, - /// Unique identifier for the target chat - chat_id: ChatId, - /// Identifier of the sent message - message_id: i32, } #[async_trait::async_trait] @@ -41,34 +40,20 @@ impl Request> for GetGameHighScores<'_> { } impl<'a> GetGameHighScores<'a> { - pub(crate) fn new( + pub(crate) fn new( bot: &'a Bot, - chat_id: C, - message_id: i32, + chat_or_inline_message: ChatOrInlineMessage, user_id: i32, - ) -> Self - where - C: Into, - { - let chat_id = chat_id.into(); + ) -> Self { Self { bot, - chat_id, - message_id, + chat_or_inline_message, user_id, } } - pub fn chat_id(mut self, val: C) -> Self - where - C: Into, - { - self.chat_id = val.into(); - self - } - - pub fn message_id(mut self, val: i32) -> Self { - self.message_id = val; + pub fn chat_or_inline_message(mut self, val: ChatOrInlineMessage) -> Self { + self.chat_or_inline_message = val; self } diff --git a/src/requests/all/get_game_high_scores_inline.rs b/src/requests/all/get_game_high_scores_inline.rs deleted file mode 100644 index d64f0d0e..00000000 --- a/src/requests/all/get_game_high_scores_inline.rs +++ /dev/null @@ -1,70 +0,0 @@ -use serde::Serialize; - -use crate::{ - network, - requests::{Request, ResponseResult}, - types::GameHighScore, - Bot, -}; - -/// Use this method to get data for high score tables. Will return the score of -/// the specified user and several of his neighbors in a game. On success, -/// returns an Array of GameHighScore objects.This method will currently return -/// scores for the target user, plus two of his closest neighbors on each side. -/// Will also return the top three users if the user and his neighbors are not -/// among them. Please note that this behavior is subject to change. -#[serde_with_macros::skip_serializing_none] -#[derive(Debug, Clone, Serialize)] -pub struct GetGameHighScoresInline<'a> { - #[serde(skip_serializing)] - bot: &'a Bot, - - /// Identifier of the inline message - inline_message_id: String, - /// Target user id - user_id: i32, -} - -#[async_trait::async_trait] -impl Request> for GetGameHighScoresInline<'_> { - async fn send(&self) -> ResponseResult> { - network::request_json( - self.bot.client(), - self.bot.token(), - "getGameHighScores", - &serde_json::to_string(self).unwrap(), - ) - .await - } -} - -impl<'a> GetGameHighScoresInline<'a> { - pub(crate) fn new( - bot: &'a Bot, - inline_message_id: I, - user_id: i32, - ) -> Self - where - I: Into, - { - let inline_message_id = inline_message_id.into(); - Self { - bot, - inline_message_id, - user_id, - } - } - - pub fn inline_message_id(mut self, val: T) -> Self - where - T: Into, - { - self.inline_message_id = val.into(); - self - } - - pub fn user_id(mut self, val: i32) -> Self { - self.user_id = val; - self - } -} diff --git a/src/requests/all/mod.rs b/src/requests/all/mod.rs index 029ba5c0..7d8e6845 100644 --- a/src/requests/all/mod.rs +++ b/src/requests/all/mod.rs @@ -10,15 +10,10 @@ mod delete_message; mod delete_sticker_from_set; mod delete_webhook; mod edit_message_caption; -mod edit_message_caption_inline; mod edit_message_live_location; -mod edit_message_live_location_inline; mod edit_message_media; -mod edit_message_media_inline; mod edit_message_reply_markup; -mod edit_message_reply_markup_inline; mod edit_message_text; -mod edit_message_text_inline; mod export_chat_invite_link; mod forward_message; mod get_chat; @@ -27,7 +22,6 @@ mod get_chat_member; mod get_chat_members_count; mod get_file; mod get_game_high_scores; -mod get_game_high_scores_inline; mod get_me; mod get_sticker_set; mod get_updates; @@ -62,11 +56,9 @@ mod set_chat_photo; mod set_chat_sticker_set; mod set_chat_title; mod set_game_score; -mod set_game_score_inline; mod set_sticker_position_in_set; mod set_webhook; mod stop_message_live_location; -mod stop_message_live_location_inline; mod stop_poll; mod unban_chat_member; mod unpin_chat_message; @@ -84,15 +76,10 @@ pub use delete_message::*; pub use delete_sticker_from_set::*; pub use delete_webhook::*; pub use edit_message_caption::*; -pub use edit_message_caption_inline::*; pub use edit_message_live_location::*; -pub use edit_message_live_location_inline::*; pub use edit_message_media::*; -pub use edit_message_media_inline::*; pub use edit_message_reply_markup::*; -pub use edit_message_reply_markup_inline::*; pub use edit_message_text::*; -pub use edit_message_text_inline::*; pub use export_chat_invite_link::*; pub use forward_message::*; pub use get_chat::*; @@ -101,7 +88,6 @@ pub use get_chat_member::*; pub use get_chat_members_count::*; pub use get_file::*; pub use get_game_high_scores::*; -pub use get_game_high_scores_inline::*; pub use get_me::*; pub use get_sticker_set::*; pub use get_updates::*; @@ -136,12 +122,10 @@ pub use set_chat_photo::*; pub use set_chat_sticker_set::*; pub use set_chat_title::*; pub use set_game_score::*; -pub use set_game_score_inline::*; pub use set_sticker_position_in_set::*; pub use set_webhook::*; pub use std::pin::Pin; pub use stop_message_live_location::*; -pub use stop_message_live_location_inline::*; pub use stop_poll::*; pub use unban_chat_member::*; pub use unpin_chat_message::*; diff --git a/src/requests/all/set_game_score.rs b/src/requests/all/set_game_score.rs index f8a9b06a..12e736d3 100644 --- a/src/requests/all/set_game_score.rs +++ b/src/requests/all/set_game_score.rs @@ -3,7 +3,7 @@ use serde::Serialize; use crate::{ network, requests::{Request, ResponseResult}, - types::{ChatId, Message}, + types::{ChatOrInlineMessage, Message}, Bot, }; @@ -17,10 +17,9 @@ pub struct SetGameScore<'a> { #[serde(skip_serializing)] bot: &'a Bot, - /// Unique identifier for the target chat - chat_id: ChatId, - /// Identifier of the sent message - message_id: i32, + #[serde(flatten)] + chat_or_inline_message: ChatOrInlineMessage, + /// User identifier user_id: i32, /// New score, must be non-negative @@ -47,21 +46,15 @@ impl Request for SetGameScore<'_> { } impl<'a> SetGameScore<'a> { - pub(crate) fn new( + pub(crate) fn new( bot: &'a Bot, - chat_id: C, - message_id: i32, + chat_or_inline_message: ChatOrInlineMessage, user_id: i32, score: i32, - ) -> Self - where - C: Into, - { - let chat_id = chat_id.into(); + ) -> Self { Self { bot, - chat_id, - message_id, + chat_or_inline_message, user_id, score, force: None, @@ -69,16 +62,8 @@ impl<'a> SetGameScore<'a> { } } - pub fn chat_id(mut self, val: C) -> Self - where - C: Into, - { - self.chat_id = val.into(); - self - } - - pub fn message_id(mut self, val: i32) -> Self { - self.message_id = val; + pub fn chat_or_inline_message(mut self, val: ChatOrInlineMessage) -> Self { + self.chat_or_inline_message = val; self } diff --git a/src/requests/all/set_game_score_inline.rs b/src/requests/all/set_game_score_inline.rs deleted file mode 100644 index 329db892..00000000 --- a/src/requests/all/set_game_score_inline.rs +++ /dev/null @@ -1,95 +0,0 @@ -use serde::Serialize; - -use crate::{ - network, - requests::{Request, ResponseResult}, - types::Message, - Bot, -}; - -/// Use this method to set the score of the specified user in a game. On -/// success, if the message was sent by the bot, returns the edited Message, -/// otherwise returns True. Returns an error, if the new score is not greater -/// than the user's current score in the chat and force is False. -#[serde_with_macros::skip_serializing_none] -#[derive(Debug, Clone, Serialize)] -pub struct SetGameScoreInline<'a> { - #[serde(skip_serializing)] - bot: &'a Bot, - - /// Identifier of the inline message - inline_message_id: String, - /// User identifier - user_id: i32, - /// New score, must be non-negative - score: i32, - /// Pass True, if the high score is allowed to decrease. This can be useful - /// when fixing mistakes or banning cheaters - force: Option, - /// Pass True, if the game message should not be automatically edited to - /// include the current scoreboard - disable_edit_message: Option, -} - -#[async_trait::async_trait] -impl Request for SetGameScoreInline<'_> { - async fn send(&self) -> ResponseResult { - network::request_json( - self.bot.client(), - self.bot.token(), - "setGameScore", - &serde_json::to_string(self).unwrap(), - ) - .await - } -} - -impl<'a> SetGameScoreInline<'a> { - pub(crate) fn new( - bot: &'a Bot, - inline_message_id: I, - user_id: i32, - score: i32, - ) -> Self - where - I: Into, - { - let inline_message_id = inline_message_id.into(); - Self { - bot, - inline_message_id, - user_id, - score, - force: None, - disable_edit_message: None, - } - } - - pub fn inline_message_id(mut self, val: T) -> Self - where - T: Into, - { - self.inline_message_id = val.into(); - self - } - - pub fn user_id(mut self, val: i32) -> Self { - self.user_id = val; - self - } - - pub fn score(mut self, val: i32) -> Self { - self.score = val; - self - } - - pub fn force(mut self, val: bool) -> Self { - self.force = Some(val); - self - } - - pub fn disable_edit_message(mut self, val: bool) -> Self { - self.disable_edit_message = Some(val); - self - } -} diff --git a/src/requests/all/stop_message_live_location.rs b/src/requests/all/stop_message_live_location.rs index fb49f696..ece22938 100644 --- a/src/requests/all/stop_message_live_location.rs +++ b/src/requests/all/stop_message_live_location.rs @@ -3,7 +3,7 @@ use serde::Serialize; use crate::{ network, requests::{Request, ResponseResult}, - types::{ChatId, InlineKeyboardMarkup, Message}, + types::{ChatOrInlineMessage, InlineKeyboardMarkup, Message}, Bot, }; @@ -16,11 +16,9 @@ pub struct StopMessageLiveLocation<'a> { #[serde(skip_serializing)] bot: &'a Bot, - /// Unique identifier for the target chat or username of the target channel - /// (in the format @channelusername) - chat_id: ChatId, - /// Identifier of the message with live location to stop - message_id: i32, + #[serde(flatten)] + chat_or_inline_message: ChatOrInlineMessage, + /// A JSON-serialized object for a new inline keyboard. reply_markup: Option, } @@ -39,29 +37,19 @@ impl Request for StopMessageLiveLocation<'_> { } impl<'a> StopMessageLiveLocation<'a> { - pub(crate) fn new(bot: &'a Bot, chat_id: C, message_id: i32) -> Self - where - C: Into, - { - let chat_id = chat_id.into(); + pub(crate) fn new( + bot: &'a Bot, + chat_or_inline_message: ChatOrInlineMessage, + ) -> Self { Self { bot, - chat_id, - message_id, + chat_or_inline_message, reply_markup: None, } } - pub fn chat_id(mut self, val: T) -> Self - where - T: Into, - { - self.chat_id = val.into(); - self - } - - pub fn message_id(mut self, val: i32) -> Self { - self.message_id = val; + pub fn chat_or_inline_message(mut self, val: ChatOrInlineMessage) -> Self { + self.chat_or_inline_message = val; self } diff --git a/src/requests/all/stop_message_live_location_inline.rs b/src/requests/all/stop_message_live_location_inline.rs deleted file mode 100644 index 004c2742..00000000 --- a/src/requests/all/stop_message_live_location_inline.rs +++ /dev/null @@ -1,63 +0,0 @@ -use serde::Serialize; - -use crate::{ - network, - requests::{Request, ResponseResult}, - types::{InlineKeyboardMarkup, Message}, - Bot, -}; - -/// Use this method to stop updating a live location message before live_period -/// expires. On success, if the message was sent by the bot, the sent Message is -/// returned, otherwise True is returned. -#[serde_with_macros::skip_serializing_none] -#[derive(Debug, Clone, Serialize)] -pub struct StopMessageLiveLocationInline<'a> { - #[serde(skip_serializing)] - bot: &'a Bot, - - /// Identifier of the inline message - inline_message_id: String, - /// A JSON-serialized object for a new inline keyboard. - reply_markup: Option, -} - -#[async_trait::async_trait] -impl Request for StopMessageLiveLocationInline<'_> { - async fn send(&self) -> ResponseResult { - network::request_json( - self.bot.client(), - self.bot.token(), - "stopMessageLiveLocation", - &serde_json::to_string(self).unwrap(), - ) - .await - } -} - -impl<'a> StopMessageLiveLocationInline<'a> { - pub(crate) fn new(bot: &'a Bot, inline_message_id: I) -> Self - where - I: Into, - { - let inline_message_id = inline_message_id.into(); - Self { - bot, - inline_message_id, - reply_markup: None, - } - } - - pub fn inline_message_id(mut self, val: T) -> Self - where - T: Into, - { - self.inline_message_id = val.into(); - self - } - - pub fn reply_markup(mut self, val: InlineKeyboardMarkup) -> Self { - self.reply_markup = Some(val); - self - } -} diff --git a/src/types/chat_or_inline_message.rs b/src/types/chat_or_inline_message.rs new file mode 100644 index 00000000..524b94c8 --- /dev/null +++ b/src/types/chat_or_inline_message.rs @@ -0,0 +1,11 @@ +use crate::types::ChatId; + +use serde::{Deserialize, Serialize}; + +/// A chat message or inline message. +#[derive(Serialize, Deserialize, Clone, Eq, Hash, PartialEq, Debug)] +#[serde(untagged)] +pub enum ChatOrInlineMessage { + Chat { chat_id: ChatId, message_id: i32 }, + Inline { inline_message_id: i32 }, +} diff --git a/src/types/mod.rs b/src/types/mod.rs index c44706b6..651271e5 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -9,6 +9,7 @@ pub use chat::*; pub use chat_action::*; pub use chat_id::*; pub use chat_member::*; +pub use chat_or_inline_message::*; pub use chat_permissions::*; pub use chat_photo::*; pub use chosen_inline_result::*; @@ -93,6 +94,7 @@ mod chat; mod chat_action; mod chat_id; mod chat_member; +mod chat_or_inline_message; mod chat_permissions; mod chat_photo; mod chosen_inline_result;