campaign_connector/docs/termux-flask-development-setup-guide.md
2025-08-25 09:41:16 -06:00

272 lines
6.8 KiB
Markdown

# Termux Flask Development Setup Guide
## SMS Campaign Manager on Android
### Prerequisites
- Termux installed on Android
- Termux:API app installed (for Android API access)
- SSH access configured (optional, for remote development)
### Initial Setup
#### 1. Update Termux Packages
```bash
pkg update && pkg upgrade
```
#### 2. Install Required System Packages
```bash
# Install Python and development tools
pkg install python python-pip
# Install Node.js (if needed for npm packages)
pkg install nodejs
# Install which command (useful for debugging)
pkg install which
# Install Termux API package
pkg install termux-api
```
### Project Setup
#### 1. Create Project Directory Structure
```bash
mkdir -p ~/projects/sms-campaign-manager
cd ~/projects/sms-campaign-manager
```
#### 2. Install Python Dependencies
```bash
pip install flask werkzeug requests
```
#### 3. Create the Flask Application
Create `app.py` with the following content:
```python
from flask import Flask, jsonify, render_template_string
import subprocess
import json
app = Flask(__name__)
@app.route('/')
def index():
return render_template_string('''
<!DOCTYPE html>
<html>
<head>
<title>SMS Campaign Manager - Termux</title>
<style>
body { font-family: Arial, sans-serif; margin: 40px; background: #f5f5f5; }
.container { background: white; padding: 30px; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
.status { background: #e8f5e8; padding: 20px; border-radius: 8px; margin: 20px 0; }
.api-test { background: #f0f8ff; padding: 15px; border-radius: 5px; margin: 10px 0; }
a { color: #0066cc; text-decoration: none; }
a:hover { text-decoration: underline; }
</style>
</head>
<body>
<div class="container">
<h1>🚀 SMS Campaign Manager</h1>
<h2>Running on Termux!</h2>
<div class="status">
<h3>✅ Flask Server Status: Active</h3>
<p><strong>Server IP:</strong> 10.0.0.193:5000</p>
<p><strong>Environment:</strong> Termux on Android</p>
</div>
<div class="api-test">
<h3>🔋 Termux API Tests</h3>
<p><a href="/battery">📱 Battery Status</a></p>
<p><a href="/notification">🔔 Send Test Notification</a></p>
</div>
</div>
</body>
</html>
''')
@app.route('/battery')
def battery():
try:
result = subprocess.run(['termux-battery-status'], capture_output=True, text=True)
battery_data = json.loads(result.stdout)
return f"""
<h2>🔋 Battery Status</h2>
<pre>{json.dumps(battery_data, indent=2)}</pre>
<p><a href='/'>← Back</a></p>
"""
except Exception as e:
return f"<h2>Error</h2><pre>{str(e)}</pre><p><a href='/'>← Back</a></p>"
@app.route('/notification')
def notification():
try:
subprocess.run(['termux-notification', '--title', 'Flask Test', '--content', 'Hello from SMS Campaign Manager!'], capture_output=True, text=True)
return f"""
<h2>🔔 Notification Sent!</h2>
<p>Check your Android notifications.</p>
<p><a href='/'>← Back</a></p>
"""
except Exception as e:
return f"<h2>Error</h2><pre>{str(e)}</pre><p><a href='/'>← Back</a></p>"
if __name__ == '__main__':
print("🚀 Starting SMS Campaign Manager on Termux...")
print("📱 Device IP: 10.0.0.193")
print("🌐 Access from Ubuntu: http://10.0.0.193:5000")
app.run(host='0.0.0.0', port=5000, debug=True)
```
### Running the Application
#### 1. Start the Flask Server
```bash
cd ~/projects/sms-campaign-manager
python app.py
```
#### 2. Access the Application
- **From Android device:** http://127.0.0.1:5000
- **From Ubuntu/network:** http://10.0.0.193:5000 (replace with your device IP)
- **From SSH:** Access via the device's network IP
### Features Implemented
1. **Web Interface**: Clean, responsive HTML interface
2. **Battery Status API**: Uses `termux-battery-status` to display device battery info
3. **Notification API**: Sends test notifications using `termux-notification`
4. **Network Access**: Configured to accept connections from any IP (0.0.0.0)
### Useful Commands
#### Check if server is running
```bash
ps aux | grep python
```
#### Find your device IP
```bash
ifconfig wlan0 | grep inet
# or
ip addr show wlan0
```
#### Kill existing Flask processes
```bash
pkill -f "python app.py"
```
#### Run in background with nohup
```bash
nohup python app.py > flask.log 2>&1 &
```
#### View logs
```bash
tail -f flask.log
```
### SSH Access from Ubuntu
If you want to develop from your Ubuntu machine:
```bash
# From Ubuntu, SSH into Termux
ssh android-dev
# Navigate to project
cd ~/projects/sms-campaign-manager
# Edit files with nano or vim
nano app.py
```
### Troubleshooting
#### Port Already in Use
```bash
# Find process using port 5000
lsof -i :5000
# Kill the process
kill -9 <PID>
```
#### Permission Issues with Termux API
Make sure you've granted permissions to Termux:API app in Android settings.
#### Network Access Issues
Ensure your device and Ubuntu machine are on the same network.
### Next Steps for SMS Campaign Manager
1. **Add SMS functionality**:
```python
# Send SMS using Termux API
subprocess.run(['termux-sms-send', '-n', phone_number, message])
```
2. **Add contact list management**:
```python
# Get contacts
subprocess.run(['termux-contact-list'])
```
3. **Add database support**:
```bash
pip install flask-sqlalchemy
```
4. **Add authentication**:
```bash
pip install flask-login
```
### Code-Server Alternative (Failed Attempt)
**Note:** Installing code-server on Termux proved problematic due to:
- Missing native module `argon2.node`
- Node version incompatibilities
- Architecture issues (ARM64)
**Recommendation:** Use SSH with nano/vim for editing, or use a mobile code editor app like Acode or Termux:Widget for quick edits.
### Quick Restart Script
Create `restart_server.sh`:
```bash
#!/data/data/com.termux/files/usr/bin/bash
pkill -f "python app.py"
cd ~/projects/sms-campaign-manager
python app.py
```
Make it executable:
```bash
chmod +x restart_server.sh
```
### Backup Your Work
```bash
# Create backup
tar -czf sms-campaign-backup.tar.gz ~/projects/sms-campaign-manager
# Copy to Ubuntu via SCP
scp sms-campaign-backup.tar.gz bunker-admin@10.0.0.9:~/backups/
```
---
## Summary
You successfully:
1. ✅ Installed Flask and dependencies on Termux
2. ✅ Created a working Flask web application
3. ✅ Integrated Termux API for Android features
4. ✅ Made it accessible from your Ubuntu machine
5. ✅ Tested battery status and notification APIs
The server was running successfully and accepting connections from your Ubuntu machine (10.0.0.9) as shown in the logs.