DEV Community

Cover image for Create a simple and free whatsapp bot: For Begineers
Ritu Raj Singh
Ritu Raj Singh

Posted on • Updated on

Create a simple and free whatsapp bot: For Begineers

This whatsapp bot can be used to send an automated message to individuals or groups. We can extend this bot for further use such as sending image/video, location, contacts.

I will be using venom-bot node module to create this whatsapp bot. Venom module is a high-performance system developed with JavaScript and it is contineously developing with new features and it is open source-project so totally free.

Requirements:

 Development Environment (VS studio)
 Basics of JavaScript
 Basic Knowledge of Node Package Manager(NPM)
Enter fullscreen mode Exit fullscreen mode

First, we need to create a folder(maybe with the name "wpbot") and go into the created folder.

Steps:

1) open command/terminal in present folder
2) give command npm init
It will automatically create a file package.json in the current folder.
3) create an index.js file in the current folder.
4) Now it's time to install the required vemon node module, give command npm i --save venom-bot. It will install the required node module and it will automatically create a new file named package-lock.json, which contains the installed package list.

Time to do some coding....
Open the index.js file in the development environment and paste the below code into the index.js file, that's it for now.

// Supports ES6
// import { create, Whatsapp } from 'venom-bot';
const venom = require('venom-bot');

venom
  .create()
  .then((client) => start(client))
  .catch((erro) => {
    console.log(erro);
  });

function start(client) {
  client.onMessage((message) => {
    if (message.body === 'Hi' && message.isGroupMsg === false) {
      client
        .sendText(message.from, 'Hello Dear')
        .then((result) => {
          console.log('Result: ', result); //return object success
        })
        .catch((erro) => {
          console.error('Error when sending: ', erro); //return object error
        });
    }
  });
}
Enter fullscreen mode Exit fullscreen mode

As you can see from the code, whenever you will get a Hi message from indivisual(not from group), bot will automatically reply Hello Dear.

5) To create/activate this bot, you need to open you command/terminal and give the node index.js command and it will open a QR code in the terminal/command. You need to scan the QR code and linked it. After this step, it will do the job.

That's all for today, I'm still developing the complex features like sending replies as GIF/image based on text analysis and many more. I'll update you in future. Till then, stay safe and connected.

Top comments (0)