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