How wppconnect-team/wppconnect works
Main Components
• Entry Point: Begin at src/index.ts, which acts as the bootstrapping module.
• API Layer: Under src/api, you have:
• Helpers: Utility functions (e.g., file-to-base64, encryption, etc.)
• Layers: Modules like business.layer.ts, catalog.layer.ts, community.layer.ts, etc. These abstract business logic and organize WhatsApp functionalities (e.g., sending messages, handling groups, contacts, etc.).
• Model: Data structures and enumerations that define WhatsApp entities (chat, contact, message, etc.).
• Whatsapp API module (src/api/whatsapp.ts) coordinating the API calls.
• Controllers: In src/controllers, you see modules for auth, browser, initializer, websocket, and welcome. These orchestrate the application flow (e.g., authentication, browser automation via Puppeteer, handling connections through websockets).
• Lib Layer (WAPI): Located in src/lib/wapi, it contains:
• Business functions, functions (sending messages, file upload/download, etc.)
• Helpers and serializer modules that process and format data.
• Listeners and event handlers for real-time events from WhatsApp.
• A webpack config for building or bundling the library.
• Config: Under src/config, configuration files for Puppeteer and other runtime options.
• Token Store: Modules (e.g., fileTokenStore, memoryTokenStore) in src/token-store that manage session tokens and validity.
• Tests & Types & Utils: Additional components that support testing, type definitions (for TypeScript), and utility functions like logging and sleep.
Relationships & Interactions
• The entry point (src/index.ts) initializes controllers (auth, browser, etc.) which in turn configure the Puppeteer browser session (from src/config/puppeteer.config.ts) and drive the API module.
• Controllers and config send commands to the API Layer (src/api) for actions (sending messages, reading chats, etc.), which then utilize various layers (business, catalog, etc.) and helpers to perform these functions.
• The Lib layer (wapi) abstracts lower-level interactions with WhatsApp Web using functions, listeners, and serializers. This ensures that API calls from controllers get translated into direct browser or network operations.
• The token store is integrated to provide session persistence, verifying and storing tokens that maintain the connection state.
• Utility modules are used across layers for logging, cryptography, and scheduling asynchronous calls like sleep.
Architectural Patterns & Principles
• Layered Architecture: There’s clear logical separation—controllers manage flow, API layers abstract functional groups, and the lib/wapi module encapsulates low-level WhatsApp Web interaction.
• Modularity: With separate directories for helpers, models, and functions, the system is built with modularity in mind.
• Event-Driven: Listeners in the lib layer suggest an event-based design for handling real-time WhatsApp messages and notifications.
• Configurable & Extensible: With ample configuration (Puppeteer settings, token management) and examples demonstrating various use cases, the architecture supports extensibility.
Including Specific Technologies & Frameworks
• Node.js and TypeScript (given the .ts files and type definitions).
• Puppeteer: Indicated by the puppeteer.config.ts file, for headless browser automation.
• GitHub Actions (workflows included for build, lint, publish) and other development tooling (ESLint, Prettier, Commitlint, Husky).
• Webpack is configured in the lib/wapi folder.