Skip to main content

teloxide_ng/
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 `9.3` of [Telegram Bot API] is
4//! supported.
5//!
6//! For a high-level overview, see [our GitHub repository](https://github.com/teloxide/teloxide).
7//!
8//! [[`examples/throw_dice.rs`](https://github.com/teloxide/teloxide/blob/master/crates/teloxide-ng/examples/throw_dice.rs)]
9//! ```no_run
10//! # #[cfg(feature = "ctrlc_handler")]
11//! use teloxide_ng::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_ng::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/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_ng::{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(user.id, format!("Hi! You sent: {message_text}")).await?;
95//!     Ok(())
96//! }
97//! ```
98//!
99//! For more info about dptree, please check out [_this guide_] on it!
100//!
101//! [update kinds]: crate::types::UpdateKind
102//! [message kinds]: crate::types::MessageKind
103//! [`Update`]: crate::types::Update
104//! [`Message`]: crate::types::Message
105//! [`Message::filter_text`]: crate::dispatching::MessageFilterExt::filter_text
106//! [`UpdateFilterExt`]: crate::dispatching::UpdateFilterExt
107//! [`MessageFilterExt`]: crate::dispatching::MessageFilterExt
108//! [`Update::filter_message`]: crate::dispatching::UpdateFilterExt::filter_message
109//! [`filter_map`]: crate::prelude::Handler::filter_map
110//! [_this guide_]: https://github.com/teloxide/teloxide/blob/master/DPTREE_GUIDE.md
111
112// This hack is used to cancel formatting for a Markdown table. See [1], [2], and [3].
113//
114// [1]: https://github.com/rust-lang/rustfmt/issues/4210
115// [2]: https://github.com/rust-lang/rustfmt/issues/4787
116// [3]: https://github.com/rust-lang/rust/issues/82768#issuecomment-803935643
117#![cfg_attr(feature = "nightly", cfg_attr(feature = "nightly", doc = include_str!("features.md")))]
118// https://github.com/teloxide/teloxide/raw/master/media/teloxide-logo.svg doesn't work in html_logo_url, I don't know why.
119#![doc(
120    html_logo_url = "https://github.com/teloxide/teloxide/raw/master/media/teloxide-logo.png",
121    html_favicon_url = "https://github.com/teloxide/teloxide/raw/master/teloxide-logo.png"
122)]
123// To properly build docs of this crate run
124// ```console
125// $ cargo docs --open
126// ```
127// (docs is an alias from `.cargo/config.toml`)
128#![cfg_attr(all(docsrs, feature = "nightly"), feature(doc_cfg))]
129#![forbid(unsafe_code)]
130#![warn(rustdoc::broken_intra_doc_links)]
131#![allow(clippy::match_bool)]
132#![allow(clippy::redundant_pattern_matching)]
133// https://github.com/rust-lang/rust-clippy/issues/7422
134#![allow(clippy::nonstandard_macro_braces)]
135
136#[cfg(feature = "ctrlc_handler")]
137pub use repls::{repl, repl_with_listener};
138
139pub mod backoff;
140pub mod dispatching;
141pub mod error_handlers;
142pub mod prelude;
143#[cfg(feature = "ctrlc_handler")]
144pub mod repls;
145pub mod stop;
146pub mod sugar;
147pub mod update_listeners;
148pub mod utils;
149
150#[doc(inline)]
151pub use teloxide_core_ng::*;
152
153#[cfg(feature = "macros")]
154pub use teloxide_macros_ng as macros;
155
156pub use dispatching::{filter_command, filter_mention_command};
157pub use dptree::{self, case as handler};
158
159#[cfg(all(feature = "nightly", doctest))]
160#[cfg_attr(feature = "nightly", cfg_attr(feature = "nightly", doc = include_str!("../../../README.md")))]
161enum ReadmeDocTests {}
162
163use teloxide_core_ng::requests::ResponseResult;
164
165/// A shortcut for `ResponseResult::Ok(val)`.
166pub fn respond<T>(val: T) -> ResponseResult<T> {
167    ResponseResult::Ok(val)
168}