1.1.3 • Published 9 months ago

@bot-wa/bot-wa-baileys v1.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

Bot Baileys

This repository contains a WhatsApp bot implemented in JavaScript using the @whiskeysockets/baileys library.

baileys.js

This file is a JavaScript module that exports the BaileysClass, which extends EventEmitter. This class has several methods for sending different types of messages through WhatsApp, such as text, images, videos, audios, files, buttons, polls, locations, contacts, and stickers.

Install

Use the stable version:

npm i @bot-wa/bot-wa-baileys

Then import your code using:

import { BaileysClass } from '@bot-wa/bot-wa-baileys'
const { BaileysClass } = require('@bot-wa/bot-wa-baileys');

Use as bot-whatsapp provider bot-whatsapp

const { BaileysClass } = require('@bot-wa/bot-wa-baileys') // OLD const BaileysProvider = require('@bot-whatsapp/provider/baileys')
const adapterProvider = createProvider(BaileysClass) // ANTES -> const adapterProvider = createProvider(BaileysProvider)
// OR CHANGE QR TO PAIRING CODE
const adapterProvider = createProvider(BaileysClass, { usePairingCode: true, phoneNumber: 'XXXXXXXX' }) // ANTES -> const adapterProvider = createProvider(BaileysProvider)

Example

Follow these steps to deploy the application:

  • Clone this repository: https://github.com/andresayac/bot-wa-baileys.git
  • Enter the bot-wa-baileys directory
  • Run the command npm i
  • Run the command npm run example to start the bot
  • Scan the QR code in WhatsApp as if it were WhatsApp Web. You can find the QR code in qr.png or terminal
  • Done!

Example Bot-Whatsapp with https://github.com/codigoencasa/bot-whatsapp

Follow the next example for create bot using bot-whatsapp this bot with provider

  • Bot with Json npm run example-json
  • Bot with InMemory mock npm run example-memory
  • Bot with Mongo npm run example-mongo
  • Bot with MYSQL npm run example-mysql

More information bot-whatsapp

Key Methods

  • initBailey: Initializes the connection with WhatsApp.
  • setUpBaileySock: Sets up the connection socket with WhatsApp.
  • handleConnectionUpdate: Handles updates to the connection with WhatsApp.
  • busEvents: Defines various events that the bot can handle.
  • sendMessage, sendMedia, sendImage, sendVideo, sendAudio, sendText, sendFile, sendButtons, sendPoll, sendLocation, sendContact, sendPresenceUpdate, sendSticker: Methods for sending different types of messages through WhatsApp.

Method Parameters

  • sendMessage(numberIn, message, options): Sends a message to a given phone number. The message can include additional options like buttons or media.
  • sendMedia(number, mediaUrl, text): Sends media to a given phone number. The media is specified by a URL, and additional text can be sent along with the media.
  • sendImage(number, filePath, text): Sends an image to a given phone number. The image is specified by a file path, and additional text can be sent along with the image.
  • sendVideo(number, filePath, text): Sends a video to a given phone number. The video is specified by a file path, and additional text can be sent along with the video.
  • sendAudio(number, audioUrl): Sends audio to a given phone number. The audio is specified by a URL.
  • sendText(number, message): Sends a text message to a given phone number.
  • sendFile(number, filePath): Sends a file to a given phone number. The file is specified by a file path.
  • sendButtons(number, text, buttons): Sends buttons to a given phone number. The buttons are displayed along with a given text.
  • sendPoll(number, text, poll): Sends a poll to a given phone number. The poll options are displayed along with a given text.
  • sendLocation(remoteJid, latitude, longitude, messages): Sends a location to a given chat ID. The location is specified by latitude and longitude, and additional messages can be sent along with the location.
  • sendContact(remoteJid, contactNumber, displayName, messages): Sends a contact to a given chat ID. The contact is specified by a phone number and a display name, and additional messages can be sent along with the contact.
  • sendPresenceUpdate(remoteJid, WAPresence): Sends a presence update (e.g., "recording") to a given chat ID.
  • sendSticker(remoteJid, url, stickerOptions, messages): Sends a sticker to a given chat ID. The sticker is specified by a URL, and additional messages can be sent along with the sticker.

