A Domain-Driven Design implementation of a therapeutic conversation bot integrated with Telegram and powered by LLM technology.
- Context-aware therapeutic guidance: Analyzes conversations to provide relevant support
- Adaptive therapeutic plans: Dynamically adjusts approach based on user needs
- Real-time risk assessment: Identifies potential crisis situations and responds appropriately
- Personalized communication styles: Adapts language to user preferences
- LLM-powered responses: Utilizes language models for natural conversation
This project follows a Domain-Driven Design (DDD) architecture with clear separation of concerns:
-
Domain Layer: Core business logic and entities
- User Interaction
- Risk Management
- Plan Management
- Context Analysis
-
Application Layer: Orchestrates domain services for use cases
- Message Processing
- Plan Revision
-
Infrastructure Layer: External integrations and technical implementations
- Telegram Bot
- LLM Integration
- Data Persistence
-
Presentation Layer: User interfaces and output formatting
- Response Formatting
- Bot Controllers
- Node.js 18+
- Telegram Bot Token (from BotFather)
- OpenAI API Key (or compatible LLM provider)
-
Clone the repository:
git clone https://github.com/yourusername/therapeutic-telegram-bot.git cd therapeutic-telegram-bot -
Install dependencies:
npm install
-
Create a
.envfile from the example:cp .env.example .env
Then edit
.envwith your Telegram token and OpenAI API key. -
Start the bot in development mode:
npm run dev
- Build and start with Docker Compose:
docker-compose up -d
Configure the bot via environment variables (see .env.example):
TELEGRAM_BOT_TOKEN: Your Telegram bot tokenOPENAI_API_KEY: Your OpenAI API keyLOW_TIER_MODEL: The model for less critical operations (default: gpt-3.5-turbo)HIGH_TIER_MODEL: The model for critical operations (default: gpt-4-turbo-preview)WEBHOOK_URL: (Optional) URL for webhook mode instead of pollingENABLE_BACKGROUND_PROCESSING: Enable/disable background analysis
- Start a conversation with your bot on Telegram
- The bot will guide users through a therapeutic conversation flow:
- Initial greeting and assessment
- Therapeutic intervention based on context
- Reflection and closure
The bot uses LLM technology for several key functions:
- Risk assessment: Analyzing messages for potential crisis indicators
- Context analysis: Understanding conversation themes and shifts
- Response generation: Creating appropriate therapeutic responses
- Plan adaptation: Revising therapeutic approaches based on user needs
- Communication styling: Formatting messages to match user preferences
- Build the project:
npm run build - Run tests:
npm test - Lint code:
npm run lint - Format code:
npm run format
This bot implements several safety measures:
- Risk level classification (Low, Moderate, High, Critical)
- Escalation protocols for high-risk situations
- Circuit breaker pattern for LLM failures
- Conservative fallbacks for error cases
MIT License