# WhatsApp AI Task Scheduling Service A Node.js/Express/TypeScript application that provides intelligent task scheduling with WhatsApp notifications and AI-powered optimization. ## Features - **Task Management**: Create, update, delete, and track tasks with priorities and due dates - **AI-Powered Scheduling**: Uses OpenAI to suggest optimal times for task completion - **WhatsApp Integration**: Sends task reminders and notifications via WhatsApp using Twilio - **User Authentication**: JWT-based authentication system - **Automated Scheduling**: Background jobs for daily reminders and overdue task notifications - **Health Monitoring**: Built-in health check endpoints ## Tech Stack - **Runtime**: Node.js with TypeScript - **Framework**: Express.js - **Database**: SQLite with Prisma ORM - **Authentication**: JWT tokens with bcrypt password hashing - **WhatsApp**: Twilio WhatsApp Business API - **AI**: OpenAI GPT-3.5 for intelligent scheduling - **Scheduling**: node-cron for background jobs - **Logging**: Winston for structured logging ## Prerequisites - Node.js 18+ and npm - Environment variables (see below) ## Installation 1. Clone the repository 2. Install dependencies: ```bash npm install ``` 3. Copy environment configuration: ```bash cp .env.example .env ``` 4. Configure environment variables in `.env` file 5. Generate Prisma client and run migrations: ```bash npm run prisma:generate npm run prisma:migrate ``` 6. Build the application: ```bash npm run build ``` 7. Start the server: ```bash npm start ``` For development: ```bash npm run dev ``` ## Required Environment Variables ```env # Database DATABASE_URL="file:./storage/db/database.db" # JWT Authentication JWT_SECRET="your-super-secret-jwt-key-here" JWT_EXPIRES_IN="24h" # Twilio WhatsApp Integration TWILIO_ACCOUNT_SID="your-twilio-account-sid" TWILIO_AUTH_TOKEN="your-twilio-auth-token" TWILIO_WHATSAPP_FROM="whatsapp:+14155238886" # OpenAI for AI Features OPENAI_API_KEY="your-openai-api-key" # Server Configuration PORT=3000 NODE_ENV="development" ``` ## API Endpoints ### Base URL - **GET** `/` - Service information and documentation links - **GET** `/docs` - API documentation - **GET** `/api/health` - Health check endpoint ### Authentication - **POST** `/api/auth/register` - Register new user - **POST** `/api/auth/login` - User login ### Tasks (Protected Routes) - **POST** `/api/tasks` - Create new task - **GET** `/api/tasks` - Get user's tasks (with pagination and filtering) - **GET** `/api/tasks/:id` - Get specific task - **PUT** `/api/tasks/:id` - Update task - **DELETE** `/api/tasks/:id` - Delete task - **POST** `/api/tasks/:id/remind` - Send WhatsApp reminder for task ## Usage Examples ### Register User ```bash curl -X POST http://localhost:3000/api/auth/register \ -H "Content-Type: application/json" \ -d '{ "email": "user@example.com", "password": "securepassword", "fullName": "John Doe", "whatsappNumber": "+1234567890" }' ``` ### Create Task ```bash curl -X POST http://localhost:3000/api/tasks \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_JWT_TOKEN" \ -d '{ "title": "Complete project proposal", "description": "Write and review the Q1 project proposal", "priority": "HIGH", "dueDate": "2024-02-15T17:00:00.000Z" }' ``` ## Features Details ### AI-Powered Scheduling The service uses OpenAI to analyze task priority, due dates, and optimal work hours to suggest the best time to work on tasks. ### WhatsApp Notifications - Daily reminders for scheduled tasks (9 AM UTC) - Overdue task notifications (6 PM UTC) - Manual reminder triggers via API ### Background Jobs - **Daily Reminders**: Sends WhatsApp reminders for tasks scheduled for today - **Overdue Checks**: Notifies users about overdue tasks ## Database Schema ### Users Table - `id`, `email`, `password` (hashed) - `fullName`, `phoneNumber`, `whatsappNumber` - `isActive`, `createdAt`, `updatedAt` ### Tasks Table - `id`, `title`, `description` - `status` (PENDING, IN_PROGRESS, COMPLETED, CANCELLED) - `priority` (LOW, MEDIUM, HIGH, URGENT) - `dueDate`, `scheduledAt`, `aiSuggestedTime` - `whatsappReminderSent`, `whatsappCompletionSent` - `ownerId`, timestamps ## Development ### Available Scripts - `npm run dev` - Start development server with hot reload - `npm run build` - Build TypeScript to JavaScript - `npm start` - Start production server - `npm run lint` - Run ESLint - `npm run lint:fix` - Fix ESLint issues - `npm run prisma:generate` - Generate Prisma client - `npm run prisma:migrate` - Run database migrations - `npm run prisma:studio` - Open Prisma Studio ### Project Structure ``` src/ ├── controllers/ # Request handlers ├── middleware/ # Express middleware ├── models/ # Database models (Prisma) ├── routes/ # API route definitions ├── services/ # Business logic services ├── types/ # TypeScript type definitions └── utils/ # Utility functions ``` ## Error Handling The application includes comprehensive error handling with: - Structured error responses - Request validation using Joi - Database error handling - Authentication error handling - Rate limiting protection ## Logging Winston logger provides structured logging with different levels: - Development: Console output with colors - Production: File-based logging (error.log, combined.log) ## Contributing This project was generated by BackendIM, an AI-powered backend generation platform.