| 1 | @client.on(events.INCOMING_CALL) |
| 2 | async 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() |