python-vk-bot 0.6.0__py3-none-any.whl → 0.7.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- python_vk_bot/limits.py +12 -0
- python_vk_bot/models/keyboards.py +4 -2
- python_vk_bot/models/messages.py +69 -1
- {python_vk_bot-0.6.0.dist-info → python_vk_bot-0.7.0.dist-info}/METADATA +1 -1
- {python_vk_bot-0.6.0.dist-info → python_vk_bot-0.7.0.dist-info}/RECORD +7 -6
- {python_vk_bot-0.6.0.dist-info → python_vk_bot-0.7.0.dist-info}/WHEEL +0 -0
- {python_vk_bot-0.6.0.dist-info → python_vk_bot-0.7.0.dist-info}/licenses/LICENSE +0 -0
python_vk_bot/limits.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"""Keyboard limits for VK API 5.199; see docs/development/api-compatibility.md."""
|
|
2
|
+
|
|
3
|
+
from typing import Final
|
|
4
|
+
|
|
5
|
+
MAX_INLINE_BUTTONS: Final[int] = 10
|
|
6
|
+
MAX_REPLY_BUTTONS: Final[int] = 40
|
|
7
|
+
MAX_BUTTONS_PER_ROW: Final[int] = 5
|
|
8
|
+
MAX_INLINE_ROWS: Final[int] = 6
|
|
9
|
+
MAX_REPLY_ROWS: Final[int] = 10
|
|
10
|
+
MAX_BUTTON_LABEL_LENGTH: Final[int] = 40
|
|
11
|
+
MAX_LOCATION_ROW_BUTTONS: Final[int] = 2
|
|
12
|
+
MAX_VKPAY_ROW_BUTTONS: Final[int] = 1
|
|
@@ -15,6 +15,8 @@ from pydantic import (
|
|
|
15
15
|
model_serializer,
|
|
16
16
|
)
|
|
17
17
|
|
|
18
|
+
from python_vk_bot.limits import MAX_BUTTON_LABEL_LENGTH
|
|
19
|
+
|
|
18
20
|
|
|
19
21
|
class ButtonColor(StrEnum):
|
|
20
22
|
"""Colors documented by the VK keyboard guide."""
|
|
@@ -64,7 +66,7 @@ class _Button(BaseModel):
|
|
|
64
66
|
|
|
65
67
|
|
|
66
68
|
class _LabeledButton(_Button):
|
|
67
|
-
label: str = Field(repr=False)
|
|
69
|
+
label: str = Field(repr=False, max_length=MAX_BUTTON_LABEL_LENGTH)
|
|
68
70
|
color: ButtonColor = ButtonColor.SECONDARY
|
|
69
71
|
|
|
70
72
|
|
|
@@ -84,7 +86,7 @@ class OpenLinkButton(_Button):
|
|
|
84
86
|
"""Open a URL using VK's link action, without a configurable color."""
|
|
85
87
|
|
|
86
88
|
type: Literal[ButtonType.OPEN_LINK] = ButtonType.OPEN_LINK
|
|
87
|
-
label: str = Field(repr=False)
|
|
89
|
+
label: str = Field(repr=False, max_length=MAX_BUTTON_LABEL_LENGTH)
|
|
88
90
|
link: str = Field(repr=False)
|
|
89
91
|
|
|
90
92
|
|
python_vk_bot/models/messages.py
CHANGED
|
@@ -1,6 +1,26 @@
|
|
|
1
1
|
"""The message fields used by this framework, not the entire VK schema."""
|
|
2
2
|
|
|
3
|
-
from
|
|
3
|
+
from typing import Self
|
|
4
|
+
|
|
5
|
+
from pydantic import (
|
|
6
|
+
ConfigDict,
|
|
7
|
+
Field,
|
|
8
|
+
JsonValue,
|
|
9
|
+
SerializerFunctionWrapHandler,
|
|
10
|
+
model_serializer,
|
|
11
|
+
model_validator,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
from python_vk_bot.limits import (
|
|
15
|
+
MAX_BUTTON_LABEL_LENGTH,
|
|
16
|
+
MAX_BUTTONS_PER_ROW,
|
|
17
|
+
MAX_INLINE_BUTTONS,
|
|
18
|
+
MAX_INLINE_ROWS,
|
|
19
|
+
MAX_LOCATION_ROW_BUTTONS,
|
|
20
|
+
MAX_REPLY_BUTTONS,
|
|
21
|
+
MAX_REPLY_ROWS,
|
|
22
|
+
MAX_VKPAY_ROW_BUTTONS,
|
|
23
|
+
)
|
|
4
24
|
|
|
5
25
|
from .attachments import Attachment, parse_attachment
|
|
6
26
|
from .base import VKModel
|
|
@@ -54,10 +74,58 @@ class User(VKModel):
|
|
|
54
74
|
|
|
55
75
|
|
|
56
76
|
class Keyboard(VKModel):
|
|
77
|
+
model_config = ConfigDict(validate_assignment=True)
|
|
78
|
+
|
|
57
79
|
buttons: list[list[TypedButton | dict[str, JsonValue]]]
|
|
58
80
|
one_time: bool = False
|
|
59
81
|
inline: bool = False
|
|
60
82
|
|
|
83
|
+
@model_validator(mode="after")
|
|
84
|
+
def validate_limits(self) -> Self:
|
|
85
|
+
self._validate_limits()
|
|
86
|
+
return self
|
|
87
|
+
|
|
88
|
+
def _validate_limits(self) -> None:
|
|
89
|
+
"""Validate dimensions and known labels without exposing button contents."""
|
|
90
|
+
max_rows = MAX_INLINE_ROWS if self.inline else MAX_REPLY_ROWS
|
|
91
|
+
max_buttons = MAX_INLINE_BUTTONS if self.inline else MAX_REPLY_BUTTONS
|
|
92
|
+
self._check_limit("Keyboard rows", len(self.buttons), max_rows)
|
|
93
|
+
self._check_limit("Keyboard buttons", sum(map(len, self.buttons)), max_buttons)
|
|
94
|
+
for row_number, row in enumerate(self.buttons, start=1):
|
|
95
|
+
row_limit = MAX_BUTTONS_PER_ROW
|
|
96
|
+
for button_number, button in enumerate(row, start=1):
|
|
97
|
+
if isinstance(button, dict):
|
|
98
|
+
action = button.get("action")
|
|
99
|
+
if not isinstance(action, dict):
|
|
100
|
+
continue
|
|
101
|
+
kind = action.get("type")
|
|
102
|
+
label = action.get("label")
|
|
103
|
+
else:
|
|
104
|
+
kind = button.type
|
|
105
|
+
label = getattr(button, "label", None)
|
|
106
|
+
if kind == "location":
|
|
107
|
+
row_limit = min(row_limit, MAX_LOCATION_ROW_BUTTONS)
|
|
108
|
+
elif kind == "vkpay":
|
|
109
|
+
row_limit = min(row_limit, MAX_VKPAY_ROW_BUTTONS)
|
|
110
|
+
if kind in ("text", "callback", "open_link") and isinstance(label, str):
|
|
111
|
+
self._check_limit(
|
|
112
|
+
f"Row {row_number} button {button_number} label characters",
|
|
113
|
+
len(label),
|
|
114
|
+
MAX_BUTTON_LABEL_LENGTH,
|
|
115
|
+
)
|
|
116
|
+
self._check_limit(f"Row {row_number} buttons", len(row), row_limit)
|
|
117
|
+
|
|
118
|
+
@staticmethod
|
|
119
|
+
def _check_limit(subject: str, actual: int, maximum: int) -> None:
|
|
120
|
+
if actual > maximum:
|
|
121
|
+
raise ValueError(f"{subject}: maximum {maximum}, got {actual}")
|
|
122
|
+
|
|
123
|
+
@model_serializer(mode="wrap", when_used="json")
|
|
124
|
+
def serialize_keyboard(self, handler: SerializerFunctionWrapHandler) -> dict[str, JsonValue]:
|
|
125
|
+
# Nested lists and raw dictionaries can change without assignment validation.
|
|
126
|
+
self._validate_limits()
|
|
127
|
+
return handler(self)
|
|
128
|
+
|
|
61
129
|
|
|
62
130
|
class MessagePage(VKModel):
|
|
63
131
|
count: int
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: python-vk-bot
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.7.0
|
|
4
4
|
Summary: An asynchronous framework for VK community bots.
|
|
5
5
|
Project-URL: Homepage, https://github.com/fomenko-ai/python-vk-bot
|
|
6
6
|
Project-URL: Repository, https://github.com/fomenko-ai/python-vk-bot
|
|
@@ -7,6 +7,7 @@ python_vk_bot/context.py,sha256=yFPT1rXCUXSfbyPCu4y2kmWv2NI_XlZ2zIbzTpeWG90,504
|
|
|
7
7
|
python_vk_bot/errors.py,sha256=N0bBnNqc_yHUIGxQ3fTwGLk7V03nn5FLQwcpBSsA7dQ,1544
|
|
8
8
|
python_vk_bot/filters.py,sha256=YuZQwSpcMI2N56SnEggx_lvImiISSF_QJ52QIfGe8Uo,2059
|
|
9
9
|
python_vk_bot/handlers.py,sha256=MXGGD7FFMugrKLazkvUvSPPIxQXfJUgoj3-xFGFbKsw,4503
|
|
10
|
+
python_vk_bot/limits.py,sha256=wE5x8R5ZO0eQqlJhBvoNN99bJOi6hF-f5T3-q3ljsE4,401
|
|
10
11
|
python_vk_bot/long_poll.py,sha256=rmAxflq0E3H-tMDybQ7fBf1cKsJQFnaU4VbyQS3BSw4,3777
|
|
11
12
|
python_vk_bot/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
12
13
|
python_vk_bot/queues.py,sha256=9OCfYBpOpcJBbW-7TwsIb18raJN2pueZbXoJUgu7MOc,2861
|
|
@@ -21,9 +22,9 @@ python_vk_bot/models/base.py,sha256=LYcbevtU8jq5QVLo8b1mOv70RzgEYjPtTmhMMe-JN3A,
|
|
|
21
22
|
python_vk_bot/models/donut.py,sha256=qV9OyjDbmTi2UJqvzfXOL6LqYMxZvFwJo2yI1dcLSi0,1373
|
|
22
23
|
python_vk_bot/models/event_answers.py,sha256=BApsLwOySfcr3aLUazQo8rryEsitT3jaEGTQpuVIk14,445
|
|
23
24
|
python_vk_bot/models/events.py,sha256=7JPZykMTRy8hiMKsX45ZZbwGiK8-sZFtzXZqprYxK18,2826
|
|
24
|
-
python_vk_bot/models/keyboards.py,sha256=
|
|
25
|
-
python_vk_bot/models/messages.py,sha256=
|
|
26
|
-
python_vk_bot-0.
|
|
27
|
-
python_vk_bot-0.
|
|
28
|
-
python_vk_bot-0.
|
|
29
|
-
python_vk_bot-0.
|
|
25
|
+
python_vk_bot/models/keyboards.py,sha256=p-Z7EGL5e-rOPoYVEg_uctgwSMmCVk-Z1Pgl1nZ32RY,3062
|
|
26
|
+
python_vk_bot/models/messages.py,sha256=kuQEc4iLZJvhzpKJAGvRcERJdASWYQpj0OUwNdjnn10,4215
|
|
27
|
+
python_vk_bot-0.7.0.dist-info/METADATA,sha256=ejTVOHazyzFMgg7_mRW9vFx7bG52Jeu9gcksJsYU6_E,13340
|
|
28
|
+
python_vk_bot-0.7.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
29
|
+
python_vk_bot-0.7.0.dist-info/licenses/LICENSE,sha256=F-35ycqPcYW243OjxMHCggr_AHQgZLX2zvtZgobVEvo,1072
|
|
30
|
+
python_vk_bot-0.7.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|