Skip to content

caioagiani/whatsapp-bot

Repository files navigation

WhatsApp BOT @caioagiani

WhatsApp Bot

A powerful, extensible WhatsApp bot built with TypeScript and modern architecture

GitHub language count GitHub top language GitHub repo size GitHub last commit License


๐Ÿ“‹ Overview

This application is a WhatsApp client that connects to WhatsApp Web using Puppeteer, enabling real-time automation and command execution. Built with TypeScript and following modern software architecture principles, it provides a robust and scalable foundation for WhatsApp automation.

โœจ Key Features

  • ๐Ÿค– Command-based Architecture - Extensible command system with interface-based design
  • ๐Ÿ”„ Alias Support - Multiple names for the same command
  • ๐Ÿ›ก๏ธ Error Handling - Robust error handling with user-friendly messages
  • ๐Ÿ“ Type Safety - Full TypeScript implementation
  • ๐ŸŽฏ Easy to Extend - Add new commands in minutes
  • ๐Ÿ” Group Validation - Built-in group-only command support
  • ๐Ÿ’ฌ Real-time Responses - Typing indicators and instant feedback

๐Ÿš€ Available Commands

Command Aliases Description
!help !ajuda, !comandos, !commands Shows all available commands with descriptions
!cotacao !moeda, !dolar, !bitcoin Get current currency exchange rates (USD, BTC, EUR)
!cep <code> - Search Brazilian postal code information
!perfil @user !foto, !avatar, !pic Get user's profile picture
!mencionar !everyone, !all, !todos Mention all group members (admin only)
!sms @user - Send SMS to mentioned user

Note: All commands start with ! prefix


๐Ÿ“ฆ Installation

Prerequisites

  • Node.js 16+
  • npm or yarn
  • WhatsApp account

Setup

# Clone the repository
git clone git@github.com:caioagiani/whatsapp-bot.git
cd whatsapp-bot

# Install dependencies
npm install --legacy-peer-deps
# or
yarn install

# Configure environment variables
cp .env.example .env
# Edit .env and add your Mobizon API credentials (optional, for SMS feature)

# Start the bot
npm run dev
# or
yarn dev

First Run

  1. When you start the bot for the first time, a QR code will appear in your terminal
  2. Open WhatsApp on your phone
  3. Go to Settings โ†’ Linked Devices โ†’ Link a Device
  4. Scan the QR code displayed in your terminal
  5. Wait for the authentication to complete

โœ… Your bot is now connected and ready to receive commands!


๐Ÿ—๏ธ Architecture

This project follows a clean, interface-based architecture that makes it easy to maintain and extend.

Project Structure

src/
โ”œโ”€โ”€ app/
โ”‚   โ”œโ”€โ”€ commands/           # All command implementations
โ”‚   โ”‚   โ”œโ”€โ”€ CepCommand.ts
โ”‚   โ”‚   โ”œโ”€โ”€ EconomyCommand.ts
โ”‚   โ”‚   โ”œโ”€โ”€ ProfileCommand.ts
โ”‚   โ”‚   โ”œโ”€โ”€ QuoteCommand.ts
โ”‚   โ”‚   โ”œโ”€โ”€ SmsCommand.ts
โ”‚   โ”‚   โ””โ”€โ”€ index.ts        # Command registration
โ”‚   โ”œโ”€โ”€ interfaces/         # TypeScript interfaces
โ”‚   โ”‚   โ”œโ”€โ”€ ICommand.ts     # Base command interface
โ”‚   โ”‚   โ””โ”€โ”€ Cep.ts
โ”‚   โ””โ”€โ”€ utils/              # Utility classes
โ”‚       โ”œโ”€โ”€ BaseCommand.ts  # Abstract base class for commands
โ”‚       โ””โ”€โ”€ CommandDispatcher.ts
โ”œโ”€โ”€ config/                 # Configuration files
โ”œโ”€โ”€ data/                   # WhatsApp session data
โ”œโ”€โ”€ services/               # External services
โ”‚   โ”œโ”€โ”€ whatsapp.ts        # WhatsApp client setup
โ”‚   โ””โ”€โ”€ mobizon.ts         # SMS service
โ””โ”€โ”€ index.ts               # Application entry point

Command System

The bot uses a modern command pattern with the following benefits:

  • โœ… Interface-based design - All commands implement ICommand
  • โœ… Base class with helpers - BaseCommand provides common functionality
  • โœ… Automatic registration - Commands are registered at startup
  • โœ… Alias support - Multiple names for the same command
  • โœ… Centralized error handling - Consistent error messages
  • โœ… Type safety - Full TypeScript support

