|
| 1 | +""" |
| 2 | +Meeting finder: suggest optimal time slots and resolve rooms. |
| 3 | +
|
| 4 | +Brings together three related patterns: |
| 5 | + 1. Find available meeting times that work for all attendees. |
| 6 | + 2. List room lists and rooms in the tenant. |
| 7 | + 3. Get free/busy schedule for a room across a time window. |
| 8 | +
|
| 9 | +Useful for scheduling assistants, booking flows, and room |
| 10 | +management tooling. |
| 11 | +
|
| 12 | +Requires delegated permissions: |
| 13 | + Calendars.ReadWrite Create and manage events |
| 14 | + MailboxSettings.Read Read user availability |
| 15 | + Place.Read.All List rooms and room lists |
| 16 | +
|
| 17 | +https://learn.microsoft.com/en-us/graph/api/user-findmeetingtimes |
| 18 | +https://learn.microsoft.com/en-us/graph/api/resources/place |
| 19 | +""" |
| 20 | + |
| 21 | +from datetime import datetime, timedelta, timezone |
| 22 | + |
| 23 | +from office365.graph_client import GraphClient |
| 24 | +from tests import test_client_id, test_password, test_tenant, test_username |
| 25 | + |
| 26 | + |
| 27 | +def main(): |
| 28 | + client = GraphClient(tenant=test_tenant).with_username_and_password(test_client_id, test_username, test_password) |
| 29 | + |
| 30 | + # -- Step 1: list room lists and rooms in the tenant -- |
| 31 | + print("Places (room lists and rooms):") |
| 32 | + all_places = client.places.get().execute_query() |
| 33 | + for p in all_places: |
| 34 | + print(f" {p.display_name:45s} type={p.entity_type_name}") |
| 35 | + print() |
| 36 | + |
| 37 | + # Try to get room lists specifically |
| 38 | + room_lists = client.places.room_lists.get().execute_query() |
| 39 | + if room_lists: |
| 40 | + print("Room lists:") |
| 41 | + for rl in room_lists: |
| 42 | + print(f" {rl.display_name:45s} {rl.email_address or ''}") |
| 43 | + # Get rooms in this list |
| 44 | + try: |
| 45 | + rooms = rl.rooms.get().execute_query() |
| 46 | + for room in rooms: |
| 47 | + print(f" ↳ {room.display_name:40s} {room.email_address or ''} ({room.capacity or '?'} seats)") |
| 48 | + except Exception: |
| 49 | + pass |
| 50 | + else: |
| 51 | + print("(No room lists found — room list management may need Place.Read.All)") |
| 52 | + |
| 53 | + # -- Step 2: find meeting times for next week -- |
| 54 | + now = datetime.now(timezone.utc) |
| 55 | + start_time = now + timedelta(days=1) |
| 56 | + end_time = now + timedelta(days=7) |
| 57 | + |
| 58 | + # Collect room email addresses if available |
| 59 | + attendee_smtp = ["meganb@contoso.onmicrosoft.com"] |
| 60 | + if room_lists: |
| 61 | + for rl in room_lists[:1]: # first room list |
| 62 | + try: |
| 63 | + rooms = rl.rooms.get().execute_query() |
| 64 | + attendee_smtp.extend(r.email_address for r in rooms[:2] if r.email_address) |
| 65 | + except Exception: |
| 66 | + pass |
| 67 | + |
| 68 | + print(f"\nFinding meeting times for: {', '.join(attendee_smtp)}") |
| 69 | + print(f" Window: {start_time.strftime('%Y-%m-%d %H:%M')} — {end_time.strftime('%Y-%m-%d %H:%M')}") |
| 70 | + |
| 71 | + suggestions = client.me.find_meeting_times( |
| 72 | + attendees=attendee_smtp, |
| 73 | + meeting_duration=timedelta(hours=1), |
| 74 | + max_candidates=5, |
| 75 | + ).execute_query() |
| 76 | + |
| 77 | + if suggestions and hasattr(suggestions, "value") and suggestions.value: |
| 78 | + print(f"\nSuggested times ({len(suggestions.value)} candidate(s)):\n") |
| 79 | + # The value is a MeetingTimeSuggestionsResult |
| 80 | + result = suggestions.value |
| 81 | + if hasattr(result, "meeting_time_suggestions") and result.meeting_time_suggestions: |
| 82 | + for s in result.meeting_time_suggestions: |
| 83 | + slot = s.meeting_time_slot |
| 84 | + print(f" {slot.start.strftime('%Y-%m-%d %H:%M')} — {slot.end.strftime('%H:%M')} (confidence: {s.confidence})") |
| 85 | + if hasattr(s, "locations") and s.locations: |
| 86 | + for loc in s.locations: |
| 87 | + print(f" Location: {loc.display_name}") |
| 88 | + else: |
| 89 | + print(" (no detailed suggestions — check permissions)") |
| 90 | + else: |
| 91 | + print(" No meeting suggestions returned.") |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == "__main__": |
| 95 | + main() |
0 commit comments