IMPORTANT: Termux:API Setup Required For this agent to read or send SMS, you MUST install the Termux:API components:
- Download App: Install the Termux:API Android App from F-Droid or the Google Play Store.
- Install Package: Run
pkg install termux-apiinside your Termux terminal.- Grant Permissions: Go to Android Settings β Apps β Termux:API β Permissions and enable SMS, Phone, and Storage.
- Disable Battery Optimization: Ensure battery optimization is disabled for both Termux and Termux:API to prevent the background service from being killed.
A production-ready, Termux-based SMS auto-responder with AI capabilities
SMS AI Agent is a modular, secure SMS auto-responder for Android devices running through Termux. It operates in two modes:
- Rule-based Template Responder - Pattern matching with customizable response templates
- AI-powered Responder - Uses LLMs (Groq/OpenRouter/Ollama) for contextual responses
- π Security First - API keys in .env, no hardcoded secrets, guardrail system
- π€ LLM Integration - Groq (Lightning fast), OpenRouter (100+ models), and Ollama (local LLM) support
- π Rules Engine - Regex, keyword, and pattern matching with priorities
- β‘ Rate Limiting - Per-recipient and global rate limits to prevent abuse
- π‘οΈ Guardrails - PII detection, content filtering, SMS length constraints
- π₯οΈ Dual UI - Web UI (FastAPI) and Terminal UI (Textual)
- πΎ SQLite Storage - Message history, logs, and settings persistence
- π Auto-start - Termux:Boot integration for device startup
- π€ Contact Personalization - Set specific AI instructions, names, and relations for individual phone numbers.
- π Loop Protection - Advanced idempotency and echo-detection to prevent infinite self-reply loops.
You can now customize how the AI interacts with specific people in your life. By setting contact-specific metadata, the AI gains awareness of who it is talking to and how it should behave.
- Name & Relation: Tell the AI who the person is (e.g., "John - Brother").
- Custom Prompts: Provide unique instructions per number (e.g., "Be very respectful to my Boss" or "Use lots of emojis with my Best Friend").
- Smart Context: These details are injected directly into the system prompt for highly personalized responses.
- Robust Tracking: The system tracks
respondedstatus for every message, ensuring it never replies twice to the same text or gets stuck in a self-reply loop.
This app works with SMS messages only!
It can:
- β Read incoming SMS messages
- β Generate AI-powered replies using OpenRouter/Ollama
- β Send SMS responses automatically
- β Work with rule-based responses
It cannot:
- β Read messages from WhatsApp, Instagram, Messenger, Telegram, etc.
- β Read notification content from other apps
- β Work with messaging apps that don't use SMS
Why? SMS is a system-level feature that Termux can access via Termux:API. Other messaging apps (WhatsApp, Instagram, etc.) use their own encrypted protocols and require special Android permissions (Accessibility Services or Notification Listener) to access.
To extend to other apps, you would need a separate Android app that acts as a notification listener or uses Accessibility Services to read messages and forward them to this SMS AI Agent.
- Android 7.0+ (API 24+)
- Termux app installed
- Termux:API app installed
pkg update
pkg install python python-pip termux-api git- Groq (Recommended): Ultra-fast inference. API key from console.groq.com. Default model:
llama-3.3-70b-versatile. - OpenRouter: API key from openrouter.ai
- Ollama: Local installation at ollama.ai
The app is pre-configured to use openrouter/free which automatically selects from free models. This means:
- No cost - Completely free AI responses
- Auto-selection - Randomly picks from available free models each request
- Always free - No credit card required
To use specific free models instead, you can set:
openrouter/free- Random free model (default)meta-llama/llama-3.3-70b-instruct:free- Llama modelgoogle/gemma-2-9b-it:free- Gemma modelmistralai/mistral-7b-instruct:free- Mistral model
# Clone or download
git clone https://github.com/your-username/sms-ai-agent.git
cd sms-ai-agent
# Run installer
chmod +x install.sh
./install.sh --first-run# Edit configuration
nano ~/.config/sms-ai-agent/.env
# Add your OpenRouter API key
OPENROUTER_API_KEY=sk-or-your-key-here# Web UI (recommended)
python main.py --web
# Terminal UI
python main.py --tui
# Background daemon
python main.py --daemonWhen prompted, grant SMS permissions to Termux:
- Go to Settings β Apps β Termux β Permissions
- Enable SMS permission
- Enable Storage permission (for configuration files)
Main configuration file: ~/.config/sms-ai-agent/config.yaml
llm:
provider: "openrouter"
model: "openrouter/free" # Uses random free model - zero cost!
temperature: 0.7
max_tokens: 150
sms:
auto_reply_enabled: true
ai_mode_enabled: true
max_response_length: 300
rate_limit:
max_messages_per_minute: 10
max_per_recipient_per_hour: 5You can fully customize the AI's personality and behavior by editing two key files in your config directory (~/.config/sms-ai-agent/):
This file defines how the AI speaks, reacts, and behaves. You can customize:
- Language Style: Hinglish, English, or any mix
- Tone: Friendly, sarcastic, formal, casual
- Behavioral Rules: How to handle flirting, abuse, confusion
- Vocabulary: Custom slang, phrases, expressions
- Example Dialogues: Sample conversations for reference
Edit at: ~/.config/sms-ai-agent/personality.md
This file controls the operational logic:
- Input Processing: How to clean and process incoming messages
- Loop Prevention: Preventing infinite self-reply loops
- Response Rules: Length limits, formatting rules
- Fallback Behavior: How to respond when confused
- Priority Hierarchy: Security, context, personality order
Edit at: ~/.config/sms-ai-agent/agent.md
- Keep responses short (under 160 characters) for natural SMS feel
- Use lowercase - real friends don't capitalize every sentence
- Add local slang and phrases to make it feel more personal
- The AI mirrors your energy - customize it to match your style!
Add custom rules in the Web UI or edit ~/.config/sms-ai-agent/rules.yaml:
rules:
- name: "greeting"
patterns: ["hello", "hi", "hey"]
match_type: "contains"
responses:
- "Hello! How can I help?"
- "Hi there!"
priority: 50sms-ai-agent/
βββ core/ # Core functionality
β βββ config.py # Configuration management
β βββ database.py # SQLite operations
β βββ exceptions.py # Custom exceptions
β βββ logging.py # Logging setup
β βββ rate_limiter.py # Rate limiting
β βββ security.py # Security utilities
βββ llm/ # LLM providers
β βββ base.py # Abstract provider
β βββ openrouter.py # OpenRouter integration
β βββ ollama.py # Ollama integration
β βββ factory.py # Provider factory
βββ rules/ # Rules engine
β βββ engine.py # Pattern matching
β βββ templates.py # Template processing
βββ services/ # Core services
β βββ sms_handler.py # Termux SMS API
β βββ guardrails.py # Response safety
β βββ ai_responder.py # AI response generation
βββ ui/ # User interfaces
β βββ web/ # FastAPI web UI
β β βββ app.py
β β βββ routes.py
β β βββ templates/
β βββ terminal/ # Textual TUI
β βββ app.py
βββ config/ # Configuration files
β βββ config.yaml # Main config
β βββ personality.md # AI personality
β βββ agent.md # Agent rules
β βββ .env.example # Environment template
βββ main.py # Entry point
βββ install.sh # Installation script
βββ requirements.txt # Python dependencies
# Show help
python main.py --help
# Start web UI
python main.py --web --port 8080
# Start terminal UI
python main.py --tui
# Run as daemon
python main.py --daemon
# Check status
python main.py --status
# Test message
python main.py --test "Hello, how are you?"
# Set API key
python main.py --api-key sk-or-your-key
# Run setup wizard
python main.py --setup- API keys are stored in
~/.config/sms-ai-agent/.env - File permissions are set to 600 (owner read/write only)
- Never commit .env files to version control
The system includes multiple safety layers:
- Length Constraints - Enforces 300 character SMS limit
- PII Detection - Blocks phone numbers, emails, credit cards
- Content Filtering - Blocks passwords, SSNs, bank info
- Pattern Blocking - Custom regex patterns
- Fallback Responses - Safe defaults when violations occur
Prevents abuse with configurable limits:
- Global: 10 messages/minute
- Per-recipient: 5/hour, 20/day
- Burst protection: 3 messages/minute
- All data stored locally on device
- No external data transmission (except LLM API calls)
- Data wipe script available
- Install Termux:Boot from F-Droid
- Run the installer with
--first-runflag - Boot script will be created at
~/.termux/boot/
- Install Termux:Widget from F-Droid
- Shortcuts are created at
~/.termux/shortcuts/ - Add widgets to home screen for quick access
For Android 10+:
# Request SMS permission
termux-sms-send -n 0000000000 "test"
# Or manually grant in settings
Settings β Apps β Termux β Permissions β SMS# Run all tests
pytest tests/
# Test SMS handler
python -c "from services.sms_handler import test_sms_handler; print(test_sms_handler())"
# Test LLM connection
python main.py --test "Hello"- Verify Termux:API is installed
- Grant SMS permission in Android settings
- Test with:
termux-sms-list
- Check API key in
.envfile - Verify internet connection
- Test with:
python main.py --status
- Check if port is available
- Try different port:
python main.py --web --port 9000 - Use
0.0.0.0for external access:python main.py --web --host 0.0.0.0
- Wait for rate limit window to reset
- Adjust limits in
config.yaml - Check status with:
python main.py --status
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests
- Submit a pull request
- Follow PEP 8 guidelines
- Use type hints
- Add docstrings to functions
- Keep functions under 50 lines
This project is licensed under the MIT License - see the LICENSE file for details.
- Termux - Android terminal emulator
- OpenRouter - LLM API gateway
- Ollama - Local LLM runtime
- Textual - Terminal UI framework
- FastAPI - Web framework
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Documentation: Wiki
| Android Version | Status | Notes |
|---|---|---|
| Android 7-9 | β Full | All features work |
| Android 10-11 | SMS permissions need manual grant | |
| Android 12+ | Background restrictions may apply |
Note: Some Android versions require additional setup for background SMS operations.
Made with β€οΈ for the Termux community