Skip to content

fix(telegram): fix broken URL parsing for Markdown messages#7387

Closed
dsocolobsky wants to merge 2 commits into
NousResearch:mainfrom
dsocolobsky:fix-telegram-markdown-parsing
Closed

fix(telegram): fix broken URL parsing for Markdown messages#7387
dsocolobsky wants to merge 2 commits into
NousResearch:mainfrom
dsocolobsky:fix-telegram-markdown-parsing

Conversation

@dsocolobsky

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes the Telegram MarkdownV2 link regex to handle URLs containing parentheses (e.g. Wikipedia links).

The link regex in format_message used [^)]+ for the URL portion, which stopped matching at the first ) character. URLs with nested parentheses like https://en.wikipedia.org/wiki/Rust_(programming_language) were truncated, producing malformed MarkdownV2 that Telegram's API rejected. This caused the entire message to fall back to plain text, losing all formatting.

The fix copies the regex used by the Slack adapter, which works.

Before and after screenshot:
image

Related Issue

N/A

Type of Change

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

Changes Made

  • gateway/platforms/telegram.py: Updated the markdown link regex in format_message from r'\[([^\]]+)\]\(([^)]+)\)' to r'\[([^\]]+)\]\(([^()]*(?:\([^()]*\)[^()]*)*)\)' to support one level of nested parentheses in URLs.

How to Test

  1. Start the gateway with Telegram enabled
  2. Ask the bot something that produces a Wikipedia link with parentheses in the URL, e.g.: "Give me a list of 10 popular programming languages with Wikipedia links formatted as markdown links"
  3. Before fix: The entire message falls back to plain text, [text](url) shown as literal text, all bold/italic/formatting lost
  4. After fix: Links render as clickable hyperlinks and all other formatting is preserved

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits (fix(scope):, feat(scope):, etc.)
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this fix/feature (no unrelated commits)
  • I've run pytest tests/ -q and all tests pass
  • I've added tests for my changes (required for bug fixes, strongly encouraged for features)
  • I've tested on my platform: macOS 15

Documentation & Housekeeping

  • I've updated relevant documentation (README, docs/, docstrings)
  • I've updated cli-config.yaml.example if I added/changed config keys
  • I've updated CONTRIBUTING.md or AGENTS.md if I changed architecture or workflows
  • I've considered cross-platform impact (Windows, macOS) per the compatibility guide
  • I've updated tool descriptions/schemas if I changed tool behavior

dsocolobsky and others added 2 commits April 10, 2026 18:03
…ersion

The link regex in format_message used [^)]+ for the URL portion, which
  stopped at the first ) character. URLs with nested parentheses (e.g.
  Wikipedia links like Python_(programming_language)) were improperly parsed.

  Use a better regex, which is the same the Slack adapter uses.
@trevorgordon981

Copy link
Copy Markdown

✅ Review Complete - LGTM

Tested in isolation - all tests passing

Test Results

Test Status
test_link_url_parentheses_escaped ✅ PASSED
Full test suite (88 tests) ✅ PASSED

Detailed Analysis

URL parentheses fix

  • The regex change from to the Slack adapter's pattern correctly handles Wikipedia-style URLs with nested parens
  • Example: now parses correctly instead of stopping at the first
  • The fix is minimal (1-line regex change) and well-targeted

Test coverage

  • validates the exact edge case
  • No regressions in the other 87 format tests
  • The fix is validated for both simple and complex URLs with multiple nested parens

Integration

  • Works correctly with MarkdownV2 link conversion
  • No impact on other formatting (bold, italic, code blocks, etc.)
  • The fix is backward compatible with existing URLs

Recommendation

Merge immediately. This fix:

  1. Resolves a real edge case (Wikipedia-style URLs with nested parens)
  2. Is minimal and well-targeted (1-line regex change)
  3. Has comprehensive test coverage
  4. Has no regressions

No additional changes needed. Ready for merge.


Reviewer: @teknium1
Tested on: macOS (Apple Silicon), Python 3.11.15
Date: April 20, 2026

teknium1 added a commit that referenced this pull request Apr 20, 2026
- Fix duplicate 'timezone' import in e2e conftest
- Fix test_text_before_command_not_detected asserting send() is awaited
  when no agent is present in mock setup (text messages don't produce
  command output)
@teknium1

Copy link
Copy Markdown
Contributor

Merged via PR #13187 (#13187). Your commit was cherry-picked onto current main with your authorship preserved in git log. Thanks @dsocolobsky!

@teknium1 teknium1 closed this Apr 20, 2026
ulasbilgen pushed a commit to ulasbilgen/hermes-adhd-agent that referenced this pull request May 1, 2026
…ousResearch#9091, NousResearch#13131

- Fix duplicate 'timezone' import in e2e conftest
- Fix test_text_before_command_not_detected asserting send() is awaited
  when no agent is present in mock setup (text messages don't produce
  command output)
aj-nt pushed a commit to aj-nt/hermes-agent that referenced this pull request May 1, 2026
…ousResearch#9091, NousResearch#13131

- Fix duplicate 'timezone' import in e2e conftest
- Fix test_text_before_command_not_detected asserting send() is awaited
  when no agent is present in mock setup (text messages don't produce
  command output)
Luminet2023 pushed a commit to Luminet2023/hermes-agent that referenced this pull request May 1, 2026
…ousResearch#9091, NousResearch#13131

- Fix duplicate 'timezone' import in e2e conftest
- Fix test_text_before_command_not_detected asserting send() is awaited
  when no agent is present in mock setup (text messages don't produce
  command output)
02356abc pushed a commit to 02356abc/hermes-agent that referenced this pull request May 14, 2026
…ousResearch#9091, NousResearch#13131

- Fix duplicate 'timezone' import in e2e conftest
- Fix test_text_before_command_not_detected asserting send() is awaited
  when no agent is present in mock setup (text messages don't produce
  command output)
gweeteve pushed a commit to gweeteve/hermes-agent that referenced this pull request Jun 2, 2026
…ousResearch#9091, NousResearch#13131

- Fix duplicate 'timezone' import in e2e conftest
- Fix test_text_before_command_not_detected asserting send() is awaited
  when no agent is present in mock setup (text messages don't produce
  command output)
Egavasyug pushed a commit to Egavasyug/hermes-agent that referenced this pull request Jun 10, 2026
…ousResearch#9091, NousResearch#13131

- Fix duplicate 'timezone' import in e2e conftest
- Fix test_text_before_command_not_detected asserting send() is awaited
  when no agent is present in mock setup (text messages don't produce
  command output)
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.

3 participants