Skip to main content

Crate rustigram_bot

Crate rustigram_bot 

Source
Expand description

High-level bot framework built on top of [rustigram-api].

This crate provides everything needed to receive and route Telegram updates:

§Quick start

use rustigram_bot::{Bot, Context, BotResult};
use rustigram_bot::filter::filters;
use rustigram_bot::handler::handler_fn;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let bot = Bot::new(std::env::var("BOT_TOKEN")?)?;

    bot.dispatcher()
        .on(filters::command("start"), handler_fn(start))
        .on(filters::message(),        handler_fn(echo))
        .build()
        .polling()
        .await?;

    Ok(())
}

async fn start(ctx: Context) -> BotResult<()> {
    if let Some(r) = ctx.reply("Hello!") { r.await?; }
    Ok(())
}

async fn echo(ctx: Context) -> BotResult<()> {
    if let (Some(text), Some(chat_id)) = (ctx.text(), ctx.chat_id()) {
        ctx.bot.send_message(chat_id, text).await?;
    }
    Ok(())
}

§Concurrency model

Each incoming update is dispatched in its own tokio::spawn task. Handlers run concurrently — a slow handler never blocks others. The dispatcher evaluates routes in registration order and stops at the first matching filter.

Re-exports§

pub use bot::Bot;
pub use context::Context;
pub use dispatcher::Dispatcher;
pub use dispatcher::DispatcherBuilder;
pub use error::BotError;
pub use error::BotResult;
pub use filter::Filter;
pub use filter::FilterExt;
pub use handler::Handler;
pub use handler::HandlerFn;
pub use state::StateStorage;

Modules§

bot
Entry-point type for creating and running a bot.
context
Handler context passed to every update handler.
dispatcher
Update dispatcher and routing.
error
Error types for the bot framework layer.
filter
Composable update filters.
handler
Handler trait and function wrapper.
state
Shared and per-user state storage.
update_listener
Update listeners (long polling and webhook).