Spaces:
Running

axeronis commited on
Commit
a843d00
·
1 Parent(s): 8a521d3

fix: booking uses AI-extracted name and niche, removed hardcoded keywords

Browse files
backend/app/api/webhook.py CHANGED
@@ -52,11 +52,25 @@ def _extract_booking_info_from_conversation(conversation_history, lead, user_tex
52
  """
53
  Extract booking info from conversation history.
54
  Returns dict with: name, phone, topic for comprehensive notes.
 
55
  """
56
  import re
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  info = {
59
- "name": lead.name if lead and lead.name else "Клиент",
60
  "phone": lead.phone if lead and lead.phone else None,
61
  "topic": None
62
  }
@@ -64,13 +78,6 @@ def _extract_booking_info_from_conversation(conversation_history, lead, user_tex
64
  # Get user messages
65
  user_messages = [msg.content for msg in conversation_history if msg.sender_type == "user"]
66
 
67
- # Patterns for name extraction
68
- name_patterns = [
69
- r'(?:меня зовут|я|мое имя|зовут меня)\s+([А-ЯЁа-яё]{2,})',
70
- r'^([А-ЯЁ][а-яё]{2,})\s+[\d\+]',
71
- r'^([A-Z][a-z]{2,})\s+[\d\+]',
72
- ]
73
-
74
  # Patterns for phone extraction
75
  phone_patterns = [
76
  r'[\+]?[0-9]{10,15}', # International format
@@ -79,47 +86,40 @@ def _extract_booking_info_from_conversation(conversation_history, lead, user_tex
79
  r'\d{3}[\s\-]?\d{3}[\s\-]?\d{4}', # 999-123-4567
80
  ]
81
 
82
- # Extract from messages
83
- for msg in reversed(user_messages[-15:]):
84
- # Extract name
85
- if info["name"] == "Клиент" or info["name"] == lead.name:
86
- for pattern in name_patterns:
87
- match = re.search(pattern, msg, re.IGNORECASE)
88
- if match:
89
- extracted = match.group(1).strip()
90
- if len(extracted) >= 2 and extracted[0].isalpha():
91
- info["name"] = extracted.capitalize()
92
- break
93
-
94
- # Extract phone
95
- if not info["phone"]:
96
  for pattern in phone_patterns:
97
  match = re.search(pattern, msg)
98
  if match:
99
  info["phone"] = match.group(0).strip()
100
  break
 
 
101
 
102
- # Extract topic keywords
103
- topic_keywords = []
104
- keywords_to_find = [
105
- "автоматизация", "продажи", "консультация", "внедрение",
106
- "аудит", "ai", "бот", "crm", "интеграция", "менеджер",
107
- "нагрузка", "тариф", "цена"
108
- ]
109
 
110
- for msg in user_messages[-10:]:
111
- msg_lower = msg.lower()
112
- for keyword in keywords_to_find:
113
- if keyword in msg_lower and keyword not in topic_keywords:
114
- topic_keywords.append(keyword)
115
- if len(topic_keywords) >= 2:
116
- break
 
 
 
 
 
 
117
 
118
- info["topic"] = ", ".join(topic_keywords) if topic_keywords else "Консультация"
119
 
120
  return info
121
 
122
 
 
123
  async def _find_available_slots(
124
  session,
125
  tenant_id: UUID,
 
52
  """
53
  Extract booking info from conversation history.
54
  Returns dict with: name, phone, topic for comprehensive notes.
55
+ Universal for any business type.
56
  """
57
  import re
58
 
59
+ # Get best available name: AI-extracted > lead.name > "Клиент"
60
+ best_name = "Клиент"
61
+
62
+ # 1. Try AI-extracted name from summary (most reliable)
63
+ if lead and lead.ai_summary_json:
64
+ ai_name = lead.ai_summary_json.get('client_name')
65
+ if ai_name and isinstance(ai_name, str) and len(ai_name) >= 2:
66
+ best_name = ai_name
67
+
68
+ # 2. Fallback to lead.name if no AI name
69
+ if best_name == "Клиент" and lead and lead.name:
70
+ best_name = lead.name
71
+
72
  info = {
73
+ "name": best_name,
74
  "phone": lead.phone if lead and lead.phone else None,
75
  "topic": None
76
  }
 
78
  # Get user messages
79
  user_messages = [msg.content for msg in conversation_history if msg.sender_type == "user"]
80
 
 
 
 
 
 
 
 
81
  # Patterns for phone extraction
82
  phone_patterns = [
83
  r'[\+]?[0-9]{10,15}', # International format
 
86
  r'\d{3}[\s\-]?\d{3}[\s\-]?\d{4}', # 999-123-4567
87
  ]
88
 
89
+ # Extract phone if not already have
90
+ if not info["phone"]:
91
+ for msg in reversed(user_messages[-15:]):
 
 
 
 
 
 
 
 
 
 
 
92
  for pattern in phone_patterns:
93
  match = re.search(pattern, msg)
94
  if match:
95
  info["phone"] = match.group(0).strip()
96
  break
97
+ if info["phone"]:
98
+ break
99
 
100
+ # Extract topic from AI summary (niche or intent) - UNIVERSAL for any business
101
+ topic_parts = []
 
 
 
 
 
102
 
103
+ if lead and lead.ai_summary_json:
104
+ # Try niche first (client's business area)
105
+ niche = lead.ai_summary_json.get('niche')
106
+ if niche and isinstance(niche, str) and len(niche) > 2:
107
+ topic_parts.append(niche)
108
+
109
+ # Then intent
110
+ intent = lead.ai_summary_json.get('intent', '')
111
+ if intent and len(intent) > 3 and len(intent) < 50:
112
+ # Clean up generic phrases
113
+ intent_clean = intent.replace('Не выявлен', '').replace('Анализ в процессе', '').strip()
114
+ if intent_clean and intent_clean not in topic_parts:
115
+ topic_parts.append(intent_clean[:40]) # Limit length
116
 
117
+ info["topic"] = " | ".join(topic_parts) if topic_parts else "Консультация"
118
 
119
  return info
120
 
121
 
122
+
123
  async def _find_available_slots(
124
  session,
125
  tenant_id: UUID,
frontend/components/ClientCard.tsx CHANGED
@@ -96,7 +96,7 @@ export function ClientCard({ lead, onTakeover, isAnalyzing, onRefresh, conversat
96
  budget: cleanValue(summary.budget || lead.budget, t('clientCard.notDefined')),
97
  authority: cleanValue(summary.authority || lead.authority, t('common.unknown')),
98
  timeline: cleanValue(summary.timeline || lead.timeline, t('clientCard.notSpecified')),
99
- industry: cleanValue(summary.industry || (lead as any).industry || summary.business_type, t('clientCard.notSpecified')),
100
  pain_points: Array.isArray(summary.pain_points) ? summary.pain_points : [],
101
  objections: Array.isArray(summary.objections) ? summary.objections : [],
102
  psychotype: cleanValue(summary.psychotype || lead.psychotype, 'unknown'),
 
96
  budget: cleanValue(summary.budget || lead.budget, t('clientCard.notDefined')),
97
  authority: cleanValue(summary.authority || lead.authority, t('common.unknown')),
98
  timeline: cleanValue(summary.timeline || lead.timeline, t('clientCard.notSpecified')),
99
+ industry: cleanValue(summary.niche || summary.industry || (lead as any).industry || summary.business_type, t('clientCard.notSpecified')),
100
  pain_points: Array.isArray(summary.pain_points) ? summary.pain_points : [],
101
  objections: Array.isArray(summary.objections) ? summary.objections : [],
102
  psychotype: cleanValue(summary.psychotype || lead.psychotype, 'unknown'),