GithubHelp home page GithubHelp logo

wwebjs-guide's Introduction

description
How to connect and send/receive messages

Getting Started

{% hint style="warning" %} Heads up!
The whatsapp-web.js guide is still a work in progress. To learn about all the features available to you in the library, please check out the documentation. {% endhint %}

Installation

You can get the module from npm:

$ npm i whatsapp-web.js

{% hint style="info" %} NodeJS v12 or higher is required {% endhint %}

First steps

Once installed, you're ready to connect:

const { Client } = require('whatsapp-web.js');
const client = new Client();

client.on('qr', (qr) => {
    console.log('QR RECEIVED', qr);
});

client.on('ready', () => {
    console.log('Client is ready!');
});

client.initialize();

QR code generation

Since whatsapp-web.js works by running WhatsApp Web in the background and automating its interaction, you'll need to authorize the client by scanning a QR code from WhatsApp on your phone.

Right now, we're just logging the text representation of that QR code to the console, but we can do better. Let's install and use qrcode-terminal so we can render the QR code:

$ npm i qrcode-terminal

And now we'll modify our code to use this new module:

const qrcode = require('qrcode-terminal');

const { Client } = require('whatsapp-web.js');
const client = new Client();

client.on('qr', qr => {
    qrcode.generate(qr, {small: true});
});

client.on('ready', () => {
    console.log('Client is ready!');
});

client.initialize();

There we go! You should now see something like this after running the file:

After scanning this QR code, the client should be authorized and you should see a Client is ready! message being printed out.

{% hint style="warning" %} If you use WhatsApp Web on any other device, the current session will be terminated. {% endhint %}

Listening for messages

Now that we can connect to WhatsApp, it's time to listen for incoming messages. Doing so with whatsapp-web.js is pretty straightforward. The client emits a message event whenever a message is received. This means we can capture it like so:

client.on('message', message => {
	console.log(message.body);
});

Running this example should log all incoming messages to the console.

Replying to messages

The messages received have a convenience function on them that allows you to directly reply to them via WhatsApp's reply feature. This will show the quoted message above the reply.

To test this out, let's build a simple ping/pong command:

client.on('message', message => {
	if(message.body === '!ping') {
		message.reply('pong');
	}
});

You could also choose not to send it as a quoted reply by using the sendMessage function available on the client:

client.on('message', message => {
	if(message.body === '!ping') {
		client.sendMessage(message.from, 'pong');
	}
});

In this case, notice that we had to specify which chat we were sending the message to.

wwebjs-guide's People

Contributors

ameybh avatar lorisbc avatar ngekoding avatar pedroslopez avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

wwebjs-guide's Issues

Sending audio like having been recorded

Hello!

First to say: I love your project! I had a lot of fun in the last months with my self-programmed chat bot. The only thing I'm missing is to send an audio file in such way that it looks for the receiver that it was a native recorded audio from within Whatsapp. Sure, I can send a mp3 file but for the receiver it is obviously an attachement and no recorded audio.
Or is this not possible?

Best regards
Marc

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.