Expand description
§tgbotrs 🦀
A fully-featured, auto-generated Telegram Bot API library for Rust.
Created and developed by Ankit Chaubey
- 📧 ankitchaubey.dev@gmail.com
- 💬 Telegram: @ankify
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§
Enums§
- BotError
- The main error type for tgbotrs.
- ChatId
- Represents a chat identifier - either a numeric ID or a username string (@username).
- Input
File - Represents a file to be sent.
- Input
File OrString - A field that can be either an InputFile or a String (file_id / URL).
- Input
Media - The
InputMediaenum — used forsendMediaGroupand related methods. - Reply
Markup - The reply_markup field can be one of four types.
Type Aliases§
- Update
Handler - A function type that handles incoming updates.