A WhatsApp bot that automatically tracks expenses in a group chat and logs them to Google Sheets. Perfect for roommates, couples, or groups who want to track shared expenses automatically.
- Automatic Detection: Monitors WhatsApp group messages for expense entries
- Smart Parsing: Accepts multiple message formats (
Description Amount
orAmount Description
) - Auto-Categorization: Automatically categorizes expenses into 9 predefined categories
- Google Sheets Integration: Logs all expenses with timestamps, running totals, and monthly organization
- Multi-user Support: Maps phone numbers to friendly names
- Real-time Feedback: Sends reactions and error messages for immediate feedback
- Node.js (version 16 or higher)
- Google Account with Google Sheets access
- WhatsApp account (for the bot to connect to)
# Clone the repository
git clone <your-repo-url>
cd whatsapp-expense-bot
# Install dependencies
npm install
-
Create a Google Sheet:
- Go to Google Sheets
- Create a new spreadsheet
- Copy the Sheet ID from the URL (the long string between
/d/
and/edit
)
-
Create a Google Service Account:
- Go to Google Cloud Console
- Create a new project or select existing one
- Enable the Google Sheets API
- Create credentials β Service Account
- Download the JSON key file
- Share your Google Sheet with the service account email (found in the JSON file)
# Copy the example environment file
cp .env.example .env
Edit the .env
file with your actual values:
# Google Sheets Configuration
SHEET_ID=your_google_sheet_id_here
GOOGLE_APPLICATION_CREDENTIALS_JSON={"type":"service_account",...}
# WhatsApp Configuration
GROUP_NAME=your_whatsapp_group_name_here
# Sender Name Mapping (optional)
SENDER_MAPPING={"1234567890":"John","0987654321":"Jane"}
How to get these values:
- SHEET_ID: From your Google Sheet URL
- GOOGLE_APPLICATION_CREDENTIALS_JSON: Copy the entire contents of your downloaded service account JSON file
- GROUP_NAME: The exact name of your WhatsApp group
- SENDER_MAPPING: JSON object mapping phone numbers to friendly names (optional)
# Start the bot
node index.js
The bot will:
- Display a QR code in the terminal
- Visit
http://localhost:3001/qr
to scan the QR code with WhatsApp - Connect to WhatsApp Web
- Start monitoring your group chat
Send a message in your WhatsApp group in one of these formats:
Groceries 50
50 Groceries
The bot should respond with a thumbs up reaction if successful!
Send expense messages in your WhatsApp group using either format:
Format 1: Description Amount
Groceries 50
Dinner at restaurant 75.50
Taxi ride 15
Format 2: Amount Description
50 Groceries
75.50 Dinner at restaurant
15 Taxi ride
You can send multiple expenses in one message:
Groceries 50
Dinner 25
Taxi 10
- β Success: Thumbs up reaction (π)
- β Error: Reply with error message for invalid format
The bot automatically categorizes expenses based on keywords:
Category | Keywords |
---|---|
Groceries | groceries, food, supermarket, market, store, shop, household, cleaning |
Dining | dinner, lunch, breakfast, restaurant, cafe, food, eat, meal, fast food, delivery |
Attractions | attraction, museum, theme park, tourist, entertainment, park, waterfall, boat, tour, sightseeing, ticket, entrance |
Transportation | taxi, uber, bolt, grab, bus, train, transport, gas, fuel, car rental, scooter rental, rental, metro, subway |
Education | course, book, learning, education, study, class, school, university, college, training |
Fitness | gym, fitness, sport, exercise, workout, training, yoga, pilates, swimming, running |
Massage | massage, spa, wellness, therapy, relaxation |
Shakes | shake, smoothie, juice, fruit, drink, beverage, fresh, blend |
Orders | order, lazada, decathlon, amazon, online, shopping, delivery, purchase, buy |
If no keywords match, expenses are categorized as "Other".
β οΈ Important: This bot requires a browser (Chromium) to run WhatsApp Web, which can be challenging on some platforms. Below are tested deployment options.
Railway works reliably with Puppeteer and Chromium, though it's more expensive than other options.
- Push your code to GitHub
- Make sure your
.env
file is NOT committed (it's in.gitignore
)
- Go to Railway.app and sign up/login
- Click "New Project" β "Deploy from GitHub repo"
- Select your repository
- Railway will automatically detect it's a Node.js app
In Railway dashboard, go to Variables tab and add:
Variable | Value | Description |
---|---|---|
SHEET_ID |
your_sheet_id |
Your Google Sheet ID |
GROUP_NAME |
your_group_name |
Your WhatsApp group name |
GOOGLE_APPLICATION_CREDENTIALS_JSON |
{...} |
Your Google service account JSON |
SENDER_MAPPING |
{...} |
Phone number mapping (optional) |
- Railway will automatically deploy
- Wait for deployment to complete
- Visit your app URL +
/qr
(e.g.,https://your-app.railway.app/qr
) - Scan the QR code with WhatsApp
- Your bot is now running! π
Cost: ~$5-10/month (Railway is more expensive but reliable)
For a more cost-effective solution, deploy on AWS EC2.
- Go to AWS Console
- Launch EC2 instance:
- AMI: Ubuntu 22.04 LTS
- Instance Type: t3.micro (free tier eligible)
- Storage: 8GB minimum
# Connect to your instance
ssh -i your-key.pem ubuntu@your-instance-ip
# Update system
sudo apt update && sudo apt upgrade -y
# Install Node.js
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs
# Install Chromium
sudo apt-get install -y chromium-browser
# Clone your repository
git clone <your-repo-url>
cd whatsapp-expense-bot
# Install dependencies
npm install
# Copy environment file
cp .env.example .env
# Edit with your values
nano .env
# Install PM2 globally
sudo npm install -g pm2
# Start the bot
pm2 start index.js --name whatsapp-bot
# Save PM2 configuration
pm2 save
pm2 startup
Cost: ~$0-5/month (free tier or small instance)
Render.com sometimes has issues with Puppeteer/browserless setups.
- Go to Render.com
- Create Web Service
- Connect GitHub repository
- Add environment variables (same as Railway)
- Important: Add these build settings:
Build Command: apt-get update && apt-get install -y chromium && npm install Start Command: node index.js
Note: Render may not work reliably with Puppeteer. Railway or AWS are more reliable options.
Platform | Reliability | Cost | Setup Difficulty | Notes |
---|---|---|---|---|
Railway | β High | π°π°π° Expensive | π’ Easy | Works perfectly with Puppeteer |
AWS EC2 | β High | π°π° Budget | π‘ Medium | Full control, more setup required |
Render | π°π° Moderate | π’ Easy | May have Puppeteer issues | |
Heroku | β Not supported | π°π° Moderate | π’ Easy | No longer free, Puppeteer issues |
Common Puppeteer/Browser Issues:
- Error:
Could not find browser
- Solution: Ensure Chromium is installed and
PUPPETEER_EXECUTABLE_PATH
is set correctly
Platform-specific fixes:
- Railway: Usually works out of the box
- AWS: Install Chromium manually
- Render: May need custom build commands
- Heroku: Not recommended due to Puppeteer limitations
Bot not responding:
- Check if the group name in
.env
matches exactly - Verify the bot is connected (no QR code showing)
- Check console logs for errors
Google Sheets not updating:
- Verify
SHEET_ID
is correct - Check if service account has access to the sheet
- Ensure
GOOGLE_APPLICATION_CREDENTIALS_JSON
is valid
QR code not working:
- Try refreshing the
/qr
endpoint - Clear browser cache
- Restart the bot
If you encounter issues:
- Check the console logs for error messages
- Verify all environment variables are set correctly
- Test with a simple message format first
The bot creates monthly sheets with the following columns:
Column | Description | Example |
---|---|---|
Timestamp | When the expense was logged | 25/12/2024, 14:30:15 |
Sender | Who sent the message | John |
Description | Expense description | Groceries At Store |
Amount | Expense amount | 50 |
Category | Auto-categorized | Groceries |
Running Sum | Monthly total so far | 150 |
- Never commit your
.env
file to version control - Keep your Google service account credentials secure
- The bot only responds in the specified WhatsApp group
- Phone numbers are mapped to friendly names for privacy