diff --git a/systems/x86_64-linux/mx/nextcloud-claude-bot/bot.py b/systems/x86_64-linux/mx/nextcloud-claude-bot/bot.py index 5d051a4..25cb550 100644 --- a/systems/x86_64-linux/mx/nextcloud-claude-bot/bot.py +++ b/systems/x86_64-linux/mx/nextcloud-claude-bot/bot.py @@ -213,29 +213,39 @@ async def handle_webhook( raise HTTPException(status_code=400, detail="Invalid JSON") log.info(f"Received webhook: {json.dumps(data, indent=2)[:500]}") - - # Extract message info - # Nextcloud Talk Bot API structure + + # Extract message info - Nextcloud Talk Bot webhook format actor = data.get("actor", {}) - actor_id = actor.get("id", "") actor_type = actor.get("type", "") - - message_data = data.get("message", {}) - message_text = message_data.get("message", "") - message_id = message_data.get("id") - - conversation = data.get("conversation", {}) - conversation_token = conversation.get("token", "") - conversation_type = conversation.get("type", 0) - - # Only respond to user messages - if actor_type != "users": + actor_id_full = actor.get("id", "") # e.g., "users/harald" + + # Extract username from "users/username" format + if "/" in actor_id_full: + actor_id = actor_id_full.split("/", 1)[1] + else: + actor_id = actor_id_full + + # Message is in object.content as JSON string + obj = data.get("object", {}) + message_id = obj.get("id") + content_str = obj.get("content", "{}") + try: + content = json.loads(content_str) + message_text = content.get("message", "") + except json.JSONDecodeError: + message_text = content_str + + # Conversation info is in target + target = data.get("target", {}) + conversation_token = target.get("id", "") + + # Only respond to user/person messages + if actor_type not in ("users", "Person"): log.info(f"Ignoring non-user actor: {actor_type}") return JSONResponse({"status": "ignored", "reason": "not a user message"}) - # For group chats (type 2, 3, 4), only respond if bot is mentioned - # Type 1 = one-to-one, Type 2 = group, Type 3 = public, Type 4 = changelog - is_direct_message = conversation_type == 1 + # For now, treat all conversations the same (respond to mentions) + is_direct_message = False # We can't easily determine this from the webhook # Check for bot mention in message (Nextcloud uses @"Bot Name" format) bot_mentioned = False