Skip to main content

rustigram_bot/
lib.rs

1//! High-level bot framework built on top of \[`rustigram-api`\].
2//!
3//! This crate provides everything needed to receive and route Telegram updates:
4//!
5//! - [`Bot`] — entry point, owns the [`crate::bot::Bot::client`] field and creates a dispatcher
6//! - [`Dispatcher`] — routes updates to handlers via composable filters
7//! - [`Context`] — passed to every handler, provides access to the update and the API client
8//! - [`filter`] — built-in and composable filter predicates
9//! - [`handler`] — the [`handler::Handler`] trait and [`handler::handler_fn`] wrapper
10//! - [`state`] — [`state::StateStorage`] for shared data and [`state::DialogueStorage`] for FSM
11//! - [`update_listener`] — long-polling and axum-based webhook implementations
12//!
13//!  # Quick start
14//!
15//! ```rust,ignore
16//! use rustigram_bot::{Bot, Context, BotResult};
17//! use rustigram_bot::filter::filters;
18//! use rustigram_bot::handler::handler_fn;
19//!
20//! #[tokio::main]
21//! async fn main() -> anyhow::Result<()> {
22//!     let bot = Bot::new(std::env::var("BOT_TOKEN")?)?;
23//!
24//!     bot.dispatcher()
25//!         .on(filters::command("start"), handler_fn(start))
26//!         .on(filters::message(),        handler_fn(echo))
27//!         .build()
28//!         .polling()
29//!         .await?;
30//!
31//!     Ok(())
32//! }
33//!
34//! async fn start(ctx: Context) -> BotResult<()> {
35//!     if let Some(r) = ctx.reply("Hello!") { r.await?; }
36//!     Ok(())
37//! }
38//!
39//! async fn echo(ctx: Context) -> BotResult<()> {
40//!     if let (Some(text), Some(chat_id)) = (ctx.text(), ctx.chat_id()) {
41//!         ctx.bot.send_message(chat_id, text).await?;
42//!     }
43//!     Ok(())
44//! }
45//! ```
46//!
47//! # Concurrency model
48//!
49//! Each incoming update is dispatched in its own [`tokio::spawn`] task.
50//! Handlers run concurrently — a slow handler never blocks others.
51//! The dispatcher evaluates routes in registration order and stops at the
52//! first matching filter.
53#![warn(missing_docs)]
54/// Entry-point type for creating and running a bot.
55pub mod bot;
56/// Handler context passed to every update handler.
57pub mod context;
58/// Update dispatcher and routing.
59pub mod dispatcher;
60/// Error types for the bot framework layer.
61pub mod error;
62/// Composable update filters.
63pub mod filter;
64/// Handler trait and function wrapper.
65pub mod handler;
66/// Shared and per-user state storage.
67pub mod state;
68/// Update listeners (long polling and webhook).
69pub mod update_listener;
70
71pub use bot::Bot;
72pub use context::Context;
73pub use dispatcher::{Dispatcher, DispatcherBuilder};
74pub use error::{BotError, BotResult};
75pub use filter::{Filter, FilterExt};
76pub use handler::{Handler, HandlerFn};
77pub use state::StateStorage;
78pub use update_listener::webhook::WebhookConfig;