#!/usr/bin/env python3 """ Claude Messaging System - Alternative to Signal bridge using Home Assistant Matrix integration """ import asyncio import httpx import json import sys from datetime import datetime MATRIX_HOMESERVER = "https://matrix.klas.chat" CLAUDE_ACCESS_TOKEN = "syt_Y2xhdWRl_CoBgPoHbtMOxhvOUcMnz_2WRPZJ" HOME_ASSISTANT_ROOM_ID = "!xZkScMybPseErYMJDz:matrix.klas.chat" async def send_message(message, sender_name="Claude"): """Send a message to the Home Assistant Matrix room""" try: async with httpx.AsyncClient() as client: headers = { "Authorization": f"Bearer {CLAUDE_ACCESS_TOKEN}", "Content-Type": "application/json" } # Format message with timestamp and sender timestamp = datetime.now().strftime("%H:%M:%S") formatted_message = f"[{timestamp}] {sender_name}: {message}" response = await client.post( f"{MATRIX_HOMESERVER}/_matrix/client/v3/rooms/{HOME_ASSISTANT_ROOM_ID}/send/m.room.message", headers=headers, json={ "msgtype": "m.text", "body": formatted_message } ) if response.status_code == 200: print(f"āœ… Message sent: {formatted_message}") return True else: print(f"āŒ Failed to send message: {response.status_code}") return False except Exception as e: print(f"āŒ Error sending message: {e}") return False async def get_recent_messages(limit=10): """Get recent messages from the Home Assistant Matrix room""" try: async with httpx.AsyncClient() as client: headers = {"Authorization": f"Bearer {CLAUDE_ACCESS_TOKEN}"} response = await client.get( f"{MATRIX_HOMESERVER}/_matrix/client/v3/rooms/{HOME_ASSISTANT_ROOM_ID}/messages", headers=headers, params={"limit": limit, "dir": "b"} ) if response.status_code == 200: data = response.json() messages = data.get("chunk", []) print(f"šŸ“¬ Recent messages in Home Assistant room:") print("=" * 50) for msg in reversed(messages): # Show oldest to newest if msg.get("type") == "m.room.message": sender = msg.get("sender", "") body = msg.get("content", {}).get("body", "") timestamp = msg.get("origin_server_ts", 0) # Format sender name if "claude" in sender: sender_name = "Claude" elif "signalbot" in sender: sender_name = "SignalBot" else: sender_name = sender.split(":")[0].replace("@", "") print(f"[{sender_name}]: {body}") return messages else: print(f"āŒ Failed to get messages: {response.status_code}") return [] except Exception as e: print(f"āŒ Error getting messages: {e}") return [] async def monitor_messages(): """Monitor messages in real-time""" print("šŸ‘ļø Monitoring Home Assistant Matrix room for messages...") print("Press Ctrl+C to stop monitoring") print("=" * 50) last_message_count = 0 try: while True: messages = await get_recent_messages(5) if len(messages) > last_message_count: print(f"\nšŸ”” New message detected! (Total: {len(messages)})") last_message_count = len(messages) await asyncio.sleep(5) # Check every 5 seconds except KeyboardInterrupt: print("\nšŸ‘‹ Monitoring stopped") async def send_notification(message): """Send a notification message to Home Assistant""" notification_msg = f"šŸ”” NOTIFICATION: {message}" return await send_message(notification_msg, "Claude-Notification") async def main(): """Main function""" if len(sys.argv) < 2: print("šŸ  Claude Messaging System") print("=" * 40) print("Usage:") print(" python claude-messaging-system.py send 'Your message here'") print(" python claude-messaging-system.py read") print(" python claude-messaging-system.py monitor") print(" python claude-messaging-system.py notify 'Notification message'") print("") print("Examples:") print(" python claude-messaging-system.py send 'Hello from Claude!'") print(" python claude-messaging-system.py read") print(" python claude-messaging-system.py monitor") return command = sys.argv[1].lower() if command == "send": if len(sys.argv) < 3: print("āŒ Please provide a message to send") return message = " ".join(sys.argv[2:]) await send_message(message) elif command == "read": await get_recent_messages(10) elif command == "monitor": await monitor_messages() elif command == "notify": if len(sys.argv) < 3: print("āŒ Please provide a notification message") return message = " ".join(sys.argv[2:]) await send_notification(message) else: print(f"āŒ Unknown command: {command}") print("Available commands: send, read, monitor, notify") if __name__ == "__main__": asyncio.run(main())