Skip to main content

Introduction

This guide covers creating and deploying WhatsApp bots on Shard Cloud using the whatsapp-web.js library.
WhatsApp bots work by connecting to WhatsApp Web. Your phone must stay connected to the internet for the bot to work.

Creating Your Bot

Setup

npm init -y
npm install whatsapp-web.js qrcode

Basic Bot Code

index.js
const { Client, LocalAuth } = require('whatsapp-web.js');
const qrcode = require('qrcode');

const client = new Client({
  authStrategy: new LocalAuth(),
  puppeteer: {
    headless: true,
    args: [
      '--no-sandbox',
      '--disable-setuid-sandbox',
    ],
  },
});

client.on('qr', async (qr) => {
  try {
    await qrcode.toFile('./qrcode.png', qr);
    console.log('QR Code saved as qrcode.png');
  } catch (err) {
    console.error('Error generating QR:', err);
  }
});

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

client.on('message', async (msg) => {
  if (msg.body === '!ping') {
    await msg.reply('Pong!');
  }
});

client.initialize();

Package.json

package.json
{
  "name": "whatsapp-bot",
  "version": "1.0.0",
  "main": "index.js",
  "dependencies": {
    "whatsapp-web.js": "^1.23.0",
    "qrcode": "^1.5.0"
  }
}

Authentication

When you first run the bot, it will generate a QR code (qrcode.png). You need to:
  1. Download the QR code from Shard Cloud’s file manager
  2. Scan it with WhatsApp on your phone (Settings → Linked Devices → Link a Device)
  3. Once authenticated, the session is saved and you won’t need to scan again
The LocalAuth strategy saves the session in a .wwebjs_auth folder. This persists between restarts.

Shard Cloud Configuration

.shardcloud
DISPLAY_NAME=WhatsApp Bot
DESCRIPTION=WhatsApp Bot with whatsapp-web.js
MAIN=index.js
MEMORY=1024
VERSION=recommended
WhatsApp bots require more memory due to Puppeteer. Use at least 512MB-1024MB.

Deploying

1

Prepare Your Files

Ensure you have:
  • index.js
  • package.json
  • .shardcloud
2

Exclude Unnecessary Files

Remove: node_modules/, package-lock.json
3

Create ZIP Archive

Compress your project folder.
4

Upload to Shard Cloud

Go to Shard Cloud Dashboard and upload.
5

Authenticate

After first start, download the QR code from the file manager and scan with WhatsApp.

Additional Resources

Visit the whatsapp-web.js documentation for more information.

Troubleshooting

  • Ensure Puppeteer has enough memory (1024MB+)
  • Check logs for Chromium errors
  • Ensure your phone stays connected to the internet
  • Keep the WhatsApp app open on your phone
Make sure you’re using the --no-sandbox and --disable-setuid-sandbox flags.