An intelligent Telegram bot that helps students solve problems from text, images, and voice messages using Google Gemini and OpenAI Speech-to-Text.
Designed for production deployment with PostgreSQL, Docker, Prometheus, and Railway.
AI Ustaz is a production-ready educational Telegram bot built to help students learn rather than simply receive answers.
The bot supports multiple input formats, automatically detects the subject of a question, maintains conversation context, and provides detailed explanations generated by AI.
Instead of acting like a chatbot, AI Ustaz behaves as a personal tutor capable of explaining mathematics, physics, chemistry, biology, geography, history, English, and other school subjects.
| Feature | Description |
|---|---|
| π€ AI Tutor | Step-by-step explanations powered by Google Gemini |
| π¬ Text Questions | Students can ask any question in plain text |
| πΌ Image Recognition | Solves homework from photos and textbook pages |
| π€ Voice Messages | Speech-to-text with OpenAI transcription |
| π§ Subject Detection | Automatically identifies the school subject |
| π Conversation Memory | Supports follow-up questions using previous context |
| π Analytics | Tracks users, requests, and activity statistics |
| π Feedback System | Collects Helpful / Not Helpful ratings |
| π Rate Limiting | Daily request limits per student |
| π Admin Commands | Built-in monitoring and administration tools |
| π Monitoring | Prometheus metrics and Health Check endpoint |
| π³ Production Ready | PostgreSQL, Docker, Railway deployment |
Students can send ordinary text questions and receive detailed explanations.
Homework, textbook pages, handwritten notes, and mathematical problems can be analyzed directly from images.
Voice messages are automatically transcribed using OpenAI and then processed by Gemini.
Each student has a profile containing request statistics and daily limits.
Administrators can monitor overall bot usage and feedback.
- Production-ready Go backend
- Modular architecture
- AI provider abstraction
- Conversation history
- Subject-aware prompting
- Daily rate limiting
- PostgreSQL persistence
- Structured logging
- Prometheus metrics
- Health checks
- Graceful shutdown
- Docker deployment
flowchart TD
User["π¨βπ Student"]
Telegram["Telegram Bot API"]
Handler["Telegram Handler"]
Tutor["Tutor Service"]
Classifier["Subject Detection"]
History["Conversation History"]
Limiter["Rate Limiter"]
Gemini["Google Gemini"]
OpenAI["OpenAI Speech-to-Text"]
Stats["Statistics"]
Postgres["PostgreSQL"]
Metrics["Prometheus"]
User --> Telegram
Telegram --> Handler
Handler --> Tutor
Tutor --> Classifier
Tutor --> History
Tutor --> Limiter
Tutor --> Stats
Tutor --> Gemini
Tutor --> OpenAI
History --> Postgres
Limiter --> Postgres
Stats --> Postgres
Tutor --> Metrics
The project follows a modular architecture where each package has a single responsibility.
The Telegram layer is responsible only for communication with Telegram.
Business logic is implemented inside services.
Infrastructure concerns such as AI providers, storage, statistics, metrics, and rate limiting are isolated behind interfaces.
This allows replacing implementations without changing business logic.
cmd/
βββ bot/ # Application entrypoint
βββ migrate/ # Database migrations
internal/
βββ access/ # User access management
βββ ai/ # Gemini & OpenAI clients
βββ app/ # Dependency initialization
βββ config/ # Environment configuration
βββ db/ # PostgreSQL connection
βββ entity/ # Domain models
βββ httpserver/ # Health server
βββ limiter/ # Daily request limits
βββ metrics/ # Prometheus metrics
βββ migrations/ # Embedded SQL migrations
βββ service/ # Business logic
βββ stats/ # Analytics
βββ storage/ # Conversation history
βββ telegram/ # Telegram handlers
pkg/
βββ logger/
docs/
βββ screenshots/
Dockerfile
docker-compose.yml
Makefile
README.md
- Go
- REST API
- Clean Architecture
- Dependency Injection
- Graceful Shutdown
- Google Gemini 2.5 Flash
- OpenAI Speech-to-Text
- Prompt Engineering
- Subject-specific prompting
- PostgreSQL
- pgx
- Goose Migrations
- Docker
- Docker Compose
- Railway
- Prometheus
- Health Check Endpoint
- Structured Logging
- go-telegram-bot-api
- Long Polling
- Voice Messages
- Image Processing
- Markdown Responses
Voice
β
βΌ
OpenAI Transcription
β
βΌ
Subject Detection
β
βΌ
Conversation Context
β
βΌ
Google Gemini
β
βΌ
Formatted Answer
- Mathematics
- Physics
- Chemistry
- Biology
- English
- History
- Geography
- General Questions
| Component | Responsibility |
|---|---|
| Telegram | User interaction |
| Tutor Service | Business logic |
| Gemini Client | AI responses |
| OpenAI Client | Voice transcription |
| Storage | Conversation history |
| Statistics | User analytics |
| Limiter | Daily request limits |
| Metrics | Prometheus monitoring |
| HTTP Server | Health endpoint |
| PostgreSQL | Persistent storage |
- β PostgreSQL persistence
- β Embedded SQL migrations
- β Conversation memory
- β Subject-aware prompting
- β Request analytics
- β Feedback collection
- β Prometheus metrics
- β Health endpoint
- β Structured logging
- β Docker deployment
- β Railway deployment
- β Graceful shutdown
- Go 1.24+
- PostgreSQL 15+
- Docker (optional)
- Google Gemini API Key
- OpenAI API Key
- Telegram Bot Token
Create a .env file:
APP_ENV=local
HTTP_PORT=8082
DATABASE_URL=postgres://postgres:password@localhost:5432/aiustaz?sslmode=disable
TELEGRAM_BOT_TOKEN=
ADMIN_TELEGRAM_ID=
AI_PROVIDER=gemini
GEMINI_API_KEY=
GEMINI_MODEL=gemini-2.5-flash
OPENAI_API_KEY=
OPENAI_TRANSCRIBE_MODEL=gpt-4o-mini-transcribeStart PostgreSQL
docker compose up -dRun migrations
make migrateStart the bot
make runInstall dependencies
go mod downloadRun migrations
go run ./cmd/migrateStart bot
go run ./cmd/bot| Command | Description |
|---|---|
| /start | Register user |
| /help | Show help |
| /profile | User profile |
| /limit | Remaining daily requests |
| /reset | Clear conversation history |
| Command | Description |
|---|---|
| /stats | Bot statistics |
GET /health
Example response
{
"status":"ok",
"database":"connected",
"version":"1.0.0"
}Prometheus endpoint
GET /metrics
Example metrics
telegram_requests_total
telegram_users_total
telegram_feedback_total
telegram_ai_requests_total
telegram_errors_total
The project follows several production practices:
- Environment variables for secrets
- PostgreSQL parameterized queries
- Daily request rate limiting
- Graceful shutdown
- Structured logging
- Health monitoring
- Conversation history isolation
Production optimizations include:
- PostgreSQL connection pooling
- Embedded SQL migrations
- Reusable AI client
- Modular services
- Lightweight HTTP server
- Memory-efficient handlers
- Split long Telegram messages
- Markdown sanitization
Run all tests
go test ./...Run with coverage
go test ./... -coverThe application is designed for cloud deployment.
Tested with:
- Railway
- Docker
- PostgreSQL
- Prometheus
Deployment flow
GitHub
β
Railway
β
Docker Container
β
PostgreSQL
β
Telegram Bot
The project structure supports future integration with:
- GitHub Actions
- Docker Hub
- Railway Deployments
- Kubernetes
- AI-powered tutoring with Google Gemini
- Text question support
- Image understanding
- Voice message transcription
- Automatic subject detection
- Conversation memory
- PostgreSQL storage
- User statistics
- Feedback system
- Daily request limits
- Prometheus metrics
- Health check endpoint
- Docker deployment
- Railway deployment
- Embedded SQL migrations
- Graceful shutdown
- Admin web dashboard
- User authentication portal
- Teacher dashboard
- Multiple AI providers
- OCR improvements
- Streaming AI responses
- Export conversation history
- Multi-language interface
- Unit & Integration tests
- GitHub Actions CI/CD
During development several engineering problems had to be solved.
The bot supports three completely different input types:
- Text
- Images
- Voice messages
Each follows a different processing pipeline before reaching the AI model.
Instead of using a single generic prompt, the bot first classifies the subject (Mathematics, Physics, Biology, etc.) and then applies specialized prompts to improve answer quality.
Telegram imposes several constraints:
- Maximum message length
- Markdown parsing
- File uploads
- Voice message handling
The project implements dedicated utilities to safely format and split long responses.
The application was designed to run continuously in production using:
- Docker
- Railway
- PostgreSQL
- Prometheus
with health checks and graceful shutdown support.
Building AI Ustaz significantly improved my knowledge in:
- Go application architecture
- Modular project organization
- PostgreSQL schema design
- Telegram Bot API
- AI prompt engineering
- Google Gemini integration
- OpenAI Speech-to-Text
- Docker deployment
- Monitoring with Prometheus
- Production logging
- Environment configuration
- Dependency management
Contributions, ideas, bug reports, and feature requests are welcome.
Feel free to open an issue or submit a pull request.
This project is licensed under the MIT License.
Bekzat Tursun
Backend Software Engineer
GitHub
https://linkedin.com/in/tursunbekzat
β If you found this project interesting, consider giving it a star.
Built with β€οΈ using Go, Google Gemini and OpenAI.




