From 8990b4a785c0c6c4b08b5914f0f50717eb17aa9f Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Thu, 16 Jul 2020 19:52:40 +0600 Subject: [PATCH] Implement a default ParseMode --- CHANGELOG.md | 6 +++ src/bot/api.rs | 138 +++++++++++++++++++++++++++++++++++++++++++++---- src/bot/mod.rs | 37 ++++++++++++- 3 files changed, 170 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6035a332..9f3d5c72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [0.3.0] - ??? +### Added + - `BotBuilder`, which allows setting a default `ParseMode`. + +### Deprecated + - `Bot::{from_env_with_client, new, with_client}`. + ### Changed - Now methods which can send file to Telegram returns tokio::io::Result. Early its could panic. ([issue 216](https://github.com/teloxide/teloxide/issues/216)) - Now provided description of unknown telegram error, by splitting ApiErrorKind at `ApiErrorKind` and `ApiErrorKindKnown` enums. ([issue 199](https://github.com/teloxide/teloxide/issues/199)) diff --git a/src/bot/api.rs b/src/bot/api.rs index a1058d49..9c33db90 100644 --- a/src/bot/api.rs +++ b/src/bot/api.rs @@ -108,6 +108,12 @@ impl Bot { /// - `chat_id`: Unique identifier for the target chat or username of the /// target supergroup or channel (in the format `@channelusername`). /// - `text`: Text of the message to be sent. + /// + /// # Notes + /// Uses [a default parse mode] if specified in [`BotBuilder`]. + /// + /// [a default parse mode]: crate::BotBuilder::parse_mode + /// [`BotBuilder`]: crate::BotBuilder pub fn send_message( self: &Arc, chat_id: C, @@ -117,7 +123,13 @@ impl Bot { C: Into, T: Into, { - SendMessage::new(Arc::clone(self), chat_id, text) + match self.parse_mode { + None => SendMessage::new(Arc::clone(self), chat_id, text), + Some(parse_mode) => { + SendMessage::new(Arc::clone(self), chat_id, text) + .parse_mode(parse_mode) + } + } } /// Use this method to forward messages of any kind. @@ -166,6 +178,12 @@ impl Bot { /// [`InputFile::FileId`]: crate::types::InputFile::FileId /// /// [More info on Sending Files »]: https://core.telegram.org/bots/api#sending-files + /// + /// # Notes + /// Uses [a default parse mode] if specified in [`BotBuilder`]. + /// + /// [a default parse mode]: crate::BotBuilder::parse_mode + /// [`BotBuilder`]: crate::BotBuilder pub fn send_photo( self: &Arc, chat_id: C, @@ -174,7 +192,13 @@ impl Bot { where C: Into, { - SendPhoto::new(Arc::clone(self), chat_id, photo) + match self.parse_mode { + None => SendPhoto::new(Arc::clone(self), chat_id, photo), + Some(parse_mode) => { + SendPhoto::new(Arc::clone(self), chat_id, photo) + .parse_mode(parse_mode) + } + } } /// @@ -182,6 +206,12 @@ impl Bot { /// # Params /// - `chat_id`: Unique identifier for the target chat or username of the /// target supergroup or channel (in the format `@channelusername`). + /// + /// # Notes + /// Uses [a default parse mode] if specified in [`BotBuilder`]. + /// + /// [a default parse mode]: crate::BotBuilder::parse_mode + /// [`BotBuilder`]: crate::BotBuilder pub fn send_audio( self: &Arc, chat_id: C, @@ -190,7 +220,13 @@ impl Bot { where C: Into, { - SendAudio::new(Arc::clone(self), chat_id, audio) + match self.parse_mode { + None => SendAudio::new(Arc::clone(self), chat_id, audio), + Some(parse_mode) => { + SendAudio::new(Arc::clone(self), chat_id, audio) + .parse_mode(parse_mode) + } + } } /// Use this method to send general files. @@ -211,6 +247,12 @@ impl Bot { /// `multipart/form-data`. [More info on Sending Files »]. /// /// [More info on Sending Files »]: https://core.telegram.org/bots/api#sending-files + /// + /// # Notes + /// Uses [a default parse mode] if specified in [`BotBuilder`]. + /// + /// [a default parse mode]: crate::BotBuilder::parse_mode + /// [`BotBuilder`]: crate::BotBuilder pub fn send_document( self: &Arc, chat_id: C, @@ -219,7 +261,13 @@ impl Bot { where C: Into, { - SendDocument::new(Arc::clone(self), chat_id, document) + match self.parse_mode { + None => SendDocument::new(Arc::clone(self), chat_id, document), + Some(parse_mode) => { + SendDocument::new(Arc::clone(self), chat_id, document) + .parse_mode(parse_mode) + } + } } /// Use this method to send video files, Telegram clients support mp4 videos @@ -243,6 +291,12 @@ impl Bot { /// [`InputFile::File`]: crate::types::InputFile::File /// [`InputFile::Url`]: crate::types::InputFile::Url /// [`InputFile::FileId`]: crate::types::InputFile::FileId + /// + /// # Notes + /// Uses [a default parse mode] if specified in [`BotBuilder`]. + /// + /// [a default parse mode]: crate::BotBuilder::parse_mode + /// [`BotBuilder`]: crate::BotBuilder pub fn send_video( self: &Arc, chat_id: C, @@ -251,7 +305,13 @@ impl Bot { where C: Into, { - SendVideo::new(Arc::clone(self), chat_id, video) + match self.parse_mode { + None => SendVideo::new(Arc::clone(self), chat_id, video), + Some(parse_mode) => { + SendVideo::new(Arc::clone(self), chat_id, video) + .parse_mode(parse_mode) + } + } } /// Use this method to send animation files (GIF or H.264/MPEG-4 AVC video @@ -266,6 +326,12 @@ impl Bot { /// - `chat_id`: Unique identifier for the target chat or username of the /// target supergroup or channel (in the format `@channelusername`). /// - `animation`: Animation to send. + /// + /// # Notes + /// Uses [a default parse mode] if specified in [`BotBuilder`]. + /// + /// [a default parse mode]: crate::BotBuilder::parse_mode + /// [`BotBuilder`]: crate::BotBuilder pub fn send_animation( self: &Arc, chat_id: C, @@ -274,7 +340,13 @@ impl Bot { where C: Into, { - SendAnimation::new(Arc::clone(self), chat_id, animation) + match self.parse_mode { + None => SendAnimation::new(Arc::clone(self), chat_id, animation), + Some(parse_mode) => { + SendAnimation::new(Arc::clone(self), chat_id, animation) + .parse_mode(parse_mode) + } + } } /// Use this method to send audio files, if you want Telegram clients to @@ -304,6 +376,12 @@ impl Bot { /// [`InputFile::Url`]: crate::types::InputFile::Url /// [`InputFile::FileId`]: crate::types::InputFile::FileId /// [More info on Sending Files »]: https://core.telegram.org/bots/api#sending-files + /// + /// # Notes + /// Uses [a default parse mode] if specified in [`BotBuilder`]. + /// + /// [a default parse mode]: crate::BotBuilder::parse_mode + /// [`BotBuilder`]: crate::BotBuilder pub fn send_voice( self: &Arc, chat_id: C, @@ -312,7 +390,13 @@ impl Bot { where C: Into, { - SendVoice::new(Arc::clone(self), chat_id, voice) + match self.parse_mode { + None => SendVoice::new(Arc::clone(self), chat_id, voice), + Some(parse_mode) => { + SendVoice::new(Arc::clone(self), chat_id, voice) + .parse_mode(parse_mode) + } + } } /// As of [v.4.0], Telegram clients support rounded square mp4 videos of up @@ -335,6 +419,7 @@ impl Bot { /// [`InputFile::Url`]: crate::types::InputFile::Url /// [`InputFile::FileId`]: crate::types::InputFile::FileId /// [More info on Sending Files »]: https://core.telegram.org/bots/api#sending-files + pub fn send_video_note( self: &Arc, chat_id: C, @@ -515,6 +600,7 @@ impl Bot { Q: Into, O: Into>, { + // FIXME: parse_mode SendPoll::new(Arc::clone(self), chat_id, question, options) } @@ -1044,6 +1130,12 @@ impl Bot { /// /// [`Message`]: crate::types::Message /// [`True`]: crate::types::True + /// + /// # Notes + /// Uses [a default parse mode] if specified in [`BotBuilder`]. + /// + /// [a default parse mode]: crate::BotBuilder::parse_mode + /// [`BotBuilder`]: crate::BotBuilder pub fn edit_message_text( self: &Arc, chat_or_inline_message: ChatOrInlineMessage, @@ -1052,7 +1144,19 @@ impl Bot { where T: Into, { - EditMessageText::new(Arc::clone(self), chat_or_inline_message, text) + match self.parse_mode { + None => EditMessageText::new( + Arc::clone(self), + chat_or_inline_message, + text, + ), + Some(parse_mode) => EditMessageText::new( + Arc::clone(self), + chat_or_inline_message, + text, + ) + .parse_mode(parse_mode), + } } /// Use this method to edit captions of messages. @@ -1064,11 +1168,27 @@ impl Bot { /// /// [`Message`]: crate::types::Message /// [`True`]: crate::types::True + /// + /// # Notes + /// Uses [a default parse mode] if specified in [`BotBuilder`]. + /// + /// [a default parse mode]: crate::BotBuilder::parse_mode + /// [`BotBuilder`]: crate::BotBuilder pub fn edit_message_caption( self: &Arc, chat_or_inline_message: ChatOrInlineMessage, ) -> EditMessageCaption { - EditMessageCaption::new(Arc::clone(self), chat_or_inline_message) + match self.parse_mode { + None => EditMessageCaption::new( + Arc::clone(self), + chat_or_inline_message, + ), + Some(parse_mode) => EditMessageCaption::new( + Arc::clone(self), + chat_or_inline_message, + ) + .parse_mode(parse_mode), + } } /// Use this method to edit animation, audio, document, photo, or video diff --git a/src/bot/mod.rs b/src/bot/mod.rs index 6a357b17..5ea0b550 100644 --- a/src/bot/mod.rs +++ b/src/bot/mod.rs @@ -1,3 +1,4 @@ +use crate::types::ParseMode; use reqwest::Client; use std::sync::Arc; @@ -9,6 +10,7 @@ mod download; pub struct Bot { token: String, client: Client, + parse_mode: Option, } impl Bot { @@ -64,7 +66,7 @@ impl Bot { where S: Into, { - Arc::new(Self { token: token.into(), client }) + Arc::new(Self { token: token.into(), client, parse_mode: None }) } } @@ -84,12 +86,13 @@ impl Bot { pub struct BotBuilder { token: Option, client: Option, + parse_mode: Option, } impl BotBuilder { #[must_use] pub fn new() -> Self { - Self { token: None, client: None } + Self::default() } /// Specifies a custom HTTPS client. Otherwise, the default will be used. @@ -112,6 +115,35 @@ impl BotBuilder { self } + /// Specifies [`ParseMode`], which will be used during all calls to: + /// + /// - [`send_message`] + /// - [`send_photo`] + /// - [`send_video`] + /// - [`send_audio`] + /// - [`send_document`] + /// - [`send_animation`] + /// - [`send_voice`] + /// - [`send_poll`] + /// - [`edit_message_text`] + /// - [`edit_message_caption`] + /// + /// [`send_message`]: crate::Bot::send_message + /// [`send_photo`]: crate::Bot::send_photo + /// [`send_video`]: crate::Bot::send_video + /// [`send_audio`]: crate::Bot::send_audio + /// [`send_document`]: crate::Bot::send_document + /// [`send_animation`]: crate::Bot::send_animation + /// [`send_voice`]: crate::Bot::send_voice + /// [`send_poll`]: crate::Bot::send_poll + /// [`edit_message_text`]: crate::Bot::edit_message_text + /// [`edit_message_caption`]: crate::Bot::edit_message_caption + #[must_use] + pub fn parse_mode(mut self, parse_mode: ParseMode) -> Self { + self.parse_mode = Some(parse_mode); + self + } + /// Builds [`Bot`]. /// /// # Panics @@ -128,6 +160,7 @@ impl BotBuilder { std::env::var("TELOXIDE_TOKEN") .expect("Cannot get the TELOXIDE_TOKEN env variable"), ), + parse_mode: self.parse_mode, } } }