A powerful and flexible framework for building Telegram bots using Python. Built on top of python-telegram-bot
, this framework provides a plugin-based architecture that makes it easy to develop, maintain, and extend Telegram bots.
- Plugin System: Modular architecture where each feature is a plugin that can be enabled/disabled at runtime
- FastAPI Integration: Each plugin can create its own HTTP endpoints
- SQLite Integration: Built-in database support for persistent storage
- Configuration Management: JSON-based configuration system for both global and per-plugin settings
- Advanced Logging: File-based logging with rotation support
- Decorator System: Handy decorators for common bot tasks like:
- Typing notifications
- Access control (private/public/owner commands)
- Command filtering (blacklists/whitelists)
- Built-in Plugins:
- Error handling
- Backup/restore functionality
- Bot control (restart/shutdown)
- Plugin management
- User message tracking
- Clone the repository:
git clone https://github.com/xian-network/tg-bot.git
cd tg-bot
- Install Poetry (Python dependency management):
curl -sSL https://install.python-poetry.org | python3 -
- Install dependencies:
poetry install
- Create
.env
file in the root directory:
TG_TOKEN=your_telegram_bot_token
LOG_LEVEL=INFO # DEBUG, INFO, WARNING, or ERROR
LOG_INTO_FILE=true
The global configuration file (cfg/global.json
) contains settings that affect the entire bot:
{
"admin_tg_id": 123456789,
"webserver_port": 5000,
"xian": {
"node": "http://127.0.0.1:26657",
"explorer": "https://explorer.xian.org",
"chain_id": "your-chain-id"
}
}
Each plugin can have its own configuration file in the cfg
folder. Common settings include:
handle
: Command triggerdependency
: List of required pluginsadmins
: List of admin user IDsdescription
: Plugin descriptioncategory
: Plugin categoryblacklist
/whitelist
: Access control listsblacklist_msg
/whitelist_msg
: Custom messages for access denied
The repository includes both a startup script (start.sh
) and PM2 configuration file (pm2.config.json
). The startup script automatically detects the Poetry virtual environment, and the PM2 configuration is pre-configured with recommended settings for production use.
First, make the startup script executable:
chmod +x start.sh
- PM2 Commands:
# Start the bot
pm2 start pm2.config.json
# View status
pm2 status
# Monitor logs
pm2 logs tg-bot
# Monitor resources
pm2 monit
# Stop the bot
pm2 stop tg-bot
# Restart the bot
pm2 restart tg-bot
# Remove from PM2
pm2 delete tg-bot
# Set up auto-start on system boot
pm2 startup
pm2 save
screen -S tgbf2
poetry run python main.py
# Press Ctrl+A, D to detach
# Use 'screen -r tgbf2' to reattach
- Stop the bot:
pm2 stop tg-bot
- Pull latest changes:
git reset --hard origin/main
git pull origin main
- Update dependencies:
poetry install
- Restart the bot:
pm2 restart tg-bot
Plugins are Python modules that extend the bot's functionality. For detailed information about creating and working with plugins, see the Plugin Development Guide.
To enable the FastAPI web server:
- In
cfg/global.json
, set:
{
"webserver_port": 5000
}