This is a Node.js-based WhatsApp bot that integrates with Google's Gemini AI for generating responses. The bot uses the whatsapp-web.js library to interact with WhatsApp and the Google Generative AI SDK to process and generate text responses.
Before you proceed, make sure you have the following installed:
Run:
node -vYou should see something like:
v18.x.x
If Node.js is not installed or the version is lower than 18, install it from nodejs.org.
Run:
npm -vIf npm is not installed, install it by downloading Node.js (npm comes bundled with it).
Run:
git -vIf git is not installed, install it by downloading Git.
-
Clone the repository (or download the source files):
git clone https://github.com/devv-leo/whatsapp-bot.git cd whatsapp-bot -
Install dependencies:
npm install
-
Set up environment variables:
- Create a
.envfile in the project directory. - Add your Google Gemini API key:
API_KEY=your_google_gemini_api_key
- Create a
- Go to Google AI Studio.
- Sign in with your Google account.
- Navigate to API Keys (found under settings or dashboard).
- Click Generate API Key.
- Copy the key and paste it into the
.envfile as shown above.
- Start the bot by running:
node index.js
- A QR code will appear in the terminal. Scan it using WhatsApp on your phone:
- Open WhatsApp on your phone.
- Tap Linked Devices.
- Scan the QR Code.
- The bot will be ready once you see:
>> Bot is Ready! <<
- Chat History: The bot maintains conversation history for each user
- Private Chat Only: Bot only responds to private messages, not group chats
- Error Handling: Graceful error handling with user-friendly messages
- Automatic Session Management: Uses local authentication to maintain sessions
- Chat History Limit: Maintains last 20 messages per user to optimize performance
- Chat with AI: Send any message, and the bot will respond using the Gemini 1.5 Flash model
- Stop the bot: Press
Ctrl + Cin the terminal
- If the QR code does not appear, try:
npm install whatsapp-web.js
- If the bot does not respond:
- Ensure the API key is correct
- Restart the bot with
node index.js
- If you get "Invalid API Key":
- Double-check your key in
.env - Ensure your API key is active in Google AI Studio
- Double-check your key in
This project is open-source under the MIT License.
Feel free to contribute to this project by submitting issues or pull requests! 🚀