Overview
The WhatsApp Assistant Bot is built with TypeScript and follows a modular architecture with clear separation of concerns. The system uses the Baileys library for WhatsApp integration and PostgreSQL with Drizzle ORM for data persistence.Technology Stack
- Runtime: Node.js with TypeScript
- WhatsApp Integration: Baileys v7.0.0
- Database: PostgreSQL with Drizzle ORM
- Scheduling: node-cron for reminders and timers
- Web Server: Express (for Spotify OAuth callbacks)
- Authentication: Multi-file auth state (Baileys)
Project Structure
Core Components
1. Baileys Integration
The bot connects to WhatsApp using the Baileys library, which implements the WhatsApp Web API:- QR code authentication
- Multi-file auth state persistence
- Event-driven message processing
- Automatic reconnection on disconnect
2. Message Handler
The message handler (src/handlers/messageHandler.ts) is the main router for incoming messages:
- Extract text from various message types (text, image captions, etc.)
- Detect and handle Instagram/Spotify links
- Parse command prefix (
!or/) - Route commands to appropriate handlers
- Update user activity
3. Command Handler Pattern
All commands implement theCommandHandler interface from src/types/commands.ts:
src/handlers/commands/index.ts:
4. Service Layer
Services contain the business logic and database operations. Each service is a static class that provides methods for CRUD operations:- TodoService: Manage todo items
- NoteService: Save and search notes
- ReminderService: Create and manage reminders
- TimerService: Countdown timers with notifications
- NotificationService: Schedule and send notifications
- UserService: User management and settings
- SpotifyService: Spotify API integration
- InstagramService: Instagram content downloading
5. Database Layer
The bot uses PostgreSQL with Drizzle ORM. Database schemas are defined insrc/db/schema.ts:
Tables:
users- User profiles and settingstodos- Todo list itemsreminders- Scheduled remindersnotes- User notestimers- Active countdown timers
6. Notification System
NotificationService manages scheduled notifications using node-cron:
- Load active reminders and timers on startup
- Schedule cron jobs for future notifications
- Send WhatsApp messages at scheduled times
- Clean up completed notifications
Application Flow
messageHandler extracts text contentKey Design Patterns
Command Pattern
Each command is self-contained with its own handler implementing a common interface. This allows:- Easy addition of new commands
- Consistent command structure
- Centralized command registration
- Type-safe command contexts
Service Layer Pattern
Business logic is separated from command handlers:- Services handle data operations
- Handlers focus on user interaction
- Services can be reused across commands
- Clear separation of concerns
Event-Driven Architecture
Baileys uses event processing for all WhatsApp events:Configuration
The bot requires these environment variables:DATABASE_URL- PostgreSQL connection stringSPOTIFY_CLIENT_ID- Spotify API credentialsSPOTIFY_CLIENT_SECRET- Spotify API credentialsPORT- Express server port (default: 3000)
Error Handling
Errors are handled at multiple levels:- Command Level: Try-catch in each handler’s execute method
- Service Level: Service methods throw errors to handlers
- Database Level: Drizzle ORM handles query errors
- Connection Level: Automatic reconnection with exponential backoff
Next Steps
- Learn how to add new commands
- Explore the service layer
- Review the command reference for all available commands