WhatsappBot
WhatsappBot is a JavaScript class that represents a WhatsApp bot. It provides functions for sending text messages using the WhatsApp API. It extends the EventEmitter class to handle event-based communication.
Installation
To use the WhatsappBot class, you need to have Node.js installed. You can install the package using npm or yarn:
npm install whatsapp-bot
or
yarn add whatsapp-bot
Usage
To use the WhatsappBot class in your JavaScript or TypeScript code, follow these steps:
-
Import the WhatsappBot class:
import WhatsappBot from "whatsapp-bot";
-
Create an instance of the WhatsappBot class:
const bot = new WhatsappBot(userAccessToken, phoneNumberId);
userAccessToken (string): The user access token for authentication.
phoneNumberId (string): The phone number ID associated with the bot.
-
Send a text message using the sendTextMessage method:
bot.sendTextMessage(textMessage, to);
textMessage (string): The text message to send.
to (string): The recipient of the message.
This method sends a text message using the WhatsApp API. If sending the message fails, an error is logged using the configured logger.
-
Subscribe to events using the on method:
bot.on(eventName, listener);
eventName (string): The name of the event you want to listen to.
listener (function): The listener function to be called when the event is triggered.
You can register event listeners to handle specific events emitted by the WhatsappBot instance.
Configuration
The WhatsappBot class can be configured with an optional custom Winston logger. If you want to use a custom logger, pass it as part of the optional configuration:
const customLogger = ...;
const optionalConfig = {
customWinstonLogger: customLogger,
};
const bot = new WhatsappBot(userAccessToken, phoneNumberId, optionalConfig);
Logging
The WhatsappBot class uses the Winston logging library for logging. By default, it uses a default logger configuration provided by the package. However, you can specify a custom Winston logger during instantiation to customize the logging behavior.
Contribution
Contributions are welcome! If you find any issues or have suggestions for improvement, please create an issue or submit a pull request in the GitHub repository.
License
This project is licensed under the MIT License.
Functionality Checklist
Please note that the current functionality of the WhatsappBot class is as follows:
Please note that the checklist is currently marked as incomplete for most items, indicating that only sending text messages is implemented.