Introduction

To develop and host a WhatsApp bot on Square Cloud, it’s essential to follow a specific sequence of configurations and prerequisites. This technical guide will cover the entire process, from initial setup to production deployment.

Essential Prerequisites

  • WhatsApp installed and configured: Required for bot authentication and linking. If you don’t have the app, visit the official WhatsApp website for download and setup.
  • Active Square Cloud account: Hosting platform for your application. Register through the signup page using your email.
  • Active paid plan: Ensures dedicated resources and optimized performance. Check our available plans and choose the most suitable for your needs.

Development Environment Setup

Node.js Installation and Configuration

  1. Node.js verification: Confirm that Node.js is installed on your system. Otherwise, download it from the official Node.js website.
  2. Project initialization: Set up a new Node.js project by running the initialization command:
Terminal
npm init -y
  1. Dependencies installation: Install the essential libraries for bot functionality:
Terminal
npm install whatsapp-web.js qrcode

WhatsApp Bot Implementation

  1. Main file creation: Develop the index.js file with the bot’s base structure:
index.js
// Import necessary modules
const { Client } = require("whatsapp-web.js");
const qrcode = require("qrcode");

// Create a new WhatsApp client instance
const client = new Client({
  puppeteer: {
    // Run Chrome in headless mode (without graphical interface)
    headless: true,
    args: [
      // Disable Chrome sandboxing features
      // Required for execution in containerized environments
      "--no-sandbox",
      // Additional sandboxing flag to disable setuid sandbox
      "--disable-setuid-sandbox",
    ],
  },
  // Web version cache configuration
  webVersionCache: {
    // Type "remote" indicates WhatsApp Web version will be fetched from remote URL
    type: "remote",
    // Remote path for WhatsApp Web version
    remotePath: "INSERT_YOUR_URL_HERE",
  },
});

// Event listener for QR Code generation
client.on("qr", async (qr) => {
  // Use qrcode library to generate and save QR Code
  try {
    await qrcode.toFile("./qrcode.png", qr);
    console.log("QR Code generated and saved as qrcode.png");
  } catch (err) {
    console.error("Error generating QR Code:", err);
  }
});

// Event listener for client ready
client.on("ready", () => {
  // Confirmation log when WhatsApp Web session is established
  console.log("WhatsApp client successfully initialized!");
});

// Event listener for created messages
client.on("message_create", (msg) => {
  // Log message content to console
  console.log("Message received:", msg.body);
  
  // Test command implementation
  if (msg.body === "!ping") {
    // Automatic response for ping command
    msg.reply("pong - Bot working correctly!");
  }
});

// Initialize WhatsApp client
client.initialize();

Square Cloud Configuration File

Technical Documentation: Square Cloud Configuration File

The squarecloud.app file constitutes the application’s configuration core, defining critical parameters such as main file, resource allocation, runtime versions, and essential project metadata.

Advanced START Field Configuration

Technical Warning: Use the START field exclusively if you have specialized knowledge about custom initialization scripts and their implications.
The START field in the Square Cloud configuration file is an optional parameter, required only when custom initialization scripts are needed. For the standard implementation presented in this tutorial, this field is not necessary.

Deployment and Hosting on Square Cloud

After completing the project file preparation, proceed with the upload process following one of the available technical methodologies:
Access the Square Cloud Dashboard and upload your project files through the web interface.

Complementary Technical Resources

To deepen your knowledge about WhatsApp bot development using whatsapp-web.js, consult the official whatsapp-web.js library documentation. The documentation offers detailed technical guides, advanced tutorials, and complete API reference to maximize implementation potential. If you continue facing technical difficulties, our specialized support team is available to assist you. Contact us and we'll be happy to help you resolve any issue.