Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

API Reference

This page provides an overview of the crate structure, key types, and how to explore the full API documentation.

Generating Documentation

The most detailed and always-up-to-date reference is the cargo doc output. Generate and open it with:

cargo doc --open -p rust-tg-bot --all-features

This builds HTML documentation for all public types, traits, and functions, including source links and cross-references.

To include the underlying crates:

cargo doc --open --workspace --all-features

Online Documentation

Crate Structure

The framework is split into four crates:

CratePurposeYou Use Directly?
rust-tg-botFacade crate – re-exports all of the belowYes
rust-tg-bot-rawLow-level Bot API types, HTTP methods, request buildersRarely
rust-tg-bot-extHigh-level Application, handlers, filters, context, persistenceRarely
rust-tg-bot-macrosProc macros (#[derive(BotCommands)])Rarely

You almost always depend only on rust-tg-bot in your Cargo.toml. It re-exports everything you need through two module paths:

  • rust_tg_bot::ext::prelude – handlers, filters, context, application
  • rust_tg_bot::raw::types – low-level Telegram types when needed

The Prelude

The rust_tg_bot::ext::prelude module re-exports the most commonly needed types:

Core Application Types

TypeDescription
ApplicationThe main application that manages handlers, dispatching, and lifecycle
ApplicationBuilderBuilder for constructing an Application with token, persistence, etc.
HandlerResultAlias for Result<(), HandlerError>
HandlerErrorError type returned by handlers
ContextAlias for CallbackContext – passed to every handler
CallbackContextThe full context type with bot, data access, and convenience methods

Handler Types

TypeDescription
CommandHandlerMatches /command messages
MessageHandlerMatches messages that pass a filter
FnHandlerCustom predicate-based handler for any update type
CallbackQueryHandlerMatches callback queries from inline keyboards

Filter Types

TypeDescription
FWrapper around Arc<dyn Filter> with &, |, ! operators
FilterThe trait every filter implements
FilterResultNoMatch, Match, or MatchWithData
TEXT()Function returning a filter for text messages
COMMAND()Function returning a filter for bot commands

Telegram Types

TypeDescription
UpdateA Telegram update (message, callback query, inline query, etc.)
ArcRe-exported std::sync::Arc for wrapping Update
MessageA Telegram message
UserA Telegram user
ChatA Telegram chat
ChatIdEnum for chat identifiers
CallbackQueryData from an inline keyboard button press

Keyboard Types

TypeDescription
InlineKeyboardMarkupLayout for inline keyboard buttons
InlineKeyboardButtonA single inline keyboard button
ReplyKeyboardMarkupLayout for reply keyboard buttons
KeyboardButtonA single reply keyboard button
ReplyKeyboardRemoveRemoves the reply keyboard
ForceReplyForces the user to reply

Constants

TypeDescription
ParseModeHtml, Markdown, MarkdownV2
ChatActionTyping, UploadPhoto, etc.
ChatTypePrivate, Group, Supergroup, Channel
MessageEntityTypeBotCommand, Mention, Url, etc.
ChatMemberStatusCreator, Administrator, Member, etc.

Data Types

TypeDescription
DataReadGuardTyped read guard for bot-wide data
DataWriteGuardTyped write guard for bot-wide data
JsonValueRe-exported serde_json::Value
HashMapRe-exported std::collections::HashMap
RwLockRe-exported tokio::sync::RwLock

Feature-Gated Types

TypeFeatureDescription
WebhookConfigwebhooksConfiguration for webhook mode
WebhookHandlerwebhooksHandles incoming webhook requests
WebhookServerwebhooksBuilt-in webhook HTTP server

Raw Types Module

For types not in the prelude, import from rust_tg_bot::raw::types:

#![allow(unused)]
fn main() {
// Inline query types
use rust_tg_bot::raw::types::inline::inline_query_result_article::InlineQueryResultArticle;
use rust_tg_bot::raw::types::inline::input_message_content::InputMessageContent;
use rust_tg_bot::raw::types::inline::input_text_message_content::InputTextMessageContent;

// Payment types
use rust_tg_bot::raw::types::payment::labeled_price::LabeledPrice;
use rust_tg_bot::raw::types::payment::shipping_option::ShippingOption;

// Chat member types
use rust_tg_bot::raw::types::chat_member::ChatMember;
}

Persistence Module

Persistence types live outside the prelude:

#![allow(unused)]
fn main() {
// The trait
use rust_tg_bot::ext::persistence::base::{
    BasePersistence, PersistenceError, PersistenceInput, PersistenceResult,
};

// Backends
use rust_tg_bot::ext::persistence::json_file::JsonFilePersistence;

#[cfg(feature = "persistence-sqlite")]
use rust_tg_bot::ext::persistence::sqlite::SqlitePersistence;
}

Filters Module

Advanced filter types beyond TEXT() and COMMAND():

#![allow(unused)]
fn main() {
use rust_tg_bot::ext::filters::base::{
    Filter, FilterResult, FnFilter, F, ALL,
    PHOTO, VIDEO, AUDIO, VOICE, LOCATION, CONTACT,
};

use rust_tg_bot::ext::filters::text::{TextFilter, CaptionFilter, CAPTION};
use rust_tg_bot::ext::filters::regex::RegexFilter;
use rust_tg_bot::ext::filters::user::UserFilter;
use rust_tg_bot::ext::filters::chat::{ChatTypePrivate, ChatTypeGroup};
use rust_tg_bot::ext::filters::update_type::{MESSAGE, EDITED_MESSAGE};
}

Bot Methods

The Bot (accessed via context.bot()) provides methods mirroring the Telegram Bot API. Most methods return a builder that you finalise with .await (builders implement IntoFuture):

#![allow(unused)]
fn main() {
// Send a message
context.bot().send_message(chat_id, "text").await?;

// Send with options
context.bot().send_message(chat_id, "text")
    .parse_mode(ParseMode::Html)
    .reply_markup(keyboard)
    .await?;

// Edit a message
context.bot().edit_message_text("new text")
    .chat_id(chat_id)
    .message_id(msg_id)
    .await?;

// Answer a callback query
context.bot().answer_callback_query(&cq_id).await?;

// Answer an inline query
context.bot().answer_inline_query(&iq_id, results).await?;

// Send an invoice
context.bot().send_invoice(chat_id, title, desc, payload, currency, prices)
    .provider_token(&token)
    .await?;

// Set webhook
context.bot().set_webhook(&url).await?;

// Delete webhook
context.bot().delete_webhook(false).await?;
}

Convenience Methods on Context

Context provides shortcuts that skip the builder pattern:

#![allow(unused)]
fn main() {
// Reply with text (extracts chat_id from update automatically)
context.reply_text(&update, "Hello!").await?;

// Reply with HTML
context.reply_html(&update, "<b>Bold</b>").await?;

// Reply with MarkdownV2
context.reply_markdown_v2(&update, "*Bold*").await?;
}

Next Steps

For the complete API surface, run cargo doc --open -p rust-tg-bot --all-features in your project. The generated documentation includes every public type, method, and trait implementation with source links.