feat: show VK photos in calculation admin

This commit is contained in:
nyan404 committed 2026-08-16 15:56:17 +03:00
1 parent 6bbd8d4b0b
commit 20b434dbcd
6 files changed
+84 -5

No files matched your search

+50 -3
View File
@@ -247,6 +247,33 @@ def build_message_request(peer_id: int, message: str, access_token: str, api_ver
return request
def extract_photo_attachments(message: dict[str, Any]) -> list[dict[str, Any]]:
"""Normalize VK photo attachments for durable calculation history."""
result: list[dict[str, Any]] = []
for attachment in message.get("attachments", []) or []:
if attachment.get("type") != "photo":
continue
photo = attachment.get("photo") or {}
sizes = photo.get("sizes") or []
largest = max(sizes, key=lambda item: item.get("width", 0) * item.get("height", 0), default={})
owner_id = photo.get("owner_id")
photo_id = photo.get("id")
if owner_id is None or photo_id is None:
continue
result.append({
"type": "photo",
"owner_id": owner_id,
"id": photo_id,
"access_key": photo.get("access_key"),
"url": largest.get("url"),
"width": largest.get("width"),
"height": largest.get("height"),
"attachment": f"photo{owner_id}_{photo_id}"
+ (f"_{photo['access_key']}" if photo.get("access_key") else ""),
})
return result
class VkApiClient:
def __init__(self, access_token: str, api_version: str = "5.199") -> None:
self.access_token = access_token
@@ -298,10 +325,14 @@ class BelkiBot:
peer_id = message.get("peer_id")
sender_id = message.get("from_id", message.get("sender_id", peer_id))
text = self._button_text(message) or message.get("text", "")
if peer_id is not None and text:
if peer_id is not None:
sender = int(sender_id or peer_id)
response = self.dialog.handle(int(peer_id), text, user_id=sender)
self.client.send(int(peer_id), response, keyboard=self.dialog.keyboard_for(sender))
photos = extract_photo_attachments(message)
if photos:
self.dialog.queue_attachments(sender, photos)
if text:
response = self.dialog.handle(int(peer_id), text, user_id=sender)
self.client.send(int(peer_id), response, keyboard=self.dialog.keyboard_for(sender))
@staticmethod
def _button_text(message: dict[str, Any]) -> str | None:
@@ -373,12 +404,25 @@ class BelkiDialog:
def __init__(self, identity_directory: IdentityDirectory | None = None, *, onboarding: bool = False,
calculation_store: CalculationStore | None = None) -> None:
self._states: dict[int, dict[str, Any]] = {}
self._pending_attachments: dict[int, list[dict[str, Any]]] = {}
self.identity_directory = identity_directory or IdentityDirectory()
self.onboarding = onboarding
self.calculation_store = calculation_store
def _reset(self, peer_id: int) -> None:
self._states.pop(peer_id, None)
self._pending_attachments.pop(peer_id, None)
def queue_attachments(self, actor_id: int, attachments: list[dict[str, Any]]) -> None:
if attachments:
pending = self._pending_attachments.setdefault(actor_id, [])
pending.extend(attachments)
del pending[:-10]
def _attach_pending(self, actor_id: int) -> None:
pending = self._pending_attachments.pop(actor_id, [])
if pending and actor_id in self._states:
self._states[actor_id].setdefault("attachments", []).extend(pending)
@staticmethod
def _button(label: str, payload: str, color: str = "secondary") -> dict[str, Any]:
@@ -706,6 +750,8 @@ class BelkiDialog:
self._reset(actor_id)
return "Текущий ввод отменён. Для нового отчёта используйте /лу, /зу или /help."
state = self._states.get(actor_id)
if state:
self._attach_pending(actor_id)
if state and state.get("confirm"):
if text.lower() in ("да", "подтвердить", "подтверждаю"):
payload = {key: value for key, value in state.items() if key not in {"confirm", "escort", "kitten", "character", "independence", "_independence_provided"}}
@@ -723,6 +769,7 @@ class BelkiDialog:
if not state:
response = self._start_bulk(actor_id, text)
if response is not None:
self._attach_pending(actor_id)
return response
return "Начните с /лу или /зу. Для списка команд отправьте /help."
try: