π Expense Tracker Bot

A robust, enterprise-grade Telegram bot for tracking personal expenses with
PostgreSQL backend, built with Go. Features comprehensive expense management,
category organization, vehicle expense tracking, and detailed analytics.
π Quick Start β’ π Features β’ ποΈ Architecture β’ π§ͺ Testing β’ π§ Development β’ π Deployment
π Features
π° Core Functionality
- π Expense Tracking: Add, edit, delete, and list expenses with ease
- π Category Management: Organized expense categories with emojis
- π Vehicle Expenses: Special handling for fuel, service, and maintenance costs
- π Reports & Analytics: Generate comprehensive expense reports and statistics
- π Dashboard: Visual overview of spending patterns and trends
π’ Enterprise Features
- π‘οΈ Robust Error Handling: Custom error types with proper error categorization
- β
Input Validation: Comprehensive validation for all user inputs
- ποΈ Service Layer Architecture: Clean separation of business logic
- π§ Middleware Support: Cross-cutting concerns like logging, rate limiting, and metrics
- π§ͺ Comprehensive Testing: Unit tests with mocking and test coverage
- π Structured Logging: JSON logging with context and correlation IDs
- ποΈ Database Abstraction: Interface-based database layer with PostgreSQL support
- βοΈ Configuration Management: Environment-based configuration with validation
- π¦ Rate Limiting: Built-in rate limiting to prevent abuse
- π Metrics Collection: Application metrics for monitoring
ποΈ Architecture
High-Level Architecture
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β Telegram API β β Bot Handler β β Service Layer β
β βββββΊβ βββββΊβ β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β β
βΌ βΌ
βββββββββββββββββββ βββββββββββββββββββ
β Middleware β β Validation β
β β β β
βββββββββββββββββββ βββββββββββββββββββ
β β
βΌ βΌ
βββββββββββββββββββ βββββββββββββββββββ
β Database Layer β β Error Handler β
β β β β
βββββββββββββββββββ βββββββββββββββββββ
β
βΌ
βββββββββββββββββββ
β PostgreSQL β
β β
βββββββββββββββββββ
π Quick Start
π One-Command Setup (Recommended)
For the fastest setup experience, use our automated setup script:
# Clone the repository
git clone https://github.com/MitulShah1/expense-tracker-bot.git
cd expense-tracker-bot
# Run the quick start script
./scripts/quick-start.sh
This script will automatically:
- β
Check all prerequisites (Go, Docker)
- π Create and configure your
.env
file
- ποΈ Start PostgreSQL database with Docker
- π§ Install dependencies and run migrations
- ποΈ Build the application
- π§ͺ Run tests
- π Provide next steps
π Prerequisites
βοΈ Manual Setup
If you prefer manual setup or the automated script doesn't work:
Step 1: Clone the Repository
git clone https://github.com/MitulShah1/expense-tracker-bot.git
cd expense-tracker-bot
Step 2: Run Complete Setup Script
# Make scripts executable
chmod +x scripts/*.sh
# Run the complete setup
./scripts/setup.sh
Step 3: Manual Environment Configuration
-
Create the environment file:
cp .env.example .env # if .env.example exists
# or create manually:
-
Configure your environment variables:
# Telegram Bot Configuration
TELEGRAM_TOKEN=your_telegram_bot_token
BOT_ID=your_bot_id
# Database Configuration
DATABASE_URL=postgres://postgres:password@localhost:5432/expense_tracker?sslmode=disable
# Application Configuration
LOG_LEVEL=info
IS_DEV_MODE=true
# Database Connection Pool (optional)
DB_MAX_OPEN_CONNS=25
DB_MAX_IDLE_CONNS=5
DB_CONN_MAX_LIFETIME=5m
Note: You'll need to create a Telegram bot first. Visit @BotFather on Telegram to create your bot and get the token.
Step 4: Manual Database Setup
-
Start PostgreSQL using Docker Compose:
docker-compose up -d postgres
-
Run database migrations:
make migrate
Step 5: Manual Application Setup
-
Install dependencies:
make deps
-
Build the application:
make build
-
Run the application:
make run
π― After Setup
Once setup is complete:
- Update your bot token: Edit
.env
file and replace your_telegram_bot_token_here
with your actual bot token
- Start the bot:
./expense-tracker-bot
or make run
- Test the bot: Use
./scripts/test_bot.sh
to test functionality
- Access pgAdmin: Visit http://localhost:8080 (admin@expense-tracker.com / admin)
π§ Available Scripts
./scripts/quick-start.sh
- One-command setup with confirmation
./scripts/setup.sh
- Complete automated setup
./scripts/setup_database.sh
- Database-only setup
./scripts/test_bot.sh
- Test the bot functionality
π§ͺ Testing
πββοΈ Running Tests
# Run all tests
make test
# Run tests with coverage
make test-coverage
# Run specific test package
go test ./internal/services/...
π Test Coverage
The application includes comprehensive unit tests with:
- β
Service layer testing with mocked dependencies
- β
Input validation testing
- β
Error handling testing
- β
Database operation testing
π§ Development
π― Code Quality
# Format code
make fmt
# Run linter
make lint
# Run all quality checks
make all
π Adding New Features
- Service Layer: Add business logic in
internal/services/
- Validation: Add input validation in
internal/validation/
- Database: Add database operations in
internal/database/
- Tests: Add corresponding tests in the same package
- Documentation: Update this README and add inline documentation
β οΈ Error Handling
The application uses custom error types for better error categorization:
ValidationError
: Input validation errors
NotFoundError
: Resource not found errors
DatabaseError
: Database operation errors
TelegramError
: Telegram API errors
UnauthorizedError
: Authorization errors
RateLimitError
: Rate limiting errors
π Monitoring & Observability
π Logging
The application uses structured JSON logging with:
- π Request correlation IDs
- π€ User context
- β‘ Performance metrics
- β Error details
π Metrics
Built-in metrics collection for:
- π Request counts
- β Error rates
- β±οΈ Response times
- π₯ Active users
π₯ Health Checks
Database connectivity and application health monitoring.
π Security
- β
Comprehensive validation for all user inputs
- π‘οΈ SQL injection prevention through parameterized queries
- π¦ Rate limiting to prevent abuse
- π User authorization checks
π Data Protection
- π Secure database connections
- βοΈ Environment-based configuration
- π« No sensitive data in logs
π Deployment
π³ Docker Deployment
# Build Docker image
docker build -t expense-tracker-bot .
# Run with Docker Compose
docker-compose up -d
π Production Considerations
- βοΈ Use environment-specific configurations
- π Set up proper logging aggregation
- ποΈ Configure database connection pooling
- π Implement monitoring and alerting
- πΎ Set up backup strategies
π€ Contributing
We welcome contributions! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
Contribution Guidelines
- π΄ Fork the repository
- πΏ Create a feature branch (
git checkout -b feature/amazing-feature
)
- βοΈ Make your changes
- π§ͺ Add tests for new functionality
- β
Ensure all tests pass (
make test
)
- π€ Commit your changes (
git commit -m 'Add some amazing feature'
)
- π Push to the branch (
git push origin feature/amazing-feature
)
- π Open a Pull Request
π Code Standards
- π Follow Go coding standards and conventions
- π§ͺ Add comprehensive tests for new functionality
- π Update documentation for any new features
- π¬ Use meaningful commit messages
- π Ensure code passes linting (
make lint
)
π License
This project is licensed under the MIT License - see the LICENSE file for details.
π Support
Need help? Here are your options:
- π Create an issue in the GitHub repository
- π Check the documentation in the
docs/
directory
- π§ͺ Review the code examples in the test files
- π¬ Join our community discussions
π Acknowledgments
Made with β€οΈ using Go and PostgreSQL
β¬οΈ Back to top