python-vk-bot 0.2.0__py3-none-any.whl → 0.3.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/__init__.py CHANGED
@@ -10,11 +10,15 @@ from .long_poll import LongPoll
10
10
  from .models import (
11
11
  Attachment,
12
12
  AudioMessageAttachment,
13
+ ButtonColor,
14
+ ButtonType,
15
+ CallbackButton,
13
16
  DocumentAttachment,
14
17
  Keyboard,
15
18
  Message,
16
19
  MessageEvent,
17
20
  PhotoAttachment,
21
+ TextButton,
18
22
  Update,
19
23
  User,
20
24
  VideoAttachment,
@@ -27,7 +31,10 @@ __all__ = [
27
31
  "AudioMessageAttachment",
28
32
  "Bot",
29
33
  "BotSettings",
34
+ "ButtonColor",
35
+ "ButtonType",
30
36
  "Callback",
37
+ "CallbackButton",
31
38
  "CommandHandler",
32
39
  "Context",
33
40
  "DocumentAttachment",
@@ -39,6 +46,7 @@ __all__ = [
39
46
  "MessageEventHandler",
40
47
  "MessageHandler",
41
48
  "PhotoAttachment",
49
+ "TextButton",
42
50
  "Update",
43
51
  "User",
44
52
  "VideoAttachment",
@@ -6,11 +6,15 @@ from .attachments import (
6
6
  VideoAttachment,
7
7
  )
8
8
  from .events import Update
9
+ from .keyboards import ButtonColor, ButtonType, CallbackButton, TextButton
9
10
  from .messages import Coordinates, Geo, Keyboard, Message, MessageEvent, User
10
11
 
11
12
  __all__ = [
12
13
  "Attachment",
13
14
  "AudioMessageAttachment",
15
+ "ButtonColor",
16
+ "ButtonType",
17
+ "CallbackButton",
14
18
  "Coordinates",
15
19
  "DocumentAttachment",
16
20
  "Geo",
@@ -18,6 +22,7 @@ __all__ = [
18
22
  "Message",
19
23
  "MessageEvent",
20
24
  "PhotoAttachment",
25
+ "TextButton",
21
26
  "Update",
22
27
  "User",
23
28
  "VideoAttachment",
@@ -0,0 +1,78 @@
1
+ """Convenient outgoing button models with VK JSON serialization."""
2
+
3
+ import json
4
+ from enum import StrEnum
5
+ from typing import Annotated, Literal
6
+
7
+ from pydantic import (
8
+ BaseModel,
9
+ ConfigDict,
10
+ Field,
11
+ JsonValue,
12
+ SerializeAsAny,
13
+ SerializerFunctionWrapHandler,
14
+ field_serializer,
15
+ model_serializer,
16
+ )
17
+
18
+
19
+ class ButtonColor(StrEnum):
20
+ """Colors documented by the VK keyboard guide."""
21
+
22
+ PRIMARY = "primary"
23
+ SECONDARY = "secondary"
24
+ POSITIVE = "positive"
25
+ NEGATIVE = "negative"
26
+
27
+
28
+ class ButtonType(StrEnum):
29
+ """Action types supported by the typed button models."""
30
+
31
+ TEXT = "text"
32
+ CALLBACK = "callback"
33
+
34
+
35
+ class _LabeledButton(BaseModel):
36
+ model_config = ConfigDict(
37
+ extra="forbid", validate_assignment=True, hide_input_in_errors=True, allow_inf_nan=False
38
+ )
39
+
40
+ label: str = Field(repr=False)
41
+ payload: dict[str, JsonValue] | SerializeAsAny[BaseModel] | str | None = Field(
42
+ default=None, repr=False
43
+ )
44
+ color: ButtonColor = ButtonColor.SECONDARY
45
+
46
+ @field_serializer("payload", when_used="json")
47
+ def serialize_payload(self, value: dict[str, JsonValue] | BaseModel | str | None) -> str | None:
48
+ if isinstance(value, BaseModel):
49
+ return value.model_dump_json(by_alias=True)
50
+ if isinstance(value, dict):
51
+ return json.dumps(value, ensure_ascii=False, allow_nan=False, separators=(",", ":"))
52
+ return value
53
+
54
+ @model_serializer(mode="wrap", when_used="json")
55
+ def serialize_button(self, handler: SerializerFunctionWrapHandler) -> dict[str, JsonValue]:
56
+ action: dict[str, JsonValue] = handler(self)
57
+ button: dict[str, JsonValue] = {"action": action}
58
+ if "color" in action:
59
+ button["color"] = action.pop("color")
60
+ # Absence of payload is represented by an omitted action field, never JSON null.
61
+ if action.get("payload") is None:
62
+ action.pop("payload", None)
63
+ return button
64
+
65
+
66
+ class TextButton(_LabeledButton):
67
+ """Send the label and optional payload as a message, handled by MessageHandler."""
68
+
69
+ type: Literal[ButtonType.TEXT] = ButtonType.TEXT
70
+
71
+
72
+ class CallbackButton(_LabeledButton):
73
+ """Emit a message_event, handled by MessageEventHandler."""
74
+
75
+ type: Literal[ButtonType.CALLBACK] = ButtonType.CALLBACK
76
+
77
+
78
+ type TypedButton = Annotated[TextButton | CallbackButton, Field(discriminator="type")]
@@ -4,6 +4,7 @@ from pydantic import Field, JsonValue
4
4
 
5
5
  from .attachments import Attachment, parse_attachment
6
6
  from .base import VKModel
7
+ from .keyboards import TypedButton
7
8
 
8
9
 
9
10
  class Coordinates(VKModel):
@@ -53,7 +54,7 @@ class User(VKModel):
53
54
 
54
55
 
55
56
  class Keyboard(VKModel):
56
- buttons: list[list[dict[str, JsonValue]]]
57
+ buttons: list[list[TypedButton | dict[str, JsonValue]]]
57
58
  one_time: bool = False
58
59
  inline: bool = False
59
60
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: python-vk-bot
3
- Version: 0.2.0
3
+ Version: 0.3.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
@@ -1,4 +1,4 @@
1
- python_vk_bot/__init__.py,sha256=VVjIFYtWN4iBjyWHD53swcdsi9iF3h2imIyLoAS2y2g,948
1
+ python_vk_bot/__init__.py,sha256=K13sH66cbtWhwr-yB2TcrW5f7L2W0pe4rhCueBDCA50,1094
2
2
  python_vk_bot/_config.py,sha256=Rju-sjCKAfk70ajQbpPMzOuefBIjtgF8tKjOuSYo0AE,744
3
3
  python_vk_bot/application.py,sha256=rHjKVJRUTGwqgpp7DRJrZOV1eP-_LZ9_ei90EYsTQcs,8741
4
4
  python_vk_bot/bot.py,sha256=zZ_lKe6Ac5eREm0gDV02XuUkIgcdU3pki4lsJLrs4NA,13379
@@ -13,13 +13,14 @@ python_vk_bot/settings.py,sha256=JcbDihGRJGRGvBBSeB3j_LPaoX7dkPHfh-WviKQCrwk,915
13
13
  python_vk_bot/transport.py,sha256=eVsq0_brPXQ0Yk1ngMhiWHD9Z6S5Y1NhH193jbMTkmE,9812
14
14
  python_vk_bot/.agents/skills/telegram-to-vk/SKILL.md,sha256=WMrNoxLkhhlu8wnS9rFpZi3OlwBlVNHCYFSNtKmkJzM,2828
15
15
  python_vk_bot/.agents/skills/telegram-to-vk/references/migration-mapping.md,sha256=KFxmCQyKbUK7Z0JuS39FocyoFdtnslLLp9j2pNeJAds,9019
16
- python_vk_bot/models/__init__.py,sha256=pY7XWrFu8HlVRMmBAM6899EDs61Y6mqO2qIH10eNpZY,486
16
+ python_vk_bot/models/__init__.py,sha256=znIpo8F8TrxEGxeEkAkA-M5qp6aqH-NPRNdblgOBBsk,638
17
17
  python_vk_bot/models/api.py,sha256=jY9pedj7TZQkt9bRROUo8rMrO2PyAO7DPJz7E1YphYs,1626
18
18
  python_vk_bot/models/attachments.py,sha256=5JWHv37BtSzRI9j_z_yAHToJ61EyBxqRjYp7Y7rP-1o,3887
19
19
  python_vk_bot/models/base.py,sha256=LYcbevtU8jq5QVLo8b1mOv70RzgEYjPtTmhMMe-JN3A,249
20
20
  python_vk_bot/models/events.py,sha256=bS8XC0TJrj_AG0u6H9fs35kMkg47EeVoxtaUH7ya1gM,2222
21
- python_vk_bot/models/messages.py,sha256=r0YZXYA63eAVzhiB4hM1COg0xEbe1b9fcBGwl0aBUqw,1494
22
- python_vk_bot-0.2.0.dist-info/METADATA,sha256=cJsI4VjKjBWt31vE9Dj2mKKusloT1KdFn24RYYXOr0w,11789
23
- python_vk_bot-0.2.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
24
- python_vk_bot-0.2.0.dist-info/licenses/LICENSE,sha256=F-35ycqPcYW243OjxMHCggr_AHQgZLX2zvtZgobVEvo,1072
25
- python_vk_bot-0.2.0.dist-info/RECORD,,
21
+ python_vk_bot/models/keyboards.py,sha256=ZtwBLSbd-DROuohl9mM0Ui4i7FRUzXVePnfDnmkYzvc,2395
22
+ python_vk_bot/models/messages.py,sha256=WAUsa7VMDNl0xK2LXa91kg6RQsWBXoQATI1aAMDiHLU,1543
23
+ python_vk_bot-0.3.0.dist-info/METADATA,sha256=E40KM33dn9qnJv51LV8dAkGJ8rdQIlEGDURo2nsUXkY,11789
24
+ python_vk_bot-0.3.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
25
+ python_vk_bot-0.3.0.dist-info/licenses/LICENSE,sha256=F-35ycqPcYW243OjxMHCggr_AHQgZLX2zvtZgobVEvo,1072
26
+ python_vk_bot-0.3.0.dist-info/RECORD,,