Production-ready FastAPI application that creates AI-powered Facebook Messenger bots. The system scrapes websites, generates a reference document using the GitHub Copilot SDK, and uses a PydanticAI agent to answer questions for people messaging a Facebook Page. This is the foundation for a turnkey product for political campaigns.
- Backend: FastAPI (async)
- AI Agent: PydanticAI with Pydantic v2 models
- LLM Engine: GitHub Copilot SDK (Python client)
- Scraping: httpx + BeautifulSoup4
- Database: Supabase (PostgreSQL)
- Deployment: Railway (FastAPI on Railway)
- CLI: Typer
CLI setup (website + tone + FB config)
↓
Scrape website → text chunks
↓
Copilot SDK: synthesize "reference doc" (markdown) from chunks
↓
Store reference doc + config in Supabase
↓
PydanticAI agent uses reference doc + tone to answer questions
↓
FastAPI webhook receives Messenger messages, calls agent
↓
Send response back via Facebook Messenger API
messenger_bot/
├── src/
│ ├── main.py # FastAPI app init
│ ├── config.py # Settings (Pydantic BaseSettings)
│ ├── api/ # API routes
│ ├── models/ # Pydantic models
│ ├── services/ # Business logic
│ ├── db/ # Database layer
│ └── cli/ # CLI commands
├── migrations/ # Database migrations
├── pyproject.toml # Project config
├── .env.example # Environment variables template
├── railway.toml # Railway deployment config
└── README.md # This file
- Python >= 3.12.8
- Supabase account and project
- Facebook App with Messenger permissions
- GitHub Copilot CLI (optional, falls back to OpenAI)
- Clone the repository
- Install dependencies:
uv sync
- Copy
.env.exampleto.envand fill in your credentials - Run database migrations:
# Apply migrations/001_initial.sql to your Supabase database - Start the development server:
uv run uvicorn src.main:app --reload
Run the interactive setup CLI:
uv run python -m src.cli.setup_cli setupThis will guide you through:
- Website URL input and scraping
- Reference document generation via Copilot
- Tone selection
- Facebook Page configuration
- Bot configuration persistence
When you choose Test the bot (or run uv run python -m src.cli.setup_cli test), conversations are persisted to Supabase (test_sessions, test_messages) so you can inspect them in the Supabase Dashboard while testing.
See .env.example for all required environment variables:
FACEBOOK_PAGE_ACCESS_TOKEN- Facebook Page access tokenFACEBOOK_VERIFY_TOKEN- Webhook verification tokenSUPABASE_URL- Supabase project URLSUPABASE_SERVICE_KEY- Supabase service role keyCOPILOT_CLI_HOST- Copilot CLI host (default: http://localhost:5909)COPILOT_ENABLED- Enable Copilot SDK (default: True)OPENAI_API_KEY- Fallback API keySENTRY_DSN- Sentry DSN for error tracking (optional)SENTRY_TRACES_SAMPLE_RATE- Sentry traces sample rate (default: 1.0)ENV- Environment (local, railway, prod)
- Connect your repository to Railway
- Set environment variables in Railway dashboard
- Railway will automatically detect
railway.tomland deploy - The app will be available at your Railway URL
After deployment, configure your Facebook webhook:
- Go to Facebook App Settings → Webhooks
- Add webhook URL:
https://your-railway-url.railway.app/webhook - Set verify token (from your
.envfile) - Subscribe to
messagesevents
GET /health- Health check endpointGET /webhook- Facebook webhook verificationPOST /webhook- Facebook message webhook
uv run pytestuv run ruff format .
uv run ruff check .For operational issues, debugging procedures, and common error solutions, see RUNBOOK.md. It contains:
- Common issues and fixes
- Debug commands for diagnosing problems
- Alert thresholds and monitoring guidance
- Service-specific troubleshooting
- Quick reference for environment variables and error messages