File size: 7,913 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
218
219
220
221
222
223
#!/usr/bin/env python3
"""
Test script for SMS integration with AIMHSA
Tests the HDEV SMS API integration and booking notifications
"""

import requests
import json
import time
import sys

# Configuration
API_BASE_URL = "https://prodevroger-ishingiro.hf.space"
SMS_API_ID = "HDEV-23fb1b59-aec0-4aef-a351-bfc1c3aa3c52-ID"
SMS_API_KEY = "HDEV-6e36c286-19bb-4b45-838e-8b5cd0240857-KEY"

def test_sms_status():
    """Test SMS service status"""
    print("πŸ” Testing SMS service status...")
    
    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"πŸ“± API ID: {data.get('api_id')}")
            print(f"πŸ”‘ API Key: {data.get('api_key_masked')}")
            print(f"πŸ”— Connection Test: {data.get('connection_test')}")
            print(f"πŸ’¬ Message: {data.get('message')}")
            return True
        else:
            print(f"❌ SMS status check failed: {response.status_code}")
            print(f"Response: {response.text}")
            return False
            
    except Exception as e:
        print(f"❌ Error testing SMS status: {e}")
        return False

def test_sms_send():
    """Test sending SMS message"""
    print("\nπŸ“€ Testing SMS send functionality...")
    
    # Get test phone number from user
    test_phone = input("Enter test phone number (Rwanda format, e.g., +250788123456): ").strip()
    if not test_phone:
        test_phone = "+250000000000"  # Dummy number for testing
    
    test_message = "AIMHSA SMS Integration Test - Service is working correctly! πŸŽ‰"
    
    try:
        response = requests.post(f"{API_BASE_URL}/admin/sms/test", json={
            "phone": test_phone,
            "message": test_message
        })
        
        if response.status_code == 200:
            data = response.json()
            print(f"βœ… SMS Test Result: {data.get('success')}")
            print(f"πŸ“± Phone: {data.get('result', {}).get('phone', 'N/A')}")
            print(f"πŸ’¬ Message: {data.get('message')}")
            
            if data.get('success'):
                print("πŸŽ‰ SMS sent successfully!")
            else:
                print("⚠️ SMS sending failed - check API credentials and phone number format")
            
            return data.get('success', False)
        else:
            print(f"❌ SMS test failed: {response.status_code}")
            print(f"Response: {response.text}")
            return False
            
    except Exception as e:
        print(f"❌ Error testing SMS send: {e}")
        return False

def test_user_registration():
    """Test user registration with phone number"""
    print("\nπŸ‘€ Testing user registration with phone number...")
    
    test_username = f"testuser_{int(time.time())}"
    test_phone = input("Enter phone number for test user (e.g., +250788123456): ").strip()
    if not test_phone:
        test_phone = "+250788123456"
    
    try:
        # Register user
        response = requests.post(f"{API_BASE_URL}/register", json={
            "username": test_username,
            "password": "password123",
            "email": f"{test_username}@example.com",
            "fullname": "Test User",
            "telephone": test_phone,
            "province": "Kigali",
            "district": "Gasabo"
        })
        
        if response.status_code == 200:
            data = response.json()
            print(f"βœ… User registered: {data.get('ok')}")
            print(f"πŸ‘€ Username: {test_username}")
            print(f"πŸ“± Phone: {test_phone}")
            return test_username
        else:
            print(f"❌ User registration failed: {response.status_code}")
            print(f"Response: {response.text}")
            return None
            
    except Exception as e:
        print(f"❌ Error registering user: {e}")
        return None

def test_booking_sms():
    """Test booking SMS notification"""
    print("\nπŸ“… Testing booking SMS notification...")
    
    # First, register a test user
    test_username = test_user_registration()
    if not test_username:
        print("❌ Cannot test booking SMS without user registration")
        return False
    
    # Create a test conversation and trigger risk assessment
    print("πŸ’¬ Creating test conversation...")
    
    try:
        # Create conversation
        conv_response = requests.post(f"{API_BASE_URL}/conversations", json={
            "account": test_username
        })
        
        if conv_response.status_code != 200:
            print(f"❌ Failed to create conversation: {conv_response.status_code}")
            return False
        
        conv_data = conv_response.json()
        conv_id = conv_data.get('id')
        print(f"βœ… Conversation created: {conv_id}")
        
        # Send a high-risk message to trigger booking
        print("🚨 Sending high-risk message to trigger booking...")
        
        risk_message = "I feel hopeless and want to end it all. I can't take this pain anymore."
        
        ask_response = requests.post(f"{API_BASE_URL}/ask", json={
            "id": conv_id,
            "query": risk_message,
            "account": test_username,
            "history": []
        })
        
        if ask_response.status_code == 200:
            ask_data = ask_response.json()
            print(f"βœ… Risk assessment completed")
            print(f"🎯 Risk level: {ask_data.get('risk_level', 'unknown')}")
            
            if ask_data.get('booking_created'):
                print("πŸŽ‰ Automated booking created!")
                print(f"πŸ“‹ Booking ID: {ask_data.get('booking_id')}")
                print(f"πŸ‘¨β€βš•οΈ Professional: {ask_data.get('professional_name')}")
                print(f"⏰ Session Type: {ask_data.get('session_type')}")
                
                # Check if SMS was sent
                print("πŸ“± SMS notifications should have been sent automatically")
                return True
            else:
                print("⚠️ No booking was created - risk level may not be high enough")
                return False
        else:
            print(f"❌ Failed to send message: {ask_response.status_code}")
            print(f"Response: {ask_response.text}")
            return False
            
    except Exception as e:
        print(f"❌ Error testing booking SMS: {e}")
        return False

def main():
    """Run all SMS integration tests"""
    print("πŸš€ AIMHSA SMS Integration Test Suite")
    print("=" * 50)
    
    # Test 1: SMS Service Status
    status_ok = test_sms_status()
    
    # Test 2: SMS Send Test
    if status_ok:
        sms_ok = test_sms_send()
    else:
        print("⚠️ Skipping SMS send test due to status check failure")
        sms_ok = False
    
    # Test 3: User Registration with Phone
    user_ok = test_user_registration()
    
    # Test 4: Booking SMS Notification
    if user_ok and sms_ok:
        booking_ok = test_booking_sms()
    else:
        print("⚠️ Skipping booking SMS test due to previous failures")
        booking_ok = False
    
    # Summary
    print("\n" + "=" * 50)
    print("πŸ“Š Test Results Summary:")
    print(f"πŸ” SMS Status: {'βœ… PASS' if status_ok else '❌ FAIL'}")
    print(f"πŸ“€ SMS Send: {'βœ… PASS' if sms_ok else '❌ FAIL'}")
    print(f"πŸ‘€ User Registration: {'βœ… PASS' if user_ok else '❌ FAIL'}")
    print(f"πŸ“… Booking SMS: {'βœ… PASS' if booking_ok else '❌ FAIL'}")
    
    if all([status_ok, sms_ok, user_ok, booking_ok]):
        print("\nπŸŽ‰ All tests passed! SMS integration is working correctly.")
        return 0
    else:
        print("\n⚠️ Some tests failed. Check the output above for details.")
        return 1

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