TelegrmBot API
TelegrmBot API is a Java-based REST API that seamlessly integrates Telegram messaging with AI-powered conversational capabilities. Built on a strict hexagonal architecture (ports and adapters), it provides a robust, maintainable foundation for managing Telegram bot interactions through a modern REST interface.What is TelegrmBot API?
This project combines three powerful capabilities:- Telegram Integration - Listens to Telegram messages via polling and sends responses programmatically
- AI-Powered Responses - Generates intelligent replies using configurable language models through OpenRouter
- REST API Management - Provides HTTP endpoints to list conversations, view message history, and send proactive messages
Key Features
JWT Authentication
Secure user registration and login system with JSON Web Tokens for API access control
Telegram Polling
Automated message listening through Telegram’s long polling mechanism with real-time processing
AI Integration
Configurable AI responses using OpenRouter’s language models with customizable temperature and prompts
Hexagonal Architecture
Clean separation between domain logic and infrastructure using ports and adapters pattern
Database Versioning
Automated schema migrations with Flyway for consistent deployments
Docker Support
Fully containerized with multi-stage builds and Docker Compose orchestration
OpenAPI Documentation
Interactive Swagger UI for exploring and testing all API endpoints
Production Ready
Includes health checks, security best practices, and comprehensive testing with Testcontainers
Architecture Overview
The application follows a strict hexagonal architecture with three distinct layers:Core Domain Layer
The innermost layer containing pure business logic with no framework dependencies:
- Entities:
User,Conversation,Message - Value Objects:
Email,Password,TelegramChatId - Ports: Interfaces defining how the application interacts with the outside world
Application Layer
Use cases that orchestrate domain logic:
RegisterUserUseCase- Handle user registrationProcessTelegramUpdateUseCase- Process incoming Telegram messagesSendMessageUseCase- Send proactive messages to Telegram chats
This architectural approach ensures the core business logic remains independent of frameworks, databases, and external APIs - making the codebase highly testable and maintainable.
Use Cases
Customer Support Bot
Automate initial customer support responses on Telegram while allowing human agents to take over through the REST API when needed.AI Assistant
Deploy a configurable AI assistant that responds to Telegram messages with customizable personality and behavior through system prompts.Notification System
Use the proactive messaging API to send alerts, reminders, or updates to Telegram users from your backend systems.Conversation Management
Build a dashboard to monitor and manage all Telegram conversations, view message history, and analyze user interactions.Technology Stack
| Component | Technology | Version |
|---|---|---|
| Language | Java | 21 |
| Framework | Spring Boot | 3.5.10 |
| Database | PostgreSQL | 15 |
| Build Tool | Maven | 3.9+ |
| Security | JWT | 0.12.5 |
| Migrations | Flyway | Latest |
| API Docs | SpringDoc OpenAPI | 2.8.15 |
| Testing | JUnit 5, Testcontainers | Latest |
Why Hexagonal Architecture?
The hexagonal architecture (also known as ports and adapters) provides several critical benefits:Request Flow Example
Here’s how a Telegram message flows through the architecture:- TelegramPollingService (scheduler) polls Telegram API for new messages
- Converts the Telegram update to a ProcessUpdateCommand
- Invokes the ProcessTelegramUpdatePort (use case interface)
- ProcessTelegramUpdateUseCase executes domain logic:
- Finds or creates conversation using repository port
- Saves incoming message
- Requests AI response through AI port
- Sends response through Telegram port
- Infrastructure adapters handle actual API calls and database operations
What’s Next?
Quick Start
Get the bot running in under 5 minutes with Docker
Installation Guide
Detailed setup for both Docker and local development