Whatsapp-web.js
This Node.js application utilizes the whatsapp-web.js library to create a WhatsApp client server. Here's a breakdown of its functionality:
Initialization and Setup: The application sets up an Express server and defines necessary dependencies like express, whatsapp-web.js, qrcode, cors, http, and mysql for database connectivity.
Database Connection: It establishes a connection with a MySQL database to store session data.
Client Initialization: The createClient function initializes a WhatsApp client instance using the provided instanceId. It handles events like QR code generation, client readiness, authentication, authentication failure, and disconnection.
Endpoints:
/add-device/:instanceId: Initializes a new device session.
/get-qr/:instanceId: Fetches the QR code for a specific instance.
/send-message/:instanceId: Sends a text message to a specified number.
/send-media/:instanceId: Sends media (image or video) to a specified number.
/get-chat-backup/:instanceId: Retrieves chat backup for a specific instance.
/get-messages/:instanceId/:userId/:limitme: Retrieves messages for a specific user within a specified limit.
/get-media/:instanceId/:messageId: Retrieves media associated with a specific message.
/instance-status/:instanceId: Checks the status of a specific instance.
/get-active-instance-info/:instanceId: Retrieves information about the active instance, such as phone number and platform.
/get-contacts/:instanceId: Retrieves contacts for a specific instance.
/delete-device/:instanceId: Deletes an existing device session.
/reinit-client/:instanceId: Attempts to reinitialize the client connection without destroying the session.
/reinit-session/:instanceId: Reinitializes a session for a specific instance.
/reinit-all-sessions: Reinitializes all sessions with timeout handling.
Database Operations:
Loading sessions from the database during initialization.
Saving authenticated sessions to the database.
Updating session status in the database on disconnection.
Error Handling: The application handles errors gracefully, logging them and providing appropriate responses to clients.
Server Setup:
The server listens on port 4000.
Overall, this application provides a robust backend for managing WhatsApp client sessions and interacting with the WhatsApp API programmatically.