@ -4,54 +4,87 @@ A WhatsApp chatbot for task management, designed to work with Evolution API in a
## 📌 Overview
## 📌 Overview
This service provides a WhatsApp interface for task management within WhatsApp groups. It:
This service provides a WhatsApp interface for task management within WhatsApp groups. It:
- Listens for `/tarea` commands in WhatsApp groups
- Listens for `/tarea` commands in WhatsApp groups via Evolution API webhooks.
- Stores tasks in a SQLite database
- Stores tasks, users, and groups in a SQLite database.
- Manages user permissions and group membership
- Synchronizes group information periodically from the Evolution API.
- Integrates with Evolution API for WhatsApp connectivity
- Manages user permissions and group membership (partially implemented).
- Integrates with Evolution API for WhatsApp connectivity.
## 🔐 Security Model
## 🔐 Security Model
- **Internal Networking**: The webhook only accepts connections from Evolution API via internal Docker networking
- **Internal Networking**: The webhook should ideally only accept connections from Evolution API via internal Docker networking (configuration dependent).
- **Environment Variables**: Sensitive configuration is managed through environment variables
- **Environment Variables**: Sensitive configuration (API keys, URLs) is managed through environment variables.
- **Group Restrictions**: Only operates within pre-approved WhatsApp groups
- **Group Restrictions**: Designed to operate within pre-approved WhatsApp groups (validation logic pending implementation).
- **Input Validation**: Sanitizes and validates all user inputs
- **Input Validation**: Basic validation exists for webhook structure; needs enhancement for command arguments and user/group IDs.
- **User/Group Validation:** No normalization of WhatsApp IDs or checking if messages originate from active, known groups. Users are not automatically added to the DB.
- **Core Command Logic:** Actual processing of `/tarea nueva` (parsing args, calling `TaskService`) is missing in `CommandService`. Other commands (`mostrar`, `completar`) not implemented.
- **Task Service Implementation:**`TaskService` needs updating to handle `created_by`, assignments, and potentially methods for listing/completing tasks.
- **Response Sending:**`ResponseQueue` does not yet send messages back via the Evolution API.
- **Database Migrations:** No system in place to manage schema changes.
- **Robust Error Handling:** Comprehensive error handling, logging, and transaction management need improvement, especially around API calls and DB operations.
## 🛠️ Setup
## 🛠️ Setup
### Environment Variables
### Environment Variables
*(Ensure these are set correctly)*
```env
```env
EVOLUTION_API_URL=http://evolution-api:3000
# Evolution API Connection
EVOLUTION_API_URL=http://evolution-api:3000 # Or your API URL
EVOLUTION_API_KEY=your-api-key
EVOLUTION_API_KEY=your-api-key
EVOLUTION_API_INSTANCE=main
EVOLUTION_API_INSTANCE=main # Your instance name
WHATSAPP_COMMUNITY_ID=your-community-id
CHATBOT_PHONE_NUMBER=1234567890
# WhatsApp Specific
WEBHOOK_URL=https://your-webhook.com
WHATSAPP_COMMUNITY_ID=your-community-id # ID of the main community to sync groups from
PORT=3007
CHATBOT_PHONE_NUMBER=1234567890 # Bot's normalized phone number (e.g., for assigning tasks)
NODE_ENV=production
# Webhook Configuration
WEBHOOK_URL=http://your-service-internal-url:3007 # URL Evolution API calls *back* to this service
PORT=3007 # Port this service listens on
# Runtime Environment
NODE_ENV=production # Or development
# Optional
# GROUP_SYNC_INTERVAL_MS=3600000 # Sync interval in ms (default: 24h)
```
```
### Development Setup
### Development Setup
@ -59,31 +92,52 @@ NODE_ENV=production
# Install dependencies
# Install dependencies
bun install
bun install
# Start development server
# Copy .env.example to .env and fill in values
cp .env.example .env
# Start development server (watches for changes)
bun run dev
bun run dev
# Run tests
# Run tests
bun test
bun test
```
```
## 📅 Roadmap
## 📅 Roadmap & Priorities (Plan)
### High Priority
- [ ] Implement ResponseQueue processing logic with retries
### Phase 1: User & Group Foundation (Highest Priority)
- [ ] Add database schema validation and migrations
- [ ] **Create WhatsApp ID Normalization Utility:** (`src/utils/whatsapp.ts`) Handle different ID formats.
- [ ] Add error recovery with transaction rollback
- [ ] **Implement `ensureUserExists`:** (`src/db.ts`) Add users to DB on first interaction.
- [ ] Implement group sync delta updates
- [ ] **Implement `isGroupActive` Check:** (`src/services/group-sync.ts`, `src/server.ts`) Validate incoming messages are from known, active groups.
- [ ] **Integrate Validation in Server:** (`src/server.ts`) Use normalization and validation before processing commands.
* **WhatsApp ID Normalization:** Crucial for consistently identifying users and groups. Needs careful implementation to handle edge cases.
* **Response Latency:** Sending responses requires an API call back to Evolution. Ensure the `ResponseQueue` processing is efficient.
* **Group Sync:** The current full sync might be slow or rate-limited with many groups. Delta updates are recommended long-term.
* **Error Handling:** Failures in command processing or response sending should be logged clearly and potentially reported back to the user. Database operations should use transactions for atomicity (especially task+assignment creation).
* **State Management:** The current design is stateless. Complex interactions might require state persistence later.
* **Security:** Ensure group/user validation logic is robust.
## 🧪 Testing
## 🧪 Testing
### Running Tests
### Running Tests
@ -92,17 +146,19 @@ bun test
```
```
### Test Coverage
### Test Coverage
- Webhook validation
- Database initialization and basic operations.
- Command parsing
- Webhook validation (basic).
- Environment checks
- Command parsing (basic structure).
- Basic error handling
- Environment checks.
- Input validation
- Basic error handling.
- **Needed:** Tests for ID normalization, `ensureUserExists`, `isGroupActive`, `CommandService` logic, `ResponseQueue` processing (mocking API), `TaskService` operations.
## 🧑💻 Contributing
## 🧑💻 Contributing
1. Fork the repository
1. Fork the repository
2. Create a feature branch
2. Create a feature branch (`git checkout -b feature/implement-user-validation`)
3. Add tests for new functionality
3. Add/update tests for new functionality
4. Submit a pull request
4. Ensure tests pass (`bun test`)
5. Submit a pull request
## 📚 Documentation
## 📚 Documentation
For detailed API documentation and architecture decisions, see the [docs/](docs/) directory.
For detailed API documentation and architecture decisions, see the [docs/](docs/) directory (if created).