Introduction

  • Before you start coding it is necessary to properly set up your bot on WhatsApp. First, you need to have WhatsApp downloaded on your phone. If you don’t have it yet, you can visit the official WhatsApp website. After having WhatsApp installed and configured, you can start programming your bot.
  • Next, you will need to create an account on Square Cloud, which can be done through the sign up page. You can use your email to create an account.
  • Finally, you need to have an active paid plan on your account. You can view our plans and purchase one according to your needs here.

Starting Project

  1. Ensure you have Node.js installed on your system. If not, you can download it from the official Node.js website.
  2. Initialize a new Node.js project using npm init. This will create a new package.json file for your project:
npm init -y
  1. Install the whatsapp-web.js and qrcode library using npm:
npm install whatsapp-web.js qrcode
  1. Create a new JavaScript file (e.g., index.js) and add the following code to create a basic Whatsapp bot:
index.js
// Importing the necessary modules
const { Client } = require("whatsapp-web.js");
const qrcode = require("qrcode");

// Creating a new instance of the client
const client = new Client({
  puppeteer: {
    // Runs Chrome in headless mode (without a user interface).
    headless: true,
    args: [
      // Disables Chrome's sandboxing features. This is necessary when running
      // Puppeteer in certain environments like Docker containers.
      "--no-sandbox",
      // Additional sandboxing flag to disable setuid sandbox.
      "--disable-setuid-sandbox",
    ],
  },
  // Setting the webVersionCache option
  webVersionCache: {
    // Setting the type as "remote", which means that the WhatsApp Web version will be fetched from a remote URL
    type: "remote",
    // Setting the remote path for the WhatsApp Web version
    remotePath: "URL PATH HERE",
  },
});

// This event is fired when whatsapp-web.js generates a new QR code
client.on("qr", async (qr) => {
  // Here we are using the qrcode library to generate a QR Code and save it as a file
  try {
    await qrcode.toFile("./qrcode.png", qr);
    console.log("QR Code saved as qrcode.png");
  } catch (err) {
    console.error(err);
  }
});

// This event is fired when the client is ready
client.on("ready", () => {
  // This log will be displayed in the console when the client is ready, i.e., after the QR Code has been successfully scanned and the WhatsApp Web session has been started
  console.log("Client is ready!");
});

// This event is fired when a message is created
client.on("message_create", (msg) => {
  // Here we are logging the body of the message to the console
  console.log(msg.body);
  // If the message is "!ping"
  if (msg.body == "!ping") {
    // Reply with "pong"
    msg.reply("pong");
  }
});

// Initializing the client
client.initialize();

Creating the squarecloud config file

Learn about: how to make the configuration file for Square Cloud.

The squarecloud.app file is a configuration file that will be used to configure your application; it will be used to define the name, description, version, main file, among other things.

Configuring the START Field

Only use this field if you are really sure about what you are doing.

In the Square Cloud configuration file, the START field is optional and only needed if you are using a custom script to start the bot. In the provided example, the START field is not necessary.

Uploading Project to Square Cloud

After preparing your project files, you can now upload them to Square Cloud and host your project.

Access the Square Cloud Dashboard and upload your project files.

Additional Resources

For more information on creating bots with whatsapp-web.js, visit the official whatsapp-web.js documentation. There, you’ll find detailed guides, tutorials, and API documentation to help you make the most of these libraries.

If you continue to experience any issues, please don’t hesitate to contact our support team.