Baileys Whatsapp library wrapper for easy integration on any NodeJS projects. This library using the latest version of @adiwajshing/baileys library for install and run
npm install --save whatsappi
yarn add whatsappi
const whatsappi = require('whatsappi');
// Or call using ES
import {
WhatsappiOptions,
WhatsappiInstance,
InstanceConnectionStatus,
Whatsappi,
generateUUID
} from 'whatsappi';
// New whatsappi client options
const config: WhatsappiOptions = {
sessionId: 'whatsappi', // Optional
sessionName: 'Whatsappi', // Required
printQRinTerminal: true, // Optional
ignoreBroadcastMessages: false, // Optional
ignoreGroupMessages: false, // Optional
ignoreServerAck: false, // Optional
markOnlineOnConnect: false, // Optional
syncFullHistory: true, // Optional
restartable: true, // Optional
webhookUrl: 'https://webhook.site/c13e9693-f22b-4531-8ee8-90d18146a2b5', // Optional
sessionToken: generateUUID(), // Optional
};
// Create new instance of Whatsappi
const whatsappi: Whatsappi = new Whatsappi(config);
// After created instance, start the Whatsappi instance
const client: WhatsappiInstance = await whatsappi.start();
// Use the exposed methods for simplify admin the state of the client
client.onQRUpdate(async (qr: string) => {
console.log('QR updated from callback', qr);
// If you require update the instance in the database, is simply using typeorm
const instance: Instance | null = await client.whatsappiDatabase
.getRepository(Instance)
.findOne({
where: {
sessionId: client.instanceOptions.sessionId,
},
});
if (instance) {
instance.qrCode = qr;
instance.connectionStatus = InstanceConnectionStatus.WAITING_FOR_QR;
await client.whatsappiDatabase.getRepository(Instance).save(instance);
console.log('QR updated in database');
console.log(instance);
}
});
client.onQRScanned(async () => {
console.log('QR scanned from callback');
const instance: Instance | null = await client.whatsappiDatabase
.getRepository(Instance)
.findOne({
where: {
sessionId: client.instanceOptions.sessionId,
},
});
if (instance) {
instance.qrCode = '';
instance.connectionStatus = InstanceConnectionStatus.INITIALIZING;
await client.whatsappiDatabase.getRepository(Instance).save(instance);
console.log('QR scanned in database');
console.log(instance);
}
});
client.onLoggedIn(async () => {
console.log('Logged in from callback');
// await client.socket.sendMessage('573203999858@s.whatsapp.net', {
// text: 'Hello from Whatsappi',
// });
const instance: Instance | null = await client.whatsappiDatabase
.getRepository(Instance)
.findOne({
where: {
sessionId: client.instanceOptions.sessionId,
},
});
if (instance) {
instance.qrCode = '';
instance.connectionStatus = InstanceConnectionStatus.CONNECTED;
await client.whatsappiDatabase.getRepository(Instance).save(instance);
console.log('Logged in in database');
console.log(instance);
}
});
client.onEvent('connection.update', (update: any) => {
console.log('Connection update from callback', update);
});
}
Options to create a new Whatsappi instance
// This is a interface of the WhatsappiOptions required to create a new client
import { WhatsappiOptions } from 'whatsappi';
export interface WhatsappiOptions {
sessionId?: string;
sessionName: string;
sessionToken?: string;
webhookUrl?: string;
restartable?: boolean;
printQRinTerminal?: boolean;
markOnlineOnConnect?: boolean;
ignoreBroadcastMessages?: boolean;
ignoreGroupMessages?: boolean;
ignoreServerAck?: boolean;
syncFullHistory?: boolean;
}
The structure of the new client created WhatsappiInstance
// This is a interface of the WhatsappiInstance clients
import { WhatsappiInstance } from 'whatsappi';
export interface WhatsappiInstance {
instanceOptions: WhatsappiOptions;
whatsappiDatabase: DataSource;
socketOptions: UserFacingSocketConfig;
socket: WASocket;
store: StoreHandle;
getStore: StoreHandle;
onQRUpdate: (callback: (qr: string) => void) => void;
onQRScanned: (callback: () => void) => void;
onLoggedIn: (callback: () => void) => void;
onEvent: (event: string, cb: any) => void;
}
Or simply want to add a new feature you have been dreaming of?
Submit a new Issue with the correct formation and relax.
Want to contribute to the project? Easy,
- Find an issue to start with
- Fork the project
- Make PR
- Get merged.
I can't guarantee or can be held responsible if you get blocked or banned by using this software. WhatsApp does not allow bots using unofficial methods on their platform, so this shouldn't be considered totally safe.
- This code is in no way affiliated, authorized, maintained, sponsored or endorsed by WA (WhatsApp) or any of its affiliates or subsidiaries.
- The official WhatsApp website can be found at https://whatsapp.com. "WhatsApp" as well as related names, marks, emblems and images are registered trademarks of their respective owners.
- This is an independent and unofficial software Use at your own risk.
- Do not spam people with this.