Toby
A simple, opinionated Telegram bot library built on top of teloxide, providing an easy-to-use interface for building Telegram bots with structured command parsing.
Features
- Simple API: Minimal boilerplate to get your bot running
- Structured Command Parsing: Parse commands with both positional arguments and key-value pairs
- Async Handler: Modern async/await support with tokio
- Type-safe Message Handling: Structured message and reply types
Installation
Add this to your Cargo.toml:
[]
= { = "." } # Or publish to crates.io and use version
= { = "1.8", = ["rt-multi-thread", "macros"] }
= "1.0"
Quick Start
use anyhow;
use ;
use ;
async
Command Format
Toby supports a special command format that allows both short arguments and key-value pairs:
Basic Command with Short Arguments
/command arg1 arg2 arg3
Command with Key-Value Pairs
/command short_arg1 short_arg2
- key1: value1
- key2: value2
- key3: value3
Examples
Simple command:
/hello world
Complex command with metadata:
/create task123
- title: Buy groceries
- priority: high
- due: 2024-01-15
API Reference
Toby
The main bot struct.
Toby::new(token, handler)
Creates a new bot instance.
token: Your Telegram bot token (get it from @BotFather)handler: A function that takes&Msgand returns aFuture<Output = anyhow::Result<ReplyMsg>>
Toby::listen()
Starts the bot and listens for incoming messages.
Msg
Represents an incoming message.
Command
Represents a parsed command.
ReplyMsg
Represents a reply message to send back.
Examples
See the examples/ directory for complete examples:
Environment Variables
TELEGRAM_BOT_TOKEN: Your bot token from BotFather (recommended for production use)
Development
Building
Running Examples
# Replace <TOKEN> with your actual bot token
TELEGRAM_BOT_TOKEN=<TOKEN> cargo
Dependencies
- teloxide - Telegram bot framework
- tokio - Async runtime
- regex - Command parsing
- chrono - Date and time handling
- anyhow - Error handling
License
This project is licensed under the MIT License.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.