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