feat(whatsapp): add group_policy to control bot response behavior in groups

This commit is contained in:
flobo3
2026-03-18 18:13:13 +03:00
committed by chengyongru
parent e4137736f6
commit b14d5a0a1d

View File

@@ -4,7 +4,7 @@ import asyncio
import json
import mimetypes
from collections import OrderedDict
from typing import Any
from typing import Any, Literal
from loguru import logger
@@ -23,6 +23,7 @@ class WhatsAppConfig(Base):
bridge_url: str = "ws://localhost:3001"
bridge_token: str = ""
allow_from: list[str] = Field(default_factory=list)
group_policy: Literal["open", "mention"] = "open" # "open" responds to all, "mention" only when @mentioned
class WhatsAppChannel(BaseChannel):
@@ -138,6 +139,13 @@ class WhatsAppChannel(BaseChannel):
self._processed_message_ids.popitem(last=False)
# Extract just the phone number or lid as chat_id
is_group = data.get("isGroup", False)
was_mentioned = data.get("wasMentioned", False)
if is_group and getattr(self.config, "group_policy", "open") == "mention":
if not was_mentioned:
return
user_id = pn if pn else sender
sender_id = user_id.split("@")[0] if "@" in user_id else user_id
logger.info("Sender {}", sender)