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