A simple Node.js library for simplifying WhatsApp Web socket connection using the WhiskeySockets/Baileys library.
You can install whatsapp.ts
via npm:
npm install whatsapp.ts
The primary purpose of this library is to provide a more user-friendly and streamlined way of creating WhatsApp Web socket connections using the WhiskeySockets/Baileys library. It simplifies the process and offers additional functionalities.
You can pass all the socket configuration options supported by the Baileys library through the baileysOptions parameter in the constructor of the WhatsAppAPI class.
With whatsapp.ts, you can access the underlying socket created by Baileys directly using the wp.socket property. This means that you can utilize any events and functions that are available in Baileys but not included in this library.
-
qr
: Triggered when a QR code is received. The QR code data is passed as a parameter. -
ready
: Fired when a successful login to WhatsApp is established. Account information is passed as a parameter. -
disconnect
: Triggered when you log out of WhatsApp. The reason for disconnection is passed as a parameter. -
message
: Triggered when a message is received. The full message details are passed as a parameter. -
/command
: Triggered when a message with a command (starting with /) is received. The full message details are passed as a parameter.
sendText(to, message)
Sends a text message to the specified phone number. The first parameter should be in the WhatsApp number format (e.g., number@c.us or number@s.whatsapp.net). The second parameter is the message you want to send.
message.reply(text)
This function is used to reply to a received message. You can pass the text
parameter to send a response to the message that triggered the event.
Below is an example of how to use the whatsapp.ts
library:
import { WhatsAppAPI } from 'whatsapp.ts';
import qrcode from 'qrcode-terminal';
let wp = new WhatsAppAPI({
deviceName: 'My Device',
sessionPath: './wp-session',
baileysOptions: {},
});
wp.on('qr', (qr) => {
qrcode.generate(qr, { small: true });
});
wp.on('ready', async (data) => {
console.log(data);
});
wp.on('disconnect', (reason) => {
console.log('Disconnected: ' + reason);
wp.initialize();
});
wp.on('message', (message) => {
if (message.text.includes('ping')) {
message.reply('pong');
}
});
wp.on('/test', (message) => {
message.reply(message.text);
});
wp.initialize();
This section can be found in LICENSE file.