Giveaway Tool is a comprehensive backend service for managing and conducting giveaways within Telegram communities. It provides robust infrastructure for creating, managing, and executing fair and transparent giveaways with blockchain integration and advanced requirement verification.
- Giveaway Management: Create and manage giveaways with multiple prizes and custom requirements
- TON Blockchain Integration: Verify wallet ownership through TON Proof and check on-chain balances
- Telegram Integration: Full integration with Telegram API for channel management and user verification
- Flexible Requirements System: Support for custom requirements including:
- Channel subscriptions
- Premium Telegram status
- TON wallet balance checks
- On-chain asset verification
- Participant Management: Track participants and verify their eligibility in real-time
- Winner Selection: Fair and random winner selection with prize distribution
- Notifications System: Automated notifications for participants and winners
- Caching Layer: Redis-based caching for optimal performance
- RESTful API: Clean and well-structured API endpoints
- Backend: Go 1.23 with Fiber framework
- Database: PostgreSQL 16 with Goose migrations
- Caching & Streams: Redis 7
- Blockchain: TON integration with Tongo library
- Authentication: Telegram Mini Apps Init Data validation
- Containerization: Docker and Docker Compose
To install and run the project, follow these steps:
-
Prerequisites:
- Docker and Docker Compose must be installed on your machine
- Go 1.23 or higher (for local development)
- Goose migration tool for database management
- A Telegram Bot Token (obtain from @BotFather)
-
Setup:
-
Clone the repository:
git clone https://github.com/OpenBuilders/giveaway-tool-backend.git cd giveaway-tool-backend -
Create environment configuration:
cp .env.example .env
Edit
.envwith your configuration (see Configuration section) -
Install Goose for migrations:
go install github.com/pressly/goose/v3/cmd/goose@latest
-
Install dependencies:
make tidy
-
-
Running with Docker (Recommended):
- Build and start all services:
docker-compose up -d
- The API will be accessible at
http://localhost:8081
- Build and start all services:
-
Running locally (Development):
- Ensure PostgreSQL and Redis are running
- Run migrations:
make goose-up
- Start the API:
make run
The application uses environment variables for configuration. Create a .env file in the root directory with the following variables:
# HTTP Server
HTTP_ADDR=:8080
# Database
DATABASE_URL=postgres://user:password@localhost:5432/giveaway?sslmode=disable
POSTGRES_USER=user
POSTGRES_PASSWORD=password
POSTGRES_DB=giveaway
# Redis
REDIS_ADDR=localhost:6379
REDIS_PASSWORD=
REDIS_DB=0
# Telegram
TELEGRAM_BOT_TOKEN=your_bot_token_here
# Security
INIT_DATA_TTL=86400 # 24 hours in seconds
# CORS
CORS_ALLOWED_ORIGINS=*| Variable | Description | Default |
|---|---|---|
HTTP_ADDR |
HTTP server address | :8080 |
DATABASE_URL |
PostgreSQL connection string | - |
REDIS_ADDR |
Redis server address | localhost:6379 |
REDIS_PASSWORD |
Redis password (if required) | - |
REDIS_DB |
Redis database number | 0 |
TELEGRAM_BOT_TOKEN |
Telegram bot token from @BotFather | - |
INIT_DATA_TTL |
Init data validation TTL in seconds | 86400 |
CORS_ALLOWED_ORIGINS |
Allowed CORS origins | * |
The project includes several make commands to simplify development and operation:
make tidy: Tidy Go module dependenciesmake build: Build the application binarymake run: Run the application locallymake test: Run all testsmake lint: Run golangci-lint for code qualitymake goose-up: Run database migrationsmake goose-down: Rollback last migrationmake goose-status: Check migration statusmake migrate-create name=<migration_name>: Create a new migration
-
Start all services:
docker-compose up -d
-
Stop all services:
docker-compose down
-
View logs:
docker-compose logs -f api
-
Rebuild containers:
docker-compose up -d --build
The project follows clean architecture principles with clear separation of concerns:
cmd/api: Application entrypoint and HTTP server initializationinternal/config: Configuration loading and validationinternal/domain: Domain models and interfacesgiveaway: Giveaway domain models and repository interfacesuser: User domain models and repository interfaces
internal/repository/postgres: PostgreSQL repository implementationsinternal/cache/redis: Redis cache implementationsuser_cache.go: User data cachingchannel_avatar_cache.go: Channel avatar URL cachingchannel_photo_cache.go: Channel photo caching
internal/service: Business logic layergiveaway: Giveaway management serviceuser: User management servicetelegram: Telegram API client wrappertonproof: TON Proof verification servicetonbalance: TON balance checking servicechannels: Telegram channel managementnotifications: Notification dispatch service
internal/http: HTTP handlers and routingfiber_app.go: Fiber application setupgiveaway_handlers.go: Giveaway endpointsuser_handlers.go: User endpointsrequirements_handlers.go: Requirements verification endpointstonproof_handlers.go: TON Proof endpointschannel_handlers.go: Channel endpointsmiddleware/: HTTP middleware (init data validation, caching)
internal/platform: Infrastructure setupdb: PostgreSQL connectionredis: Redis connection
internal/workers: Background workersredis_stream.go: Redis stream consumer for async tasks
internal/utils: Utility functionsrandom: Random number generation and shufflingtelegram: Telegram-specific utilities
migrations: Database migrations (Goose format)
The API provides the following endpoint groups:
- Giveaways: Create, update, retrieve, and manage giveaways
- Participants: Register participants and check eligibility
- Winners: Select winners and retrieve winner lists
- Requirements: Verify participant requirements
- Users: User profile management
- Channels: Channel information and verification
- TON Proof: Wallet verification and proof generation
We welcome contributions to Giveaway Tool! Here's how you can contribute:
- Fork the repository
- Create a feature branch:
git checkout -b feat/your-feature-name - Set up your local environment (see Installation section)
- Make your changes
- Run tests:
make test - Run linter:
make lint - Commit your changes:
git commit -m "feat: add some feature" - Push to the branch:
git push origin feat/your-feature-name - Submit a pull request
- Go code should follow standard Go conventions and pass
golangci-lint - Use meaningful variable and function names
- Add comments for exported functions and types
- Keep functions small and focused on a single responsibility
- Write tests for new features and bug fixes
- Ensure all tests pass before submitting PR
We follow conventional commits format:
feat:- New featurefix:- Bug fixdocs:- Documentation changesrefactor:- Code refactoringtest:- Adding or updating testschore:- Maintenance tasks
Before submitting a pull request, ensure that:
-
All tests pass:
make test -
Code passes linting:
make lint
-
Migrations work correctly:
make goose-up make goose-down
The application follows Clean Architecture principles:
┌─────────────────────────────────────────────────────┐
│ HTTP Layer │
│ (Fiber Handlers) │
└────────────────────┬────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────┐
│ Service Layer │
│ (Business Logic) │
└────────────────────┬────────────────────────────────┘
│
▼
┌──────────────────────────────┬──────────────────────┐
│ Repository Layer │ Cache Layer │
│ (PostgreSQL) │ (Redis) │
└──────────────────────────────┴──────────────────────┘
- Dependency Injection: Services receive dependencies through constructors
- Interface-based Design: Domain layer defines interfaces, implementations are in infrastructure layer
- Context Propagation: Context is passed through all layers for cancellation and tracing
- Error Handling: Consistent error handling with proper error wrapping
- Caching Strategy: Strategic caching of frequently accessed data (user info, channel data)
This project is licensed under the MIT License — see the LICENSE file for details.
- Giveaway Tool is developed and maintained by Open Builders
- Built with ❤️ for the Telegram and TON communities
- Special thanks to all contributors who have helped shape this project
For issues, questions, or contributions, please:
- Open an issue on GitHub
- Submit a pull request
- Contact the maintainers
Built by Open Builders | Part of the Tools.tg ecosystem