feat: add quick choices to VK dialog
This commit is contained in:
1 parent
94665a24ad
commit
fe70303e87
2 files changed
+53
No files matched your search
@@ -4,6 +4,7 @@ import json
|
||||
import os
|
||||
import random
|
||||
import re
|
||||
from datetime import datetime
|
||||
from typing import Any, Iterator
|
||||
from urllib.parse import urlencode
|
||||
from urllib.request import Request, urlopen
|
||||
@@ -316,6 +317,9 @@ class BelkiBot:
|
||||
return {
|
||||
"menu:lu": "/лу", "menu:zu": "/зу", "menu:training": "/урок",
|
||||
"menu:inspection": "/осмотр", "menu:help": "/help", "menu:cancel": "/отмена",
|
||||
"quick:today": "__today__", "quick:no_resources": "без ресурсов",
|
||||
"place:hollow": "дупло", "place:crevice": "расщелина",
|
||||
"skill:lu": "ЛУ", "skill:zu": "ЗУ",
|
||||
"confirm:yes": "да", "confirm:no": "нет",
|
||||
"independence:self": "сам", "independence:nonself": "несам",
|
||||
}.get(str(value))
|
||||
@@ -402,6 +406,16 @@ class BelkiDialog:
|
||||
}
|
||||
if state and state.get("mode") == "coaching":
|
||||
next_key = next((key for key, _ in self._ORDERS["coaching"] if not self._has_field(state, key)), None)
|
||||
if next_key == "skill":
|
||||
return {"one_time": False, "buttons": [
|
||||
[self._button("ЛУ", "skill:lu", "primary"), self._button("ЗУ", "skill:zu", "primary")],
|
||||
[self._button("Отмена", "menu:cancel", "negative")],
|
||||
]}
|
||||
if next_key == "date":
|
||||
return {"one_time": False, "buttons": [
|
||||
[self._button("Сегодня", "quick:today", "primary")],
|
||||
[self._button("Отмена", "menu:cancel", "negative")],
|
||||
]}
|
||||
if next_key == "independence":
|
||||
return {
|
||||
"one_time": False,
|
||||
@@ -409,6 +423,18 @@ class BelkiDialog:
|
||||
[self._button("Отмена", "menu:cancel", "negative")]],
|
||||
}
|
||||
return {"one_time": False, "buttons": [[self._button("Отмена", "menu:cancel", "negative")]]}
|
||||
if state and state.get("mode") == "inspection":
|
||||
next_key = next((key for key, _ in self._ORDERS["inspection"] if not self._has_field(state, key)), None)
|
||||
if next_key == "place":
|
||||
return {"one_time": False, "buttons": [
|
||||
[self._button("Дупло", "place:hollow", "primary"), self._button("Расщелина", "place:crevice", "primary")],
|
||||
[self._button("Отмена", "menu:cancel", "negative")],
|
||||
]}
|
||||
if next_key == "resources":
|
||||
return {"one_time": False, "buttons": [
|
||||
[self._button("Без ресурсов", "quick:no_resources", "secondary")],
|
||||
[self._button("Отмена", "menu:cancel", "negative")],
|
||||
]}
|
||||
if state:
|
||||
return {"one_time": False, "buttons": [[self._button("Отмена", "menu:cancel", "negative")]]}
|
||||
return {
|
||||
@@ -555,6 +581,9 @@ class BelkiDialog:
|
||||
def _consume_field(self, state: dict[str, Any], text: str) -> str:
|
||||
key = next((key for key, _ in self._ORDERS[state["mode"]] if not self._has_field(state, key)), None)
|
||||
if key == "date":
|
||||
if text.lower() in ("сегодня", "__today__"):
|
||||
state[key] = datetime.now().strftime("%d.%m")
|
||||
return self._ask_next(state)
|
||||
if not re.fullmatch(r"\d{2}\.\d{2}", text):
|
||||
return "Дата должна быть в формате ДД.ММ, например 15.08."
|
||||
state[key] = text
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import unittest
|
||||
from datetime import datetime
|
||||
from tempfile import TemporaryDirectory
|
||||
|
||||
from app.belki.identity import Identity, IdentityDirectory
|
||||
@@ -154,6 +155,29 @@ class VkConversationTests(unittest.TestCase):
|
||||
labels = [button["action"]["label"] for row in dialog.keyboard_for(503)["buttons"] for button in row]
|
||||
self.assertEqual(labels[:2], ["Сам", "Несам"])
|
||||
|
||||
def test_quick_buttons_cover_safe_choices(self):
|
||||
dialog = BelkiDialog()
|
||||
dialog.handle(504, "15.08 11:00 11:20 Белка 1 Котёнок 2")
|
||||
labels = [button["action"]["label"] for row in dialog.keyboard_for(504)["buttons"] for button in row]
|
||||
self.assertIn("ЛУ", labels)
|
||||
dialog = BelkiDialog()
|
||||
dialog.handle(504, "/лу")
|
||||
labels = [button["action"]["label"] for row in dialog.keyboard_for(504)["buttons"] for button in row]
|
||||
self.assertIn("Сегодня", labels)
|
||||
response = dialog.handle(504, "__today__")
|
||||
self.assertIn("начал", response.lower())
|
||||
self.assertEqual(dialog._states[504]["date"], datetime.now().strftime("%d.%m"))
|
||||
|
||||
inspection = BelkiDialog()
|
||||
inspection.handle(505, "/осмотр")
|
||||
labels = [button["action"]["label"] for row in inspection.keyboard_for(505)["buttons"] for button in row]
|
||||
self.assertEqual(labels[:2], ["Дупло", "Расщелина"])
|
||||
inspection.handle(505, "дупло")
|
||||
inspection.handle(505, "0")
|
||||
inspection.handle(505, "Серолап [2]")
|
||||
labels = [button["action"]["label"] for row in inspection.keyboard_for(505)["buttons"] for button in row]
|
||||
self.assertIn("Без ресурсов", labels)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user