Skip to main content

teloxide/
lib.rs

1//! A full-featured framework that empowers you to easily build [Telegram bots]
2//! using [Rust]. It handles all the difficult stuff so you can focus only on
3//! your business logic. Currently, version `10.5` of [Telegram Bot API] is
4//! supported.
5//!
6//! For a high-level overview, see [our GitHub repository](https://github.com/teloxide-fork/teloxide).
7//!
8//! [[`examples/throw_dice.rs`](https://github.com/teloxide-fork/teloxide/blob/master/crates/teloxide/examples/throw_dice.rs)]
9//! ```no_run
10//! # #[cfg(feature = "ctrlc_handler")]
11//! use teloxide::prelude::*;
12//!
13//! # #[cfg(feature = "ctrlc_handler")]
14//! # #[tokio::main]
15//! # async fn main() {
16//! pretty_env_logger::init();
17//! log::info!("Starting throw dice bot...");
18//!
19//! let bot = Bot::from_env();
20//!
21//! teloxide::repl(bot, |bot: Bot, msg: Message| async move {
22//!     bot.send_dice(msg.chat.id).await?;
23//!     Ok(())
24//! })
25//! .await;
26//! # } #[cfg(not(feature = "ctrlc_handler"))] fn main(){}
27//! ```
28//!
29//! <div align="center">
30//!   <kbd>
31//!     <img src=https://github.com/teloxide-fork/teloxide/raw/master/media/throw-dice.gif width=420px />
32//!   </kbd>
33//! </div>
34//!
35//! [Telegram Bot API]: https://core.telegram.org/bots/api
36//! [Telegram bots]: https://telegram.org/blog/bot-revolution
37//! [`async`/`.await`]: https://rust-lang.github.io/async-book/01_getting_started/01_chapter.html
38//! [Rust]: https://www.rust-lang.org/
39
40//! ## Working with Updates and Messages
41//!
42//! There is a great number of [update kinds] and [message kinds] to work with!
43//! Usually it's essential to filter specific ones and process them in `handler
44//! functions`. Teloxide provides some `filter methods` for [`Update`] and
45//! [`Message`] types in [`UpdateFilterExt`] and [`MessageFilterExt`] traits
46//! respectively. In addition to filtering, these methods will `inject` the
47//! appropriate type into your handler functions. For instance, if you use
48//! [`Update::filter_message`], the instance of the [`Message`] will be
49//! available as a parameter for your handler functions. Similarly the use of
50//! [`Message::filter_text`] will inject a [`String`] into the context.
51//!
52//! Moreover, [`filter_map`] function can inject some dependencies according to
53//! the schema flow. More in the example below!
54//!
55//! Here is a quick example (filter text message and inject it's text into the
56//! handler function):
57//! ```no_run
58//! use teloxide::{prelude::*, types::User};
59//!
60//! pub type Error = Box<dyn std::error::Error + Send + Sync>;
61//!
62//! #[tokio::main]
63//! async fn main() -> Result<(), Error> {
64//!     let bot = Bot::from_env();
65//!     let schema = Update::filter_message()
66//!         /*
67//!            Inject the `User` object representing the author of an incoming
68//!            message into every successive handler function (1)
69//!         */
70//!         .filter_map(|update: Update| update.from().cloned())
71//!         .branch(
72//!             /*
73//!                Use filter_text method of MessageFilterExt to accept
74//!                only textual messages. Others will be ignored by this handler (2)
75//!             */
76//!             Message::filter_text().endpoint(process_text_message),
77//!         );
78//!
79//!     Dispatcher::builder(bot, schema).build().dispatch().await;
80//!     Ok(())
81//! }
82//!
83//! /// Replies to the user's text messages
84//! async fn process_text_message(bot: Bot, user: User, message_text: String) -> Result<(), Error> {
85//!     /*
86//!        The id of a chat with a user is the same as his telegram_id
87//!        from the bot's perspective.
88//!
89//!        Injected dependencies:
90//!        - Bot is provided by the Dispatcher::dispatch
91//!        - User is provided by the (1)
92//!        - String is provided by the (2)
93//!     */
94//!     let _ = bot.send_message(format!("Hi! You sent: {message_text}")).chat_id(user.id).await?;
95//!     Ok(())
96//! }
97//! ```
98//!
99//! For more info about dptree, please check out [_this guide_] on it!
100//!
101//! To consume updates directly without a dispatcher, see [`update_stream`].
102//!
103//! [update kinds]: crate::types::UpdateKind
104//! [message kinds]: crate::types::MessageKind
105//! [`Update`]: crate::types::Update
106//! [`Message`]: crate::types::Message
107//! [`Message::filter_text`]: crate::dispatching::MessageFilterExt::filter_text
108//! [`UpdateFilterExt`]: crate::dispatching::UpdateFilterExt
109//! [`MessageFilterExt`]: crate::dispatching::MessageFilterExt
110//! [`Update::filter_message`]: crate::dispatching::UpdateFilterExt::filter_message
111//! [`filter_map`]: crate::prelude::Handler::filter_map
112//! [_this guide_]: https://github.com/teloxide-fork/teloxide/blob/master/DPTREE_GUIDE.md
113
114// This hack is used to cancel formatting for a Markdown table. See [1], [2], and [3].
115//
116// [1]: https://github.com/rust-lang/rustfmt/issues/4210
117// [2]: https://github.com/rust-lang/rustfmt/issues/4787
118// [3]: https://github.com/rust-lang/rust/issues/82768#issuecomment-803935643
119#![cfg_attr(feature = "nightly", cfg_attr(feature = "nightly", doc = include_str!("features.md")))]
120// https://github.com/teloxide-fork/teloxide/raw/master/media/teloxide-logo.svg doesn't work in html_logo_url, I don't know why.
121#![doc(
122    html_logo_url = "https://github.com/teloxide-fork/teloxide/raw/master/media/teloxide-logo.png",
123    html_favicon_url = "https://github.com/teloxide-fork/teloxide/raw/master/teloxide-logo.png"
124)]
125// To properly build docs of this crate run
126// ```console
127// $ cargo docs --open
128// ```
129// (docs is an alias from `.cargo/config.toml`)
130#![cfg_attr(all(docsrs, feature = "nightly"), feature(doc_cfg))]
131#![forbid(unsafe_code)]
132#![warn(rustdoc::broken_intra_doc_links)]
133#![allow(clippy::match_bool)]
134#![allow(clippy::redundant_pattern_matching)]
135// https://github.com/rust-lang/rust-clippy/issues/7422
136#![allow(clippy::nonstandard_macro_braces)]
137
138#[cfg(feature = "ctrlc_handler")]
139pub use repls::{repl, repl_with_listener};
140
141pub mod backoff;
142pub mod dispatching;
143pub mod error_handlers;
144pub mod prelude;
145#[cfg(feature = "ctrlc_handler")]
146pub mod repls;
147pub mod stop;
148pub mod sugar;
149pub mod update_listeners;
150pub mod update_stream;
151pub mod utils;
152
153#[doc(inline)]
154pub use teloxide_core::*;
155
156#[cfg(feature = "macros")]
157pub use teloxide_macros as macros;
158
159pub use dispatching::{filter_command, filter_mention_command};
160pub use dptree::{self, case as handler};
161
162#[cfg(all(feature = "nightly", doctest))]
163#[cfg_attr(feature = "nightly", cfg_attr(feature = "nightly", doc = include_str!("../../../README.md")))]
164enum ReadmeDocTests {}
165
166use teloxide_core::requests::ResponseResult;
167
168/// A shortcut for `ResponseResult::Ok(val)`.
169pub fn respond<T>(val: T) -> ResponseResult<T> {
170    ResponseResult::Ok(val)
171}