Giter Site home page Giter Site logo

baileys's Introduction

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 '[email protected]'
  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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.