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