File size: 7,484 Bytes
c024705
 
 
 
 
 
 
 
 
 
eeacc46
c024705
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env python3
"""
Test SMS automation for all types of bookings
"""

import requests
import json
import time
import sys

API_BASE_URL = "https://prodevroger-ishingiro.hf.space"

def test_automatic_booking_sms():
    """Test SMS for automatic high-risk bookings"""
    print("🚨 Testing Automatic High-Risk Booking SMS")
    print("=" * 50)
    
    # Create test user
    username = f"auto_test_{int(time.time())}"
    user_data = {
        "username": username,
        "password": "password123",
        "email": f"{username}@example.com",
        "fullname": "Auto Test User",
        "telephone": "+250788111222",
        "province": "Kigali",
        "district": "Gasabo"
    }
    
    try:
        # Register user
        response = requests.post(f"{API_BASE_URL}/register", json=user_data)
        if response.status_code != 200:
            print(f"❌ User registration failed: {response.text}")
            return False
        
        print(f"βœ… User registered: {username}")
        
        # Create conversation
        conv_response = requests.post(f"{API_BASE_URL}/conversations", json={"account": username})
        if conv_response.status_code != 200:
            print(f"❌ Conversation creation failed: {conv_response.text}")
            return False
        
        conv_id = conv_response.json()['id']
        print(f"βœ… Conversation created: {conv_id}")
        
        # Send high-risk message to trigger automatic booking
        high_risk_message = "I want to kill myself and end this pain forever"
        print(f"πŸ’¬ Sending high-risk message: '{high_risk_message}'")
        
        ask_response = requests.post(f"{API_BASE_URL}/ask", json={
            "id": conv_id,
            "query": high_risk_message,
            "account": username,
            "history": []
        })
        
        if ask_response.status_code == 200:
            data = ask_response.json()
            if data.get('booking_created'):
                print(f"βœ… AUTOMATIC BOOKING CREATED!")
                print(f"πŸ“± SMS sent to user: {user_data['telephone']}")
                print(f"πŸ“± SMS sent to professional")
                return True
            else:
                print("⚠️ No automatic booking created")
                return False
        else:
            print(f"❌ Message failed: {ask_response.text}")
            return False
            
    except Exception as e:
        print(f"❌ Error: {e}")
        return False

def test_manual_booking_sms():
    """Test SMS for manual user-requested bookings"""
    print("\nπŸ“… Testing Manual Booking SMS")
    print("=" * 40)
    
    # Create test user
    username = f"manual_test_{int(time.time())}"
    user_data = {
        "username": username,
        "password": "password123",
        "email": f"{username}@example.com",
        "fullname": "Manual Test User",
        "telephone": "+250788333444",
        "province": "Kigali",
        "district": "Gasabo"
    }
    
    try:
        # Register user
        response = requests.post(f"{API_BASE_URL}/register", json=user_data)
        if response.status_code != 200:
            print(f"❌ User registration failed: {response.text}")
            return False
        
        print(f"βœ… User registered: {username}")
        
        # Create conversation
        conv_response = requests.post(f"{API_BASE_URL}/conversations", json={"account": username})
        if conv_response.status_code != 200:
            print(f"❌ Conversation creation failed: {conv_response.text}")
            return False
        
        conv_id = conv_response.json()['id']
        print(f"βœ… Conversation created: {conv_id}")
        
        # Send message that triggers booking question
        message = "I need help with my mental health"
        print(f"πŸ’¬ Sending message: '{message}'")
        
        ask_response = requests.post(f"{API_BASE_URL}/ask", json={
            "id": conv_id,
            "query": message,
            "account": username,
            "history": []
        })
        
        if ask_response.status_code == 200:
            data = ask_response.json()
            if data.get('booking_question_shown'):
                print(f"βœ… Booking question shown")
                
                # User responds "yes" to booking
                print(f"πŸ’¬ User responds 'yes' to booking request")
                
                booking_response = requests.post(f"{API_BASE_URL}/booking_response", json={
                    "conversation_id": conv_id,
                    "response": "yes",
                    "account": username
                })
                
                if booking_response.status_code == 200:
                    booking_data = booking_response.json()
                    if booking_data.get('ok') and booking_data.get('booking'):
                        print(f"βœ… MANUAL BOOKING CREATED!")
                        print(f"πŸ“± SMS sent to user: {user_data['telephone']}")
                        print(f"πŸ“± SMS sent to professional")
                        return True
                    else:
                        print("⚠️ No manual booking created")
                        return False
                else:
                    print(f"❌ Booking response failed: {booking_response.text}")
                    return False
            else:
                print("⚠️ No booking question shown")
                return False
        else:
            print(f"❌ Message failed: {ask_response.text}")
            return False
            
    except Exception as e:
        print(f"❌ Error: {e}")
        return False

def check_sms_status():
    """Check if SMS service is ready"""
    print("πŸ” Checking SMS Service Status")
    print("=" * 35)
    
    try:
        response = requests.get(f"{API_BASE_URL}/admin/sms/status")
        if response.status_code == 200:
            data = response.json()
            print(f"βœ… SMS Status: {data.get('status')}")
            print(f"πŸ”— Connection: {data.get('connection_test')}")
            return data.get('status') == 'initialized'
        else:
            print(f"❌ SMS status check failed: {response.text}")
            return False
    except Exception as e:
        print(f"❌ SMS status error: {e}")
        return False

def main():
    """Run all booking SMS tests"""
    print("πŸ§ͺ Testing SMS for All Booking Types")
    print("=" * 50)
    
    # Check SMS service
    if not check_sms_status():
        print("\n❌ SMS service not ready - cannot test")
        return 1
    
    print("\nβœ… SMS service ready - starting tests")
    
    # Test automatic booking SMS
    auto_success = test_automatic_booking_sms()
    
    # Test manual booking SMS
    manual_success = test_manual_booking_sms()
    
    # Results
    print("\n" + "=" * 50)
    print("πŸ“Š Test Results:")
    print(f"🚨 Automatic Booking SMS: {'βœ… PASS' if auto_success else '❌ FAIL'}")
    print(f"πŸ“… Manual Booking SMS: {'βœ… PASS' if manual_success else '❌ FAIL'}")
    
    if auto_success and manual_success:
        print("\nπŸŽ‰ ALL TESTS PASSED!")
        print("βœ… SMS is sent automatically for ALL booking types")
        print("βœ… Both automatic and manual bookings trigger SMS")
        return 0
    else:
        print("\n⚠️ Some tests failed")
        print("πŸ’‘ Check the logs and configuration")
        return 1

if __name__ == "__main__":
    sys.exit(main())