Facebook Gemini AI Agents - Complete Implementation
This is a comprehensive multi-agent system for Facebook page management using CrewAI and Google Gemini AI.
Facebook Gemini AI Agents - Complete Implementation
This is a comprehensive multi-agent system for Facebook page management using CrewAI and Google Gemini AI.
π Features
AI Agents
- Content Creation Agent: Auto-generates engaging posts, captions, and hashtags
- Customer Service Agent: Automated Messenger responses and lead generation
- Analytics Agent: Performance monitoring and content optimization
Capabilities
- β Intelligent content generation with Gemini AI
- β Automated customer service with intent recognition
- β Performance analytics and insights
- β Facebook API integration (Pages, Messenger, Marketing)
- β Web dashboard for agent management
- β RESTful API with comprehensive endpoints
- β Docker containerization support
- β Comprehensive testing suite
π Prerequisites
Before running the application, ensure you have:
-
Facebook Developer Account with:
- Facebook App with Page Management permissions
- Page Access Token
- App ID and App Secret
-
Google Cloud Account with:
- Gemini API key
- Enabled Generative AI API
-
Python 3.11+ installed
π§ Installation
1. Clone and Setup
git clone <repository-url>
cd facebook-gemini-agents
pip install -r requirements.txt
2. Configure Environment
cp .env.example .env
# Edit .env with your API credentials
3. Configure API Keys
Edit .env file with your credentials:
# Facebook API
FACEBOOK_APP_ID=your_app_id
FACEBOOK_APP_SECRET=your_app_secret
FACEBOOK_ACCESS_TOKEN=your_access_token
FACEBOOK_PAGE_ID=your_page_id
# Google Gemini
GOOGLE_API_KEY=your_gemini_api_key
π Quick Start
Run the Application
# Start the API server
python src/api/main.py
# Or use the CLI
python main.py content-generate --topic "summer vacation ideas"
Access the Web Interface
Open your browser to: http://localhost:8000/web/
π Usage Examples
Content Generation
from src.agents.content_agent import content_agent
# Generate a Facebook post
post = content_agent.create_post(
topic="summer vacation",
tone="friendly",
length="medium"
)
print(post['content'])
print(post['hashtags'])
Customer Service
from src.agents.customer_service_agent import customer_service_agent
# Handle customer message
result = customer_service_agent.handle_message(
message="I need help with my order",
sender_info={'name': 'John'}
)
print(result['response'])
print(f"Intent: {result['intent']}")
Analytics
from src.agents.analytics_agent import analytics_agent
# Analyze page performance
insights = analytics_agent.analyze_page_performance(
page_id="your_page_id",
date_range="last_30d"
)
print(insights['insights_summary'])
π API Endpoints
Content Agent
POST /api/agents/content/generate- Generate contentPOST /api/agents/content/optimize- Optimize existing contentPOST /api/agents/content/calendar- Generate content calendar
Customer Service Agent
POST /api/agents/service/respond- Handle customer messagePOST /api/agents/service/quick-reply- Handle quick replyGET /api/agents/service/metrics- Get service metrics
Analytics Agent
POST /api/agents/analytics/insights- Get page insightsPOST /api/agents/analytics/trending- Get trending contentPOST /api/agents/analytics/schedule- Get posting schedule
Facebook Integration
GET /api/facebook/pages- List Facebook pagesPOST /api/facebook/post- Create Facebook postGET /api/facebook/posts/{page_id}- Get page posts
π§ͺ Testing
Run Tests
# Run all tests
pytest
# Run specific test file
pytest tests/test_agents.py -v
# Run with coverage
pytest --cov=src tests/
Test Coverage
The test suite includes:
- β Unit tests for all agents
- β API endpoint testing
- β Integration tests
- β Mock external services
- β Error handling tests
π³ Docker Deployment
Build and Run
# Build and start services
docker-compose up -d
# Check logs
docker-compose logs -f
# Stop services
docker-compose down
Services
- app: Main application (port 8000)
- redis: Redis cache (port 6379)
- nginx: Reverse proxy (port 80)
π Project Structure
facebook-gemini-agents/
βββ src/
β βββ agents/
β β βββ base_agent.py # Base agent class
β β βββ content_agent.py # Content creation agent
β β βββ customer_service_agent.py # Customer service agent
β β βββ analytics_agent.py # Analytics agent
β βββ config/
β β βββ settings.py # Configuration settings
β βββ tools/
β β βββ facebook_tools.py # Facebook API client
β β βββ gemini_tools.py # Gemini AI tools
β β βββ utils.py # Utility functions
β βββ api/
β β βββ main.py # FastAPI application
β βββ web/
β βββ index.html # Web dashboard
βββ tests/
β βββ test_agents.py # Agent tests
β βββ test_api.py # API tests
βββ requirements.txt # Python dependencies
βββ docker-compose.yml # Docker services
βββ Dockerfile # App container
βββ main.py # CLI entry point
π§ Configuration
Agent Settings
Configure agent behavior in src/config/settings.py:
AGENT_CONFIGS = {
'content_agent': {
'model': 'gemini-pro',
'temperature': 0.7,
'max_tokens': 1000,
},
'customer_service_agent': {
'model': 'gemini-pro',
'temperature': 0.3,
'max_tokens': 500,
},
'analytics_agent': {
'model': 'gemini-pro',
'temperature': 0.1,
'max_tokens': 2000,
}
}
Facebook API Settings
Configure Facebook API endpoints and permissions in the same file.
π¨ Error Handling
The system includes comprehensive error handling:
- API Errors: Graceful handling of Facebook API errors
- Network Issues: Retry mechanisms for failed requests
- Validation: Input validation for all endpoints
- Logging: Detailed logging for debugging
- Fallback Responses: Backup responses when AI fails
π Security
Security measures include:
- Environment Variables: All secrets in environment variables
- Input Sanitization: Clean user inputs
- Rate Limiting: Prevent API abuse
- Webhook Verification: Secure Facebook webhooks
- HTTPS: SSL/TLS encryption
π Performance Optimization
- Caching: Redis caching for frequently accessed data
- Async Processing: Background tasks for heavy operations
- Connection Pooling: Efficient database connections
- Lazy Loading: Load resources only when needed
π€ Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
π License
This project is licensed under the MIT License - see the LICENSE file for details.
π Support
For support and questions:
- Create an issue in the GitHub repository
- Check the documentation in the
docs/folder - Review the API documentation at
http://localhost:8000/docs
π Acknowledgments
- CrewAI - Multi-agent orchestration framework
- Google Gemini - AI model for content generation
- Facebook - Social media platform and APIs
- FastAPI - Modern web framework
- Tailwind CSS - Utility-first CSS framework
Ready to automate your Facebook page management with AI? π
Start by configuring your environment variables and running the application. The intelligent agents will handle content creation, customer service, and analytics automatically!
Related Documents
Browser-only development
This document provides guidance for AI assistants working on the Image MetaHub codebase.
Claude Agents β Reference & Recommendations
Quick guide to available agents. Pick the one that best matches your task.
Golden DKG Prototype -- Master Plan
Rust prototype of the Golden non-interactive Distributed Key Generation protocol.
Swarms Examples Index
A comprehensive index of examples from the [Swarms Framework](https://github.com/The-Swarm-Corporation/swarms-examples).