Skip to content

cobrich/ai-tutor-bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

27 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

License Go Version Last Commit

πŸŽ“ AI Ustaz

AI-powered Telegram Tutor built with Go

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.

Go PostgreSQL Docker Gemini OpenAI Railway Prometheus


✨ Overview

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.


πŸš€ Key Features

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

πŸ“Έ Demo

πŸ’¬ Text Question

Students can send ordinary text questions and receive detailed explanations.

Text Question


πŸ–Ό Image Question

Homework, textbook pages, handwritten notes, and mathematical problems can be analyzed directly from images.

Image Question


🎀 Voice Question

Voice messages are automatically transcribed using OpenAI and then processed by Gemini.

Voice Question


πŸ‘€ User Profile

Each student has a profile containing request statistics and daily limits.

Profile


πŸ“Š Bot Statistics

Administrators can monitor overall bot usage and feedback.

Statistics


🎯 Engineering Highlights

  • 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

πŸ— Architecture

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
Loading

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.


πŸ“‚ Project Structure

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

πŸ›  Tech Stack

Backend

  • Go
  • REST API
  • Clean Architecture
  • Dependency Injection
  • Graceful Shutdown

Artificial Intelligence

  • Google Gemini 2.5 Flash
  • OpenAI Speech-to-Text
  • Prompt Engineering
  • Subject-specific prompting

Database

  • PostgreSQL
  • pgx
  • Goose Migrations

Infrastructure

  • Docker
  • Docker Compose
  • Railway

Monitoring

  • Prometheus
  • Health Check Endpoint
  • Structured Logging

Telegram

  • go-telegram-bot-api
  • Long Polling
  • Voice Messages
  • Image Processing
  • Markdown Responses

⚑ Core Functionality

AI Pipeline

Voice
    β”‚
    β–Ό
OpenAI Transcription
    β”‚
    β–Ό
Subject Detection
    β”‚
    β–Ό
Conversation Context
    β”‚
    β–Ό
Google Gemini
    β”‚
    β–Ό
Formatted Answer

Supported Subjects

  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • English
  • History
  • Geography
  • General Questions

πŸ”‘ Main Components

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

πŸ“Š Production Features

  • βœ… 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

πŸš€ Getting Started

Prerequisites

  • Go 1.24+
  • PostgreSQL 15+
  • Docker (optional)
  • Google Gemini API Key
  • OpenAI API Key
  • Telegram Bot Token

βš™ Configuration

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-transcribe

🐳 Run with Docker

Start PostgreSQL

docker compose up -d

Run migrations

make migrate

Start the bot

make run

πŸ’» Local Development

Install dependencies

go mod download

Run migrations

go run ./cmd/migrate

Start bot

go run ./cmd/bot

πŸ“‹ Available Commands

User Commands

Command Description
/start Register user
/help Show help
/profile User profile
/limit Remaining daily requests
/reset Clear conversation history

Admin Commands

Command Description
/stats Bot statistics

❀️ Health Check

GET /health

Example response

{
    "status":"ok",
    "database":"connected",
    "version":"1.0.0"
}

πŸ“ˆ Metrics

Prometheus endpoint

GET /metrics

Example metrics

telegram_requests_total

telegram_users_total

telegram_feedback_total

telegram_ai_requests_total

telegram_errors_total

πŸ”’ Security

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

⚑ Performance

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

πŸ§ͺ Testing

Run all tests

go test ./...

Run with coverage

go test ./... -cover

🚒 Deployment

The application is designed for cloud deployment.

Tested with:

  • Railway
  • Docker
  • PostgreSQL
  • Prometheus

Deployment flow

GitHub

↓

Railway

↓

Docker Container

↓

PostgreSQL

↓

Telegram Bot

πŸ”„ CI/CD Ready

The project structure supports future integration with:

  • GitHub Actions
  • Docker Hub
  • Railway Deployments
  • Kubernetes

πŸ—Ί Roadmap

Completed

  • 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

Planned

  • 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

πŸ’‘ Engineering Challenges

During development several engineering problems had to be solved.

Multi-modal AI

The bot supports three completely different input types:

  • Text
  • Images
  • Voice messages

Each follows a different processing pipeline before reaching the AI model.


Subject-aware prompting

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 limitations

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.


Production deployment

The application was designed to run continuously in production using:

  • Docker
  • Railway
  • PostgreSQL
  • Prometheus

with health checks and graceful shutdown support.


πŸ“š Lessons Learned

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

🀝 Contributing

Contributions, ideas, bug reports, and feature requests are welcome.

Feel free to open an issue or submit a pull request.


πŸ“„ License

This project is licensed under the MIT License.


πŸ‘¨β€πŸ’» Author

Bekzat Tursun

Backend Software Engineer

GitHub

https://github.com/cobrich

LinkedIn

https://linkedin.com/in/tursunbekzat

Email

tursunbekzat07@gmail.com


⭐ If you found this project interesting, consider giving it a star.

Built with ❀️ using Go, Google Gemini and OpenAI.

About

Production-ready AI-powered Telegram tutor built with Go, Google Gemini, OpenAI Speech-to-Text, PostgreSQL and Docker.

Topics

Resources

License

Stars

1 star

Watchers

0 watching

Forks

Packages

 
 
 

Contributors

Languages