fix(telegram): use word-boundary matching for bot mention detection - #12848
Merged
Conversation
Replaces the word-boundary regex scan with pure MessageEntity-based detection. Telegram's server emits MENTION entities for real @username mentions and TEXT_MENTION entities for @firstname mentions; the text- scanning fallback was both redundant (entities are always present for real mentions) and broken (matched raw substrings like email addresses, URLs, code-block contents, and forwarded literal text). Entity-only detection: - Closes bug #12545 ("foo@hermes_bot.example" false positive). - Also fixes edge cases the regex fix would still miss: @Handles inside URLs and code blocks, where Telegram does not emit mention entities. Tests rewritten to exercise realistic Telegram payloads (real mentions carry entities; substring false positives don't).
teknium1
force-pushed
the
hermes/hermes-84b246d9
branch
from
April 20, 2026 05:52
81b5941 to
c4d8bff
Compare
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.
Salvages #12826 onto current main and takes the fix further.
Summary
Telegram mention detection now relies only on the MessageEntity objects Telegram's server emits for real mentions. The text-scanning path is gone entirely.
Fixes #12545.
Why this approach over the original PR's regex
Telegram's server parses every message and emits
MENTION/TEXT_MENTIONentities for real @Handles. Those entities are the authoritative signal — it's what renders the blue clickable link in clients. Text-scanning (substring OR regex) is both redundant (entities are always present when there's a real mention) and unreliable in contexts Telegram specifically does not treat as mentions:@hermes_bot hello(real mention)foo@hermes_bot.example(bug #12545)@hermes_botx hi@hermes_botinside a URL@hermes_botinside a code block@hermes_botChanges
gateway/platforms/telegram.py: drop the substring/regex text scan in_message_mentions_bot; keep only the MENTION / TEXT_MENTION entity check that was already there. Net -3 lines of live code plus a comment explaining the rationale.tests/gateway/test_telegram_mention_boundaries.py: 18 tests grouped into real-mention detection, substring-false-positive rejection, entity edge cases (malformed offset/length, different target user), and case-insensitivity.Validation
scripts/run_tests.sh tests/gateway/test_telegram_mention_boundaries.py tests/gateway/test_telegram_group_gating.py→ 27 passed.TelegramAdapterwith realistic payloads: the exact bug reprofoo@hermes_bot.example→ False; real@hermes_botwith a MENTION entity → True;@hermes_botinside URL/code without an entity → False; TEXT_MENTION targeting the bot → True; TEXT_MENTION targeting a different user → False.test_telegram_approval_buttons.py(3 failures when the whole telegram suite runs together) is unrelated — reproduces on stashed main.Credit
Bug caught and originally fixed by @Tranquil-Flow in #12826 — the problem statement and test scaffolding were theirs. Cherry-picked onto current main with authorship preserved, then a follow-up commit from us swaps the regex approach for entity-only detection and expands the test coverage to the URL/code-block cases.