A Telegram bot powered by large language models that provides intelligent conversation, memory management, and proactive care features. This AI companion builds emotional connections with users over time.
- Intelligent Conversation - Natural language dialogue using LLM
- Memory System - Long-term memory storage with importance scoring
- Proactive Care - Scheduled check-ins and caring messages
- User Profiling - Tracks relationship stage and emotional state
- Emotion Analysis - Analyzes user emotions in real-time
- Relationship Development - Evolves from stranger to partner over time
- System Monitoring - Monitors computer usage patterns (CPU, memory, idle time)
- Smart Triggers - Initiates conversations based on user activity
- Daily Greetings - Morning and night automated messages
- Web Dashboard - Real-time logs and monitoring interface
- Skills System - Extensible plugin system for external integrations
- Xiaohongshu (Little Red Book) Integration - Search, post, and manage Xiaohongshu content
bot/
├── src/
│ ├── __init__.py
│ ├── bot.py # Main Telegram bot application
│ ├── chat_manager.py # Chat and conversation management
│ ├── memory_manager.py # Memory extraction and retrieval
│ ├── model_manager.py # LLM provider abstraction
│ ├── database.py # SQLAlchemy models and utilities
│ ├── proactive_scheduler.py # Scheduled task scheduler
│ ├── system_monitor.py # System metrics collection
│ ├── trigger_engine.py # Event trigger logic
│ ├── web_server.py # Flask web server
│ └── webapp/ # Frontend dashboard
│ ├── index.html
│ ├── main.ts
│ ├── main.js
│ ├── style.css
│ └── package.json
├── bot/ # Bot data directory
├── requirements.txt
├── .env.example
├── main.py
└── README.md
# Navigate to project directory
cd /Users/lsy/bot
# Create virtual environment (recommended)
python -m venv venv
source venv/bin/activate # Linux/Mac
# or
venv\Scripts\activate # Windows
# Install dependencies
pip install -r requirements.txt# Copy environment template
cp .env.example .env
# Edit .env with your preferred LLM provider and API keys- Open Telegram and search for @BotFather
- Send
/newbotto create a new bot - Follow prompts to set bot name and username
- Copy the Bot Token to your
.envfile
# Run directly
python src/bot.py
# Or use the main entry point
python main.pyThe bot will start polling for messages. Open Telegram and send /start to begin!
| Command | Description |
|---|---|
/start |
Start conversation with the bot |
/help |
Display help information |
/status |
View current relationship status |
/reset |
Reset conversation memory |
/settings |
View/settings preferences |
/monitor |
View system monitoring stats |
/memories |
View stored long-term memories |
/logs |
Open web dashboard |
/xhs |
Xiaohongshu skill operations |
/skills |
List all available skills |
| Variable | Description | Required |
|---|---|---|
| TELEGRAM_BOT_TOKEN | Token from @BotFather | Yes |
| MODEL_PROVIDER | LLM provider (see below) | Yes |
| API_KEY | Provider-specific API key | Yes |
Supported providers: openai, zhipuai, dashscope, deepseek, minimax, moonshot
| Variable | Description | Default |
|---|---|---|
| DATABASE_URL | SQLAlchemy database URL | sqlite:///./bot.db |
| MAX_MEMORY_TOKENS | Maximum conversation memory tokens | 2000 |
| MEMORY_SUMMARY_TOKENS | Summary generation token limit | 500 |
| ENABLE_PROACTIVE_CHAT | Enable scheduled messages | true |
| PROACTIVE_CHAT_INTERVAL | Seconds between proactive messages | 3600 |
| PROACTIVE_CHAT_START_HOUR | Active hours start (24h) | 9 |
| PROACTIVE_CHAT_END_HOUR | Active hours end (24h) | 22 |
| MONITOR_INTERVAL | System check interval (seconds) | 5.0 |
| TRIGGER_COOLDOWN | Trigger cooldown period (seconds) | 1800 |
| CPU_THRESHOLD | CPU alert threshold (%) | 80.0 |
| IDLE_THRESHOLD | Idle detection threshold (seconds) | 600 |
| ACTIVITY_THRESHOLD | Activity detection threshold | 100 |
| HTTP_PROXY | HTTP proxy URL | - |
| HTTPS_PROXY | HTTPS proxy URL | - |
| WEB_APP_URL | Web dashboard URL | https://your-domain.com |
OPENAI_API_KEYOPENAI_MODEL(default: gpt-4)OPENAI_BASE_URL(for proxy)OPENAI_TEMPERATURE(default: 0.8)
ZHIPUAI_API_KEYZHIPUAI_MODEL(default: chatglm3-6b)
DASHSCOPE_API_KEYDASHSCOPE_MODEL(default: qwen-turbo)
DEEPSEEK_API_KEYDEEPSEEK_MODEL(default: deepseek-chat)
MINIMAX_API_KEYMINIMAX_GROUP_IDMINIMAX_MODEL(default: abab5.5-chat)
MOONSHOT_API_KEYMOONSHOT_MODEL(default: moonshot-v1-8k)
The bot tracks relationship development through 4 stages:
- Stranger (陌生人) - Initial state
- Acquaintance (熟人) - After basic interaction
- Friend (朋友) - Regular conversation
- Partner (恋人) - Deep emotional connection
Relationship advances based on conversation quality and interaction frequency.
- Python 3.8+ - Runtime environment
- python-telegram-bot - Telegram Bot API
- LangChain - Memory and prompt management
- SQLAlchemy - Database ORM
- APScheduler - Async task scheduling
- Flask - Web dashboard server
- psutil - System monitoring
The bot includes a web-based dashboard for real-time monitoring:
- View conversation logs
- Monitor system metrics
- Track bot performance
Access via /logs command in Telegram (production) or open http://localhost:PORT locally.
Uses SQLite by default. Database schema includes:
- users - User profiles and states
- conversations - Message history
- memories - Extracted long-term memories
- proactive_messages - Scheduled message logs
The codebase includes several optimizations:
- Database indexes on frequently queried columns
- Connection pooling for database efficiency
- Async/await for non-blocking operations
- Memory summarization to reduce token usage
- Keep API keys confidential
- Review proxy settings for your environment
- Regularly backup the database
- Use environment variables, never commit secrets
src/
├── bot.py # Entry point and command handlers
├── chat_manager.py # Chat logic and prompt templates
├── memory_manager.py # Memory extraction and retrieval
├── model_manager.py # Multi-provider LLM abstraction
├── database.py # ORM models and connection pooling
├── proactive_scheduler.py # Scheduled task management
├── system_monitor.py # OS metrics collection
└── trigger_engine.py # Event trigger evaluation
- Extend
ModelManagerfor new LLM providers - Add new triggers in
TriggerEngine - Create new scheduler jobs in
ProactiveScheduler - Add database models in
database.py - Add new skills by extending
BaseSkillinskill_manager.py
The bot includes a extensible skills system that allows integration with external services. See Skills Guide for detailed information.
- xiaohongshu - Xiaohongshu (Little Red Book) integration
from src.skill_manager import BaseSkill, SkillResult
class MyCustomSkill(BaseSkill):
name = "my_skill"
description = "My custom skill"
async def execute(self, params: dict) -> SkillResult:
# Your implementation
return SkillResult(success=True, result="Done")Then register it in SkillManager.
MIT License
Made with 💕 for AI companionship