mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-05-25 05:52:34 +00:00
feat(gateway): add Telegram guest mention mode
This commit is contained in:
parent
369cee018d
commit
55f518e521
6 changed files with 168 additions and 25 deletions
|
|
@ -3127,6 +3127,15 @@ class TelegramAdapter(BasePlatformAdapter):
|
|||
return bool(configured)
|
||||
return os.getenv("TELEGRAM_REQUIRE_MENTION", "false").lower() in ("true", "1", "yes", "on")
|
||||
|
||||
def _telegram_guest_mode(self) -> bool:
|
||||
"""Return whether non-allowlisted groups may trigger via direct @mention."""
|
||||
configured = self.config.extra.get("guest_mode")
|
||||
if configured is not None:
|
||||
if isinstance(configured, str):
|
||||
return configured.lower() in ("true", "1", "yes", "on")
|
||||
return bool(configured)
|
||||
return os.getenv("TELEGRAM_GUEST_MODE", "false").lower() in ("true", "1", "yes", "on")
|
||||
|
||||
def _telegram_free_response_chats(self) -> set[str]:
|
||||
raw = self.config.extra.get("free_response_chats")
|
||||
if raw is None:
|
||||
|
|
@ -3286,6 +3295,14 @@ class TelegramAdapter(BasePlatformAdapter):
|
|||
return True
|
||||
return False
|
||||
|
||||
def _is_guest_mention(self, message: Message) -> bool:
|
||||
"""Return True for the narrow guest-mode bypass: group + explicit bot mention."""
|
||||
return (
|
||||
self._telegram_guest_mode()
|
||||
and self._is_group_chat(message)
|
||||
and self._message_mentions_bot(message)
|
||||
)
|
||||
|
||||
def _clean_bot_trigger_text(self, text: Optional[str]) -> Optional[str]:
|
||||
if not text or not self._bot or not getattr(self._bot, "username", None):
|
||||
return text
|
||||
|
|
@ -3297,16 +3314,18 @@ class TelegramAdapter(BasePlatformAdapter):
|
|||
"""Apply Telegram group trigger rules.
|
||||
|
||||
DMs remain unrestricted. Group/supergroup messages are accepted when:
|
||||
- the chat passes the ``allowed_chats`` whitelist (when set)
|
||||
- the chat passes the ``allowed_chats`` whitelist (when set), or
|
||||
``guest_mode`` is enabled and the bot is explicitly mentioned
|
||||
- the chat is explicitly allowlisted in ``free_response_chats``
|
||||
- ``require_mention`` is disabled
|
||||
- the message replies to the bot
|
||||
- the bot is @mentioned
|
||||
- the text/caption matches a configured regex wake-word pattern
|
||||
|
||||
When ``allowed_chats`` is non-empty, it acts as a hard gate — messages
|
||||
from any chat not in the list are ignored regardless of the other
|
||||
rules. When ``require_mention`` is enabled, slash commands are not given
|
||||
When ``allowed_chats`` is non-empty, it remains a hard gate except for
|
||||
the narrow ``guest_mode`` bypass: group/supergroup messages that
|
||||
explicitly @mention this bot. Replies and regex wake words do not bypass
|
||||
``allowed_chats``. When ``require_mention`` is enabled, slash commands are not given
|
||||
special treatment — they must pass the same mention/reply checks
|
||||
as any other group message. Users can still trigger commands via
|
||||
the Telegram bot menu (``/command@botname``) or by explicitly
|
||||
|
|
@ -3315,14 +3334,7 @@ class TelegramAdapter(BasePlatformAdapter):
|
|||
"""
|
||||
if not self._is_group_chat(message):
|
||||
return True
|
||||
# allowed_chats check (whitelist — must pass before other gating).
|
||||
# When set, group messages from chats NOT in this whitelist are
|
||||
# silently ignored, even if @mentioned. DMs are already excluded above.
|
||||
allowed = self._telegram_allowed_chats()
|
||||
if allowed:
|
||||
chat_id_str = str(getattr(getattr(message, "chat", None), "id", ""))
|
||||
if chat_id_str not in allowed:
|
||||
return False
|
||||
|
||||
thread_id = getattr(message, "message_thread_id", None)
|
||||
if thread_id is not None:
|
||||
try:
|
||||
|
|
@ -3330,7 +3342,19 @@ class TelegramAdapter(BasePlatformAdapter):
|
|||
return False
|
||||
except (TypeError, ValueError):
|
||||
logger.warning("[%s] Ignoring non-numeric Telegram message_thread_id: %r", self.name, thread_id)
|
||||
if str(getattr(getattr(message, "chat", None), "id", "")) in self._telegram_free_response_chats():
|
||||
|
||||
chat_id_str = str(getattr(getattr(message, "chat", None), "id", ""))
|
||||
guest_mention = self._is_guest_mention(message)
|
||||
|
||||
# allowed_chats check (whitelist). When set, group messages from chats
|
||||
# outside the whitelist are ignored unless guest_mode permits this
|
||||
# exact message as an explicit direct mention. DMs are excluded above.
|
||||
allowed = self._telegram_allowed_chats()
|
||||
if allowed and chat_id_str not in allowed:
|
||||
return guest_mention
|
||||
if guest_mention:
|
||||
return True
|
||||
if chat_id_str in self._telegram_free_response_chats():
|
||||
return True
|
||||
if not self._telegram_require_mention():
|
||||
return True
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue