Skip to content

sorke/Baileys

Repository files navigation

Baileys

Reverse Engineered WhatsApp Web API in Node.js. Baileys does not require Selenium or any other browser to be interface with WhatsApp Web, it does so directly using WebSockets.

Thank you to Sigalor for writing the guide reverse engineering WhatsApp Web and to the go reimplementation written by Rhymen

Baileys is super easy to use:

  1. Install from npm using npm install github:adiwajshing/Baileys
  2. Then import in your code using
        const WhatsAppWeb = require('Baileys') 
  3. Create an instance of Baileys & connect using
        let client = new WhatsAppWeb() 
        client.connect()
    If the connection is successful, you will see a QR code printed on your terminal screen, scan it with WhatsApp on your phone and you'll be logged in!
  4. Implement the following event handlers in your code:
        client.handlers.onConnected = () => { /* when you're successfully authenticated with the WhatsApp Web servers */ } 
        client.handlers.onUnreadMessage = (message) => { /* called when you have a pending unread message or recieve a new message */ } 
        client.handlers.onError = (error) => { /* called when there was an error */ } 
        client.handlers.onDisconnect = () => { /* called when internet gets disconnected */ } 
  5. Send a text message using
    client.sendTextMessage(id, txtMessage) 
    The id is the phone number of the person the message is being sent to, it must be in the format '[country code][phone number]@s.whatsapp.net', for example '+19999999999@s.whatsapp.net'
  6. Send a read reciept using
        client.sendReadReceipt(id, messageID) 
    The id is in the same format as mentioned earlier. The message ID is the unique identifier of the message that you are marking as read
  7. Tell someone what your status is right now by using
        client.updatePresence(id, presence) 
    Presence can be one of the following:
        static Presence = {
            available: "available", // "online"
            unavailable: "unavailable", // offline
            composing: "composing", // "typing..."
            recording: "recording", // "recording..."
            paused: "paused" // I have no clue
        }
  8. Once you want to close your session, you can get your authentication credentials using:
       const authJSON = client.base64EncodedAuthInfo() 
    and then save this JSON to a file
  9. If you want to restore your session (i.e. log back in without having to scan the QR code), simply retreive your previously saved credentials and use
        const authJSON = JSON.parse( fs.readFileSync("auth_info.json") )
        client.login( authJSON )
    This will use the credentials to connect & log back in. No need to call connect() after calling this function
  10. If you want to query whether a number is registered on WhatsApp, use:
        client.isOnWhatsApp ("[countrycode][some10digitnumber]@s.whatsapp.net", (exists, id) => {
            if (exists) {
                console.log(id + " is on WhatsApp")
            } else {
                console.log(id + " is not on WhatsApp :(")
            }
        })
    Of course, replace [countrycode][some10digitnumber] with an actual country code & number

Do check out test.js to see example usage of all these functions.

Note

I am in no way affiliated with WhatsApp. This was written for educational purposes. Use at your own discretion.

About

Reverse Engineered WhatsApp Web API for Node.js

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published