This system automatically captures messages from your Facebook Page and saves them to Google Sheets in real-time.
- ✅ Automatic message capture from Facebook Page
- ✅ Real-time data entry into Google Sheets
- ✅ Optional Excel export functionality
- ✅ Webhook-based architecture (no polling needed)
- ✅ Easy setup and configuration
- ✅ Support for message text, sender info, timestamps
- Python 3.7+ installed on your system
- Facebook Page with admin access
- Facebook App (to set up webhooks)
- Google Cloud Project with Sheets API enabled
- Google Service Account credentials
- Public URL for webhooks (use ngrok for local testing)
pip install -r requirements.txt- Go to Google Cloud Console
- Create a new project or select an existing one
- Enable Google Sheets API and Google Drive API:
- Navigate to "APIs & Services" > "Library"
- Search for "Google Sheets API" and enable it
- Search for "Google Drive API" and enable it
- Create a Service Account:
- Go to "APIs & Services" > "Credentials"
- Click "Create Credentials" > "Service Account"
- Create the service account and download the JSON key file
- Rename it to
credentials.jsonand place it in the project root
- Share your Google Sheet with the service account email:
- Open your Google Sheet (or create a new one)
- Click "Share" and add the service account email (found in credentials.json)
- Give it "Editor" permissions
- Copy
config_example.envto.env(recommended for local development) - Set the following variables:
FB_VERIFY_TOKEN: A random string for webhook verification (same as Facebook webhook)FB_PAGE_ACCESS_TOKEN: Your Facebook Page Access Token (for fetching sender names)GOOGLE_SHEET_ID: Your Google Sheet ID (found in the sheet URL)GOOGLE_SHEET_NAME: Name of the worksheet (default: "Messages")GOOGLE_CREDENTIALS_FILE: Path to credentials.json (default: "credentials.json")EXCEL_EXPORT_ENABLED: Set to "true" to also save to Excel (optional)EXCEL_FILENAME: Name of Excel file (default: "fb_messages.xlsx")
- Go to Facebook Developers
- Create a new app or use an existing one
- Add "Messenger" product to your app
- Configure Webhooks:
- Go to Messenger > Settings > Webhooks
- Click "Add Callback URL"
- Enter your webhook URL:
https://your-domain.com/webhook - Enter your Verify Token (same as
FB_VERIFY_TOKEN) - Subscribe to:
messagesandmessaging_postbacks
- Get Page Access Token:
- Go to Messenger > Settings > Access Tokens
- Generate a token for your page
- Copy the token and save it in your
.envfile asFB_PAGE_ACCESS_TOKEN - This token is used to fetch sender names from Facebook profiles
For local testing, use ngrok:
# Install ngrok: https://ngrok.com/download
ngrok http 5000Copy the HTTPS URL (e.g., https://abc123.ngrok.io) and use it in your Facebook webhook callback URL.
For production, deploy to:
- Heroku
- AWS Lambda
- Google Cloud Run
- DigitalOcean App Platform
- Any hosting service that supports Python/Flask
Before running the server, verify your setup:
python test_setup.pyThis will check:
- Python dependencies
- Environment variables
- Google credentials and sheet access
python app.pyOr with environment variables:
export FB_VERIFY_TOKEN=your_token
export GOOGLE_SHEET_ID=your_sheet_id
python app.pyMessages are automatically parsed and saved to Google Sheets with the following columns:
| Timestamp | Name | Contact Number | Purok | Barangay | Mensahe | Please Upload Picture | Email Address | Status |
|---|---|---|---|---|---|---|---|---|
| 2025-10-15 02:15:51 | Joy Parreño | 09914567842 | Purok Cadena De Amor | Brgy Abuanan | Good morning sir... | (URL if attached) | (email if provided) | New Message |
The system automatically extracts:
- Name: From Facebook profile (via Graph API) or message content
- Contact Number: Phone numbers (Philippine format: 09xxxxxxxxx)
- Purok: Detected from patterns like "purok X" or "prk X"
- Barangay: Detected from patterns like "barangay X" or "brgy X"
- Mensahe: Full message text
- Please Upload Picture: URLs of attached images/files
- Email Address: Email addresses found in messages
- Status: Default "New Message" (can be customized)
- Send a test message to your Facebook Page
- Check your Google Sheet - the message should appear automatically
- Check server logs for any errors
- Ensure
FB_VERIFY_TOKENmatches in both your app and Facebook webhook settings - Check that your server is accessible from the internet
- Verify
credentials.jsonis in the correct location - Check that the service account email has edit access to the sheet
- Verify
GOOGLE_SHEET_IDis correct (from the sheet URL) - Check server logs for error messages
- Verify webhook is subscribed to
messagesevent - Check that your page has messages enabled
- Ensure your webhook URL is accessible and returns 200 status
- Check Facebook App Dashboard for webhook delivery logs
- Never commit
credentials.jsonto version control - Use environment variables for sensitive tokens
- Add
credentials.jsonto.gitignore - Use HTTPS in production
- Rotate access tokens periodically
- Support for attachments/images
- Sender profile name fetching
- Reply automation
- Message filtering/routing
- Excel export option
- Database backup
This project is provided as-is for personal and commercial use.
For issues and questions:
- Check the troubleshooting section
- Review Facebook Messenger Platform documentation
- Review Google Sheets API documentation