Files
hermes-agent/plugins/platforms/telegram/callback_answer.py
Teknium bf77c21427 fix(gateway): picker cards and the /update prompt answer only to their opener
A picker card (/model, /reasoning, /fast, the usage-limit switch offer)
and the /update Yes/No prompt only checked the chat allowlist on a tap,
and a pending /update prompt took the next message ANY admitted member
sent in the session as its answer. In a shared group with
group_allow_admin_from set, a member refused /reasoning or /model could
tap the admin's open card and apply it, and anyone's chat text answered
the admin's "Restore local changes now?".

The gateway now records the opener (sender id, name, chat scope) when it
sends the card (prompt_authority on the selection callback) and in the
/update pending marker. Every renderer refuses another member's tap with
the existing localized "Only <name> can answer this approval." note and
leaves the card open: Telegram mp:/cp:/update_prompt:, Discord
ModelPickerView/ChoicePickerView/UpdatePromptView (one _gate), Slack
model picker (ephemeral), Matrix picker reactions, Feishu choice picker
and update card (toast). A non-opener's typed text during a pending
/update prompt falls through as an ordinary message. The check is
gateway.approval_actor.may_answer, the same rule exec approvals use:
the opener and the chat scope's admins always pass; a sender-less card
(shared observed route) keeps the old anyone-admitted behaviour.

QQBot's update button already accepts only the session's own user.
picker_refusal / update_prompt_refusal join approval_refusal in the
published plugin API so platform plugins can apply the same check.
2026-11-10 14:06:57 -08:00

91 lines
4.0 KiB
Python

"""Callback-query answers that survive a tap on an expired Telegram card.
Telegram refuses ``answerCallbackQuery`` once the query is older than its window, with
``BadRequest: Query is too old and response timeout expired or query id is invalid``. The
adapter awaited that call unguarded in every button handler, so a tap on a card that had been
sitting in the chat raised out of the handler: PTB logged the update as failed, whatever the
handler had left to do never ran (a model switch kept its picker state, since the pop sits
after the toast), and the tap produced no feedback at all.
``answer_callback`` is the one choke point for those answers. A refused answer is reported as
``False`` instead of raising. ``answer_or_note`` covers taps on a menu that has already expired —
the one case where the toast was the only thing the user would have seen, and "Picker expired —
use /model again." is still a usable next step as a normal message, posted in the chat and forum
topic the card lives in. Every other error keeps raising: a broken answer must stay visible.
"""
import logging
from typing import Any, Optional
logger = logging.getLogger(__name__)
# Telegram truncates ``answerCallbackQuery`` text at 200 chars.
TOAST_LIMIT = 200
# Wording of the errors that mean "this tap arrived after the query expired".
_STALE_QUERY_PHRASES = (
"query is too old",
"query id is invalid",
"response timeout expired",
)
def is_stale_query_error(exc: BaseException) -> bool:
"""True when *exc* is Telegram refusing an answer because the callback query expired.
Matched on the wording first (cheap, and it works where python-telegram-bot is not
importable), then narrowed to PTB's ``BadRequest`` when the library is present so an
unrelated failure that happens to carry the text still fails loudly.
"""
text = str(exc).lower()
if not any(phrase in text for phrase in _STALE_QUERY_PHRASES):
return False
try:
from telegram.error import BadRequest
except ImportError:
return True
return isinstance(exc, BadRequest)
async def answer_callback(query: Any, text: Optional[str] = None, *, show_alert: bool = False) -> bool:
"""Ack a button tap; ``False`` when Telegram refused it because the query expired."""
try:
if text is None:
await query.answer()
elif show_alert:
await query.answer(text=text, show_alert=True)
else:
await query.answer(text=text)
except Exception as exc:
if not is_stale_query_error(exc):
raise
logger.debug("[Telegram] callback answer refused for an expired query", exc_info=True)
return False
return True
async def answer_or_note(adapter: Any, query: Any, text: str) -> None:
"""Answer an expired-menu tap; when Telegram refused the late answer, post *text* as a normal
message through the adapter's topic-aware control send, in the tapped card's forum topic."""
message = getattr(query, "message", None)
if await answer_callback(query, text) or getattr(message, "chat_id", None) is None:
return
thread_id = getattr(message, "message_thread_id", None)
in_topic = thread_id is not None and getattr(message, "is_topic_message", False) is True
try:
await adapter._send_control_message(
str(message.chat_id), text, parse_mode=None, thread_id=str(thread_id) if in_topic else None, metadata=None)
except Exception:
logger.debug("[Telegram] expired-tap chat note could not be sent", exc_info=True)
async def refuse_tap(query: Any, requester: Optional[str]) -> bool:
"""Answer a tap on someone else's prompt (``gateway.approval_actor`` refused it: *requester* is
the opener's name) with the localized "only X can answer" alert; the card stays open for them."""
if not requester:
return False
from gateway.approval_actor import refusal_text
await answer_callback(query, refusal_text(requester)[:TOAST_LIMIT], show_alert=True)
return True