Spaces:
Sleeping
Sleeping
axeronis commited on
Commit ·
e9f9cca
1
Parent(s): b40dcb1
fix: welcome message save/load, save to history, filter ClientCard by selected fields
Browse files- backend/app/api/tenants.py +8 -0
- backend/app/api/webhook.py +30 -1
- frontend/components/ClientCard.tsx +62 -39
backend/app/api/tenants.py
CHANGED
|
@@ -43,6 +43,7 @@ class TenantResponse(BaseModel):
|
|
| 43 |
referred_by: Optional[Dict[str, str]] = None
|
| 44 |
ai_enabled: bool = True # Master AI toggle
|
| 45 |
company_name: Optional[str] = None # Alias for business_name for frontend
|
|
|
|
| 46 |
|
| 47 |
# Voice Settings
|
| 48 |
voice_id: Optional[str] = "professional_male"
|
|
@@ -64,6 +65,9 @@ class UpdateTenantRequest(BaseModel):
|
|
| 64 |
company_name: Optional[str] = None
|
| 65 |
notification_settings: Optional[Dict[str, Any]] = None
|
| 66 |
|
|
|
|
|
|
|
|
|
|
| 67 |
# Voice Settings
|
| 68 |
voice_id: Optional[str] = None
|
| 69 |
voice_enabled: Optional[bool] = None
|
|
@@ -304,6 +308,10 @@ async def update_my_tenant(
|
|
| 304 |
if request.notification_settings is not None:
|
| 305 |
tenant.notification_settings = request.notification_settings
|
| 306 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 307 |
# Voice Settings
|
| 308 |
if request.voice_id is not None:
|
| 309 |
tenant.voice_id = request.voice_id
|
|
|
|
| 43 |
referred_by: Optional[Dict[str, str]] = None
|
| 44 |
ai_enabled: bool = True # Master AI toggle
|
| 45 |
company_name: Optional[str] = None # Alias for business_name for frontend
|
| 46 |
+
telegram_welcome_message: Optional[str] = None # Telegram /start instant message
|
| 47 |
|
| 48 |
# Voice Settings
|
| 49 |
voice_id: Optional[str] = "professional_male"
|
|
|
|
| 65 |
company_name: Optional[str] = None
|
| 66 |
notification_settings: Optional[Dict[str, Any]] = None
|
| 67 |
|
| 68 |
+
# Telegram Welcome Message (instant /start response)
|
| 69 |
+
telegram_welcome_message: Optional[str] = None
|
| 70 |
+
|
| 71 |
# Voice Settings
|
| 72 |
voice_id: Optional[str] = None
|
| 73 |
voice_enabled: Optional[bool] = None
|
|
|
|
| 308 |
if request.notification_settings is not None:
|
| 309 |
tenant.notification_settings = request.notification_settings
|
| 310 |
|
| 311 |
+
# Telegram Welcome Message for /start command
|
| 312 |
+
if request.telegram_welcome_message is not None:
|
| 313 |
+
tenant.telegram_welcome_message = request.telegram_welcome_message
|
| 314 |
+
|
| 315 |
# Voice Settings
|
| 316 |
if request.voice_id is not None:
|
| 317 |
tenant.voice_id = request.voice_id
|
backend/app/api/webhook.py
CHANGED
|
@@ -635,7 +635,7 @@ async def telegram_webhook(
|
|
| 635 |
# Extract text (or caption from media) or transcribe voice
|
| 636 |
user_text = message.get("text") or message.get("caption") or ""
|
| 637 |
|
| 638 |
-
# 👋 HANDLE /start COMMAND - Send welcome message instantly
|
| 639 |
if user_text.strip().lower() == '/start':
|
| 640 |
from app.services.telegram_service import telegram_service
|
| 641 |
|
|
@@ -652,6 +652,22 @@ async def telegram_webhook(
|
|
| 652 |
else:
|
| 653 |
welcome_message = "Здравствуйте! Чем могу помочь?"
|
| 654 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 655 |
try:
|
| 656 |
await telegram_service.send_message(
|
| 657 |
token=tenant.telegram_bot_token,
|
|
@@ -663,6 +679,19 @@ async def telegram_webhook(
|
|
| 663 |
chat_id=chat_id,
|
| 664 |
tenant_id=str(tenant_id)
|
| 665 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 666 |
except Exception as e:
|
| 667 |
logger.error(f"❌ Failed to send welcome message: {e}")
|
| 668 |
|
|
|
|
| 635 |
# Extract text (or caption from media) or transcribe voice
|
| 636 |
user_text = message.get("text") or message.get("caption") or ""
|
| 637 |
|
| 638 |
+
# 👋 HANDLE /start COMMAND - Send welcome message instantly AND save to history
|
| 639 |
if user_text.strip().lower() == '/start':
|
| 640 |
from app.services.telegram_service import telegram_service
|
| 641 |
|
|
|
|
| 652 |
else:
|
| 653 |
welcome_message = "Здравствуйте! Чем могу помочь?"
|
| 654 |
|
| 655 |
+
# Create or get lead and conversation BEFORE sending (so we can save to history)
|
| 656 |
+
lead = await get_or_create_lead(
|
| 657 |
+
session=session,
|
| 658 |
+
tenant_id=tenant_id,
|
| 659 |
+
telegram_id=telegram_id,
|
| 660 |
+
telegram_user=telegram_user
|
| 661 |
+
)
|
| 662 |
+
|
| 663 |
+
conversation = await get_or_create_conversation(
|
| 664 |
+
session=session,
|
| 665 |
+
tenant_id=tenant_id,
|
| 666 |
+
lead_id=lead.id,
|
| 667 |
+
channel="telegram"
|
| 668 |
+
)
|
| 669 |
+
|
| 670 |
+
# Send welcome message
|
| 671 |
try:
|
| 672 |
await telegram_service.send_message(
|
| 673 |
token=tenant.telegram_bot_token,
|
|
|
|
| 679 |
chat_id=chat_id,
|
| 680 |
tenant_id=str(tenant_id)
|
| 681 |
)
|
| 682 |
+
|
| 683 |
+
# SAVE to conversation history so AI knows what was said
|
| 684 |
+
from app.models.lead import Message
|
| 685 |
+
welcome_msg = Message(
|
| 686 |
+
conversation_id=conversation.id,
|
| 687 |
+
sender_type="ai",
|
| 688 |
+
content=welcome_message
|
| 689 |
+
)
|
| 690 |
+
session.add(welcome_msg)
|
| 691 |
+
await session.commit()
|
| 692 |
+
|
| 693 |
+
logger.info("📝 Welcome message saved to conversation history")
|
| 694 |
+
|
| 695 |
except Exception as e:
|
| 696 |
logger.error(f"❌ Failed to send welcome message: {e}")
|
| 697 |
|
frontend/components/ClientCard.tsx
CHANGED
|
@@ -30,16 +30,29 @@ export function ClientCard({ lead, onTakeover, isAnalyzing, onRefresh, conversat
|
|
| 30 |
const [expandedSection, setExpandedSection] = useState<string | null>(null)
|
| 31 |
const [isReanalyzing, setIsReanalyzing] = useState(false)
|
| 32 |
const [currentStage, setCurrentStage] = useState<any>(null)
|
|
|
|
| 33 |
|
| 34 |
useEffect(() => {
|
| 35 |
-
|
| 36 |
fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/v1/playbook/stages`, {
|
| 37 |
headers: { 'Authorization': `Bearer ${localStorage.getItem('auth_token')}` }
|
| 38 |
})
|
| 39 |
.then(r => r.ok ? r.json() : [])
|
| 40 |
.then(stages => {
|
| 41 |
-
|
| 42 |
-
if (
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
})
|
| 44 |
.catch(console.error)
|
| 45 |
}, [lead.current_stage_id])
|
|
@@ -218,43 +231,53 @@ export function ClientCard({ lead, onTakeover, isAnalyzing, onRefresh, conversat
|
|
| 218 |
</div>
|
| 219 |
|
| 220 |
<div className="grid grid-cols-2 gap-2">
|
| 221 |
-
{Object.entries((lead as any).qualification_data)
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
</div>
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
isNotFound ? "text-slate-600 italic" : "text-slate-200"
|
| 252 |
-
)}>
|
| 253 |
-
{isNotFound ? '...' : String(value)}
|
| 254 |
-
</span>
|
| 255 |
-
</div>
|
| 256 |
-
)
|
| 257 |
-
})}
|
| 258 |
</div>
|
| 259 |
</div>
|
| 260 |
)}
|
|
|
|
| 30 |
const [expandedSection, setExpandedSection] = useState<string | null>(null)
|
| 31 |
const [isReanalyzing, setIsReanalyzing] = useState(false)
|
| 32 |
const [currentStage, setCurrentStage] = useState<any>(null)
|
| 33 |
+
const [selectedFields, setSelectedFields] = useState<string[]>([])
|
| 34 |
|
| 35 |
useEffect(() => {
|
| 36 |
+
// Fetch all stages and collect exit_criteria (selected fields) for filtering
|
| 37 |
fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/v1/playbook/stages`, {
|
| 38 |
headers: { 'Authorization': `Bearer ${localStorage.getItem('auth_token')}` }
|
| 39 |
})
|
| 40 |
.then(r => r.ok ? r.json() : [])
|
| 41 |
.then(stages => {
|
| 42 |
+
// Find current stage if lead has one
|
| 43 |
+
if (lead.current_stage_id) {
|
| 44 |
+
const stage = stages.find((s: any) => s.id === lead.current_stage_id)
|
| 45 |
+
if (stage) setCurrentStage(stage)
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
// Collect ALL exit_criteria from ALL stages (these are the "selected" fields)
|
| 49 |
+
const allExitCriteria = new Set<string>()
|
| 50 |
+
stages.forEach((s: any) => {
|
| 51 |
+
if (Array.isArray(s.exit_criteria)) {
|
| 52 |
+
s.exit_criteria.forEach((field: string) => allExitCriteria.add(field))
|
| 53 |
+
}
|
| 54 |
+
})
|
| 55 |
+
setSelectedFields(Array.from(allExitCriteria))
|
| 56 |
})
|
| 57 |
.catch(console.error)
|
| 58 |
}, [lead.current_stage_id])
|
|
|
|
| 231 |
</div>
|
| 232 |
|
| 233 |
<div className="grid grid-cols-2 gap-2">
|
| 234 |
+
{Object.entries((lead as any).qualification_data)
|
| 235 |
+
.filter(([field]) => {
|
| 236 |
+
// Show only selected fields (those in exit_criteria)
|
| 237 |
+
if (selectedFields.length === 0) return true // Show all if no filter loaded yet
|
| 238 |
+
return selectedFields.some(sf =>
|
| 239 |
+
sf.toLowerCase() === field.toLowerCase() ||
|
| 240 |
+
field.toLowerCase().includes(sf.toLowerCase()) ||
|
| 241 |
+
sf.toLowerCase().includes(field.toLowerCase())
|
| 242 |
+
)
|
| 243 |
+
})
|
| 244 |
+
.map(([field, value]: [string, any]) => {
|
| 245 |
+
const isNotFound = !value || value.toLowerCase?.().includes('не выявлено') || value.toLowerCase?.().includes('не указано') || value.toLowerCase?.().includes('нет данных')
|
| 246 |
+
|
| 247 |
+
// Dynamic Icon selection
|
| 248 |
+
let Icon = Target
|
| 249 |
+
const f = field.toLowerCase()
|
| 250 |
+
if (f.includes('имя')) Icon = UserIcon
|
| 251 |
+
if (f.includes('тел') || f.includes('контакт') || f.includes('номер')) Icon = Phone
|
| 252 |
+
if (f.includes('бюджет') || f.includes('деньги') || f.includes('цена')) Icon = DollarSign
|
| 253 |
+
if (f.includes('срок') || f.includes('когда') || f.includes('даты')) Icon = Clock
|
| 254 |
+
if (f.includes('почта') || f.includes('email')) Icon = Mail
|
| 255 |
+
if (f.includes('бизнес') || f.includes('компания')) Icon = Briefcase
|
| 256 |
+
if (f.includes('город') || f.includes('адрес')) Icon = MapPin
|
| 257 |
+
|
| 258 |
+
return (
|
| 259 |
+
<div
|
| 260 |
+
key={field}
|
| 261 |
+
className={cn(
|
| 262 |
+
"p-2.5 rounded-xl border transition-all duration-300 flex flex-col gap-1",
|
| 263 |
+
isNotFound
|
| 264 |
+
? "bg-white/[0.02] border-white/5 opacity-60 hover:opacity-100"
|
| 265 |
+
: "bg-indigo-500/[0.03] border-indigo-500/10 hover:border-indigo-500/30 shadow-sm"
|
| 266 |
+
)}
|
| 267 |
+
>
|
| 268 |
+
<div className="flex items-center gap-1.5 overflow-hidden">
|
| 269 |
+
<Icon className={cn("w-3 h-3 shrink-0", isNotFound ? "text-slate-500" : "text-indigo-400")} />
|
| 270 |
+
<span className="text-[10px] text-slate-500 font-medium truncate">{field}</span>
|
| 271 |
+
</div>
|
| 272 |
+
<span className={cn(
|
| 273 |
+
"text-[11px] font-semibold truncate leading-tight",
|
| 274 |
+
isNotFound ? "text-slate-600 italic" : "text-slate-200"
|
| 275 |
+
)}>
|
| 276 |
+
{isNotFound ? '...' : String(value)}
|
| 277 |
+
</span>
|
| 278 |
</div>
|
| 279 |
+
)
|
| 280 |
+
})}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
</div>
|
| 282 |
</div>
|
| 283 |
)}
|