๐Ÿ“š View Technical Documentation for detailed architecture information.


๐Ÿ”ง Adding New Commands

Creating a new command is simple:

1. Create Command File

// src/app/commands/HelloCommand.ts
import { BaseCommand } from '../utils/BaseCommand';
import type { Message } from 'whatsapp-web.js';

export class HelloCommand extends BaseCommand {
  name = 'hello';
  description = 'Responds with a greeting';
  aliases = ['hi', 'hey', 'ola'];
  
  async execute(message: Message, args: string[]): Promise<Message> {
    await this.sendTyping(message);
    
    const name = args.join(' ') || 'friend';
    
    return message.reply(`๐Ÿ‘‹ Hello, ${name}! How can I help you?`);
  }
}

2. Register Command

// src/app/commands/index.ts
import { HelloCommand } from './HelloCommand';

export const initializeCommands = (): void => {
  // ... other commands
  commandDispatcher.register(new HelloCommand());
};

That's it! Your command is now available with all aliases: !hello, !hi, !hey, !ola


๐Ÿ› ๏ธ Configuration

Environment Variables

Create a .env file in the root directory:

# Mobizon SMS Service (optional)
MOBIZON_URL_SRV=https://api.mobizon.com.br
MOBIZON_API_KEY=your_api_key_here

Group Configuration

For admin-only commands (like !mencionar), configure authorized users in:

src/config/integrantes.json
{
  "company": [
    {
      "numero": "5511999999999",
      "admin": true
    }
  ]
}

๐Ÿ“– Usage Examples

Get Help

User: !help
Bot: ๐Ÿ“š Available Commands

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”

๐Ÿ”น !help
   Shows all available commands with their descriptions
   Aliases: !ajuda, !comandos, !commands

๐Ÿ”น !cotacao
   Shows current exchange rates (USD, BTC, EUR)
   Aliases: !moeda, !dolar, !bitcoin

...

Get Currency Rates

User: !cotacao
Bot: ๐Ÿ’Ž Cotaรงรฃo Atual ๐Ÿ’ฐ๐Ÿค‘๐Ÿ’น

๐Ÿ’ฒ Dรณlar Americano (USD)
Valor atual: R$ 5.25
Valor mais alto: R$ 5.30
Valor mais baixo: R$ 5.20
...

Search Postal Code

User: !cep 01310-100
Bot: ๐Ÿ“ฎ Informaรงรตes do CEP

CEP: 01310-100
Logradouro: Avenida Paulista
Bairro: Bela Vista
Cidade: Sรฃo Paulo
UF: SP

Get Profile Picture

User: !perfil @John
Bot: ๐Ÿ” Buscando foto de perfil...
[Sends profile picture]

๐Ÿงช Testing

# Run linter
npm run lint

# Run in development mode with auto-reload
npm run dev

Manual Testing Checklist

  • !cotacao - Returns currency rates
  • !moeda - Works as alias for cotacao
  • !cep 01310-100 - Returns postal code info
  • !cep - Returns usage error
  • !perfil @user - Returns profile picture (in groups)
  • !perfil @user - Returns error (in private chat)
  • !invalidcommand - Silently ignored

๐Ÿค Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Development Guidelines

  • Follow TypeScript best practices
  • Extend BaseCommand for new commands
  • Add proper error handling
  • Include JSDoc comments
  • Test your changes thoroughly

๐Ÿ“ License

Copyright ยฉ 2022-2025 Caio Agiani

This project is licensed under the GNU AGPL License.


โš ๏ธ Disclaimer

This project is not affiliated, associated, authorized, endorsed by, or in any way officially connected with WhatsApp or any of its subsidiaries or affiliates. The official WhatsApp website can be found at https://whatsapp.com.

"WhatsApp" as well as related names, marks, emblems and images are registered trademarks of their respective owners.

Use this bot responsibly and in accordance with WhatsApp's Terms of Service.


๐Ÿ‘ฅ Contributors

๐Ÿ™ Acknowledgments

Special thanks to:

  • @pedroslopez - whatsapp-web.js library
  • All contributors who have helped improve this project

๐Ÿ“ž Contact


๐ŸŒŸ Star History

If you find this project useful, please consider giving it a โญ๏ธ!

Star History Chart


Made with โค๏ธ by Caio Agiani