Skip to content

Latest commit

Β 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ€– BotWA - WhatsApp Bot

JavaScript Baileys Node.js

A powerful and extensible WhatsApp bot built with JavaScript and the Baileys library. This bot allows you to automate interactions on WhatsApp with a modular command system and plugin architecture. Fully compatible with Node.js runtime

WhatsApp Group

✨ Features

  • πŸ”§ Modular Command System: Easy to add and manage commands through a plugin system
  • πŸ“± Multi-device Support: Works with WhatsApp's multi-device protocol
  • πŸ‘₯ Group Management: Welcome messages and group participation tracking
  • πŸ” Permission System: Owner-only, group-only, and private chat commands
  • πŸ”„ File Watching: Automatically reloads commands when plugin files are modified
  • πŸ“Ž Media Handling: Support for processing various media types
  • πŸ“š Message History: Stores and processes historical messages

🧰 Prerequisites

  • 🟒 Node.js (v18 or higher)
  • πŸ“¦ npm or yarn package manager
  • πŸ“± WhatsApp account with a verified phone number

πŸš€ Installation

  1. Clone the repository:

    git clone <repository-url>
    cd botwa
  2. Install dependencies:

    npm install

βš™οΈ Configuration

  1. Create a .env file in the root directory (if not already present) and set the following variables:

    OWNER=6281910094713  # Your phone number in international format
    isSelf=true          # Set to true to enable self-mode (only respond to owner)
    
  2. Make sure to replace OWNER with your actual phone number in international format without the + sign.

🎯 Usage

For Development:

npm run dev

For Production:

Start the bot:

npm start
  1. On first run, the bot will prompt you to enter your phone number and provide a pairing code. Follow the instructions to connect your WhatsApp account.

  2. The bot will generate authentication credentials in the baileys_auth_info/ directory. These files will be used for subsequent logins.

πŸ› οΈ Commands

The bot uses a command system with the following syntax:

  • Commands start with !, /, or .
  • Example: !menu, /help, .list

Available Commands

  • πŸ“‹ Menu Command: Displays all available commands

    • Usage: !menu, !help, or !list
    • Shows commands organized by category with descriptions and usage information
  • πŸ” Category-specific Menu: Display commands for a specific category

    • Usage: !menu [category]
    • Example: !menu info

Adding Custom Commands

The bot supports a plugin system for adding custom commands:

  1. Create a new JavaScript file in the src/plugins/ directory

  2. Use the command registration system:

    import cmd from "../commands/map.js";
    
    cmd.add({
      name: "hello",
      alias: ["hi", "greeting"],
      category: ["general"],
      desc: "Sends a greeting message",
      async run({ m }) {
        m.reply("Hello! How are you?");
      },
    });
  3. The bot automatically detects and loads new commands when the file is saved (thanks to the file watcher).

Running Code Without Commands (Using Middleware)

The bot also supports middleware functionality that allows you to execute code for every message received, regardless of whether it's a command or not. This is particularly useful for: /

  • Auto-replying to specific keywords or phrases
  • Logging all messages
  • Tracking user activity
  • Implementing automatic responses based on content

To use middleware functionality:

  1. Create a new file in the src/plugins/ directory (e.g., auto-reply.js)

  2. Use the middleware property instead of the run property:

    import cmd from "../commands/map.js";
    
    cmd.add({
      middleware: async ({ m, sock }) => {
        // This function runs for every received message
        const messageText = m.body.toLowerCase();
    
        // Example: Auto-reply to messages containing "hello"
        if (messageText.includes("hello")) {
          m.reply("Hi there! I received your message.");
        }
    
        // Example: Log all messages to console
        console.log(`Received message: ${m.body} from ${m.sender}`);
      },
    });
  3. The middleware function will execute for every incoming message, whether it's a command or not

  4. Unlike the run property which only executes when the command is specifically called, middleware runs on every message

  5. Remember that middleware runs frequently, so optimize your code for performance

πŸ”’ Permissions System

Commands can have different permission levels:

  • πŸ‘‘ Owner-only: Only the number specified in OWNER environment variable can use
  • πŸ‘₯ Group-only: Only works in group chats
  • πŸ’¬ Private-only: Only works in private messages
  • πŸ€– Self-only: Only responds to messages from the bot itself

πŸ“ Project Structure

botwa/
β”œβ”€β”€ index.js              # Main entry point
β”œβ”€β”€ .env                  # Environment variables
β”œβ”€β”€ .gitignore
β”œβ”€β”€ package.json
β”œβ”€β”€ README.md
β”œβ”€β”€ baileys_auth_info/    # Authentication credentials
β”œβ”€β”€ node_modules/
└── src/
    β”œβ”€β”€ data-store.js     # Data persistence layer
    β”œβ”€β”€ commands/
    β”‚   β”œβ”€β”€ handler.js    # Command processing logic
    β”‚   β”œβ”€β”€ map.js        # Command registry interface
    β”‚   └── register.js   # Command loading and watching
    β”œβ”€β”€ plugins/          # Command plugins (.js files)
    └── utils/
        β”œβ”€β”€ msg.js        # Message processing utilities
        └── fmt.js        # Message formatting utilities

🌐 Environment Variables

  • OWNER: WhatsApp number in international format (without +) of the bot owner
  • isSelf: When set to true, bot only responds to owner's commands

πŸ§‘β€πŸ’» Development

To contribute or modify the bot:

  1. Make changes to the source code in the src/ directory
  2. For development, use npm run dev to run with automatic reloading
  3. For plugin changes, the bot automatically reloads when you save
  4. For core changes, restart the bot to see the changes

πŸ—οΈ Running the Bot

The bot is already in JavaScript format and ready to run:

npm start

For development mode with auto-reload:

npm run dev

πŸ“š Common Commands in the Bot

The bot comes with a default command system. Use !menu, !help, or !list to see all available commands once the bot is running.

πŸ› Troubleshooting

  • If you get authentication errors, delete the baileys_auth_info/ directory and restart the bot to re-authenticate
  • Make sure your phone number is in international format in the .env file
  • Check that the bot has proper permissions for the actions you're trying to perform
  • For development, use npm run dev for easier debugging

🀝 Community & Support

Join our WhatsApp group for discussions, support, and updates:

WhatsApp Group

Stay updated with other projects by following our WhatsApp channel:

WhatsApp Channel

πŸ–₯️ Hosting Your Bot

Looking for reliable hosting for your WhatsApp bot? Check out pwcraft cloud for affordable and stable VPS solutions perfect for running your bot 24/7:

pwcraft cloud

πŸ“„ License

This project is open source and available under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages