A secure, self-hosted Telegram bot for managing Two-Factor Authentication (2FA) TOTP codes. Store your TOTP secrets securely and generate time-based one-time passwords directly through Telegram.
git clone https://github.com/your-username/tg-totp.git
cd tg-totp
pip install -r requirements.txt
Environment Configuration
Create a .env
file in the project root:
# Telegram Bot Configuration
BOT_TOKEN=your_bot_token_here
API_ID=your_api_id
API_HASH=your_api_hash
# Database Configuration
DATABASE_URL=sqlite:///./tg_totp.db
python manage.py migrate
python main.py
For development with auto-restart on file changes:
python watcher.py
Variable | Description | Required | Default |
---|---|---|---|
BOT_TOKEN |
Telegram Bot Token from BotFather | β | - |
API_ID |
Telegram API ID from my.telegram.org | β | - |
API_HASH |
Telegram API Hash from my.telegram.org | β | - |
DATABASE_URL |
Database connection URL | β | sqlite:///./tg_totp.db |
/newbot
api_id
and api_hash
Command | Description | Usage |
---|---|---|
/start |
Initialize bot and check status | /start |
/help |
Show help message or command usage | /help [command] |
Command | Description | Usage |
---|---|---|
/add |
Add a new TOTP secret manually | /add secret=ABC123,issuer=Google |
/adduri |
Add secret from TOTP URI | /adduri otpauth://totp/... |
/addurifile |
Add secrets from uploaded file | /addurifile (with file) |
/list |
List all stored secrets | /list [page] |
/get |
Get TOTP code for specific service | /get google |
/rm |
Remove a secret by ID | /rm 123 |
/reset |
Remove all secrets (with confirmation) | /reset |
/total |
Show total count of stored secrets | /total |
Command | Description | Usage |
---|---|---|
/export |
Export secrets as text/file | /export [id] |
/exportqr |
Export secrets as QR codes | /exportqr [id] |
Command | Description | Usage |
---|---|---|
/temp |
Generate TOTP without saving | /temp secret=ABC123,issuer=Test |
/settings |
Manage bot settings | /settings page_size=20 |
Method 1: Manual Entry
/add secret=JBSWY3DPEHPK3PXP,issuer=Google,[email protected]
Method 2: URI Format
/adduri otpauth://totp/Google:[email protected]?secret=JBSWY3DPEHPK3PXP&issuer=Google
Method 3: File Upload
/addurifile
command# With custom settings
/add secret=JBSWY3DPEHPK3PXP,issuer=GitHub,name=myaccount,digits=8,period=60,algorithm=SHA256
# Supported parameters:
# - secret: (Required) Base32 encoded secret
# - issuer: (Required) Service name
# - name: Account identifier
# - digits: OTP length (6-8, default: 6)
# - period: Validity period in seconds (15-120, default: 30)
# - algorithm: Hash algorithm (SHA1/SHA256/SHA512, default: SHA1)
# Search by service name
/get google
# Search by account name
/get [email protected]
# Response includes:
# - Service name and account
# - Current TOTP code
# - Time remaining until next code
# - Secret ID for management
# Export all secrets as text
/export
# Export specific secret by ID
/export 123
# Export all as QR codes (ZIP file)
/exportqr
# Export specific secret as QR code
/exportqr 123
The bot uses SQLite with Django ORM:
id
: Auto-increment primary keyname
: Userβs display nametelegram_id
: Unique Telegram user IDstatus
: Account status (active/suspended/banned)settings
: JSON field for user preferencesjoining_date
: Account creation timestamplast_updated
: Last modification timestampid
: Auto-increment primary keyuser
: Foreign key to Usersecret
: Encrypted TOTP secret (unique)issuer
: Service provider nameaccount_id
: Account identifierdigits
: OTP digit count (default: 6)period
: Time period in seconds (default: 30)algorithm
: Hash algorithm (default: SHA1)joining_date
: Secret creation timestamplast_updated
: Last modification timestamptg-totp/
βββ main.py # Application entry point
βββ manage.py # Django management
βββ watcher.py # Development auto-reload
βββ requirements.txt # Python dependencies
βββ pyproject.toml # Project configuration
βββ sqlitedb/ # Database models and utilities
β βββ models.py # Django models
β βββ migrations/ # Database migrations
β βββ utils.py # Database utilities
βββ telegram/ # Telegram bot implementation
β βββ commands/ # Bot command handlers
β βββ replier.py # Main bot class
β βββ utils.py # Telegram utilities
β βββ strings.py # Bot messages
βββ totp/ # TOTP generation logic
βββ totp.py # TOTP implementation
# Install test dependencies
pip install pytest pytest-django pytest-cov
# Run tests
pytest
# Run with coverage
pytest --cov=.
The project uses several tools for code quality:
# Install pre-commit hooks
pre-commit install
# Run linting
pre-commit run --all-files
# Manual linting
ruff check .
black .
mypy .
telegram/commands/
telegram/replier.py
telegram/commands/help.py
Create a Dockerfile
:
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
RUN python manage.py migrate
CMD ["python", "main.py"]
Create /etc/systemd/system/tg-totp.service
:
[Unit]
Description=Telegram TOTP Bot
After=network.target
[Service]
Type=simple
User=your-user
WorkingDirectory=/path/to/tg-totp
ExecStart=/usr/bin/python3 main.py
Restart=always
RestartSec=10
Environment=PATH=/usr/bin:/usr/local/bin
EnvironmentFile=/path/to/tg-totp/.env
[Install]
WantedBy=multi-user.target
Enable and start:
sudo systemctl enable tg-totp
sudo systemctl start tg-totp
git checkout -b feature/amazing-feature
)git commit -m 'Add amazing feature'
)git push origin feature/amazing-feature
)This project is licensed under the MIT License - see the LICENSE file for details.
.env
file secure and never commit it to version controlBot not responding:
BOT_TOKEN
is correctDatabase errors:
python manage.py migrate
DATABASE_URL
formatImport/Export issues:
/help
command in the botLive Bot: You can check the progress here
Made with β€οΈ for secure 2FA management