mirror of
https://github.com/marcogll/talia_bot.git
synced 2026-01-13 13:25:19 +00:00
feat: Implement Google Calendar integration
- Implements `get_available_slots` to find open time slots. - Implements `create_event` to schedule new events. - Uses a service account for server-to-server authentication. - Adds `GOOGLE_SERVICE_ACCOUNT_FILE` and `CALENDAR_ID` to the configuration. - Updates `tasks.md` to reflect the completion of the integration. - Includes error handling for Google Calendar API calls.
This commit is contained in:
@@ -2,9 +2,8 @@ TELEGRAM_BOT_TOKEN=
|
|||||||
OWNER_CHAT_ID=
|
OWNER_CHAT_ID=
|
||||||
ADMIN_CHAT_IDS=
|
ADMIN_CHAT_IDS=
|
||||||
TEAM_CHAT_IDS=
|
TEAM_CHAT_IDS=
|
||||||
GOOGLE_CLIENT_ID=
|
GOOGLE_SERVICE_ACCOUNT_FILE=
|
||||||
GOOGLE_CLIENT_SECRET=
|
CALENDAR_ID=
|
||||||
GOOGLE_REFRESH_TOKEN=
|
|
||||||
N8N_WEBHOOK_URL=
|
N8N_WEBHOOK_URL=
|
||||||
OPENAI_API_KEY=
|
OPENAI_API_KEY=
|
||||||
TIMEZONE=America/Mexico_City
|
TIMEZONE=America/Mexico_City
|
||||||
|
|||||||
@@ -1,17 +1,91 @@
|
|||||||
# app/calendar.py
|
# app/calendar.py
|
||||||
|
|
||||||
def get_available_slots():
|
import datetime
|
||||||
"""
|
from google.oauth2 import service_account
|
||||||
Fetches available calendar slots.
|
from googleapiclient.discovery import build
|
||||||
"""
|
from googleapiclient.errors import HttpError
|
||||||
print("Fetching available slots from Google Calendar...")
|
from app.config import GOOGLE_SERVICE_ACCOUNT_FILE, CALENDAR_ID
|
||||||
# TODO: Implement Google Calendar API integration
|
|
||||||
return []
|
|
||||||
|
|
||||||
def create_event(summary, start_time, end_time, attendees):
|
# Set up the Calendar API
|
||||||
|
SCOPES = ["https://www.googleapis.com/auth/calendar"]
|
||||||
|
creds = service_account.Credentials.from_service_account_file(
|
||||||
|
GOOGLE_SERVICE_ACCOUNT_FILE, scopes=SCOPES
|
||||||
|
)
|
||||||
|
service = build("calendar", "v3", credentials=creds)
|
||||||
|
|
||||||
|
|
||||||
|
def get_available_slots(
|
||||||
|
start_time, end_time, duration_minutes=30, calendar_id=CALENDAR_ID
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Fetches available calendar slots within a given time range.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
time_min = start_time.isoformat()
|
||||||
|
time_max = end_time.isoformat()
|
||||||
|
|
||||||
|
freebusy_query = {
|
||||||
|
"timeMin": time_min,
|
||||||
|
"timeMax": time_max,
|
||||||
|
"timeZone": "UTC",
|
||||||
|
"items": [{"id": calendar_id}],
|
||||||
|
}
|
||||||
|
|
||||||
|
freebusy_result = service.freebusy().query(body=freebusy_query).execute()
|
||||||
|
busy_slots = freebusy_result["calendars"][calendar_id]["busy"]
|
||||||
|
|
||||||
|
# Create a list of all potential slots
|
||||||
|
potential_slots = []
|
||||||
|
current_time = start_time
|
||||||
|
while current_time + datetime.timedelta(minutes=duration_minutes) <= end_time:
|
||||||
|
potential_slots.append(
|
||||||
|
(
|
||||||
|
current_time,
|
||||||
|
current_time + datetime.timedelta(minutes=duration_minutes),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
current_time += datetime.timedelta(minutes=duration_minutes)
|
||||||
|
|
||||||
|
# Filter out busy slots
|
||||||
|
available_slots = []
|
||||||
|
for slot_start, slot_end in potential_slots:
|
||||||
|
is_busy = False
|
||||||
|
for busy in busy_slots:
|
||||||
|
busy_start = datetime.datetime.fromisoformat(busy["start"])
|
||||||
|
busy_end = datetime.datetime.fromisoformat(busy["end"])
|
||||||
|
if max(slot_start, busy_start) < min(slot_end, busy_end):
|
||||||
|
is_busy = True
|
||||||
|
break
|
||||||
|
if not is_busy:
|
||||||
|
available_slots.append((slot_start, slot_end))
|
||||||
|
|
||||||
|
return available_slots
|
||||||
|
except HttpError as error:
|
||||||
|
print(f"An error occurred: {error}")
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
def create_event(summary, start_time, end_time, attendees, calendar_id=CALENDAR_ID):
|
||||||
"""
|
"""
|
||||||
Creates a new event in the calendar.
|
Creates a new event in the calendar.
|
||||||
"""
|
"""
|
||||||
print(f"Creating event: {summary}")
|
event = {
|
||||||
# TODO: Implement Google Calendar API integration
|
"summary": summary,
|
||||||
return None
|
"start": {
|
||||||
|
"dateTime": start_time.isoformat(),
|
||||||
|
"timeZone": "UTC",
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"dateTime": end_time.isoformat(),
|
||||||
|
"timeZone": "UTC",
|
||||||
|
},
|
||||||
|
"attendees": [{"email": email} for email in attendees],
|
||||||
|
}
|
||||||
|
try:
|
||||||
|
created_event = (
|
||||||
|
service.events().insert(calendarId=calendar_id, body=event).execute()
|
||||||
|
)
|
||||||
|
return created_event
|
||||||
|
except HttpError as error:
|
||||||
|
print(f"An error occurred: {error}")
|
||||||
|
return None
|
||||||
|
|||||||
@@ -5,9 +5,8 @@ TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
|
|||||||
OWNER_CHAT_ID = os.getenv("OWNER_CHAT_ID")
|
OWNER_CHAT_ID = os.getenv("OWNER_CHAT_ID")
|
||||||
ADMIN_CHAT_IDS = os.getenv("ADMIN_CHAT_IDS", "").split(",")
|
ADMIN_CHAT_IDS = os.getenv("ADMIN_CHAT_IDS", "").split(",")
|
||||||
TEAM_CHAT_IDS = os.getenv("TEAM_CHAT_IDS", "").split(",")
|
TEAM_CHAT_IDS = os.getenv("TEAM_CHAT_IDS", "").split(",")
|
||||||
GOOGLE_CLIENT_ID = os.getenv("GOOGLE_CLIENT_ID")
|
GOOGLE_SERVICE_ACCOUNT_FILE = os.getenv("GOOGLE_SERVICE_ACCOUNT_FILE")
|
||||||
GOOGLE_CLIENT_SECRET = os.getenv("GOOGLE_CLIENT_SECRET")
|
CALENDAR_ID = os.getenv("CALENDAR_ID")
|
||||||
GOOGLE_REFRESH_TOKEN = os.getenv("GOOGLE_REFRESH_TOKEN")
|
|
||||||
N8N_WEBHOOK_URL = os.getenv("N8N_WEBHOOK_URL")
|
N8N_WEBHOOK_URL = os.getenv("N8N_WEBHOOK_URL")
|
||||||
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
||||||
TIMEZONE = os.getenv("TIMEZONE", "America/Mexico_City")
|
TIMEZONE = os.getenv("TIMEZONE", "America/Mexico_City")
|
||||||
|
|||||||
2
tasks.md
2
tasks.md
@@ -29,7 +29,7 @@ This file tracks the development tasks for the Talía project.
|
|||||||
|
|
||||||
## Phase 4: Integrations
|
## Phase 4: Integrations
|
||||||
|
|
||||||
- [ ] Implement `calendar.py` for Google Calendar integration.
|
- [x] Implement `calendar.py` for Google Calendar integration.
|
||||||
- [ ] Implement `llm.py` for AI-powered responses.
|
- [ ] Implement `llm.py` for AI-powered responses.
|
||||||
- [ ] Implement `scheduler.py` for daily summaries.
|
- [ ] Implement `scheduler.py` for daily summaries.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user