Automated Action ef749e4878 Implement WhatsApp Message Analytics Service
- Set up FastAPI application structure
- Implement SQLite database with SQLAlchemy
- Create WhatsApp webhook endpoints
- Implement message storage and analysis
- Integrate Gemini 2.5 Pro for message analysis
- Add email delivery of insights
- Configure APScheduler for weekend analysis
- Add linting with Ruff
2025-05-22 13:29:12 +00:00

63 lines
1.8 KiB
Python

"""
Service for interacting with Google's Gemini API.
"""
import logging
import google.generativeai as genai
from app.core.config import settings
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Configure Gemini API
genai.configure(api_key=settings.GEMINI_API_KEY)
async def analyze_messages(messages: list[str]) -> str | None:
"""
Analyze a list of WhatsApp messages using Gemini 2.5 Pro.
Args:
messages: List of message texts to analyze
Returns:
Analysis text or None if analysis failed
"""
try:
# Join messages into a single string with line breaks
messages_text = "\n\n".join(messages)
# Prepare prompt for Gemini
prompt = f"""
You are an expert in analyzing WhatsApp conversations. I will provide you with a set of WhatsApp messages.
Please analyze these messages and provide the following insights:
1. Key topics and themes discussed
2. Sentiment analysis (overall mood of the conversations)
3. Frequent participants and their engagement levels
4. Any action items or follow-ups mentioned
5. Suggestions or recommendations based on the conversations
Format your response in a well-organized manner with clear headings and bullet points.
Here are the messages to analyze:
{messages_text}
"""
# Get Gemini model
model = genai.GenerativeModel(model_name="gemini-1.5-pro")
# Generate response
response = await model.generate_content_async(prompt)
# Return the analysis text
return response.text
except Exception as e:
logger.exception(f"Error analyzing messages with Gemini: {str(e)}")
return None