diff --git a/app/belki/vk_bot.py b/app/belki/vk_bot.py index 22fb5df..e4daf2b 100644 --- a/app/belki/vk_bot.py +++ b/app/belki/vk_bot.py @@ -49,6 +49,8 @@ class VkError(RuntimeError): def _person(value: str) -> tuple[str, int]: # Users often copy the example sentence together with its final period. value = value.strip().rstrip(".!?").strip() + # VK users sometimes add a grouping space inside a long ID: [1 566767]. + value = re.sub(r"\[(\d[\d\s]*)\]", lambda match: f"[{match.group(1).replace(' ', '')}]", value) match = _PERSON_RE.fullmatch(value) if not match: raise ValueError("Укажите имя и ID в формате Имя [12345]") @@ -620,9 +622,12 @@ class BelkiDialog: f"Ресурсы: {resources}\n" "Подтвердить? Ответьте «да» или «нет»." ) - extra_line = ( - f"Дополнительные котята: {', '.join(f'{name} [{kitten_id}]' for name, kitten_id, _ in state.get('extra_kittens', []))}\n" - if state.get("extra_kittens") else "" + kittens = [(state["kitten_name"], state["kitten_id"])] + [ + (name, kitten_id) for name, kitten_id, _ in state.get("extra_kittens", []) + ] + kitten_lines = "\n".join( + f"Котёнок {index}: {name} [{kitten_id}]" + for index, (name, kitten_id) in enumerate(kittens, start=1) ) return ( "Я распознал данные:\n" @@ -630,9 +635,8 @@ class BelkiDialog: f"Дата: {state['date']}\n" f"Время: {state['start']}–{state['end']}\n" f"Сопровождающий: {state['escort_name']} [{state['escort_id']}]\n" - f"Котёнок: {state['kitten_name']} [{state['kitten_id']}]\n" + f"{kitten_lines}\n" f"Статус: {state['kitten_independence']}\n" - f"{extra_line}" "Подтвердить? Ответьте «да» или «нет»." ) diff --git a/tests/test_vk_conversation.py b/tests/test_vk_conversation.py index 5117988..c883992 100644 --- a/tests/test_vk_conversation.py +++ b/tests/test_vk_conversation.py @@ -51,6 +51,17 @@ class VkConversationTests(unittest.TestCase): self.assertEqual(dialog._states[79]["kitten_id"], 1566767) self.assertEqual(dialog._states[79]["extra_kittens"][0][1], 123456) + def test_confirmation_numbers_kittens_and_normalizes_spaced_id(self): + dialog = BelkiDialog() + dialog.handle(80, "/лу") + for answer in ("15.08", "11:00", "16:41", "Дикая Вишня [1604614]"): + dialog.handle(80, answer) + response = dialog.handle(80, "Беззубик [1 566767], Малыш [123456].") + dialog.handle(80, "сам") + self.assertIn("Котёнок 1: Беззубик [1566767]", dialog._confirmation(dialog._states[80])) + self.assertIn("Котёнок 2: Малыш [123456]", dialog._confirmation(dialog._states[80])) + self.assertIn("самостоятель", response.lower()) + def test_bulk_input_is_detected_and_confirmed(self): dialog = BelkiDialog() confirmation = dialog.handle(