Skip to content

fix(telegram): use word-boundary matching for bot mention detection - #12848

Merged
teknium1 merged 2 commits into
mainfrom
hermes/hermes-84b246d9
Apr 20, 2026
Merged

fix(telegram): use word-boundary matching for bot mention detection#12848
teknium1 merged 2 commits into
mainfrom
hermes/hermes-84b246d9

Conversation

@teknium1

@teknium1 teknium1 commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

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_MENTION entities 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:

Input Old substring Regex fix (#12826) Entity-only (this PR)
@hermes_bot hello (real mention) match match match
foo@hermes_bot.example (bug #12545) false positive correct correct
@hermes_botx hi false positive correct correct
@hermes_bot inside a URL false positive false positive correct
@hermes_bot inside a code block false positive false positive correct
Forwarded quoted text containing @hermes_bot false positive false positive correct

Changes

  • 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.
  • E2E against a live TelegramAdapter with realistic payloads: the exact bug repro foo@hermes_bot.example → False; real @hermes_bot with a MENTION entity → True; @hermes_bot inside URL/code without an entity → False; TEXT_MENTION targeting the bot → True; TEXT_MENTION targeting a different user → False.
  • Pre-existing xdist pollution in 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.

Tranquil-Flow and others added 2 commits April 19, 2026 22:04
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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

telegram: group mention gating treats raw substrings as valid mentions

2 participants