Spaces:
Running

axeronis commited on
Commit
fda4900
·
1 Parent(s): 06f2947

fix: SyntaxError - fixed continue not in loop, proper if/else structure

Browse files
Files changed (1) hide show
  1. backend/app/api/webhook.py +38 -43
backend/app/api/webhook.py CHANGED
@@ -693,51 +693,46 @@ async def process_ai_response_background(
693
  ai_text = ai_text.replace(booking_match.group(0), "").strip()
694
  ai_text += "\n\n⚠️ К сожалению, это время уже занято. Пожалуйста, выберите другое время."
695
  ai_response["text"] = ai_text
696
- continue # Skip creating this appointment
697
-
698
- # Create new appointment with comprehensive notes
699
- booking_info = _extract_booking_info_from_conversation(conversation_history, lead, user_text)
700
-
701
- # Build informative notes
702
- notes_parts = [booking_info["name"]]
703
- if booking_info["phone"]:
704
- notes_parts.append(f"📞 {booking_info['phone']}")
705
- notes_parts.append(f"Тема: {booking_info['topic']}")
706
- notes = " | ".join(notes_parts)
707
-
708
- new_appt = Appointment(
709
- tenant_id=tenant_id,
710
- lead_id=lead_id,
711
- service_id=default_srv.id,
712
- start_time=start_time,
713
- end_time=end_time,
714
- status=AppointmentStatus.SCHEDULED,
715
- notes=notes
716
- )
717
- session.add(new_appt)
718
- await session.commit()
719
-
720
- logger.info(f"📅 AI Booked Appointment: {date_str} for Lead {lead_id}")
721
 
722
- # 🔔 NOTIFY ADMIN
723
- try:
724
- from app.services.notification import notification_service
725
- # We need to load relationship or ensure it's available.
726
- # new_appt might not have 'lead' loaded yet.
727
- # We pass the object, service handles it. Ideally refresh or pass ID.
728
- # For speed, let's refresh or just pass what we have.
729
- new_appt.lead = lead # Manually attach to avoid DB refetch
730
- new_appt.service = default_srv
731
- await notification_service.notify_new_appointment(tenant, new_appt)
732
- except Exception as ns_err:
733
- logger.error(f"⚠️ Notification failed: {ns_err}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
734
 
735
- # Remove tag from text
736
- ai_text = ai_text.replace(booking_match.group(0), "").strip()
737
- ai_text += "\n\n✅ (Система: Запись создана успешно!)"
738
-
739
- # Update response text in object
740
- ai_response["text"] = ai_text
741
 
742
  except Exception as e:
743
  logger.error(f"❌ Failed to process AI booking tag: {e}")
 
693
  ai_text = ai_text.replace(booking_match.group(0), "").strip()
694
  ai_text += "\n\n⚠️ К сожалению, это время уже занято. Пожалуйста, выберите другое время."
695
  ai_response["text"] = ai_text
696
+ else:
697
+ # Create new appointment with comprehensive notes
698
+ booking_info = _extract_booking_info_from_conversation(conversation_history, lead, user_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
699
 
700
+ # Build informative notes
701
+ notes_parts = [booking_info["name"]]
702
+ if booking_info["phone"]:
703
+ notes_parts.append(f"📞 {booking_info['phone']}")
704
+ notes_parts.append(f"Тема: {booking_info['topic']}")
705
+ notes = " | ".join(notes_parts)
706
+
707
+ new_appt = Appointment(
708
+ tenant_id=tenant_id,
709
+ lead_id=lead_id,
710
+ service_id=default_srv.id,
711
+ start_time=start_time,
712
+ end_time=end_time,
713
+ status=AppointmentStatus.SCHEDULED,
714
+ notes=notes
715
+ )
716
+ session.add(new_appt)
717
+ await session.commit()
718
+
719
+ logger.info(f"📅 AI Booked Appointment: {date_str} for Lead {lead_id}")
720
+
721
+ # 🔔 NOTIFY ADMIN
722
+ try:
723
+ from app.services.notification import notification_service
724
+ new_appt.lead = lead
725
+ new_appt.service = default_srv
726
+ await notification_service.notify_new_appointment(tenant, new_appt)
727
+ except Exception as ns_err:
728
+ logger.error(f"⚠️ Notification failed: {ns_err}")
729
 
730
+ # Remove tag from text
731
+ ai_text = ai_text.replace(booking_match.group(0), "").strip()
732
+ ai_text += "\n\n✅ (Система: Запись создана успешно!)"
733
+
734
+ # Update response text in object
735
+ ai_response["text"] = ai_text
736
 
737
  except Exception as e:
738
  logger.error(f"❌ Failed to process AI booking tag: {e}")