A powerful and extensible WhatsApp bot built with TypeScript 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
- π§ 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
- π’ Node.js (v18 or higher)
- π¦ npm or yarn package manager
- π± WhatsApp account with a verified phone number
-
Clone the repository:
git clone <repository-url> cd botwa
-
Install dependencies:
npm install
-
Create a
.envfile 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) -
Make sure to replace
OWNERwith your actual phone number in international format without the+sign.
npm run dev-
Build the project:
npm run build
-
Start the bot:
npm start
-
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.
-
The bot will generate authentication credentials in the
baileys_auth_info/directory. These files will be used for subsequent logins.
The bot uses a command system with the following syntax:
- Commands start with
!,/, or. - Example:
!menu,/help,.list
-
π Menu Command: Displays all available commands
- Usage:
!menu,!help, or!list - Shows commands organized by category with descriptions and usage information
- Usage:
-
π Category-specific Menu: Display commands for a specific category
- Usage:
!menu [category] - Example:
!menu info
- Usage:
The bot supports a plugin system for adding custom commands:
-
Create a new TypeScript file in the
src/plugins/directory -
Use the command registration system:
import cmd, { type CommandContext } from "../commands/map"; cmd.add({ name: "hello", alias: ["hi", "greeting"], category: ["general"], desc: "Sends a greeting message", async run({ m }: CommandContext) { m.reply("Hello! How are you?"); } });
-
The bot automatically detects and loads new commands when the file is saved (thanks to the file watcher).
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:
-
Create a new file in the
src/plugins/directory (e.g.,auto-reply.ts) -
Use the
middlewareproperty instead of therunproperty:import cmd, { type CommandContext } from "../commands/map"; cmd.add({ middleware: async ({ m, sock }: CommandContext) => { // 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}`); } });
-
The middleware function will execute for every incoming message, whether it's a command or not
-
Unlike the
runproperty which only executes when the command is specifically called,middlewareruns on every message -
Remember that middleware runs frequently, so optimize your code for performance
Commands can have different permission levels:
- π Owner-only: Only the number specified in
OWNERenvironment 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
botwa/
βββ index.ts # Main entry point
βββ .env # Environment variables
βββ .gitignore
βββ package.json
βββ README.md
βββ tsconfig.json # TypeScript configuration
βββ baileys_auth_info/ # Authentication credentials
βββ node_modules/
βββ dist/ # Compiled JavaScript files (after build)
βββ src/
βββ data-store.ts # Data persistence layer
βββ commands/
β βββ handler.ts # Command processing logic
β βββ map.ts # Command registry interface
β βββ register.ts # Command loading and watching
βββ plugins/ # Command plugins (.ts files)
βββ utils/
βββ msg.ts # Message processing utilities
βββ fmt.ts # Message formatting utilities
OWNER: WhatsApp number in international format (without +) of the bot ownerisSelf: When set totrue, bot only responds to owner's commands
To contribute or modify the bot:
- Make changes to the source code in the
src/directory - For development, use
npm run devto run with automatic reloading - For plugin changes, the bot automatically reloads when you save
- For core changes, restart the bot to see the changes
To compile the TypeScript code to JavaScript:
npm run buildThis will create a dist/ directory with the compiled JavaScript files.
The bot comes with a default command system. Use !menu, !help, or !list to see all available commands once the bot is running.
- 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
.envfile - Check that the bot has proper permissions for the actions you're trying to perform
- For development, use
npm run devfor easier debugging
Join our WhatsApp group for discussions, support, and updates:
Stay updated with other projects by following our WhatsApp channel:
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:
This project is open source and available under the MIT License.