feat: include reply/quote context in Claude prompt - #194
timurvafin wants to merge 1 commit into
Conversation
When a user replies to a message (especially with a highlighted
fragment — Telegram's partial-quote feature, Bot API 7.0+), the bot
previously read only `update.message.text` and dropped the quoted
context, so Claude had to guess what the user was referring to.
This PR extracts the quoted fragment (partial quote has priority
over the full replied-to message) and prepends it to the user's
new text as a markdown blockquote::
> quoted fragment line 1
> quoted fragment line 2
new user text
Works for both agentic_text and classic handle_text via a shared
helper `src/bot/utils/quote_prompt.build_user_prompt`. Falls back to
plain `message.text` when there's no reply/quote, preserving existing
behaviour.
Also handles replies to media (uses `caption` when `text` is absent)
and guards against MagicMock attributes in unit-test fixtures.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
RichardAtCT
left a comment
There was a problem hiding this comment.
Nice change — small, well tested, and wired the same way in both modes. Tests and lint pass.
One thing before I merge. The quoted text goes into the prompt as a bare markdown blockquote, with nothing telling Claude where it came from (src/bot/utils/quote_prompt.py:38).
In a group, the replied-to message can come from anyone, not just an allowed user. Please add a short label, for example "Quoted message (reference only):", so Claude treats it as material to read rather than instructions to follow.
One line. Then I will merge.
|
Hi @timurvafin — I prepared the small follow-up requested in the review:
The commit is based directly on the current PR HEAD ( It can be applied with: git fetch https://github.com/FrundlesTian/claude-code-telegram.git fix/pr-194-quote-label
git cherry-pick f16ef49431a67fba503731f7846d9c9346ba475eVerification: all 540 tests pass; Black, isort, flake8, and mypy are clean. |
|
Hi @RichardAtCT — quick review update: I prepared a single-commit patch for the outstanding request to label quoted content as reference-only context, including updates to all existing quote-prompt tests. The patch is based directly on the current PR HEAD ( All 540 tests pass, and Black, isort, flake8, and mypy are clean. The PR branch itself has not been updated yet; it is currently waiting for @timurvafin to cherry-pick the patch. Once applied, it should address the remaining review request. |
Summary
When a user replies to a message (especially with a highlighted fragment — Telegram's partial-quote feature, Bot API 7.0+), the bot was reading only
update.message.textand dropping the quoted context. Claude then had to guess what the user was referring to.This PR extracts the quoted fragment and prepends it to the user's new text as a markdown blockquote, so Claude sees both the fragment and the response in a single prompt.
Before vs after
Before:
After:
Design
message.quote.text(partial quote, Bot API 7.0+) wins over the fullreply_to_message.text. If the user highlighted a fragment, that's what they mean.caption.> line\n> line\n\ntext), natural for Claude.src/bot/utils/quote_prompt.py— bothagentic_textand classichandle_textgo through it, so classic mode gets the feature for free.Changes
src/bot/utils/quote_prompt.py— newbuild_user_prompt(message) -> strhelper with priority logic and markdown-blockquote rendering.src/bot/orchestrator.py—agentic_textusesbuild_user_prompt(update.message)instead ofupdate.message.text.src/bot/handlers/message.py—handle_text_message(classic) same wiring;.pdfunaffected.tests/unit/test_bot/test_quote_prompt.py— 10 tests: plain/reply/partial-quote priority/multiline/media-caption/edge cases (no-text, no-reply, empty user text).Live verification
Reply via Telegram client with highlighted fragment of Claude's previous answer; bot's database prompt now includes the blockquote; Claude's reply is contextually correct.
Test plan
make test— 540 passed (10 new)black/isort/flake8clean🤖 Generated with Claude Code