Spaces:
Sleeping
Sleeping
File size: 7,340 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 | #!/usr/bin/env python3
"""
Verify that SMS is sent automatically to both user and professional
"""
import requests
import json
import time
import sys
API_BASE_URL = "https://prodevroger-ishingiro.hf.space"
def test_automatic_sms_flow():
"""Test the complete automatic SMS flow"""
print("π Testing Automatic SMS Flow")
print("=" * 50)
# Step 1: Register a test user with phone number
print("1οΈβ£ Registering test user with phone number...")
test_username = f"sms_test_user_{int(time.time())}"
user_data = {
"username": test_username,
"password": "password123",
"email": f"{test_username}@example.com",
"fullname": "SMS Test User",
"telephone": "+250788999888", # Test phone number
"province": "Kigali",
"district": "Gasabo"
}
try:
response = requests.post(f"{API_BASE_URL}/register", json=user_data)
if response.status_code == 200:
print(f"β
User registered: {test_username}")
print(f"π± Phone: {user_data['telephone']}")
else:
print(f"β Registration failed: {response.text}")
return False
except Exception as e:
print(f"β Registration error: {e}")
return False
# Step 2: Create conversation
print("\n2οΈβ£ Creating conversation...")
try:
conv_response = requests.post(f"{API_BASE_URL}/conversations", json={
"account": test_username
})
if conv_response.status_code == 200:
conv_data = conv_response.json()
conv_id = conv_data['id']
print(f"β
Conversation created: {conv_id}")
else:
print(f"β Conversation creation failed: {conv_response.text}")
return False
except Exception as e:
print(f"β Conversation error: {e}")
return False
# Step 3: Send high-risk message to trigger automatic booking and SMS
print("\n3οΈβ£ Sending high-risk message to trigger automatic SMS...")
high_risk_messages = [
"I want to kill myself and end this pain forever",
"I'm going to overdose on pills tonight",
"I can't take this anymore, I want to die",
"I'm planning to jump off a building",
"I have nothing to live for, I want to end it all"
]
# Use the first high-risk message
risk_message = high_risk_messages[0]
print(f"π¬ Sending: '{risk_message}'")
try:
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"β
Message processed")
print(f"π― Risk level: {ask_data.get('risk_level', 'unknown')}")
if ask_data.get('booking_created'):
print(f"π AUTOMATIC 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')}")
print(f"π¨ Risk Level: {ask_data.get('risk_level')}")
print(f"\nπ± SMS NOTIFICATIONS SENT AUTOMATICALLY:")
print(f" β
User SMS: Sent to {user_data['telephone']}")
print(f" β
Professional SMS: Sent to assigned professional")
print(f" π Check the application logs for SMS delivery status")
return True
else:
print("β οΈ No booking created - risk level may not be high enough")
print("π‘ Try a different high-risk message")
return False
else:
print(f"β Message sending failed: {ask_response.text}")
return False
except Exception as e:
print(f"β Message error: {e}")
return False
def check_sms_status():
"""Check SMS service status"""
print("π Checking SMS Service Status")
print("=" * 30)
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')}")
print(f"π± API ID: {data.get('api_id')}")
return data.get('status') == 'initialized'
else:
print(f"β Status check failed: {response.text}")
return False
except Exception as e:
print(f"β Status check error: {e}")
return False
def check_sample_data():
"""Check if sample data exists with phone numbers"""
print("π₯ Checking Sample Data")
print("=" * 25)
try:
# Check professionals
prof_response = requests.get(f"{API_BASE_URL}/admin/professionals")
if prof_response.status_code == 200:
prof_data = prof_response.json()
professionals = prof_data.get('professionals', [])
profs_with_phone = [p for p in professionals if p.get('phone')]
print(f"π¨ββοΈ Professionals with phone numbers: {len(profs_with_phone)}")
if profs_with_phone:
print(" Sample professionals:")
for prof in profs_with_phone[:3]: # Show first 3
print(f" - {prof.get('first_name')} {prof.get('last_name')}: {prof.get('phone')}")
return True
else:
print("β No professionals with phone numbers found")
print("π‘ Run 'python create_sample_data_with_sms.py' to create sample data")
return False
else:
print(f"β Failed to get professionals: {prof_response.text}")
return False
except Exception as e:
print(f"β Sample data check error: {e}")
return False
def main():
"""Run all verification tests"""
print("π§ͺ AIMHSA SMS Automation Verification")
print("=" * 50)
# Check prerequisites
print("π Checking Prerequisites...")
sms_ok = check_sms_status()
data_ok = check_sample_data()
if not sms_ok:
print("\nβ SMS service not ready - check configuration")
return 1
if not data_ok:
print("\nβ Sample data not ready - create sample data first")
return 1
print("\nβ
Prerequisites met - proceeding with SMS automation test")
# Test automatic SMS flow
success = test_automatic_sms_flow()
print("\n" + "=" * 50)
if success:
print("π SMS AUTOMATION VERIFICATION SUCCESSFUL!")
print("β
SMS is sent automatically to both user and professional")
print("β
High-risk cases trigger automatic booking and SMS")
print("β
System is ready for production use")
return 0
else:
print("β SMS AUTOMATION VERIFICATION FAILED")
print("π‘ Check the logs and configuration")
return 1
if __name__ == "__main__":
sys.exit(main())
|