fix(telegram): reject messages that @mention a different bot in group chats - #20361
Closed
saheljalal wants to merge 1 commit into
Closed
fix(telegram): reject messages that @mention a different bot in group chats#20361saheljalal wants to merge 1 commit into
saheljalal wants to merge 1 commit into
Conversation
saheljalal
force-pushed
the
fix/telegram-mention-other-bot-filter
branch
2 times, most recently
from
May 5, 2026 19:37
d496173 to
9865699
Compare
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
force-pushed
the
fix/telegram-mention-other-bot-filter
branch
from
May 5, 2026 19:41
9865699 to
a021fb3
Compare
saheljalal
marked this pull request as ready for review
May 5, 2026 19:45
5 tasks
Contributor
|
Automated hermes-sweeper review: this Telegram multi-bot routing fix is already implemented on current main. Evidence:
Thanks for the original report and fix direction; the same bug class is now handled on main. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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@mentionsa 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 checkedfree_response_chatsthenrequire_mention— if false, it returnedTrueimmediately. There was no gate for messages that explicitly target another entity via@mentionortext_mentionentities (only/command@otherbotwas 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
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()afterfree_response_chatsbut beforerequire_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
require_mention: falserequire_mention: truehello everyone@hermes_bot help@otherbot help@hermes_bot @otherbot help@john_doe hello/status@other_botDiscord Parity
Mirrors the multi-agent filtering in
discord.py(~L710-730). Telegram does not providem.botfor mention entities, so we use entity text comparison instead.How to Test
pytest tests/gateway/test_telegram_group_gating.py -v— all 25 tests pass (14 existing + 11 new)require_mention: false. @mention one bot and verify only that bot responds.Checklist
Code
fix(gateway): ...)pytest tests/ -qand all tests passDocumentation and Housekeeping
cli-config.yaml.example— N/A (no new config keys)CONTRIBUTING.md/AGENTS.md— N/A (no architecture change)Related Issue
Fixes #20373