A modular Telegram bot powered by python-telegram-bot.
This bot is designed for scalability, maintainability, and follows clean architecture principles.
- Uses python-telegram-bot for handling Telegram interactions.
- Structured with SOLID principles for modularity.
- Poetry-based dependency management (previously Pipenv).
- Built-in error handling and logging.
- Uses environment variables (
.env
) to store sensitive data.
We are actively improving the kernel to be more reusable, scalable, and efficient. The following enhancements are planned:
-
Short-Term Goals
- Support for Webhooks: Implement run_webhook() to allow event-driven execution.
- Testing with SQLite: Create unit tests using pytest for config_loader.py, database.py, and handlers.
- Verify PostgreSQL Integration: Ensure database compatibility with PostgreSQL.
-
Mid-Term Goals
- Plugin System for Dynamic Modules: Load new commands automatically from bot/plugins/.
- Multi-language Support (i18n): Implement a translation system with JSON-based locales.
- Message Middleware: Filter messages before they reach command handlers.
-
Long-Term Goals
- Redis Caching for Performance Optimization: Store user sessions in memory for faster responses.
- External API Integration: Create api_client.py for connecting with third-party services.
- WebSockets for Real-Time Events: Enable instant notifications and live event handling.
- Admin Dashboard with FastAPI: Develop an API to manage and monitor the bot remotely.
Poetry is used to manage dependencies and virtual environments:
pip install poetry
poetry install
Create a .env file in the project root and add your Telegram Bot Token:
# TOKEN
TELEGRAM_BOT_TOKEN=your_actual_bot_token_here
# Add Telegram user IDs separated by commas
AUTHORIZED_USERS=12345678,98765432
# Change as needed
# LOG_LEVEL=DEBUG # Shows detailed information for debugging.
# LOG_LEVEL=INFO # Shows general information about program operation.
LOG_LEVEL=WARNING # Shows warnings about potential problems.
# LOG_LEVEL=ERROR # Shows errors that prevent correct operation.
DEBUG_MODE=False
# Options: DATABASE_TYPE sqlite or postgres
# If using SQLite:
DATABASE_TYPE=sqlite
# If using PostgreSQL: (Upcoming)
# DATABASE_TYPE=postgres
# Only required if using PostgreSQL
# DATABASE_URL=postgresql://user:password@localhost:5432/mydatabase
DATABASE_FILE=bot_database.db
You can start the bot in two ways:
poetry run python bot/main.py
Make sure run.sh is executable:
chmod +x run.sh
./run.sh
poetry run python bot/main.py --webhook
kernel-telegram-bot/
βββ bot/ # Main application logic
β βββ __init__.py # Package init file
β βββ main.py # Entry point of the bot
β β
β βββ handlers/ # Telegram command handlers
β β βββ __init__.py # Package init file
β β βββ start.py # /start command
β β βββ help.py # /help command
β β βββ unknown.py # Unknown command handler
β β βββ errors.py # Error handling
β β
β βββ services/ # Business logic (auth, db, etc.)
β β βββ __init__.py # Package init file
β β βββ auth.py # Manages user authentication & permissions.
β β βββ database.py # Handles data storage (SQLite/PostgreSQL).
β β βββ cache.py # (Upcoming) Redis-based caching (for faster performance)
β β βββ api_client.py # (Upcoming) Handles API integrations (with external APIs)
β β
β βββ utils/ # Utility functions (config, logging)
β β βββ __init__.py # Package init file
β β βββ config_loader.py # Load config files or .env
β β βββ logger.py # Logging setup
β β βββ middleware.py # (Upcoming) Preprocess messages before reaching handlers (spam filtering, normalization)
β β
β βββ plugins/ # (Upcoming) Dynamic plugin modules)
β βββ locales/ # (Upcoming) Multi-language support
β βββ dashboard/ # (Upcoming) FastAPI-based admin panel
β βββ config.py # Main bot configuration
β
βββ tests/ # Unit and integration tests
β βββ test_config.py # Tests for config loader
β βββ test_database.py # Tests for database functionality
β βββ test_handlers.py # Tests for bot command handlers
β
βββ docs/ # Documentation files
β βββ SETUP.md # Setup guide
β βββ TESTING.md # Testing guide
β βββ DEPLOYMENT.md # Deployment instructions
β
βββ .gitignore # Git ignore rules
βββ .env # Environment variables
βββ LICENSE # License file
βββ README.md # Project documentation
βββ CODE_OF_CONDUCT.md # Code of conduct for contributors
βββ AUTHORS.md # List of contributors
βββ CONTRIBUTING.md # Contribution guidelines
βββ pyproject.toml # Poetry configuration
βββ poetry.lock # Poetry dependencies
βββ run.sh # Script to run the bot
To run unit tests:
poetry run pytest
Fork this repository.
git clone https://github.com/LoboGuardian/kernel-telegram-bot.git
poetry install
git checkout -b feature/new-feature
git commit -m "Added a new feature"
Push and create a pull request.
This project is licensed under the GPL-3.0 License. See the LICENSE file for details.
- Created by LoboGuardian
We welcome contributions! Our current focus areas include:
-
Expanding testing coverage.
-
Optimizing database performance.
-
Enhancing real-time event handling with WebSockets.
-
If you want to contribute, fork the repo and submit a PR.
Feel free to discuss improvements via GitHub Issues.