All 285 types and 165 methods of the Telegram Bot API β strongly typed, fully async, automatically kept up-to-date.
π¦ Install β’ π Quick Start β’ π Examples β’ π§ API Reference β’ π Auto-Codegen β’ π€ Contributing
β¨ Features
π€ Complete API Coverage
- All 285 types β structs, enums, markers
- All 165 methods β fully async
- All 21 union types as Rust enums
- 100 optional params structs with builder pattern
π Auto-Generated & Always Fresh
- Generated from the official spec
- Daily automated check for API updates
- PR auto-opened on every new API version
- Zero manual work to stay up-to-date
π¦ Idiomatic Rust
- Fully
async/awaitwith Tokio Into<ChatId>β acceptsi64or"@username"Into<String>on all text paramsOption<T>for all optional fieldsBox<T>to break recursive type cycles
π‘οΈ Type Safe
ChatIdβ integer or username, no stringly typingInputFileβ file_id / URL / raw bytesReplyMarkupβ unified enum for all 4 keyboard typesInputMediaβ typed enum for media groups- Compile-time guarantees on all API calls
π‘ Flexible HTTP Layer
- Custom API server support (local Bot API)
- Multipart file uploads
- Configurable timeout
- Flood-wait aware error handling
reqwestbackend
π¬ Built-in Polling
- Long-polling dispatcher included
- Spawns a Tokio task per update
- Configurable timeout, limit, allowed_updates
- Clean concurrent update processing
π¦ Installation
Add to your Cargo.toml:
[]
= "0.1"
= { = "1", = ["full"] }
Requirements: Rust 1.75+ Β· Tokio async runtime
π Quick Start
use Bot;
async
π Examples
π Echo Bot β Long Polling
use ;
async
π¬ Formatted Messages
use SendMessageParams;
let params = new
.parse_mode
.disable_notification;
bot.send_message.await?;
πΉ Inline Keyboards
use ;
use ;
let keyboard = InlineKeyboardMarkup ;
let params = new
.reply_markup;
bot.send_message.await?;
πΈ Send Photos
use ;
let params = new.caption;
// By file_id (fastest β already on Telegram's servers)
bot.send_photo.await?;
// By URL
bot.send_photo.await?;
// Upload raw bytes
let data = read.await?;
bot.send_photo.await?;
π¬ Media Groups
use ;
use ;
let media = vec!;
bot.send_media_group.await?;
β¨οΈ Reply Keyboards
use ;
use ;
let keyboard = ReplyKeyboardMarkup ;
let params = new
.reply_markup;
bot.send_message.await?;
π Polls
use ;
use InputPollOption;
let options = vec!;
let params = new.is_anonymous;
bot.send_poll.await?;
β‘ Callback Queries
use AnswerCallbackQueryParams;
use MaybeInaccessibleMessage;
let handler: UpdateHandler = Boxnew;
πͺ Inline Queries
use ;
let results = vec!;
bot.answer_inline_query.await?;
π Payments
use ;
use LabeledPrice;
let prices = vec!;
bot.send_invoice.await?;
π Webhooks
use SetWebhookParams;
let params = new
.max_connections
.allowed_updates
.secret_token;
bot.set_webhook.await?;
π Local Bot API Server
let bot = with_api_url.await?;
π οΈ Error Handling
use BotError;
match bot.send_message.await
π§ API Reference
Bot β Core Struct
| Constructor | Description |
|---|---|
Bot::new(token) |
Create bot, calls getMe, verifies token |
Bot::with_api_url(token, url) |
Create with a custom/local API server |
Bot::new_unverified(token) |
Create without calling getMe |
| Method | Description |
|---|---|
bot.call_api(method, body) |
Raw JSON POST API call |
bot.call_api_multipart(method, form) |
Multipart POST (for file uploads) |
bot.endpoint(method) |
Returns full URL for a method |
ChatId β Flexible Chat Identifier
// All of these work wherever ChatId is expected:
bot.send_message.await?;
bot.send_message.await?;
bot.send_message.await?;
bot.send_message.await?;
InputFile β File Sending
// Reference an already-uploaded file by file_id
file_id
// Let Telegram download from a URL
url
// Upload raw bytes directly
let data = read.await?;
memory
ReplyMarkup β All Keyboard Types
// Inline keyboard (buttons inside messages)
InlineKeyboard
// Reply keyboard (custom keyboard at bottom of screen)
ReplyKeyboard
// Remove the reply keyboard
ReplyKeyboardRemove
// Force the user to reply
ForceReply
Poller β Long Polling Dispatcher
new
.timeout // Seconds to long-poll (0 = short poll)
.limit // Max updates per request (1-100)
.allowed_updates
.start
.await?;
BotError β Error Handling
// Helper methods:
error.is_api_error_code // β bool
error.flood_wait_seconds // β Option<i64>
Builder Pattern for Optional Params
Every method with optional parameters has a *Params struct with a builder API:
// Pattern: MethodNameParams::new().field(value).field(value)
let params = new
.parse_mode
.disable_notification
.protect_content
.message_thread_id
.reply_parameters
.reply_markup;
π Coverage Statistics
| Category | Count | Status |
|---|---|---|
| Total Types | 285 | β 100% |
| β³ Struct types | 257 | β |
| β³ Union/Enum types | 21 | β |
| β³ Marker types | 7 | β |
| Total Methods | 165 | β 100% |
β³ set* methods |
30 | β |
β³ get* methods |
29 | β |
β³ send* methods |
22 | β |
β³ edit* methods |
12 | β |
β³ delete* methods |
11 | β |
| β³ Other methods | 61 | β |
| Params structs | 100 | β |
| Lines generated | ~11,258 | β |
π Auto-Codegen
tgbotrs is the only Rust Telegram library that automatically stays in sync with the official API spec via GitHub Actions.
Architecture
Every Day at 08:00 UTC
β
βΌ
βββββββββββββββββββ
β Fetch latest β βββ github.com/ankit-chaubey/api-spec
β api.json spec β
ββββββββββ¬βββββββββ
β
βΌ
βββββββββββββββββββ
β Compare with βββ No change? βββΊ Stop β
β pinned version β
ββββββββββ¬βββββββββ
β Changed!
βΌ
βββββββββββββββββββ
β diff_spec.py β βββ Full semantic diff
β β β’ Added/removed types
β β β’ Added/removed methods
β β β’ Per-field changes
ββββββββββ¬βββββββββ
β
βΌ
βββββββββββββββββββ
β codegen.py β βββ Pure Python, zero dependencies
β β Generates:
β β β’ gen_types.rs (5,821 lines)
β β β’ gen_methods.rs (5,437 lines)
ββββββββββ¬βββββββββ
β
βΌ
βββββββββββββββββββ
β validate.py β βββ Verify 100% coverage
β β All types & methods present
ββββββββββ¬βββββββββ
β
βΌ
βββββββββββββββββββ
β Open PR with β βββ Rich description:
β full report β β’ Summary table
β β β’ New/removed items
β β β’ Per-field diff (collapsible)
β β β’ Checklist
ββββββββββ¬βββββββββ
β
βΌ
βββββββββββββββββββ
β GitHub Issue β βββ Notification with full changelog
β notification β
ββββββββββ¬βββββββββ
β
βΌ
βββββββββββββββββββ
β On PR merge: β
β β’ Bump semver β
β β’ Git tag β
β β’ GitHub Releaseβ
β β’ crates.io β
βββββββββββββββββββ
Regenerate Manually
# 1. Download latest spec
# 2. Run codegen (pure Python, no pip installs needed)
# 3. Rebuild your project
GitHub Actions Workflows
| Workflow | Trigger | Purpose |
|---|---|---|
auto-regenerate.yml |
β° Daily 08:00 UTC + manual | Fetch spec β diff β codegen β PR |
ci.yml |
Every push/PR | Build, test, lint, validate sync |
release.yml |
PR merged β main | Version bump + crates.io publish |
notify.yml |
After regen | GitHub Issue with change summary |
Setting Up in Your Fork
Add these secrets in Settings β Secrets β Actions:
| Secret | Purpose |
|---|---|
CRATES_IO_TOKEN |
API token from crates.io/settings/tokens |
Enable PR creation in Settings β Actions β General β Workflow permissions.
ποΈ Project Structure
tgbotrs/
β
βββ π README.md β You are here
βββ π CHANGELOG.md β Auto-updated on each release
βββ π LICENSE β MIT β Ankit Chaubey 2024-present
βββ π api.json β Pinned Telegram Bot API spec
βββ π spec_commit β Pinned spec commit SHA
βββ π Cargo.toml β Workspace root
β
βββ ποΈ .github/
β βββ workflows/
β β βββ auto-regenerate.yml β Daily spec sync + codegen + PR opener
β β βββ ci.yml β Build/test on 3 OSes Γ 2 Rust versions
β β βββ release.yml β Semver bump + tag + publish
β β βββ notify.yml β Issue creation on API updates
β βββ scripts/
β βββ diff_spec.py β Semantic diff: added/removed/changed
β βββ validate_generated.pyβ Verifies 100% type + method coverage
β βββ build_pr_body.py β Generates rich PR descriptions
β βββ coverage_report.py β Markdown coverage table for CI
β βββ update_changelog.py β Auto-prepends entries to CHANGELOG.md
β
βββ ποΈ codegen/
β βββ Cargo.toml
β βββ codegen.py β Main codegen: Python, zero deps
β βββ src/main.rs β Rust codegen binary (alternative)
β
βββ ποΈ tgbotrs/ β The library crate
βββ Cargo.toml
βββ examples/
β βββ echo_bot.rs β Basic echo bot
β βββ advanced_bot.rs β Keyboards, photos, callbacks
βββ src/
βββ lib.rs β Crate root + public API + re-exports
βββ bot.rs β Bot struct + HTTP + JSON API layer
βββ error.rs β BotError with all error variants
βββ chat_id.rs β ChatId (i64 | @username)
βββ input_file.rs β InputFile + InputFileOrString
βββ reply_markup.rs β ReplyMarkup (4-variant enum)
βββ polling.rs β Poller (long-polling dispatcher)
βββ types.rs β Re-exports gen_types
βββ gen_types.rs β β‘ AUTO-GENERATED β 5,821 lines
βββ gen_methods.rs β β‘ AUTO-GENERATED β 5,437 lines
π€ Contributing
Contributions are very welcome!
Report Issues
- π Bug? β Open a bug report
- π‘ Feature request? β Open a feature request
- π Security issue? β Email ankitchaubey.dev@gmail.com directly
Development
# Clone the repo
# Build everything
# Run tests
# Regenerate from latest spec
# Validate 100% coverage
# Lint
# Format
PR Guidelines
- One concern per PR
- Run
cargo fmtandcargo clippybefore submitting - Add examples for new helpers
- Keep generated files (
gen_*.rs) untouched β editcodegen.pyinstead
π Changelog
See CHANGELOG.md for the full release history.
π Credits
| Telegram | The official Bot API this library implements |
| PaulSonOfLars / gotgbot | Design inspiration for the auto-generation approach and code structure |
| ankit-chaubey / api-spec | Machine-readable Telegram Bot API spec used as the codegen source |
π¬ Contact
| π§ Email | ankitchaubey.dev@gmail.com |
| π¬ Telegram | @ankify |
| π GitHub | github.com/ankit-chaubey |
| π¦ crates.io | crates.io/crates/tgbotrs |
| π docs.rs | docs.rs/tgbotrs |
π License
MIT License
Copyright (c) 2024-present Ankit Chaubey
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Created and developed by Ankit Chaubey
If tgbotrs saved you time, a β on GitHub means a lot!