#!/usr/bin/env python3 """ Send message to Matrix room via direct API call """ import asyncio import json import sys from typing import Optional import httpx # Matrix configuration MATRIX_HOMESERVER = "https://matrix.klas.chat" MATRIX_ACCESS_TOKEN = "syt_a2xhcw_ZcjbRgfRFEdMHnutAVOa_1M7eD4" HOME_ASSISTANT_ROOM_ID = "!OQkQcCnlrGwGKJjXnt:matrix.klas.chat" # Need to find this async def send_matrix_message(room_id: str, message: str, access_token: str = MATRIX_ACCESS_TOKEN) -> bool: """Send a message to a Matrix room""" try: url = f"{MATRIX_HOMESERVER}/_matrix/client/v3/rooms/{room_id}/send/m.room.message" headers = { "Authorization": f"Bearer {access_token}", "Content-Type": "application/json" } data = { "msgtype": "m.text", "body": message } async with httpx.AsyncClient() as client: response = await client.post(url, headers=headers, json=data, timeout=30.0) if response.status_code == 200: print(f"✅ Message sent successfully to room {room_id}") return True else: print(f"❌ Failed to send message: {response.status_code}") print(f"Response: {response.text}") return False except Exception as e: print(f"❌ Error sending message: {e}") return False async def find_home_assistant_room(): """Find the Home Assistant room ID""" try: url = f"{MATRIX_HOMESERVER}/_matrix/client/v3/joined_rooms" headers = { "Authorization": f"Bearer {MATRIX_ACCESS_TOKEN}" } async with httpx.AsyncClient() as client: response = await client.get(url, headers=headers, timeout=30.0) if response.status_code == 200: data = response.json() print("🔍 Searching for Home Assistant room...") for room_id in data.get("joined_rooms", []): # Get room name room_url = f"{MATRIX_HOMESERVER}/_matrix/client/v3/rooms/{room_id}/state/m.room.name" room_response = await client.get(room_url, headers=headers, timeout=30.0) if room_response.status_code == 200: room_data = room_response.json() room_name = room_data.get("name", "") print(f" Found room: {room_name} ({room_id})") if "Home Assistant" in room_name: print(f"✅ Found Home Assistant room: {room_id}") return room_id print("❌ Home Assistant room not found") return None else: print(f"❌ Failed to get rooms: {response.status_code}") return None except Exception as e: print(f"❌ Error finding room: {e}") return None async def main(): """Main function""" message = "🤖 Test message from LangMem MCP Server implementation!" if len(sys.argv) > 1: message = " ".join(sys.argv[1:]) print(f"📤 Sending message: {message}") # Find Home Assistant room room_id = await find_home_assistant_room() if room_id: success = await send_matrix_message(room_id, message) if success: print("🎉 Message sent successfully!") else: print("❌ Failed to send message") else: print("❌ Could not find Home Assistant room") if __name__ == "__main__": asyncio.run(main())