This project is a simple WhatsApp bot that uses the Groq API to generate AI-powered replies. It is built with Flask and can be deployed on any server that supports Python 3.8+.
- Receives WhatsApp messages via Meta's webhook
- Replies using Groq's LLaMA models (model:
llama3-8b-8192by default) - Handles both incoming messages and status updates (delivered/read)
- Easy
.env-based configuration
- Python 3.8 or newer
- WhatsApp Cloud API access (Meta for Developers)
- Groq API key (get one here)
- Install dependencies:
python -m pip install -r requirements.txt- Create a
.envfile in the project root with the following keys:
WHATSAPP_TOKEN=your_whatsapp_access_token
PHONE_NUMBER_ID=your_phone_number_id
VERIFY_TOKEN=your_webhook_verify_token
GROQ_API_KEY=your_groq_api_keyWHATSAPP_TOKEN: Get from your Meta for Developers dashboard (WhatsApp Cloud API > API Token).PHONE_NUMBER_ID: Also from Meta dashboard (WhatsApp > Phone Numbers).VERIFY_TOKEN: Any random string you choose; must match the one set in your Meta webhook config. You can generate a secure token in Python:Copy the output and set it as your VERIFY_TOKEN inimport secrets print(secrets.token_hex(16))
.env.GROQ_API_KEY: Get from Groq Console (requires a Groq account).
-
Use
worker.pyto automatically restart the bot when you editmain.pyduring development. This script watches for changes and reloads the bot, so you don't have to restart manually.python worker.py
Press Ctrl+C to stop the watcher and the bot.
- The bot uses the
llama3-8b-8192model by default for fast, cost-effective, and concise WhatsApp replies. You can change the model inmain.py(see thegenerate_ai_replyfunction) to other Groq models likellama3-70b-8192for more advanced responses (may be slower or costlier). - The system prompt is set in code to instruct the LLaMA model to be concise, helpful, and friendly, and to keep replies under 2-3 sentences. This prompt is always sent before the user's message, so the bot's tone and style are consistent for every chat.
- You can customize the prompt or model in
main.pyto fit your use case.
-
Run the bot:
python main.py
-
Expose your server to the internet (for Meta webhook):
- Install ngrok if you don't have it:
- Download from https://ngrok.com/download
- Or install via package manager (see ngrok docs)
- Start a tunnel to your Flask server:
ngrok http 5000
- Copy the HTTPS forwarding URL from ngrok's output (e.g.,
https://abc123.ngrok.io). - Go to your Meta for Developers App Dashboard, select your app, and navigate to WhatsApp > API Setup > Webhooks.
- Set the webhook URL to
https://<your-ngrok-subdomain>.ngrok.io/webhookand use yourVERIFY_TOKENfrom.env.
- Install ngrok if you don't have it:
- Flask - Web server
- python-dotenv - Loads environment variables
- requests - HTTP requests
- groq - Groq API client
- WhatsApp Cloud API:
- Follow the official Meta guide: Get Started with WhatsApp Cloud API
- This covers creating a Meta developer account, setting up an app, getting your WhatsApp token, phone number ID, and configuring webhooks.
- Groq API:
- Go to Groq Console
- Sign in, create an API key, and copy it to your
.envasGROQ_API_KEY.
- This bot only handles text messages. You can extend it to handle media or interactive messages by modifying the webhook handler in
main.py. - For production, always use HTTPS and keep your tokens/keys secret.
- In the Meta developer dashboard, use the provided test number for development. Using your real number may result in a ban.
- If you see errors about missing keys or tokens, double-check your
.envfile and restart the bot. - If you get a
Groq configure error, your API key may be invalid or expired. - If the bot only echoes messages, your Groq API key is missing or not loaded.
-
Go to Meta Business Dashboard
-
Open Meta Business Settings .
Make sure you’re inside the correct Business Account where your WhatsApp number lives.
Check User Roles
Navigate to Accounts → WhatsApp Accounts.
Select the correct WhatsApp account.
Ensure your system user (or yourself) has Admin access to this account.
Create a System User (if not already)
Go to Users → System Users.
Add a new system user (give it a name like whatsapp-bot-user) and assign Admin role.
Generate a token for this system user.
While generating, choose the App linked to your WhatsApp account.
Assign Assets & Permissions
In System User → Assign Assets, attach your WhatsApp account.
Grant the following permissions:
whatsapp_business_messaging
whatsapp_business_management
Save changes.
Generate a Token
In the System User section, click Generate Token.
Pick the right App.
Select required permissions (whatsapp_business_messaging, whatsapp_business_management).
Choose Never Expire if available (some system user tokens allow this).
Copy the token.
Use the Token in Your App