Spaces:
Running

axeronis commited on
Commit
2400a38
·
1 Parent(s): 178f42d

fix: booking data extraction - client name, topic, date parsing

Browse files
backend/app/api/webhook.py CHANGED
@@ -19,6 +19,41 @@ from app.core.logging import get_logger
19
 
20
  logger = get_logger(__name__)
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  router = APIRouter(prefix="/webhook", tags=["Webhooks"])
23
 
24
 
@@ -527,7 +562,7 @@ async def process_ai_response_background(
527
  start_time=start_time,
528
  end_time=end_time,
529
  status=AppointmentStatus.SCHEDULED,
530
- notes="Booked via AI Chat"
531
  )
532
  session.add(new_appt)
533
  await session.commit()
 
19
 
20
  logger = get_logger(__name__)
21
 
22
+
23
+ def _extract_booking_notes(lead, user_message: str, conversation_history) -> str:
24
+ """
25
+ Extract booking notes from conversation context.
26
+ Includes: client name, topic/reason for booking.
27
+ """
28
+ # 1. Get client name
29
+ client_name = lead.name if lead and lead.name else "Unknown"
30
+
31
+ # 2. Extract topic from recent user messages
32
+ topic_keywords = []
33
+ keywords_to_find = [
34
+ "автоматизация", "продажи", "консультация", "внедрение",
35
+ "аудит", "ai", "бот", "crm", "интеграция", "помощь",
36
+ "партнер", "цена", "стоимость", "демо", "обучение"
37
+ ]
38
+
39
+ # Check last 5 user messages for keywords
40
+ user_messages = [msg.content.lower() for msg in conversation_history[-10:] if msg.sender_type == "user"]
41
+ user_messages.append(user_message.lower())
42
+
43
+ for msg in user_messages:
44
+ for keyword in keywords_to_find:
45
+ if keyword in msg and keyword not in topic_keywords:
46
+ topic_keywords.append(keyword)
47
+ if len(topic_keywords) >= 3:
48
+ break
49
+
50
+ # 3. Build notes string
51
+ topic_str = ", ".join(topic_keywords) if topic_keywords else "общий вопрос"
52
+ notes = f"Клиент: {client_name} | Тема: {topic_str} | Booked via AI Chat"
53
+
54
+ return notes
55
+
56
+
57
  router = APIRouter(prefix="/webhook", tags=["Webhooks"])
58
 
59
 
 
562
  start_time=start_time,
563
  end_time=end_time,
564
  status=AppointmentStatus.SCHEDULED,
565
+ notes=_extract_booking_notes(lead, user_text, conversation_history)
566
  )
567
  session.add(new_appt)
568
  await session.commit()
backend/app/services/ai_memory_manager.py CHANGED
@@ -267,10 +267,18 @@ class AIMemoryManager:
267
  {banned_words_text}
268
 
269
  ═══════════════════════════════════════════════════════════════
270
- [📅 СИСТЕМА ЗАПИСИ И ЗАКАЗОВ]
271
  ═══════════════════════════════════════════════════════════════
272
- • Запись на встречу: Спроси дату и время, затем подтверди [BOOK_CONFIRMED: YYYY-MM-DD HH:MM]
273
- • Заказ товара: Предложи оформить заказ
 
 
 
 
 
 
 
 
274
 
275
  """
276
  # E. PRODUCT CATALOG
 
267
  {banned_words_text}
268
 
269
  ═══════════════════════════════════════════════════════════════
270
+ [📅 СИСТЕМА ЗАПИСИ]
271
  ═══════════════════════════════════════════════════════════════
272
+ Когда клиент хочет записаться:
273
+ 1. Уточни дату и время если не указаны
274
+ 2. КОНВЕРТИРУЙ относительные даты в YYYY-MM-DD:
275
+ - "завтра" = следующий день
276
+ - "послезавтра" = через 2 дня
277
+ - "в среду" = ближайшая среда
278
+ 3. Подтверди запись ТЕГОМ: [BOOK_CONFIRMED: YYYY-MM-DD HH:MM]
279
+
280
+ Пример: клиент пишет "завтра в 15:00" (сегодня 2025-12-20)
281
+ → Твой ответ включает: [BOOK_CONFIRMED: 2025-12-21 15:00]
282
 
283
  """
284
  # E. PRODUCT CATALOG