System architecture
The VBB Telegram Bot is built as a modern asynchronous Python application with the following core components:- Bot Framework: aiogram 3.x for Telegram Bot API integration
- Dialog System: aiogram-dialog for conversational UI flows
- Database: SQLAlchemy 2.0 (async) with PostgreSQL
- Background Jobs: APScheduler for scheduled tasks
- External API: VBB (Verkehrsverbund Berlin-Brandenburg) REST API v6
Application startup flow
The bot initializes through a structured startup sequence inapp/__main__.py:62:
Startup lifecycle
- Configuration Loading: Parse
config.tomland command-line arguments - Database Initialization: Create async session manager with connection pool
- Bot Instance Creation: Configure aiogram Bot with HTML parse mode
- Dispatcher Setup: Create dispatcher with memory-based FSM storage
- Middleware Registration: Register throttling and database session middlewares
- Handler Registration: Load owner and user routers with commands and dialogs
- Background Services: Launch journey checker loop and APScheduler jobs
- Polling Start: Begin receiving updates from Telegram
Component structure
Core modules
Handler and dialog architecture
The bot uses a two-tier routing system:Handler routers
Handlers are organized by user role inapp/handlers/__init__.py:4:
- Owner Router: Administrative commands restricted to bot owner
- User Router: Standard user commands and dialog entry points
Dialog system
The bot uses aiogram-dialog for stateful conversations. Each dialog is defined with:- States: FSM state groups in
dialogs/states.py - Windows: UI screens with keyboards and text
- Getters: Async functions that fetch data for rendering
- Handlers: Button click handlers and input processors
State groups
Middleware pipeline
Two middlewares process all updates:Database session middleware
Injects an async SQLAlchemy session into every handler viamiddleware_data:
Throttling middleware
Implements rate limiting to prevent abuse:- Configurable delay between requests per user
- “Too many requests” error message on throttle
Data flow
Journey search flow
- User opens journeys dialog (via command or scheduled notification)
- Dialog getter function fetches user preferences from database
get_journeys()calls VBB API with user’s home/destination addresses- API response parsed into
Journeyobjects with nestedLegmodels - Message builder formats journey data into HTML
- User navigates results with prev/next pagination buttons
Registration flow
- User sends
/startcommand - Bot checks if user exists in database
- If new user, launches registration dialog
- User provides home address (text or location)
- Address geocoded and stored in
addressestable - User provides destination address
- User record created with foreign keys to addresses
- Main menu dialog launched
Global state management
Global instances are stored inapp/__init__.py:17:
__main__.py and accessed by importing:
Error handling
The application uses Python’s standard logging module with coloredlogs:- Debug level for test mode (
--testflag) - Info level for production
- All VBB API calls logged with full URLs
- Database operations log errors with stack traces
Next steps
Database schema
Learn about the User, Address, and JourneyDB models
VBB API integration
Explore the VBB REST API wrapper functions
Background services
Understand scheduled jobs and notifications