Appointment Booking and Confirmation

View as Markdown

An AI agent handles appointment scheduling, checks availability against a calendar system, and confirms bookings.

Overview

This use case demonstrates:

  • Calendar/scheduling system integration
  • Multi-turn conversational data gathering
  • Real-time availability checking
  • SMS confirmation after booking
  • Fallback connect to a human when automation cannot fulfill the request

State flow: PENDING -> ANSWERED -> DISCONNECTED (or -> ANSWERED -> CONNECTED -> DISCONNECTED on fallback)

Example

1@client.on(events.INCOMING_CALL)
2async def appointment_booking(call: ActiveCall):
3 await call.answer()
4
5 customer = await db.get_customer_by_phone(call.caller_number)
6 existing_appointments = await calendar.get_upcoming(customer.id) if customer else []
7
8 if existing_appointments:
9 next_appt = existing_appointments[0]
10 msg = (
11 f"Hi {customer.name}, I see you have an appointment on "
12 f"{next_appt.date}. Would you like to reschedule or book a new one?"
13 )
14 await call.send_audio(await tts.synthesize(msg))
15 else:
16 await call.send_audio(
17 await tts.synthesize("Hi! I can help you book an appointment.")
18 )
19
20 # AI conversational loop to gather appointment details
21 appointment_details = await ai_model.gather_appointment_info(call)
22
23 # Check availability
24 available_slots = await calendar.get_available_slots(
25 date=appointment_details["date"],
26 service=appointment_details["service"],
27 )
28
29 if available_slots:
30 # Book the appointment
31 booking = await calendar.create_booking(
32 customer_id=customer.id,
33 slot=available_slots[0],
34 service=appointment_details["service"],
35 )
36 await call.send_audio(
37 await tts.synthesize(
38 f"You're booked for {booking.date} at {booking.time}. "
39 "You'll receive a confirmation SMS shortly."
40 )
41 )
42 await sms.send_confirmation(call.caller_number, booking)
43 else:
44 await call.send_audio(
45 await tts.synthesize(
46 "Unfortunately there are no slots available on that date. "
47 "Let me connect you with our scheduling team."
48 )
49 )
50 # Connect to the original callee and leave
51 await call.connect()
52 await call.close()
53 return
54
55 await call.disconnect()

How It Works

  1. The AI answers and checks if the caller has existing appointments
  2. If so, it proactively mentions them and asks if the caller wants to reschedule
  3. The AI gathers details conversationally: preferred date, time, and service type
  4. It queries the calendar system for available slots
  5. If a slot is available, the AI books it, confirms verbally, and sends an SMS
  6. If no slots are available, the AI explains the situation, connects the caller to the original callee using connect(), and then leaves the call with close()

Key Commands Used

  • answer() - Answer the incoming call
  • send_audio() - Verbal confirmation and prompts
  • connect() - Fallback to callee for scheduling
  • close() - Leave after connecting the caller to the scheduling team
  • disconnect() - End the call after a successful booking flow