#!/usr/bin/env python3 """ Calendar Agent for Discord Channel 1474636036905631867 Simple agent to add/view Home Assistant calendar events via natural language. """ import sys import os sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from api.ha_calendar import ( add_calendar_event, get_todays_events, get_upcoming_events, format_events, format_upcoming, ) from datetime import datetime import re class CalendarAgent: """Lightweight calendar agent for Discord.""" def __init__(self): self.channel_id = "1474636036905631867" def handle_message(self, message: str) -> str: """Process incoming message and return response.""" msg = message.lower().strip() # Add event patterns add_patterns = [ r"add\s+(.+?)\s+on\s+(\d{4}-\d{2}-\d{2})\s*(?:at\s*)?(\d{1,2}:?\d{2})?\s*(am|pm)?", r"schedule\s+(.+?)\s+for\s+(\d{4}-\d{2}-\d{2})\s*(?:at\s*)?(\d{1,2}:?\d{2})?\s*(am|pm)?", r"new\s+event:\s*(.+?)\s+(\d{4}-\d{2}-\d{2})\s*(\d{1,2}:?\d{2})?", ] for pattern in add_patterns: match = re.search(pattern, msg, re.IGNORECASE) if match: return self._handle_add_event(match) # Today's events if any(p in msg for p in ["today's events", "what do i have today", "today's schedule", "today schedule"]): return self._handle_today() # Upcoming events if any(p in msg for p in ["upcoming", "what's coming", "whats coming", "show me", "this week", "this month"]): return self._handle_upcoming() return None # Not a calendar command def _handle_add_event(self, match) -> str: """Handle adding a new event.""" groups = match.groups() summary = groups[0].strip() date = groups[1] time = groups[2] if len(groups) > 2 else None ampm = groups[3] if len(groups) > 3 else None # Build datetime string if time: time = time.replace(":", "") if ampm and ampm.lower() == "pm" and len(time) <= 2: # Convert 3pm to 15:00 hour = int(time) if hour < 12: hour += 12 time = f"{hour:02d}:00" elif ampm and ampm.lower() == "am": time = f"{int(time):02d}:00" start_dt = f"{date} {time}" else: # Default to 9 AM start_dt = f"{date} 09:00" result = add_calendar_event(summary, start_dt) if result.get("success"): return f"[OK] Added: **{summary}** on {date} at {time or '9:00 AM'}" else: return f"[ERROR] Failed to add event: {result.get('error', 'Unknown error')}" def _handle_today(self) -> str: """Get today's events.""" events = get_todays_events() return format_events(events) def _handle_upcoming(self, days: int = 7) -> str: """Get upcoming events.""" events = get_upcoming_events(days) return format_upcoming(events) def main(): """CLI entrypoint for testing.""" agent = CalendarAgent() print("Calendar Agent ready. Type 'quit' to exit.") print("Try: 'Add meeting on 2026-02-22 at 3pm' or 'What do I have today?'") print() while True: user_input = input("You: ").strip() if user_input.lower() in ["quit", "exit", "q"]: break response = agent.handle_message(user_input) if response: print(f"Agent: {response}") else: print("Agent: I'm a calendar agent. Try: 'Add event on [date] at [time]' or 'Show upcoming events'") if __name__ == "__main__": main()