#!/usr/bin/env python3 """ Debug Matrix API connection step by step """ import asyncio import json import httpx # Matrix configuration MATRIX_HOMESERVER = "https://matrix.klas.chat" MATRIX_ACCESS_TOKEN = "syt_a2xhcw_ZcjbRgfRFEdMHnutAVOa_1M7eD4" HOME_ASSISTANT_ROOM_ID = "!xZkScMybPseErYMJDz:matrix.klas.chat" async def test_matrix_connection(): """Test Matrix connection step by step""" print("šŸ” Testing Matrix API connection...") headers = { "Authorization": f"Bearer {MATRIX_ACCESS_TOKEN}", "Content-Type": "application/json" } try: async with httpx.AsyncClient() as client: # Test 1: Check if we can access the Matrix server print("\n1. Testing Matrix server access...") response = await client.get(f"{MATRIX_HOMESERVER}/_matrix/client/versions", timeout=30.0) print(f" Status: {response.status_code}") if response.status_code == 200: print(" āœ… Matrix server is accessible") else: print(" āŒ Matrix server access failed") return False # Test 2: Check if our access token is valid print("\n2. Testing access token...") response = await client.get(f"{MATRIX_HOMESERVER}/_matrix/client/v3/account/whoami", headers=headers, timeout=30.0) print(f" Status: {response.status_code}") if response.status_code == 200: data = response.json() print(f" āœ… Access token valid, user: {data.get('user_id')}") else: print(" āŒ Access token invalid") print(f" Response: {response.text}") return False # Test 3: Check if we can access the Home Assistant room print("\n3. Testing Home Assistant room access...") response = await client.get(f"{MATRIX_HOMESERVER}/_matrix/client/v3/rooms/{HOME_ASSISTANT_ROOM_ID}/state/m.room.name", headers=headers, timeout=30.0) print(f" Status: {response.status_code}") if response.status_code == 200: data = response.json() print(f" āœ… Room accessible, name: {data.get('name')}") else: print(" āŒ Room access failed") print(f" Response: {response.text}") return False # Test 4: Send a simple test message print("\n4. Sending test message...") message_data = { "msgtype": "m.text", "body": "šŸ”§ Matrix API test message from LangMem debug script" } response = await client.post( f"{MATRIX_HOMESERVER}/_matrix/client/v3/rooms/{HOME_ASSISTANT_ROOM_ID}/send/m.room.message", headers=headers, json=message_data, timeout=30.0 ) print(f" Status: {response.status_code}") if response.status_code == 200: data = response.json() print(f" āœ… Message sent successfully!") print(f" Event ID: {data.get('event_id')}") return True else: print(" āŒ Message sending failed") print(f" Response: {response.text}") return False except Exception as e: print(f"āŒ Error during testing: {e}") return False async def main(): """Main function""" success = await test_matrix_connection() if success: print("\nšŸŽ‰ All Matrix API tests passed!") else: print("\nāŒ Matrix API tests failed!") if __name__ == "__main__": asyncio.run(main())