File size: 3,858 Bytes
ffb5f88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""

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'