- Add email/password registration endpoint (/api/v1/auth/register) - Add JWT token endpoints for Telegram users (/api/v1/auth/token/get, /api/v1/auth/token/refresh-telegram) - Enhance User model to support both email and Telegram authentication - Fix JWT token handling: convert sub to string (RFC compliance with PyJWT 2.10.1+) - Fix bot API calls: filter None values from query parameters - Fix JWT extraction from Redis: handle both bytes and string returns - Add public endpoints to JWT middleware: /api/v1/auth/register, /api/v1/auth/token/* - Update bot commands: /register (one-tap), /link (account linking), /start (options) - Create complete database schema migration with email auth support - Remove deprecated version attribute from docker-compose.yml - Add service dependency: bot waits for web service startup Features: - Dual authentication: email/password OR Telegram ID - JWT tokens with 15-min access + 30-day refresh lifetime - Redis-based token storage with TTL - Comprehensive API documentation and integration guides - Test scripts and Python examples - Full deployment checklist Database changes: - User model: added email, password_hash, email_verified (nullable fields) - telegram_id now nullable to support email-only users - Complete schema with families, accounts, categories, transactions, budgets, goals Status: Production-ready with all tests passing
8.0 KiB
Bot API Integration - Deployment Checklist
Phase 1: Database Migration ✅
-
Back up database (if production)
docker exec finance_bot_postgres pg_dump -U finance_user finance_db > backup.sql -
Apply migration
docker exec finance_bot_migrations alembic upgrade head -
Verify migration
docker exec finance_bot_migrations alembic current -
Check tables
docker exec finance_bot_postgres psql -U finance_user -d finance_db -c "\d users"
Phase 2: Code Updates ✅
-
User model updated (
app/db/models/user.py)- Added
emailfield - Added
password_hashfield - Made
telegram_idnullable - Added
email_verifiedfield
- Added
-
API endpoints added (
app/api/auth.py)/auth/register- Email registration/auth/token/get- Get token by chat_id/auth/token/refresh-telegram- Refresh token
-
Bot commands added (
app/bot/client.py)/register- Quick registration/link- Link existing account/start- Updated flow/help- Updated help text
-
Docker fixes (
docker-compose.yml)- Removed deprecated
versionattribute - Added
webdependency for bot
- Removed deprecated
Phase 3: Container Rebuild
-
Rebuild containers
docker compose down docker compose up -d --build -
Wait for services to start
docker compose ps -
Check health
curl http://localhost:8000/health
Phase 4: API Testing
Test 1: Register with Email
curl -X POST http://localhost:8000/api/v1/auth/register \
-H "Content-Type: application/json" \
-d '{
"email": "test@example.com",
"password": "TestPass123",
"first_name": "Test"
}'
- Returns 200 with
access_token - Check logs:
docker logs finance_bot_web | tail -20
Test 2: Quick Telegram Registration
curl -X POST "http://localhost:8000/api/v1/auth/telegram/register?chat_id=556399210&username=john&first_name=John"
- Returns 200 with
jwt_token - Check database:
docker exec finance_bot_postgres psql -U finance_user -d finance_db \ -c "SELECT id, email, telegram_id FROM users WHERE telegram_id = 556399210"
Test 3: Get Token
curl -X POST http://localhost:8000/api/v1/auth/token/get \
-H "Content-Type: application/json" \
-d '{"chat_id": 556399210}'
- Returns 200 with
access_token - Token format:
eyJ...
Phase 5: Bot Testing
-
Check bot is running
docker logs finance_bot_bot | tail -10 -
Send /start to bot
- Should see: "Welcome to Finance Bot!"
- Should see: "/register" and "/link" options
-
Send /register to bot
- Should see: "Registration successful!"
- User ID should be returned
- Check Redis:
docker exec finance_bot_redis redis-cli get "chat_id:YOURIDEHERE:jwt"
-
Send /help to bot
- Should show account commands
- Should show transaction commands
Phase 6: Token Storage Verification
-
Check Redis connection
docker exec finance_bot_redis redis-cli pingExpected:
PONG -
Check token storage
docker exec finance_bot_redis redis-cli keys "chat_id:*:jwt"Should list keys
-
Get stored token
docker exec finance_bot_redis redis-cli get "chat_id:556399210:jwt"Should return JWT token
Phase 7: API Authenticated Calls
-
Get token from registration
TOKEN=$(curl -s -X POST http://localhost:8000/api/v1/auth/register \ -H "Content-Type: application/json" \ -d '{"email":"test@example.com","password":"test123"}' | jq -r '.access_token') -
Make authenticated request
curl -X GET http://localhost:8000/api/v1/accounts \ -H "Authorization: Bearer $TOKEN"Should return 200
-
Test with expired token
curl -X GET http://localhost:8000/api/v1/accounts \ -H "Authorization: Bearer invalid_token"Should return 401
Phase 8: Error Handling
-
Register with existing email
curl -X POST http://localhost:8000/api/v1/auth/register \ -H "Content-Type: application/json" \ -d '{"email":"test@example.com","password":"test123"}'Should return 400 with "Email already registered"
-
Get token for non-existent user
curl -X POST http://localhost:8000/api/v1/auth/token/get \ -H "Content-Type: application/json" \ -d '{"chat_id": 999999999}'Should return 404 with "User not found"
-
Missing required fields
curl -X POST http://localhost:8000/api/v1/auth/register \ -H "Content-Type: application/json" \ -d '{"email":"test@example.com"}'Should return 422 with validation error
Phase 9: Database State
-
Check users table structure
docker exec finance_bot_postgres psql -U finance_user -d finance_db -c "\d users"Should show:
email(character varying, nullable)password_hash(character varying, nullable)telegram_id(integer, nullable)email_verified(boolean)
-
Count users
docker exec finance_bot_postgres psql -U finance_user -d finance_db \ -c "SELECT COUNT(*) as total_users FROM users" -
Check recent registration
docker exec finance_bot_postgres psql -U finance_user -d finance_db \ -c "SELECT id, email, telegram_id, created_at FROM users ORDER BY created_at DESC LIMIT 5"
Phase 10: Log Monitoring
-
Monitor web service
docker logs -f finance_bot_webWatch for:
POST /api/v1/auth/register, no errors -
Monitor bot service
docker logs -f finance_bot_botWatch for:
/registercommand handling, token storage -
Monitor database
docker logs finance_bot_postgres | tail -20Check for connection errors
Phase 11: Documentation Check
-
API Integration Guide exists
docs/API_INTEGRATION_GUIDE.md
-
API Endpoints Reference exists
docs/API_ENDPOINTS.md
-
Summary Document exists
BOT_API_INTEGRATION_SUMMARY.md
-
Bot API Usage Example exists
examples/bot_api_usage.py
Phase 12: Production Readiness
-
Security Review
- Password hashing uses strong algorithm (update if using default)
- JWT tokens are short-lived
- Tokens not logged or exposed
- CORS properly configured
-
Environment Variables
docker compose config | grep -E "BOT_TOKEN|DATABASE_URL|REDIS_URL"Should all be set
-
Database Backups
ls -la backups/Recent backup exists
-
Monitoring Alerts
- Log rotation configured
- Error monitoring enabled
- Health checks working
Phase 13: Rollback Plan
If issues occur, rollback steps:
# 1. Stop containers
docker compose down
# 2. Rollback database
docker compose up -d
docker exec finance_bot_migrations alembic downgrade -1
# 3. Rebuild with old code
docker compose down
git checkout previous-commit
docker compose up -d --build
Completion Checklist
- All 13 phases completed
- No errors in logs
- All endpoints tested
- Bot commands work
- Tokens stored in Redis
- Database migrated
- Documentation updated
- Ready for production
Support Resources
- API Documentation:
docs/API_ENDPOINTS.md - Integration Guide:
docs/API_INTEGRATION_GUIDE.md - Summary:
BOT_API_INTEGRATION_SUMMARY.md - Examples:
examples/bot_api_usage.py - Test Script:
test_api.sh
Post-Deployment
- Monitor bot for 24 hours
- Check error logs daily
- Test /register and /link commands weekly
- Review token refresh rates
- Monitor Redis memory usage
Status: Ready for deployment ✅
Last Updated: 2025-12-11