This project is a WhatsApp bot built using whatsapp-web.js. The bot can summarize text and translate messages into different languages using the @vitalets/google-translate-api.
- Summarization: Summarizes long text messages into shorter versions.
- Translation: Translates text into the specified language.
- Automatic Reconnection: Handles disconnections and authentication failures gracefully.
- Error Handling: Provides meaningful feedback for invalid commands and errors.
The bot supports the following commands:
- Command:
summarize: <text> - Description: Summarizes the provided text into a shorter version.
- Example:
- Input:
summarize: This is a long message that I want to summarize for better readability. - Output:
Summary: This is a long message that I want to summarize...
- Input:
- Command:
translate to <language>: <text> - Description: Translates the provided text into the specified language.
- Example:
- Input:
translate to fr: Hello, how are you? - Output:
Translation (fr): Bonjour, comment ça va ? - Input:
translate to es: Good morning! - Output:
Translation (es): ¡Buenos días!
- Input:
- If the command is not recognized, the bot responds with:
Sorry, I didn't understand that. Please try again with supported commands like: - summarize: <text> - translate to <language>: <text>
Ensure you have the following installed on your system:
- Node.js (v16 or later)
- npm (bundled with Node.js)
- A WhatsApp account
-
Clone the Repository:
git clone https://github.com/arpit-kaushal/AI-Function-Calling-Developer-WhatsApp-Bot.git cd AI-Function-Calling-Developer-WhatsApp-Bot -
Install Dependencies: Install the required npm packages:
npm install
This will install:
whatsapp-web.jsfor interacting with WhatsApp Webqrcode-terminalfor generating QR codes@vitalets/google-translate-apifor translationspuppeteerfor headless browser automation
-
Run the Bot: Start the bot using the following command:
npx ts-node src/app.ts
-
Authenticate:
- A QR code will appear in the terminal.
- Scan the QR code using the WhatsApp mobile app.
- The bot will log in and start listening for messages.
my-new-project/
├── src/
│ ├── app.ts # Main bot logic
├── node_modules/ # Installed dependencies
├── package.json # Project metadata and scripts
├── tsconfig.json # TypeScript configuration
└── README.md # Project documentation
- Open a terminal and navigate to the project directory:
cd C:\Users\arpit\Desktop\my-new-project
- Start the bot:
npx ts-node src/app.ts
- Open WhatsApp and send a message to the bot using one of the supported commands:
summarize: <text>translate to <language>: <text>
If you encounter errors related to Puppeteer, ensure the following:
- Install required dependencies:
- On Windows: Install the latest Microsoft Visual C++ Redistributable.
- On Linux:
sudo apt-get install -y gconf-service libgbm-dev libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
- Ensure sufficient system resources (RAM, CPU).
- The bot uses
LocalAuthto persist the login session. - If you want to reset the session, delete the
.wwebjs_authfolder:rmdir /s /q .wwebjs_auth # On Windows rm -rf .wwebjs_auth # On Linux/macOS
| Error Message | Solution |
|---|---|
Error: Failed to launch the browser process |
Ensure Puppeteer dependencies are installed (see above). Increase timeout if needed. |
Error: Authentication failed |
Restart the bot and re-scan the QR code. |
TypeError: translateAPI is not a function |
Ensure @vitalets/google-translate-api is correctly installed and imported in app.ts. |
Command not recognized |
Ensure the message is formatted correctly as per the supported commands. |
- Open Notepad and paste the following:
cd C:\Users\arpit\Desktop\my-new-project npx ts-node src/app.ts - Save the file as
start-bot.batin your project directory. - Double-click the batch file to start the bot.
- Create a file named
start-bot.sh:#!/bin/bash cd /path/to/my-new-project npx ts-node src/app.ts
- Make the script executable:
chmod +x start-bot.sh
- Run the script:
./start-bot.sh
Some ideas for future development:
- Add more commands (e.g., weather, jokes, etc.).
- Deploy the bot on a server or cloud platform.
- Add logging and monitoring for better debugging.
- Implement a database to store user interactions.