Skip to content

fix(telegram): reject messages that @mention a different bot in group chats - #20361

Closed
saheljalal wants to merge 1 commit into
NousResearch:mainfrom
saheljalal:fix/telegram-mention-other-bot-filter
Closed

fix(telegram): reject messages that @mention a different bot in group chats#20361
saheljalal wants to merge 1 commit into
NousResearch:mainfrom
saheljalal:fix/telegram-mention-other-bot-filter

Conversation

@saheljalal

@saheljalal saheljalal commented May 5, 2026

Copy link
Copy Markdown

What does this PR do?

When multiple Hermes bots share a Telegram group and require_mention: false, every bot responds to all messages — including messages where a user explicitly @mentions a different bot. This adds filtering to reject such messages before processing, achieving parity with the Discord adapter which already handles this correctly.

Root Cause

The Telegram _should_process_message() flow checked free_response_chats then require_mention — if false, it returned True immediately. There was no gate for messages that explicitly target another entity via @mention or text_mention entities (only /command@otherbot was handled).

Additionally, _message_mentions_bot() and the new filtering both needed to scan the same entities, causing duplicate iteration when called sequentially from _should_process_message().

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)

Changes Made

  • gateway/platforms/telegram.py — Extract _iter_entity_sources() (static) and _resolve_entity_targets() (single-pass scanner returning (self_mentioned, other_mentioned, other_command)). Refactor _message_mentions_bot() to delegate to the shared scanner. Add _message_targets_other_bot() that also delegates, positioned in _should_process_message() after free_response_chats but before require_mention.
  • tests/gateway/test_telegram_group_gating.py — 11 new tests covering: @Otherbot rejected (require_mention false/true), @self not rejected, @self+@other dual mention not rejected, general messages unchanged, @human_user rejected, /command@otherbot rejected, @Otherbot in caption entities, text_mention of other user rejected, text_mention of self not rejected.

Behavior

Message require_mention: false require_mention: true
hello everyone ✅ accepted ❌ rejected (no mention)
@hermes_bot help ✅ accepted ✅ accepted
@otherbot help rejected (new) ❌ rejected
@hermes_bot @otherbot help ✅ accepted (self mentioned) ✅ accepted
@john_doe hello rejected (new) ❌ rejected
/status@other_bot rejected (new) ❌ rejected

Discord Parity

Mirrors the multi-agent filtering in discord.py (~L710-730). Telegram does not provide m.bot for mention entities, so we use entity text comparison instead.

How to Test

  1. pytest tests/gateway/test_telegram_group_gating.py -v — all 25 tests pass (14 existing + 11 new)
  2. Manual: deploy two Hermes bots to a shared group with require_mention: false. @mention one bot and verify only that bot responds.

Checklist

Code

  • I have read the Contributing Guide
  • My commit messages follow Conventional Commits (fix(gateway): ...)
  • I searched for existing PRs to make sure this is not a duplicate
  • My PR contains only changes related to this fix (no unrelated commits)
  • I have run pytest tests/ -q and all tests pass
  • I have added tests for my changes
  • I have tested on my platform: Ubuntu (Linux)

Documentation and Housekeeping

  • Updated relevant docstrings — N/A for README/docs
  • Updated cli-config.yaml.example — N/A (no new config keys)
  • Updated CONTRIBUTING.md/AGENTS.md — N/A (no architecture change)
  • Cross-platform impact — N/A (Telegram gateway, no OS interaction)
  • Updated tool descriptions/schemas — N/A (no tool changes)

Related Issue

Fixes #20373

@alt-glitch alt-glitch added type/bug Something isn't working comp/gateway Gateway runner, session dispatch, delivery platform/telegram Telegram bot adapter P2 Medium — degraded but workaround exists labels May 5, 2026
@saheljalal
saheljalal force-pushed the fix/telegram-mention-other-bot-filter branch 2 times, most recently from d496173 to 9865699 Compare May 5, 2026 19:37
When multiple Hermes bots share a Telegram group and require_mention is
false, every bot responds to all messages — including messages where a
user explicitly @mentions a different bot.

Extract _resolve_entity_targets() as a shared single-pass entity scanner
handling mention, text_mention, and bot_command types across both text
and caption. Both _message_mentions_bot() and the new
_message_targets_other_bot() delegate to it, eliminating the duplicate
iteration that occurred when _should_process_message called both methods
sequentially on the same message.

Also extract _iter_entity_sources() as a static method for reuse.

The new _message_targets_other_bot() check runs regardless of
require_mention, positioned after free_response_chats but before the
require_mention gate — achieving parity with the Discord adapter.

11 new tests + 14 existing, all passing (25 total).
@saheljalal
saheljalal force-pushed the fix/telegram-mention-other-bot-filter branch from 9865699 to a021fb3 Compare May 5, 2026 19:41
@saheljalal
saheljalal marked this pull request as ready for review May 5, 2026 19:45
@teknium1

Copy link
Copy Markdown
Contributor

Automated hermes-sweeper review: this Telegram multi-bot routing fix is already implemented on current main.

Evidence:

  • gateway/platforms/telegram.py:5251 now rejects messages where explicit bot mentions exclude this adapter before the free_response_chats and require_mention gates.
  • gateway/platforms/telegram.py:4820 extracts explicit bot usernames from text/caption entities, including /command@botname disambiguation.
  • tests/gateway/test_telegram_group_gating.py:498 covers require_mention: false with @Interntestnumber1bot: the non-target bot ignores it and the mentioned bot processes it.
  • tests/gateway/test_telegram_group_gating.py:508 covers /stop@Interntestnumber1bot exclusivity when mentions are not required.
  • The relevant implementation landed across ce4d8570215a7e601e13de5936853b64c355ef5d and follow-up fbfe2948827b8bdd6e0c0f1aa71eb897938f9635.

Thanks for the original report and fix direction; the same bug class is now handled on main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/gateway Gateway runner, session dispatch, delivery P2 Medium — degraded but workaround exists platform/telegram Telegram bot adapter sweeper:implemented-on-main Sweeper: behavior already present on current main type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Telegram: bots respond to messages @mentioning a different bot when require_mention is false

3 participants