Getting Started

Installing

Prerequisites:

Assuming you’ve already installed Node.js, create a directory to hold your application, and make that your working directory.

$ mkdir myApp && cd myApp 

Use the npm init command to create a package.json file for your application. For more information on how package.json works, see Specifics of npm’s package.json handling.

Now install Zalojs in the myApp directory and save it in the dependencies list. For example:

Basic Usage

Configuration files :

  1. Create a configuration file (config.json) with the following structure:

    {
        "groupid": "your-group/user-id",
        "groupname": "your-group/user-name"
    }

    Replace placeholders with your actual group ID, group name, and set headless to true for headless mode.

  2. For reference, an example configuration is provided:

    {
        "groupid": "g5701541405487732670",
        "groupname": "Testing"
    }

Remember, the example configuration is for illustrative purposes only and should be replaced with your specific information. Creating the main file Open your code editor and create a new file. We suggest that you save the file as index.js, but you may name it whatever you wish. Here's the base code to get you started:

//index.js
const { init } = require("zalojs");
const config = require("./config.json");
const Client = require("zalojs").default;
const fs = require("fs");
const path = require("path");

const prefix = "!";

(async () => {
  const { browser, page } = await init({
    groupName: config.groupid,
    groupID: config.groupName,
    headless: config.headless,
  });

  const client = new Client(page);

  client.on("message", async (message) => {
    if (message === "!ping") {
      client.send({ message: `!Pong` });
    }
  })
  client.once("ready", () => {
    console.log(`Bot is ready! ${client.user.name}`);
  });
})();

Running your application Open your terminal and run node index.js to start the process. Upon running the program, a server will automatically start at http://localhost:3000/. Next, use your mobile device to scan the QR code displayed there using the Mobile Zalo Application.

Command Handling

Unless your bot project is small, it's not a very good idea to have a single file with a giant if/else if chain for commands. If you want to implement features into your bot and make your development process a lot less painful, you'll want to implement a command handler. Let's get started on that! Loading command files Now that your command files have been created, your bot needs to load these files on startup.

In your index.js file, make these additions to the base template:

const { init } = require('zalojs');
const config = require('./config.json');
const Client = require('zalojs').default;
const fs = require('fs');
const path = require('path');

Next, using the modules imported above, dynamically retrieve your command files with a few more additions to the index.js file: Loading Commands:

const commandFiles = fs
  .readdirSync(path.join(__dirname, "commands"))
  .filter((file) => file.endsWith(".js"));
// ... (rest of your code)

Handling

client.on("message", async (message) => {
  if (!message.content) return;
  if (!message.content.startsWith(prefix)) return;
  const args = message.content.slice(prefix.length).trim().split(/ +/);
  const commandName = args.shift().toLowerCase();

  const commandFile = commandFiles.find(
    (file) => file.split(".").shift() === commandName,
  );
  if (!commandFile) return;

  const command = require(path.join(__dirname, "commands", commandFile));
  try {
    await command(message, client, args);
  } catch (error) {
    console.log(error);
    await client.send({ message: "There was an error executing the command." });
  }
});

Example Command File (commands/ping.js):

module.exports = async (message, client) => {
    await client.send({ message : 'Pong!' });
};

Last updated