tools.py•5.26 kB
"""
📅 Ferramentas de integração com Google Calendar.
"""
import asyncio
from datetime import datetime, timedelta
from typing import Optional, List, Dict, Any
from modules.base import BaseModule
from config.settings import settings
class CalendarTools(BaseModule):
"""Módulo de integração com Google Calendar."""
def __init__(self):
super().__init__()
self.service = None
self.credentials = None
async def is_available(self) -> bool:
"""Verifica se as credenciais do Google estão configuradas."""
return settings.has_google_credentials
async def initialize(self):
"""Inicializa o serviço do Google Calendar."""
try:
# Aqui você implementaria o fluxo OAuth2
# Por simplicidade, assumindo credenciais já configuradas
self.logger.info("Módulo Calendar inicializado")
self.initialized = True
except Exception as e:
self.logger.error(f"Erro ao inicializar Calendar: {e}")
raise
def get_tools(self) -> Dict[str, callable]:
"""Retorna as ferramentas do calendário."""
return {
"create_event": self.create_event,
"list_events": self.list_events,
"update_event": self.update_event,
"delete_event": self.delete_event,
"search_events": self.search_events
}
async def create_event(self, title: str, start_time: str, end_time: str,
description: str = "", location: str = "") -> str:
"""
Cria um novo evento no Google Calendar.
Args:
title: Título do evento
start_time: Data/hora de início (ISO format)
end_time: Data/hora de fim (ISO format)
description: Descrição do evento
location: Local do evento
Returns:
ID do evento criado
"""
try:
# Validar e parsear datas
try:
start_dt = datetime.fromisoformat(start_time.replace('Z', '+00:00'))
end_dt = datetime.fromisoformat(end_time.replace('Z', '+00:00'))
except ValueError as e:
return f"Erro: Data inválida. Use formato ISO (ex: 2025-01-20T10:00:00). {e}"
if start_dt >= end_dt:
raise ValueError("Data de início deve ser anterior à data de fim")
# Criar evento (implementação simplificada)
event = {
'summary': title,
'description': description,
'location': location,
'start': {'dateTime': start_time, 'timeZone': 'UTC'},
'end': {'dateTime': end_time, 'timeZone': 'UTC'}
}
self.logger.info(f"Evento criado: {title}")
return f"Evento '{title}' criado com sucesso"
except Exception as e:
self.logger.error(f"Erro ao criar evento: {e}")
return f"Erro ao criar evento: {str(e)}"
async def list_events(self, days_ahead: int = 7, max_results: int = 10) -> str:
"""
Lista eventos dos próximos dias.
Args:
days_ahead: Número de dias para buscar
max_results: Máximo de eventos a retornar
Returns:
Lista de eventos formatada
"""
try:
now = datetime.utcnow()
time_max = now + timedelta(days=days_ahead)
# Simular busca de eventos
events = [
{
'summary': 'Reunião de equipe',
'start': {'dateTime': now.isoformat() + 'Z'},
'description': 'Reunião semanal da equipe'
}
]
if not events:
return "Nenhum evento encontrado"
result = f"Próximos {len(events)} eventos:\n"
for event in events:
start = event['start'].get('dateTime', event['start'].get('date'))
result += f"📅 {event['summary']} - {start}\n"
if event.get('description'):
result += f" {event['description']}\n"
return result
except Exception as e:
self.logger.error(f"Erro ao listar eventos: {e}")
return f"Erro ao listar eventos: {str(e)}"
async def search_events(self, query: str) -> str:
"""
Busca eventos por texto.
Args:
query: Texto para buscar nos eventos
Returns:
Eventos encontrados
"""
try:
# Implementação de busca seria aqui
self.logger.info(f"Buscando eventos com: {query}")
return f"Busca por '{query}' concluída"
except Exception as e:
self.logger.error(f"Erro ao buscar eventos: {e}")
return f"Erro ao buscar eventos: {str(e)}"
async def update_event(self, event_id: str, **kwargs) -> str:
"""Atualiza um evento existente."""
# Implementação aqui
return f"Evento {event_id} atualizado"
async def delete_event(self, event_id: str) -> str:
"""Deleta um evento."""
# Implementação aqui
return f"Evento {event_id} deletado"