Complete directory structure for the Facebook Messenger AI Bot project with explanations of each file and directory.
messenger_bot/
├── src/
│ ├── __init__.py # Package initialization
│ ├── main.py # FastAPI app initialization
│ ├── config.py # Settings (Pydantic BaseSettings)
│ ├── api/ # API routes and endpoints
│ │ ├── __init__.py
│ │ ├── webhook.py # Facebook webhook endpoints
│ │ ├── setup.py # HTTP setup endpoints (optional)
│ │ └── health.py # /health for Railway
│ ├── models/ # Pydantic models for data validation
│ │ ├── __init__.py
│ │ ├── messenger.py # Incoming/outgoing FB models
│ │ ├── config_models.py # Bot + FB config models
│ │ └── agent_models.py # AgentContext, AgentResponse
│ ├── services/ # Business logic and service layer
│ │ ├── __init__.py
│ │ ├── scraper.py # Website scraping & chunking
│ │ ├── copilot_service.py # Copilot SDK wrapper
│ │ ├── reference_doc.py # Build reference doc via Copilot
│ │ ├── agent_service.py # PydanticAI agent
│ │ └── facebook_service.py # Send messages to FB Graph API
│ ├── db/ # Database client and repository
│ │ ├── __init__.py
│ │ ├── client.py # Supabase client
│ │ └── repository.py # Bot config / message history
│ └── cli/ # CLI commands for setup
│ ├── __init__.py
│ └── setup_cli.py # Typer-based interactive setup
├── migrations/ # Database migrations
│ └── 001_initial.sql # Initial schema
├── pyproject.toml # Project configuration
├── .env.example # Environment variables template
├── railway.toml # Railway deployment config
├── README.md # Project documentation
├── RUNBOOK.md # Operational troubleshooting guide
├── ARCHITECTURE.md # System architecture and design
├── AGENTS.md # AI agent guidelines
├── TESTING.md # Testing strategies
├── PROJECT_STRUCTURE.md # This file
└── GUARDRAILS.md # Safety boundaries and validation rules
- pyproject.toml: Python project configuration, dependencies, and build system
- .env.example: Template for environment variables
- railway.toml: Railway deployment configuration
- README.md: Project documentation and quick start guide
- RUNBOOK.md: Operational troubleshooting guide with common issues, fixes, debug commands, and alert thresholds
- ARCHITECTURE.md: System architecture, component interactions, and design decisions
- AGENTS.md: Guidelines for AI agents, code standards, testing practices, and operational maintenance
- TESTING.md: Testing strategies including property-based testing, stateful testing, and evaluation sets
- GUARDRAILS.md: Safety boundaries, validation rules, and risk mitigation strategies
- PROJECT_STRUCTURE.md: This file
Main application package.
FastAPI application initialization:
- Creates FastAPI app instance
- Registers routers (webhook, health, setup)
- Startup event handlers (Supabase init, Copilot check)
- Uvicorn configuration for Railway
Application configuration using Pydantic BaseSettings:
- Loads environment variables
- Provides type-safe settings access
- Includes
get_settings()helper withlru_cache()
API routes and endpoints.
Facebook webhook endpoints:
GET /webhook: Webhook verificationPOST /webhook: Message event handling
Health check endpoint:
GET /health: Returns{status: "ok"}for Railway
Optional HTTP setup endpoints (alternative to CLI).
Pydantic models for data validation.
Facebook Messenger models:
MessengerEntry: Webhook entryMessengerMessageIn: Incoming messageMessengerWebhookPayload: Full webhook payload
Configuration models:
WebsiteInput: Website URL inputTonePreference: Communication toneFacebookConfig: Facebook app configurationBotConfiguration: Complete bot configuration
Agent models:
AgentContext: Context for agent responsesAgentResponse: Agent response with confidence
Business logic and service layer.
Website scraping service:
- Uses
httpx.AsyncClientfor async requests - Parses HTML with BeautifulSoup
- Chunks text into 500-800 word segments
GitHub Copilot SDK wrapper:
- Async wrapper over Copilot SDK runtime
- Fallback to OpenAI when unavailable
- Methods:
is_available(),synthesize_reference(),chat()
Reference document builder:
- Uses Copilot to synthesize markdown from chunks
- Stores in
reference_documentstable
PydanticAI agent service:
- Builds system prompt with reference doc and tone
- Returns typed
AgentResponse - Handles message context
Facebook Graph API wrapper:
- Sends messages via
me/messagesendpoint - Uses page access token
Database client and repository layer.
Supabase client initialization:
- Creates and configures Supabase client
- Handles connection management
Database repository:
- Bot configuration CRUD operations
- Message history storage
- Reference document queries
CLI commands for setup and management.
Interactive setup CLI using Typer:
- Website URL input and validation
- Scraping and reference doc generation
- Tone selection
- Facebook configuration
- Bot persistence
Database migration files:
- 001_initial.sql: Initial schema with all tables, indexes, and triggers
# From src/main.py
from src.api import webhook, health
from src.config import get_settings
from src.db.client import get_supabase_client
# From services
from src.services.scraper import scrape_website
from src.services.copilot_service import CopilotService
from src.services.agent_service import MessengerAgentService
# From models
from src.models.messenger import MessengerWebhookPayload
from src.models.config_models import BotConfiguration
from src.models.agent_models import AgentContext, AgentResponse