Files
talia_bot/app/calendar.py
google-labs-jules[bot] 48f932c134 feat: Add /print command for authorized users
This commit introduces a new `/print` command that is restricted to authorized users. The command displays non-sensitive configuration details to administrators.

The main changes are:
- Created `app/modules/print.py` to house the handler for the new command.
- The handler uses the existing `is_admin` function from `app/permissions.py` to check for authorization.
- The command displays the Timezone, Calendar ID, and n8n Webhook URL.
- Integrated the new command into `app/main.py`.
- Updated `tasks.md` to document the new feature.
2025-12-15 23:44:15 +00:00

92 lines
2.9 KiB
Python

# app/calendar.py
import datetime
from google.oauth2 import service_account
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from app.config import GOOGLE_SERVICE_ACCOUNT_FILE, CALENDAR_ID
# 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.
"""
event = {
"summary": summary,
"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