Spaces:
Sleeping
Sleeping
| #!/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()) | |