feat(bot): enhance group chat handling and mention detection

- Updated bot to only respond in group chats when explicitly mentioned.
- Added mention detection using regex for "Claude" patterns and cleaned up the message text for processing.
- Improved help message to clarify usage in direct messages and group chats.
This commit is contained in:
Harald Hoyer 2026-02-03 16:09:25 +01:00
parent 8404f0998b
commit b1370b5fc6

View file

@ -11,6 +11,7 @@ import hmac
import json
import logging
import os
import re
import subprocess
from datetime import datetime
from typing import Optional
@ -209,15 +210,40 @@ async def handle_webhook(
conversation_token = conversation.get("token", "")
conversation_type = conversation.get("type", 0)
# Only respond to user messages in one-on-one chats (type 1)
# Only respond to user messages
if actor_type != "users":
log.info(f"Ignoring non-user actor: {actor_type}")
return JSONResponse({"status": "ignored", "reason": "not a user message"})
if conversation_type != 1: # 1 = one-to-one
log.info(f"Ignoring non-DM conversation type: {conversation_type}")
return JSONResponse({"status": "ignored", "reason": "not a direct 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
# Check for bot mention in message (Nextcloud uses @"Bot Name" format)
bot_mentioned = False
clean_message = message_text
# Look for mention patterns: @Claude or @"Claude"
mention_patterns = [
r'@"?Claude"?\s*',
r'@"?claude"?\s*',
]
for pattern in mention_patterns:
if re.search(pattern, message_text, re.IGNORECASE):
bot_mentioned = True
clean_message = re.sub(pattern, '', message_text, flags=re.IGNORECASE).strip()
break
# In group chats, only respond if mentioned
if not is_direct_message and not bot_mentioned:
log.info(f"Ignoring message in group chat without mention")
return JSONResponse({"status": "ignored", "reason": "not mentioned in group chat"})
# Use clean message (without mention) for processing
if bot_mentioned:
message_text = clean_message
# Check allowed users
if ALLOWED_USERS and actor_id not in ALLOWED_USERS:
log.warning(f"User {actor_id} not in allowed list")
@ -253,6 +279,10 @@ async def handle_webhook(
Schreib mir einfach eine Nachricht und ich antworte dir.
**Nutzung:**
In Direktnachrichten: Einfach schreiben
In Gruppenchats: @Claude gefolgt von deiner Frage
**Befehle:**
`/clear` oder `/reset` Konversation zurücksetzen
`/help` oder `/hilfe` Diese Hilfe anzeigen