ChatbotRAG / scenario_handlers /exit_intent_rescue.py
minhvtt's picture
Upload 36 files
ffb5f88 verified
raw
history blame
3.86 kB
"""
Exit Intent Rescue Scenario Handler
Quick flow to retain users about to leave
"""
from typing import Dict, Any
from .base_handler import BaseScenarioHandler
class ExitIntentRescueHandler(BaseScenarioHandler):
"""
Handle exit intent rescue flow
Very short 2-step flow to capture leaving users
Steps:
1. Offer discount coupon
2. Collect email to send coupon
"""
def start(self, initial_data: Dict = None) -> Dict[str, Any]:
"""Start exit intent flow with urgency"""
return {
"message": "Khoan đã! 😭 Trước khi bạn rời đi…\n\nChúng mình sắp có mã giảm **5%** cho lần mua đầu tiên. Bạn muốn nhận không?",
"new_state": {
"active_scenario": "exit_intent_rescue",
"scenario_step": 1,
"scenario_data": initial_data or {}
}
}
def next_step(self, current_step: int, user_input: str, scenario_data: Dict) -> Dict[str, Any]:
"""Process and try to capture email quickly"""
# STEP 1: Want coupon?
if current_step == 1:
choice = self._detect_yes_no(user_input)
if choice == 'yes':
return {
"message": "Tuyệt vời! 🎉 Cho mình xin email để gửi mã giảm giá nhé?",
"new_state": {
"active_scenario": "exit_intent_rescue",
"scenario_step": 2,
"scenario_data": scenario_data
},
"scenario_active": True
}
else:
return {
"message": "Okie! Hẹn gặp lại bạn nhé 👋",
"new_state": None,
"scenario_active": False,
"end_scenario": True
}
# STEP 2: Collect email and send coupon
elif current_step == 2:
email = user_input.strip()
if not self._validate_email(email):
return {
"message": "Email này có vẻ không đúng. Bạn nhập lại nhanh giúp mình nhé?",
"new_state": None,
"scenario_active": True
}
scenario_data['email'] = email
# Save lead with high priority (exit intent)
try:
self.lead_storage.save_lead(
event_name="Exit Intent Coupon",
email=email,
interests={"exit_intent": True, "discount_5_percent": True},
session_id=scenario_data.get('session_id')
)
print(f"🎯 Exit intent lead saved: {email}")
except Exception as e:
print(f"⚠️ Error saving lead: {e}")
return {
"message": "Đã gửi mã **FIRST5** vào email rồi nha! ✨\n\nDùng ngay hôm nay nhé, mã chỉ có hiệu lực 24h thôi!",
"new_state": None,
"scenario_active": False,
"end_scenario": True,
"action": "send_coupon_email"
}
# Fallback
return {
"message": "Hẹn gặp lại bạn! 👋",
"new_state": None,
"scenario_active": False,
"end_scenario": True
}
def _detect_yes_no(self, user_input: str) -> str:
"""Detect yes/no quickly"""
input_lower = user_input.lower()
return 'yes' if any(k in input_lower for k in ['có', 'yes', 'ok', 'được', 'ừ', 'muốn']) else 'no'