|
| 1 | +""" |
| 2 | +Microsoft Bookings — manage businesses, services, appointments, |
| 3 | +staff, and customers. |
| 4 | +
|
| 5 | +Bookings is Microsoft's appointment scheduling solution. This example |
| 6 | +covers the full lifecycle: |
| 7 | + - List booking businesses |
| 8 | + - List services for a business |
| 9 | + - Get staff availability |
| 10 | + - Create an appointment |
| 11 | + - List customers |
| 12 | +
|
| 13 | +Requires delegated permission ``Bookings.ReadWrite.All``, |
| 14 | +``BookingsAppointment.ReadWrite.All``. |
| 15 | +
|
| 16 | +https://learn.microsoft.com/en-us/graph/api/resources/booking-api-overview |
| 17 | +""" |
| 18 | + |
| 19 | +import sys |
| 20 | +from datetime import datetime, timedelta, timezone |
| 21 | + |
| 22 | +from office365.graph_client import GraphClient |
| 23 | +from tests import test_client_id, test_client_secret, test_tenant |
| 24 | + |
| 25 | + |
| 26 | +def main(): |
| 27 | + client = GraphClient(tenant=test_tenant).with_client_secret(test_client_id, test_client_secret) |
| 28 | + |
| 29 | + # -- Step 1: list booking businesses -- |
| 30 | + businesses = client.solutions.booking_businesses.get().execute_query() |
| 31 | + print(f"Booking businesses: {len(businesses)}\n") |
| 32 | + |
| 33 | + for b in businesses: |
| 34 | + address = b.address |
| 35 | + display = b.display_name or "(unnamed)" |
| 36 | + city = address.city if address else "?" |
| 37 | + print(f" {display:40s} city={city}") |
| 38 | + |
| 39 | + if not businesses: |
| 40 | + print(" (no booking businesses — create one first at https://outlook.office.com/bookings)") |
| 41 | + print() |
| 42 | + # Create a sample business (commented) |
| 43 | + # from office365.booking.business.collection import BookingBusinessCollection |
| 44 | + # new_biz = client.solutions.booking_businesses.add(displayName="Demo Consulting").execute_query() |
| 45 | + # print(f"Created: {new_biz.display_name}") |
| 46 | + return |
| 47 | + |
| 48 | + biz = businesses[0] |
| 49 | + print(f"\nInspecting: {biz.display_name}") |
| 50 | + print(f" Business hours:") |
| 51 | + if biz.business_hours and biz.business_hours.value: |
| 52 | + for wh in biz.business_hours.value: |
| 53 | + print(f" {wh.properties.get('day', '?'):10s} {wh.properties.get('timeSlots', [])}") |
| 54 | + |
| 55 | + # -- Step 2: list services -- |
| 56 | + services = biz.services.get().execute_query() |
| 57 | + print(f"\n Services ({len(services)}):") |
| 58 | + for s in services: |
| 59 | + display = s.properties.get("displayName", "?") |
| 60 | + duration = s.properties.get("duration", "?") |
| 61 | + price = s.properties.get("defaultPrice", "?") |
| 62 | + currency = s.properties.get("defaultPriceType", "?") |
| 63 | + print(f" {display:35s} duration={duration} price={price} {currency}") |
| 64 | + |
| 65 | + # -- Step 3: get staff availability -- |
| 66 | + print(f"\n Getting staff availability for next 7 days...") |
| 67 | + staff = biz.staff_members.get().execute_query() |
| 68 | + print(f" Staff members: {len(staff)}") |
| 69 | + for s in staff[:3]: |
| 70 | + name = s.properties.get("displayName", "?") |
| 71 | + email = s.properties.get("emailAddress", "?") |
| 72 | + availability = biz.get_staff_availability( |
| 73 | + staff_ids=[s.id], |
| 74 | + start_date=datetime.now(timezone.utc), |
| 75 | + end_date=datetime.now(timezone.utc) + timedelta(days=7), |
| 76 | + ).execute_query() |
| 77 | + print(f" {name:25s} {email:30s}") |
| 78 | + |
| 79 | + # -- Step 4: create an appointment -- |
| 80 | + if services: |
| 81 | + service = services[0] |
| 82 | + now = datetime.now(timezone.utc) |
| 83 | + start = now + timedelta(days=1, hours=9) |
| 84 | + end = start + timedelta(hours=1) |
| 85 | + |
| 86 | + appointment = biz.appointments.add( |
| 87 | + serviceId=service.id, |
| 88 | + serviceName=service.properties.get("displayName", "Consultation"), |
| 89 | + startDateTime={"dateTime": start.isoformat(), "timeZone": "UTC"}, |
| 90 | + endDateTime={"dateTime": end.isoformat(), "timeZone": "UTC"}, |
| 91 | + customerEmail="client@example.com", |
| 92 | + customerName="John Doe", |
| 93 | + customerPhone="+1 555 123 4567", |
| 94 | + additionalInformation="Initial consultation booking via API", |
| 95 | + ).execute_query() |
| 96 | + print(f"\n ✓ Appointment created: {appointment.id}") |
| 97 | + |
| 98 | + # -- Step 5: list customers -- |
| 99 | + customers = biz.customers.get().execute_query() |
| 100 | + print(f"\n Customers: {len(customers)}") |
| 101 | + for c in customers: |
| 102 | + name = c.properties.get("displayName", "?") |
| 103 | + email = c.properties.get("emailAddress", "?") |
| 104 | + print(f" {name:30s} {email}") |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == "__main__": |
| 108 | + main() |
0 commit comments