feat(applications): add keyboard flow and export outbox
This commit is contained in:
1 parent
cfeee5a814
commit
27bcc291db
11 files changed
+255
-10
No files matched your search
+56
-3
@@ -16,6 +16,7 @@ from .history import CalculationStore
|
||||
from .image_host import ImageHostClient
|
||||
from .identity import Identity, IdentityDirectory
|
||||
from .applications import APPLICATION_TYPES, ApplicationError, ApplicationStore, validate_application
|
||||
from .google_sheets_export import GoogleSheetsExportService
|
||||
|
||||
"""
|
||||
Обработка VK-бота:
|
||||
@@ -445,6 +446,47 @@ class ApplicationDialog:
|
||||
def active(self, actor_id: int) -> bool:
|
||||
return actor_id in self.states
|
||||
|
||||
@classmethod
|
||||
def is_application_command(cls, text: str) -> bool:
|
||||
return text.strip().lower() in {"/заявка", "/заявки", *cls._commands}
|
||||
|
||||
@staticmethod
|
||||
def _button(label: str, payload: str, color: str = "secondary") -> dict[str, Any]:
|
||||
return {
|
||||
"action": {"type": "text", "label": label, "payload": json.dumps({"command": payload}, ensure_ascii=False)},
|
||||
"color": color,
|
||||
}
|
||||
|
||||
def keyboard_for(self, actor_id: int) -> dict[str, Any] | None:
|
||||
"""Контекстная VK-клавиатура для всех выбираемых шагов заявки."""
|
||||
state = self.states.get(actor_id)
|
||||
if not state:
|
||||
return None
|
||||
cancel = self._button("Отмена", "menu:cancel", "negative")
|
||||
if state.get("application_menu"):
|
||||
return {"one_time": False, "buttons": [
|
||||
[self._button("Пожертвовать перо", "application:feathers", "secondary")],
|
||||
[self._button("Стать бельчонком", "application:kitten", "primary")],
|
||||
[self._button("Стать летягой", "application:flying", "primary")],
|
||||
[self._button("Стать куратором", "application:curator", "primary")],
|
||||
[cancel],
|
||||
]}
|
||||
if "name" not in state:
|
||||
return {"one_time": False, "buttons": [[cancel]]}
|
||||
if state["type"] in ("kitten", "curator"):
|
||||
return {"one_time": False, "buttons": [
|
||||
[self._button("Готово", "application:done", "positive")], [cancel],
|
||||
]}
|
||||
if state["type"] == "flying_squirrel":
|
||||
if "vk_link" not in state:
|
||||
return {"one_time": False, "buttons": [[self._button("Нет ссылки VK", "application:skip-vk")], [cancel]]}
|
||||
if "practiced_before" not in state:
|
||||
return {"one_time": False, "buttons": [[
|
||||
self._button("Да", "application:practice-yes", "positive"),
|
||||
self._button("Нет", "application:practice-no", "negative"),
|
||||
], [cancel]]}
|
||||
return {"one_time": False, "buttons": [[cancel]]}
|
||||
|
||||
def queue_attachments(self, actor_id: int, attachments: list[dict[str, Any]]) -> None:
|
||||
if actor_id in self.states:
|
||||
target = self.states[actor_id].setdefault("attachments", [])
|
||||
@@ -505,6 +547,7 @@ class ApplicationDialog:
|
||||
def handle(self, actor_id: int, peer_id: int, text: str) -> str | None:
|
||||
command = text.strip().lower()
|
||||
if command in ("/заявка", "/заявки"):
|
||||
self.states[actor_id] = {"application_menu": True}
|
||||
return self._menu()
|
||||
if command in self._commands:
|
||||
return self._start(actor_id, self._commands[command])
|
||||
@@ -555,6 +598,7 @@ class BelkiBot:
|
||||
)
|
||||
self.application_store = ApplicationStore.from_environment()
|
||||
self.application_dialog = ApplicationDialog(identities, self.application_store)
|
||||
self.application_exporter = GoogleSheetsExportService(self.application_store)
|
||||
|
||||
def response_for_text(self, text: str) -> str:
|
||||
"""Обработка текстового сообщения и возврат ответа."""
|
||||
@@ -581,16 +625,20 @@ class BelkiBot:
|
||||
if peer_id is not None:
|
||||
sender = int(sender_id or peer_id)
|
||||
photos = extract_photo_attachments(message)
|
||||
if self.application_dialog.is_application_command(text):
|
||||
self.dialog._reset(sender)
|
||||
if photos:
|
||||
self.dialog.queue_attachments(sender, photos)
|
||||
self.application_dialog.queue_attachments(sender, photos)
|
||||
if self.application_dialog.active(sender) or self.application_dialog.is_application_command(text):
|
||||
self.application_dialog.queue_attachments(sender, photos)
|
||||
else:
|
||||
self.dialog.queue_attachments(sender, photos)
|
||||
if text:
|
||||
response = self.application_dialog.handle(sender, int(peer_id), text)
|
||||
if response is None:
|
||||
response = self.dialog.handle(int(peer_id), text, user_id=sender)
|
||||
keyboard = self.dialog.keyboard_for(sender)
|
||||
else:
|
||||
keyboard = None
|
||||
keyboard = self.application_dialog.keyboard_for(sender) or self.dialog.keyboard_for(sender)
|
||||
self.client.send(int(peer_id), response, keyboard=keyboard)
|
||||
|
||||
def notify_accepted_applications(self) -> None:
|
||||
@@ -619,6 +667,10 @@ class BelkiBot:
|
||||
return {
|
||||
"menu:lu": "/лу", "menu:zu": "/зу", "menu:training": "/урок",
|
||||
"menu:inspection": "/осмотр", "menu:help": "/help", "menu:cancel": "/отмена", "menu:application": "/заявка",
|
||||
"application:feathers": "/перо", "application:kitten": "/бельчонок",
|
||||
"application:flying": "/летяга", "application:curator": "/куратор",
|
||||
"application:skip-vk": "нет", "application:practice-yes": "да", "application:practice-no": "нет",
|
||||
"application:done": "готово",
|
||||
"quick:today": "__today__", "quick:no_resources": "без ресурсов",
|
||||
"place:hollow": "дупло", "place:crevice": "расщелина",
|
||||
"skill:lu": "ЛУ", "skill:zu": "ЗУ",
|
||||
@@ -653,6 +705,7 @@ class BelkiBot:
|
||||
def notification_loop() -> None:
|
||||
while True:
|
||||
self.notify_accepted_applications()
|
||||
self.application_exporter.sync_pending()
|
||||
time.sleep(15)
|
||||
threading.Thread(target=notification_loop, name="belki-application-notifier", daemon=True).start()
|
||||
for event in self.events():
|
||||
|
||||
Reference in new issue
Block a user