Skip to main content

Crate tgbotrs

Crate tgbotrs 

Source
Expand description

§tgbotrs 🦀

A fully-featured, auto-generated Telegram Bot API library for Rust.

Crates.io docs.rs

Created and developed by Ankit Chaubey

All 285 types and 165 methods from Telegram Bot API 9.4 are fully implemented and auto-generated from the api-spec repository.


§Quick Start

use tgbotrs::Bot;

#[tokio::main]
async fn main() {
    let bot = Bot::new("YOUR_BOT_TOKEN").await.unwrap();
    println!("Running as @{}", bot.me.username.as_deref().unwrap_or("unknown"));

    let msg = bot.send_message(123456789i64, "Hello from tgbotrs! 🦀", None).await.unwrap();
    println!("Sent: #{}", msg.message_id);
}

§Echo Bot with Long Polling

use tgbotrs::{Bot, Poller, UpdateHandler};

#[tokio::main]
async fn main() {
    let bot = Bot::new("YOUR_TOKEN").await.unwrap();

    let handler: UpdateHandler = Box::new(|bot, update| {
        Box::pin(async move {
            let Some(msg) = update.message else { return };
            let Some(text) = msg.text else { return };
            let _ = bot.send_message(msg.chat.id, text, None).await;
        })
    });

    Poller::new(bot, handler).timeout(30).start().await.unwrap();
}

§Webhook Server

Enable the webhook feature for a built-in webhook server:

tgbotrs = { version = "0.1", features = ["webhook"] }
ⓘ
use tgbotrs::{Bot, UpdateHandler, WebhookServer};

#[tokio::main]
async fn main() {
    let bot = Bot::new("YOUR_TOKEN").await.unwrap();
    let handler: UpdateHandler = Box::new(|bot, upd| {
        Box::pin(async move {
            if let Some(msg) = upd.message {
                let _ = bot.send_message(msg.chat.id, "pong!", None::<tgbotrs::gen_methods::SendMessageParams>).await;
            }
        })
    });
    WebhookServer::new(bot, handler)
        .port(8080)
        .secret_token("my_secret")
        .start("https://yourdomain.com")
        .await.unwrap();
}

§Regenerating from the Latest API Spec

curl -o api.json https://raw.githubusercontent.com/ankit-chaubey/api-spec/main/api.json
python3 codegen/codegen.py api.json tgbotrs/src/
cargo build

§License

MIT — Copyright (c) 2024-present Ankit Chaubey

Re-exports§

pub use types::*;

Modules§

gen_methods
types
All Telegram Bot API types, auto-generated from the official spec.

Structs§

Bot
The main Bot struct. Create one per bot token.
Poller
Long-polling update dispatcher.

Enums§

BotError
The main error type for tgbotrs.
ChatId
Represents a chat identifier - either a numeric ID or a username string (@username).
InputFile
Represents a file to be sent.
InputFileOrString
A field that can be either an InputFile or a String (file_id / URL).
InputMedia
The InputMedia enum — used for sendMediaGroup and related methods.
ReplyMarkup
The reply_markup field can be one of four types.

Type Aliases§

UpdateHandler
A function type that handles incoming updates.