mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 14:35:36 +01:00
Improve documentation and fix typos
This commit is contained in:
parent
f2e8e86509
commit
0993cbc5ae
2 changed files with 69 additions and 50 deletions
|
@ -88,7 +88,6 @@ define_message_ext! {
|
|||
(filter_migration, Message::chat_migration),
|
||||
(filter_migration_from, Message::migrate_from_chat_id),
|
||||
(filter_migration_to, Message::migrate_to_chat_id),
|
||||
|
||||
(filter_reply_to_message, Message::reply_to_message),
|
||||
(filter_forward_from, Message::forward_from),
|
||||
// Rest variants of a MessageKind
|
||||
|
|
|
@ -30,60 +30,80 @@
|
|||
//! <img src=https://github.com/teloxide/teloxide/raw/master/media/throw-dice.gif width=420px />
|
||||
//! </kbd>
|
||||
//! </div>
|
||||
//!
|
||||
//! ## Working with Updates and Messages
|
||||
//! There're a great number of [update kinds](https://docs.rs/teloxide/latest/teloxide/types/enum.UpdateKind.html) and
|
||||
//! [message kinds](https://docs.rs/teloxide/latest/teloxide/types/enum.MessageKind.html) to work with! Usually it's essential to filter specific ones
|
||||
//! and process them in *handler functions*. *Teloxide* provides some `filter methods` for `Update` and `Message` types in [UpdateFilterExt](https://docs.rs/teloxide/latest/teloxide/dispatching/trait.UpdateFilterExt.html)
|
||||
//! and [MessageFilterExt](https://docs.rs/teloxide/latest/teloxide/dispatching/trait.MessageFilterExt.html) traits respectively. In addition to filtering, these
|
||||
//! methods will inject the appropriate type into your handler functions. For instance, if you use `Update::filter_message`, the `Message` object will be available as a parameter
|
||||
//! at your handler functions. Analogously the use of `Message::filter_text` will inject the `String` object.
|
||||
//!
|
||||
//! (Note: `filter_text` actually uses a function that returns Option<&str> value, so every filter_.. fn *always* return an `Owned` version of a type)
|
||||
//!
|
||||
//! Moreover, *filter_map* function can inject some object according to the schema flow. More in the example below!
|
||||
//!
|
||||
//! Here is a quick example (filter text message and inject it's text into the handler function):
|
||||
//! ```no_run
|
||||
//! # #[cfg(feature = "ctrlc_handler")]
|
||||
//! use teloxide::prelude::*;
|
||||
//!
|
||||
//! # #[cfg(feature = "ctrlc_handler")]
|
||||
//! # #[tokio::main]
|
||||
//! # async fn main() {
|
||||
//! let bot = Bot::from_env();
|
||||
//! let schema = Update::filter_message()
|
||||
//! // Inject the `User` object representing the author of an incoming
|
||||
//! // message into every successive handler function
|
||||
//! .filter_map(|update: Update| update.user().cloned())
|
||||
//! .branch(
|
||||
//! // Use filter_text method of MessageFilterExt to accept
|
||||
//! // only textual messages. Others will be ignored by this handler
|
||||
//! Message::filter_text().endpoint(process_text_message)
|
||||
//! );
|
||||
//!
|
||||
//! Dispatcher::builder(bot, schema)
|
||||
//! .build()
|
||||
//! .dispatch()
|
||||
//! .await;
|
||||
//! Ok(())
|
||||
//! })
|
||||
//! .await;
|
||||
//! # } #[cfg(not(feature = "ctrlc_handler"))] fn main(){}
|
||||
//!
|
||||
//! /// Replies to the user's text messages
|
||||
//! async fn process_text_message(bot: Bot, user: User, message_text: String) -> Result<(), teloxide::RequestError> {
|
||||
//! // The id of a chat with a user is the same as his telegram_id
|
||||
//! // from the bot's perspective
|
||||
//! bot.send_message(user.id, format!("Hi! You sent: {message_text}"))
|
||||
//! Ok(())
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
//! [Telegram bots]: https://telegram.org/blog/bot-revolution
|
||||
//! [`async`/`.await`]: https://rust-lang.github.io/async-book/01_getting_started/01_chapter.html
|
||||
//! [Rust]: https://www.rust-lang.org/
|
||||
|
||||
//! ## Working with Updates and Messages
|
||||
//!
|
||||
//! There is a great number of [update kinds] and [message kinds] to work with!
|
||||
//! Usually it's essential to filter specific ones and process them in `handler
|
||||
//! functions`. Teloxide provides some `filter methods` for [Update] and
|
||||
//! [Message] types in [UpdateFilterExt] and [MessageFilterExt] traits
|
||||
//! respectively. In addition to filtering, these methods will `inject` the
|
||||
//! appropriate type into your handler functions. For instance, if you use
|
||||
//! [Update::filter_message], the instance of the [Message] will be available as
|
||||
//! a parameter for your handler functions. Similarly the use of
|
||||
//! [Message::filter_text] will inject a [String] into the context.
|
||||
//!
|
||||
//! Moreover, [filter_map] function can inject some dependencies according to
|
||||
//! the schema flow. More in the example below!
|
||||
//!
|
||||
//! Here is a quick example (filter text message and inject it's text into the
|
||||
//! handler function):
|
||||
//! ```no_run
|
||||
//! use teloxide::{prelude::*, types::User};
|
||||
//!
|
||||
//! pub type Error = Box<dyn std::error::Error + Send + Sync>;
|
||||
//!
|
||||
//! #[tokio::main]
|
||||
//! async fn main() -> Result<(), Error> {
|
||||
//! let bot = Bot::from_env();
|
||||
//! let schema = Update::filter_message()
|
||||
//! /*
|
||||
//! Inject the `User` object representing the author of an incoming
|
||||
//! message into every successive handler function (1)
|
||||
//! */
|
||||
//! .filter_map(|update: Update| update.from().cloned())
|
||||
//! .branch(
|
||||
//! /*
|
||||
//! Use filter_text method of MessageFilterExt to accept
|
||||
//! only textual messages. Others will be ignored by this handler (2)
|
||||
//! */
|
||||
//! Message::filter_text().endpoint(process_text_message),
|
||||
//! );
|
||||
//!
|
||||
//! Dispatcher::builder(bot, schema).build().dispatch().await;
|
||||
//! Ok(())
|
||||
//! }
|
||||
//!
|
||||
//! /// Replies to the user's text messages
|
||||
//! async fn process_text_message(bot: Bot, user: User, message_text: String) -> Result<(), Error> {
|
||||
//! /*
|
||||
//! The id of a chat with a user is the same as his telegram_id
|
||||
//! from the bot's perspective.
|
||||
//!
|
||||
//! Injected dependencies:
|
||||
//! - Bot is provided by the Dispatcher::dispatch
|
||||
//! - User is provided by the (1)
|
||||
//! - String is provided by the (2)
|
||||
//! */
|
||||
//! bot.send_message(user.id, format!("Hi! You sent: {message_text}"));
|
||||
//! Ok(())
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
//! [update kinds]: crate::types::UpdateKind
|
||||
//! [message kinds]: crate::types::MessageKind
|
||||
//! [Update]: crate::types::Update
|
||||
//! [Message]: crate::types::Message
|
||||
//! [Message::filter_text]: crate::dispatching::MessageFilterExt::filter_text
|
||||
//! [UpdateFilterExt]: crate::dispatching::MessageFilterExt
|
||||
//! [MessageFilterExt]: crate::dispatching::UpdateFilterExt
|
||||
//! [Update::filter_message]: crate::dispatching::UpdateFilterExt::filter_message
|
||||
//! [filter_map]: crate::prelude::Handler::filter_map
|
||||
|
||||
// This hack is used to cancel formatting for a Markdown table. See [1], [2], and [3].
|
||||
//
|
||||
// [1]: https://github.com/rust-lang/rustfmt/issues/4210
|
||||
|
|
Loading…
Reference in a new issue