Please note that these methods are asynchronous, meaning they return a promise that resolves once the action is completed.

Usage QR CODE

Here is an example of how to use the BaileysClass:

import {BaileysClass} from '@bot-wa/bot-wa-baileys';

const botBaileys = new BaileysClass({});

botBaileys.on('auth_failure', async (error) => console.log("ERROR BOT: ", error));
botBaileys.on('qr', (qr) => console.log("NEW QR CODE: ", qr));
botBaileys.on('ready', async () => console.log('READY BOT'))

let awaitingResponse = false;

botBaileys.on('message', async (message) => {
    if (!awaitingResponse) {
        await botBaileys.sendPoll(message.from, 'Select an option', {
            options: ['text', 'media', 'file', 'sticker'],
            multiselect: false
        });
        awaitingResponse = true;
    } else {
        const command = message.body.toLowerCase().trim();
        switch (command) {
            case 'text':
                await botBaileys.sendText(message.from, 'Hello world');
                break;
            case 'media':
                await botBaileys.sendMedia(message.from, 'https://www.w3schools.com/w3css/img_lights.jpg', 'Hello world');
                break;
            case 'file':
                await botBaileys.sendFile(message.from, 'https://github.com/pedrazadixon/sample-files/raw/main/sample_pdf.pdf');
                break;
            case 'sticker':
                await botBaileys.sendSticker(message.from, 'https://gifimgs.com/animations/anime/dragon-ball-z/Goku/goku_34.gif', { pack: 'User', author: 'Me' });
                break;
            default:
                await botBaileys.sendText(message.from, 'Sorry, I did not understand that command. Please select an option from the poll.');
                break;
        }
        awaitingResponse = false;
    }
});

Usage Pairing Code

Here is an example of how to use the BaileysClass:

import {BaileysClass} from '@bot-wa/bot-wa-baileys';

const botBaileys = new BaileysClass({ usePairingCode: true, phoneNumber: 'XXXXXXXXXXX' });

botBaileys.on('auth_failure', async (error) => console.log("ERROR BOT: ", error));
botBaileys.on('pairing_code', (code) => console.log("NEW PAIRING CODE: ", code));
botBaileys.on('ready', async () => console.log('READY BOT'))

let awaitingResponse = false;

botBaileys.on('message', async (message) => {
    if (!awaitingResponse) {
        await botBaileys.sendPoll(message.from, 'Select an option', {
            options: ['text', 'media', 'file', 'sticker'],
            multiselect: false
        });
        awaitingResponse = true;
    } else {
        const command = message.body.toLowerCase().trim();
        switch (command) {
            case 'text':
                await botBaileys.sendText(message.from, 'Hello world');
                break;
            case 'media':
                await botBaileys.sendMedia(message.from, 'https://www.w3schools.com/w3css/img_lights.jpg', 'Hello world');
                break;
            case 'file':
                await botBaileys.sendFile(message.from, 'https://github.com/pedrazadixon/sample-files/raw/main/sample_pdf.pdf');
                break;
            case 'sticker':
                await botBaileys.sendSticker(message.from, 'https://gifimgs.com/animations/anime/dragon-ball-z/Goku/goku_34.gif', { pack: 'User', author: 'Me' });
                break;
            default:
                await botBaileys.sendText(message.from, 'Sorry, I did not understand that command. Please select an option from the poll.');
                break;
        }
        awaitingResponse = false;
    }
});

Acknowledgements

This project was inspired by ideas and code from the bot-whatsapp repository by codigoencasa. Their work on creating automated conversation flows and setting up automated responses for frequently asked questions was particularly influential. We appreciate their contributions to the open-source community and their work on WhatsApp bot development.

Contribution

If you want to contribute to this project, feel free to do so. Any type of improvement, bug fix or new features are welcome.

Licencia

This project is licensed under the MIT.

1.1.3

9 months ago

1.1.2

9 months ago

1.1.1

9 months ago

1.1.0

10 months ago

1.0.9

10 months ago

1.0.8

10 months ago

1.0.7

10 months ago

1.0.6

10 months ago

1.0.5

10 months ago

1.0.4

10 months ago

1.0.3

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago