telegram_resolved_prompt.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334
  1. """Resolved-prompt edits that keep the request visible, kept out of the adapter facade.
  2. Replacing an approval or slash-confirm prompt with only the decision ("✅ Approved once by Ana")
  3. erases what was approved, so a shared chat loses its record. The edit keeps the prompt's own
  4. rendering (``Message.text_html`` rebuilds it from the entities, code block included), drops the
  5. "if you don't answer within N minutes" line that no longer applies, and appends the decision.
  6. """
  7. from __future__ import annotations
  8. import html
  9. import logging
  10. from typing import Any
  11. logger = logging.getLogger(__name__)
  12. async def edit_keeping_prompt(adapter: Any, query: Any, decision: str) -> None:
  13. """Append *decision* to the prompt and drop its buttons; the adapter's decision-only edit runs when
  14. the prompt has no readable body or the appended line pushes it past Telegram's length cap."""
  15. from telegram.error import BadRequest, NetworkError
  16. body = getattr(query.message, "text_html", None) if query.message else None
  17. if isinstance(body, str) and body:
  18. body = body.replace(adapter._ea_deadline_line(), "") # same escaping as text_html
  19. try:
  20. await query.edit_message_text(text=f"{body}\n\n— {html.escape(decision)}", parse_mode="HTML", reply_markup=None)
  21. return
  22. except BadRequest: # too long / unparsable: the decision-only edit below still clears the buttons
  23. pass
  24. except NetworkError as exc: # network/timeout: the edit may have landed, so keep the prompt as is
  25. logger.debug("Telegram resolved-prompt edit failed: %s", exc)
  26. return
  27. await adapter._edit_md_quiet(query, decision)