diff --git a/app/belki/vk_bot.py b/app/belki/vk_bot.py index 0c82461..cb66924 100644 --- a/app/belki/vk_bot.py +++ b/app/belki/vk_bot.py @@ -378,7 +378,17 @@ class BelkiDialog: @staticmethod def _button(label: str, payload: str, color: str = "secondary") -> dict[str, Any]: - return {"action": {"type": "text", "label": label, "payload": payload}, "color": color} + # VK validates button payload as JSON, even though the API field is + # documented as a string. Keep the command inside a small object; the + # event parser already accepts this shape. + 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]: """Return a context-aware VK keyboard for the author's current dialog state.""" diff --git a/tests/test_vk_bot.py b/tests/test_vk_bot.py index 679c915..2c82dbd 100644 --- a/tests/test_vk_bot.py +++ b/tests/test_vk_bot.py @@ -2,6 +2,7 @@ import unittest import json from app.belki.vk_bot import ( + BelkiDialog, HELP_TEXT, build_message_request, parse_text_command, @@ -39,6 +40,14 @@ class VkBotTests(unittest.TestCase): self.assertEqual(BelkiBot._button_text({"payload": "menu:lu"}), "/лу") self.assertEqual(BelkiBot._button_text({"payload": '{"command":"confirm:yes"}'}), "да") + def test_keyboard_button_payloads_are_valid_json(self): + keyboard = BelkiDialog().keyboard_for(503) + for row in keyboard["buttons"]: + for button in row: + payload = json.loads(button["action"]["payload"]) + self.assertIsInstance(payload, dict) + self.assertIn("command", payload) + if __name__ == "__main__": unittest.main()