Skip to content

Latest commit

 

History

History
215 lines (175 loc) · 7.13 KB

README.md

File metadata and controls

215 lines (175 loc) · 7.13 KB

WhatsAppi

WhatsAppi Baileys Library Wrapper

Buy Me A Coffee

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


Libraries Used

Usage

Installation

From NPM

npm install --save whatsappi

From YARN

yarn add whatsappi

Power Up New Client

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);
  });
}

Whatsappi Interfaces

WhatsappiOptions

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;
}

WhatsappiInstance

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;
}

Found an Issue?

Or simply want to add a new feature you have been dreaming of?
Submit a new Issue with the correct formation and relax.

For Developers

Want to contribute to the project? Easy,

  1. Find an issue to start with
  2. Fork the project
  3. Make PR
  4. Get merged.

Note

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.

Legal

  • 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.