Skip to content

fix(telegram): expand text_link entities to Markdown in incoming messages - #31075

Open
dskwe wants to merge 2 commits into
NousResearch:mainfrom
dskwe:fix/telegram-text-link-entities
Open

fix(telegram): expand text_link entities to Markdown in incoming messages#31075
dskwe wants to merge 2 commits into
NousResearch:mainfrom
dskwe:fix/telegram-text-link-entities

Conversation

@dskwe

@dskwe dskwe commented May 23, 2026

Copy link
Copy Markdown
Contributor

Problem

When a Telegram user sends a rich-text hyperlink (text_link entity), Hermes passes only the visible text to the agent, discarding the underlying URL. The agent cannot access or open the hidden link.

Example: a message visually appears as 财联社 linked to https://mp.weixin.qq.com/s/..., but the agent only receives the text "财联社".

Closes #31071

Root Cause

_handle_text_message() sets event.text = msg.text directly without processing Telegram entities. The text_link entity type carries a .url attribute that is never inspected. Same issue affects media captions in _handle_media_message().

Fix

Add _expand_text_links() static method that converts text_link entities to Markdown [text](url) syntax. Called after building the MessageEvent but before _clean_bot_trigger_text(), for both text messages and media captions.

Processes entities in reverse offset order so earlier replacements don't shift subsequent offsets.

Changes

  • gateway/platforms/telegram.py — add _expand_text_links(), call in _handle_text_message() and media caption handling
  • tests/gateway/test_telegram_text_links.py — 9 new tests

How to Test

python -m pytest tests/gateway/test_telegram_text_links.py -v

@dskwe dskwe changed the title Fix/telegram text link entities fix(telegram): expand text_link entities to Markdown in incoming messages May 23, 2026
@dskwe
dskwe force-pushed the fix/telegram-text-link-entities branch from 4ae8805 to dbcb6b8 Compare May 23, 2026 18:43
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/gateway Gateway runner, session dispatch, delivery platform/telegram Telegram bot adapter labels May 23, 2026
dskwe added 2 commits May 26, 2026 00:08
…ages (NousResearch#31071)

Telegram text_link entities carry a hidden URL that was silently discarded
when building MessageEvent.text. The agent only received the visible text
(e.g. '财联社') without the underlying link.

Add _expand_text_links() to convert text_link entities into [text](url)
Markdown syntax. Handles UTF-16 offset conversion for emoji and
supplementary characters. Called for both message text and media captions.
11 tests covering: single/multiple links, CJK text, emoji (UTF-16
surrogate pairs), mixed ASCII+CJK, non-text_link entities, missing URL,
empty input, adjacent links, and mid-message links.
@dskwe
dskwe force-pushed the fix/telegram-text-link-entities branch from dbcb6b8 to 433abc4 Compare May 25, 2026 16:12

@teknium1 teknium1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for isolating the Telegram entity behavior and accounting for UTF-16 offsets.

Problems

  • Current main moved the adapter from gateway/platforms/telegram.py to plugins/platforms/telegram/adapter.py in 5600105, so this patch and its gateway.platforms.telegram test import need to be ported.
  • The current observed-group paths also preserve raw text: text observation builds and persists an unmodified event at plugins/platforms/telegram/adapter.py:7524-7527 and :7331-7354; observed media assigns its raw caption at :7773-7782. The proposed calls only cover normal dispatch, leaving hidden links absent from observed transcript context.

Suggested changes

  • Port the helper to the bundled Telegram adapter and normalize entities for normal and observed text/caption ingress.
  • Add handler-level coverage for those paths, in addition to the helper cases.

Automated hermes-sweeper review.

@@ -4816,6 +4870,7 @@ async def _handle_text_message(self, update: Update, context: ContextTypes.DEFAU
await self._ensure_forum_commands(update.message)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please ensure the same normalization runs for observed-group text as well. Current main has a separate skipped-message path that builds and persists the event without passing through this dispatched-message branch (plugins/platforms/telegram/adapter.py:7524-7527, :7331-7354), so otherwise hidden URLs remain absent from that agent-visible transcript.

@teknium1 teknium1 added sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages sweeper:risk-compatibility Sweeper risk: may break existing users, config, migrations, defaults, or upgrades sweeper:blast-moderate Sweeper blast radius: moderate — a subsystem or single platform labels Jul 13, 2026
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:blast-moderate Sweeper blast radius: moderate — a subsystem or single platform sweeper:risk-compatibility Sweeper risk: may break existing users, config, migrations, defaults, or upgrades sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Telegram rich-text link URLs are not exposed to the agent

3 participants