fix(telegram): honor message.quote for partial-quote reply context - #22632
Closed
briandevans wants to merge 1 commit into
Closed
briandevans wants to merge 1 commit into
briandevans wants to merge 1 commit into
Conversation
When a Telegram user replies using the native quote feature to select only part of a prior message, _build_message_event was injecting the ENTIRE replied-to message into reply_to_text via message.reply_to_message.text/caption. python-telegram-bot exposes the user-selected substring as message.quote (TextQuote.text); we now prefer that and fall back to the full replied-to text only when no native quote is present. The agent-visible "[Replying to: \"...\"]" prefix can otherwise expand the user's narrow quote into the full prior message, causing the agent to act on unrelated actionable-looking text the user did not select (e.g. multi-item briefings where the user quotes one bullet but the prefix injects every bullet). Falls back cleanly when message.quote is absent (PTB <21 or replies that don't quote a substring). Fixes NousResearch#22619 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes Telegram reply-context extraction so that when a user replies with Telegram’s native partial-quote feature, the gateway prefers the user-selected substring (message.quote.text) for reply_to_text instead of injecting the entire replied-to message body.
Changes:
- Update
TelegramAdapter._build_message_eventto prefermessage.quote.textwhen present/truthy, with a fallback toreply_to_message.textorreply_to_message.caption. - Add a focused regression test suite covering partial-quote, no-quote fallback, caption fallback, and empty-quote defensive behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
gateway/platforms/telegram.py |
Prefer Telegram native partial quote text for reply_to_text, falling back to full replied-to message text/caption. |
tests/gateway/test_telegram_reply_quote.py |
Adds regression coverage for partial-quote handling and fallbacks. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This was referenced May 9, 2026
Collaborator
|
Merged via salvage PR #22676. Your commit was cherry-picked onto current main with your authorship preserved in git log (rebase-merge). Thanks for the contribution! |
2 tasks
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.
Summary
When a Telegram user replies using Telegram's native partial-quote feature (selecting a substring of a prior message),
_build_message_eventwas injecting the entire replied-to message intoreply_to_text. Now it prefersmessage.quote.text(the user-selected substring) and falls back to the full replied-to message only when no native quote is present.The bug
gateway/platforms/telegram.py::_build_message_eventextracted reply context as:message.reply_to_message.textis always the full prior message body. If Hermes sent a multi-section message and the user replied to one quoted bullet with "mark this one as done", the agent saw:python-telegram-bot >=21exposesMessage.quote: Optional[TextQuote]which carriesTextQuote.textfor the user-selected substring (PTB docs: "For replies that quote part of the original message, the quoted part of the message."). The repo pinspython-telegram-bot[webhooks]>=22.6,<23(pyproject.toml:58) soquoteis reliably available — the production code just wasn't reading it.The fix
Read
getattr(message, "quote", None)and preferquote.textwhen truthy. Fall back toreply_to_message.text or reply_to_message.captionfor replies without a native quote (older PTB, or replies that don't select a substring) and for replied-to media messages where only the caption is set.getattr+is not Nonechecks keep the path safe ifquoteis ever absent or empty.Test plan
quote.textdefensive casetests/gateway/test_dm_topics.py,test_telegram_reply_mode.py,test_reply_to_injection.py(70 tests passed)test_native_partial_quote_used_as_reply_to_textfails withAssertionError: 'Briefing:\\n- Item A:...' == 'Item B: rotate keys'— proves the test exercises the new code pathRun focused tests:
Related
MessageEventschema and downstream_prepare_inbound_message_textinjection are unchanged.