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:
parent
8404f0998b
commit
b1370b5fc6
1 changed files with 36 additions and 6 deletions
|
|
@ -11,6 +11,7 @@ import hmac
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
@ -209,15 +210,40 @@ async def handle_webhook(
|
||||||
conversation_token = conversation.get("token", "")
|
conversation_token = conversation.get("token", "")
|
||||||
conversation_type = conversation.get("type", 0)
|
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":
|
if actor_type != "users":
|
||||||
log.info(f"Ignoring non-user actor: {actor_type}")
|
log.info(f"Ignoring non-user actor: {actor_type}")
|
||||||
return JSONResponse({"status": "ignored", "reason": "not a user message"})
|
return JSONResponse({"status": "ignored", "reason": "not a user message"})
|
||||||
|
|
||||||
if conversation_type != 1: # 1 = one-to-one
|
# For group chats (type 2, 3, 4), only respond if bot is mentioned
|
||||||
log.info(f"Ignoring non-DM conversation type: {conversation_type}")
|
# Type 1 = one-to-one, Type 2 = group, Type 3 = public, Type 4 = changelog
|
||||||
return JSONResponse({"status": "ignored", "reason": "not a direct message"})
|
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
|
# Check allowed users
|
||||||
if ALLOWED_USERS and actor_id not in ALLOWED_USERS:
|
if ALLOWED_USERS and actor_id not in ALLOWED_USERS:
|
||||||
log.warning(f"User {actor_id} not in allowed list")
|
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.
|
Schreib mir einfach eine Nachricht und ich antworte dir.
|
||||||
|
|
||||||
|
**Nutzung:**
|
||||||
|
• In Direktnachrichten: Einfach schreiben
|
||||||
|
• In Gruppenchats: @Claude gefolgt von deiner Frage
|
||||||
|
|
||||||
**Befehle:**
|
**Befehle:**
|
||||||
• `/clear` oder `/reset` – Konversation zurücksetzen
|
• `/clear` oder `/reset` – Konversation zurücksetzen
|
||||||
• `/help` oder `/hilfe` – Diese Hilfe anzeigen
|
• `/help` oder `/hilfe` – Diese Hilfe anzeigen
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue