fix: send VK keyboard payloads as JSON
This commit is contained in:
1 parent
3d58b4fc9b
commit
94665a24ad
2 files changed
+20
-1
No files matched your search
+11
-1
@@ -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."""
|
||||
|
||||
@@ -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()
|
||||
Reference in new issue
Block a user