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 src/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.
You can start the bot in two ways:
poetry run python src/main.py
Make sure run.sh is executable:
chmod +x run.sh
./run.sh
poetry run python src/main.py --webhook
kernel-telegram-bot/
βββ src/ # Core application logic
β βββ __init__.py # Marks the src directory as a package
β βββ main.py # Bot entry point and event loop starter
β β
β βββ handlers/ # Telegram command handlers (organized by category)
β β βββ __init__.py # Initializes the handlers package
β β βββ common/ # General-purpose commands
β β β βββ __init__.py
β β β βββ start.py # Handles the /start command
β β β βββ help.py # Provides a list of available commands (/help)
β β β βββ ping.py # Health check (/ping)
β β β βββ about.py # Shows bot info (/about)
β β β
β β βββ fun/ # Fun and entertainment-related commands
β β β βββ __init__.py
β β β βββ joke.py # Returns a random joke (/joke)
β β β βββ roll.py # Rolls a dice emoji (/roll)
β β β βββ quote.py # (Optional) Sends a random quote (/quote)
β β β
β β βββ utility/ # Utility commands (tools, lookups, etc.)
β β β βββ __init__.py
β β β βββ weather.py # Fetches weather info for a city (/weather <city>)
β β β βββ currency.py # Converts currency values (/currency USD EUR 100)
β β β βββ echo.py # Repeats the user's input (/echo <text>)
β β β βββ time.py # Shows current server time (/time)
β β β
β β βββ admin/
β β β βββ __init__.py
β β β βββ status.py # /status - Check bot or system status
β β β βββ ban.py # /ban <user_id> - Ban a user
β β β βββ unban.py # /unban <user_id> - Unban a user
β β β βββ list_groups.py # /listgroups - List groups where the bot is active
β β β βββ leave_group.py # /leavegroup <group_id> - Force the bot to leave a group
β β β βββ broadcast.py # /broadcast <message> - Send message to all groups/channels
β β β βββ admins.py # /admins - List group/channel admins
β β β βββ promote.py # /promote <user_id>
β β β βββ demote.py # /demote <user_id>
β β β βββ mute.py # /mute <user_id> [duration]
β β β βββ unmute.py # /unmute <user_id>
β β β βββ warn.py # /warn <user_id> - Issue warning
β β β βββ kick.py # /kick <user_id> - Kick user
β β β βββ purge.py # /purge - Clean messages
β β β βββ set_rules.py # /setrules <text> - Define rules
β β β βββ rules.py # /rules - Show current rules
β β β βββ pin_message.py # /pin - Pin the replied message (admin only)
β β β
β β βββ fallback/ # Fallbacks and error handlers
β β βββ __init__.py
β β βββ unknown.py # Handles unknown or invalid commands
β β βββ errors.py # Global error and exception handling
β β
β βββ services/ # Business logic and backend integrations
β β βββ __init__.py # Initializes the services package
β β βββ auth.py # User authentication and permission checks
β β βββ database.py # Data persistence (supports SQLite & PostgreSQL)
β β βββ cache.py # (Planned) Redis-based caching for performance
β β βββ api_client.py # (Planned) Integration with third-party APIs
β β
β βββ utils/ # Utility modules for shared logic
β β βββ __init__.py # Initializes the utils package
β β βββ config_loader.py # Loads environment variables and settings
β β βββ logger.py # Sets up structured logging for the app
β β βββ middleware.py # (Planned) Preprocessing for messages (e.g., spam filtering)
β β
β βββ plugins/ # (Planned) Dynamic plugin loader for modular extensions
β βββ locales/ # (Planned) Internationalization and language packs
β βββ dashboard/ # (Planned) FastAPI-powered admin panel for bot management
β βββ config.py # Centralized bot configuration and constants
β
βββ tests/ # Automated unit and integration tests
β βββ test_config.py # Tests for configuration loading
β βββ test_database.py # Tests for data persistence logic
β βββ test_handlers.py # Tests for Telegram command responses
β
βββ docs/ # Documentation and guides
β βββ SETUP.md # How to install and configure the bot
β βββ TESTING.md # How to write and run tests
β βββ DEPLOYMENT.md # Deployment guide and best practices
β
βββ .gitignore # Specifies intentionally untracked files to ignore in Git
βββ .env # Environment-specific variables (e.g., API keys, tokens)
βββ LICENSE # License file for open source usage terms
βββ README.md # Main project overview and usage documentation
βββ CODE_OF_CONDUCT.md # Guidelines for respectful community behavior
βββ AUTHORS.md # List of project authors and contributors
βββ CONTRIBUTING.md # Instructions for contributing to the project
βββ pyproject.toml # Project configuration for Poetry (dependencies, metadata)
βββ poetry.lock # Locked dependency versions for reproducible builds
βββ run.sh # Shell script to launch the bot with one command
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.