> For the complete documentation index, see [llms.txt](https://zalojs.gitbook.io/docs-1/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://zalojs.gitbook.io/docs-1/getting-started.md).

# Getting Started

### Installing <a href="#installing" id="installing"></a>

**Prerequisites:**

Assuming you’ve already installed [Node.js](https://nodejs.org/), create a directory to hold your application, and make that your working directory.

```bash
$ 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](https://docs.npmjs.com/files/package.json).

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

### Basic Usage <a href="#basic-usage" id="basic-usage"></a>

**Configuration files :**

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

   ```json
   {
       "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:

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

{% embed url="<https://github.com/DaQMinh/Zalojs/assets/114815151/96438cf2-7f42-4778-b832-bf66d99d8fd8>" %}
How to get id
{% endembed %}

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:

```javascript
//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 <a href="#command-handling" id="command-handling"></a>

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:

```javascript
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:

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

#### Handling <a href="#handling" id="handling"></a>

```javascript
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):

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