python-vk-bot 0.7.0__py3-none-any.whl → 0.8.1__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 +2 -0
- python_vk_bot/bot.py +130 -11
- python_vk_bot/models/__init__.py +2 -0
- python_vk_bot/models/api.py +28 -0
- {python_vk_bot-0.7.0.dist-info → python_vk_bot-0.8.1.dist-info}/METADATA +42 -3
- {python_vk_bot-0.7.0.dist-info → python_vk_bot-0.8.1.dist-info}/RECORD +8 -8
- {python_vk_bot-0.7.0.dist-info → python_vk_bot-0.8.1.dist-info}/WHEEL +0 -0
- {python_vk_bot-0.7.0.dist-info → python_vk_bot-0.8.1.dist-info}/licenses/LICENSE +0 -0
python_vk_bot/__init__.py
CHANGED
|
@@ -28,6 +28,7 @@ from .models import (
|
|
|
28
28
|
LocationButton,
|
|
29
29
|
Message,
|
|
30
30
|
MessageEvent,
|
|
31
|
+
MessageSendResult,
|
|
31
32
|
OpenLinkButton,
|
|
32
33
|
PhotoAttachment,
|
|
33
34
|
ShowSnackbar,
|
|
@@ -64,6 +65,7 @@ __all__ = [
|
|
|
64
65
|
"MessageEvent",
|
|
65
66
|
"MessageEventHandler",
|
|
66
67
|
"MessageHandler",
|
|
68
|
+
"MessageSendResult",
|
|
67
69
|
"OpenLinkButton",
|
|
68
70
|
"PhotoAttachment",
|
|
69
71
|
"ShowSnackbar",
|
python_vk_bot/bot.py
CHANGED
|
@@ -16,8 +16,10 @@ from .models.api import (
|
|
|
16
16
|
DocumentUpload,
|
|
17
17
|
LongPollServer,
|
|
18
18
|
MessageDeleteResult,
|
|
19
|
+
MessageSendResult,
|
|
19
20
|
PhotoUpload,
|
|
20
21
|
UploadServer,
|
|
22
|
+
_MessageSendResponse,
|
|
21
23
|
)
|
|
22
24
|
from .models.attachments import (
|
|
23
25
|
Attachment,
|
|
@@ -62,6 +64,52 @@ def _message_identifier(message_id: int | None, conversation_message_id: int | N
|
|
|
62
64
|
return identifier
|
|
63
65
|
|
|
64
66
|
|
|
67
|
+
def _send_parameters(
|
|
68
|
+
text: str,
|
|
69
|
+
random_id: int | None,
|
|
70
|
+
keyboard: Keyboard | None,
|
|
71
|
+
attachment: str | Attachment | None,
|
|
72
|
+
dont_parse_links: bool,
|
|
73
|
+
) -> dict[str, Parameter]:
|
|
74
|
+
attachment_id = serialize_attachment(attachment)
|
|
75
|
+
if not text and not attachment_id:
|
|
76
|
+
raise ValueError("Provide text or an attachment")
|
|
77
|
+
return {
|
|
78
|
+
"message": text,
|
|
79
|
+
"random_id": _random_id(random_id),
|
|
80
|
+
"keyboard": keyboard.model_dump_json(exclude_none=True) if keyboard else None,
|
|
81
|
+
"attachment": attachment_id,
|
|
82
|
+
"dont_parse_links": dont_parse_links,
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _peer_identifier(peer_id: int) -> int:
|
|
87
|
+
if type(peer_id) is not int or peer_id == 0:
|
|
88
|
+
raise ValueError("peer_id must be a nonzero integer")
|
|
89
|
+
return peer_id
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _message_send_result(payload: JsonValue, peer_id: int) -> MessageSendResult:
|
|
93
|
+
if not isinstance(payload, list) or len(payload) != 1:
|
|
94
|
+
raise InvalidResponseError("Expected one message send result")
|
|
95
|
+
sent = _parse(_MessageSendResponse, payload[0])
|
|
96
|
+
if sent.peer_id != peer_id:
|
|
97
|
+
raise InvalidResponseError("Message send result has an unexpected recipient")
|
|
98
|
+
if sent.error is not None:
|
|
99
|
+
error_type = RateLimitError if sent.error.code == 6 else APIError
|
|
100
|
+
raise error_type(sent.error.code, "messages.send")
|
|
101
|
+
if sent.message_id is None or sent.conversation_message_id is None:
|
|
102
|
+
raise InvalidResponseError("Message send result has no message identifiers")
|
|
103
|
+
if sent.conversation_message_id == 0:
|
|
104
|
+
raise InvalidResponseError("Message send result has no usable conversation identifier")
|
|
105
|
+
return MessageSendResult(
|
|
106
|
+
peer_id=sent.peer_id,
|
|
107
|
+
message_id=sent.message_id or None,
|
|
108
|
+
conversation_message_id=sent.conversation_message_id,
|
|
109
|
+
**(sent.model_extra or {}),
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
|
|
65
113
|
class Bot:
|
|
66
114
|
def __init__(
|
|
67
115
|
self,
|
|
@@ -115,10 +163,39 @@ class Bot:
|
|
|
115
163
|
return users[0] if users else None
|
|
116
164
|
|
|
117
165
|
async def get_message(self, peer_id: int, conversation_message_id: int) -> Message | None:
|
|
166
|
+
"""Fetch one message, returning None only for confirmed absence.
|
|
167
|
+
|
|
168
|
+
Invalid identifiers raise ValueError before the request. Malformed or
|
|
169
|
+
inconsistent responses raise InvalidResponseError; API and transport
|
|
170
|
+
errors propagate unchanged. No retries or fallback requests are made.
|
|
171
|
+
"""
|
|
172
|
+
peer_id = _peer_identifier(peer_id)
|
|
173
|
+
conversation_message_id = _message_identifier(None, conversation_message_id)
|
|
118
174
|
result = await self.request(
|
|
119
175
|
"messages.getByConversationMessageId",
|
|
120
176
|
{"peer_id": peer_id, "conversation_message_ids": conversation_message_id},
|
|
121
177
|
)
|
|
178
|
+
if not isinstance(result, dict):
|
|
179
|
+
raise InvalidResponseError("Invalid message lookup response")
|
|
180
|
+
count = result.get("count")
|
|
181
|
+
items = result.get("items")
|
|
182
|
+
if (
|
|
183
|
+
type(count) is not int
|
|
184
|
+
or count not in (0, 1)
|
|
185
|
+
or not isinstance(items, list)
|
|
186
|
+
or count != len(items)
|
|
187
|
+
):
|
|
188
|
+
raise InvalidResponseError("Invalid message lookup count or items")
|
|
189
|
+
if count == 1:
|
|
190
|
+
item = items[0]
|
|
191
|
+
if not isinstance(item, dict) or any(
|
|
192
|
+
type(item.get(field)) is not int or item[field] != expected
|
|
193
|
+
for field, expected in (
|
|
194
|
+
("peer_id", peer_id),
|
|
195
|
+
("conversation_message_id", conversation_message_id),
|
|
196
|
+
)
|
|
197
|
+
):
|
|
198
|
+
raise InvalidResponseError("Message lookup result has unexpected identifiers")
|
|
122
199
|
page = _parse(MessagePage, result)
|
|
123
200
|
return page.items[0] if page.items else None
|
|
124
201
|
|
|
@@ -132,22 +209,34 @@ class Bot:
|
|
|
132
209
|
attachment: str | Attachment | None = None,
|
|
133
210
|
dont_parse_links: bool = False,
|
|
134
211
|
) -> int:
|
|
135
|
-
|
|
136
|
-
if not text and not attachment_id:
|
|
137
|
-
raise ValueError("Provide text or an attachment")
|
|
212
|
+
parameters = _send_parameters(text, random_id, keyboard, attachment, dont_parse_links)
|
|
138
213
|
result = await self.request(
|
|
139
214
|
"messages.send",
|
|
140
|
-
{
|
|
141
|
-
"peer_id": peer_id,
|
|
142
|
-
"message": text,
|
|
143
|
-
"random_id": _random_id(random_id),
|
|
144
|
-
"keyboard": keyboard.model_dump_json(exclude_none=True) if keyboard else None,
|
|
145
|
-
"attachment": attachment_id,
|
|
146
|
-
"dont_parse_links": dont_parse_links,
|
|
147
|
-
},
|
|
215
|
+
{"peer_id": peer_id, **parameters},
|
|
148
216
|
)
|
|
149
217
|
return _integer(result)
|
|
150
218
|
|
|
219
|
+
async def send_message_and_get_ids(
|
|
220
|
+
self,
|
|
221
|
+
peer_id: int,
|
|
222
|
+
text: str = "",
|
|
223
|
+
*,
|
|
224
|
+
random_id: int | None = None,
|
|
225
|
+
keyboard: Keyboard | None = None,
|
|
226
|
+
attachment: str | Attachment | None = None,
|
|
227
|
+
dont_parse_links: bool = False,
|
|
228
|
+
) -> MessageSendResult:
|
|
229
|
+
"""Send to one peer and return IDs; an unavailable global ID becomes None.
|
|
230
|
+
|
|
231
|
+
Uses peer_ids to obtain a conversation-scoped ID. Recipient errors raise
|
|
232
|
+
APIError (RateLimitError for code 6); malformed results raise InvalidResponseError.
|
|
233
|
+
No automatic retry or fallback send is performed.
|
|
234
|
+
"""
|
|
235
|
+
_peer_identifier(peer_id)
|
|
236
|
+
parameters = _send_parameters(text, random_id, keyboard, attachment, dont_parse_links)
|
|
237
|
+
result = await self.request("messages.send", {"peer_ids": str(peer_id), **parameters})
|
|
238
|
+
return _message_send_result(result, peer_id)
|
|
239
|
+
|
|
151
240
|
async def edit_message(
|
|
152
241
|
self,
|
|
153
242
|
peer_id: int,
|
|
@@ -332,6 +421,36 @@ class Bot:
|
|
|
332
421
|
)
|
|
333
422
|
return _integer(result)
|
|
334
423
|
|
|
424
|
+
async def forward_message_and_get_ids(
|
|
425
|
+
self,
|
|
426
|
+
peer_id: int,
|
|
427
|
+
*,
|
|
428
|
+
from_peer_id: int,
|
|
429
|
+
conversation_message_id: int,
|
|
430
|
+
random_id: int | None = None,
|
|
431
|
+
) -> MessageSendResult:
|
|
432
|
+
"""Forward one source peer's message and return the new message's IDs.
|
|
433
|
+
|
|
434
|
+
conversation_message_id belongs to from_peer_id, not the destination.
|
|
435
|
+
Recipient errors and response validation follow send_message_and_get_ids.
|
|
436
|
+
"""
|
|
437
|
+
_peer_identifier(peer_id)
|
|
438
|
+
_peer_identifier(from_peer_id)
|
|
439
|
+
_message_identifier(None, conversation_message_id)
|
|
440
|
+
forward = {
|
|
441
|
+
"peer_id": from_peer_id,
|
|
442
|
+
"conversation_message_ids": [conversation_message_id],
|
|
443
|
+
}
|
|
444
|
+
result = await self.request(
|
|
445
|
+
"messages.send",
|
|
446
|
+
{
|
|
447
|
+
"peer_ids": str(peer_id),
|
|
448
|
+
"random_id": _random_id(random_id),
|
|
449
|
+
"forward": json.dumps(forward),
|
|
450
|
+
},
|
|
451
|
+
)
|
|
452
|
+
return _message_send_result(result, peer_id)
|
|
453
|
+
|
|
335
454
|
async def send_message_event_answer(
|
|
336
455
|
self,
|
|
337
456
|
event_id: str,
|
python_vk_bot/models/__init__.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
from .api import MessageSendResult
|
|
1
2
|
from .attachments import (
|
|
2
3
|
Attachment,
|
|
3
4
|
AudioMessageAttachment,
|
|
@@ -33,6 +34,7 @@ __all__ = [
|
|
|
33
34
|
"LocationButton",
|
|
34
35
|
"Message",
|
|
35
36
|
"MessageEvent",
|
|
37
|
+
"MessageSendResult",
|
|
36
38
|
"OpenLinkButton",
|
|
37
39
|
"PhotoAttachment",
|
|
38
40
|
"ShowSnackbar",
|
python_vk_bot/models/api.py
CHANGED
|
@@ -51,6 +51,34 @@ class DocumentUpload(VKModel):
|
|
|
51
51
|
file: str = Field(min_length=1, repr=False)
|
|
52
52
|
|
|
53
53
|
|
|
54
|
+
class MessageSendResult(VKModel):
|
|
55
|
+
"""Identifiers of one sent message; a global ID is not always available."""
|
|
56
|
+
|
|
57
|
+
peer_id: int = Field(strict=True)
|
|
58
|
+
message_id: int | None = Field(strict=True, gt=0)
|
|
59
|
+
conversation_message_id: int = Field(strict=True, gt=0)
|
|
60
|
+
|
|
61
|
+
@field_validator("peer_id")
|
|
62
|
+
@classmethod
|
|
63
|
+
def validate_peer_id(cls, value: int) -> int:
|
|
64
|
+
if value == 0:
|
|
65
|
+
raise ValueError("peer_id must be nonzero")
|
|
66
|
+
return value
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class _MessageSendError(VKModel):
|
|
70
|
+
code: int = Field(strict=True, gt=0)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class _MessageSendResponse(VKModel):
|
|
74
|
+
"""Recipient errors may carry zero or omitted message identifiers."""
|
|
75
|
+
|
|
76
|
+
peer_id: int = Field(strict=True)
|
|
77
|
+
message_id: int | None = Field(default=None, strict=True, ge=0)
|
|
78
|
+
conversation_message_id: int | None = Field(default=None, strict=True, ge=0)
|
|
79
|
+
error: _MessageSendError | None = Field(default=None, repr=False)
|
|
80
|
+
|
|
81
|
+
|
|
54
82
|
class MessageDeleteError(VKModel):
|
|
55
83
|
code: int = Field(strict=True)
|
|
56
84
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: python-vk-bot
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.8.1
|
|
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
|
|
@@ -194,10 +194,49 @@ connection data. `Bot.request(method, parameters)` exposes raw JSON results for
|
|
|
194
194
|
VK methods; nested request parameters must be JSON encoded explicitly.
|
|
195
195
|
|
|
196
196
|
Messages use `peer_id`; `Message` exposes both `id` and `conversation_message_id`.
|
|
197
|
-
`send_message` returns an integer
|
|
198
|
-
`random_id`. Sends are never retried automatically.
|
|
197
|
+
`send_message` returns an integer, which may be `0` for a community send to a chat,
|
|
198
|
+
and accepts an optional nonzero int32 `random_id`. Sends are never retried automatically.
|
|
199
|
+
`edit_message` accepts exactly one
|
|
199
200
|
message identifier and maps `conversation_message_id` to VK's `cmid` parameter.
|
|
200
201
|
|
|
202
|
+
### Send, edit and forward using conversation IDs
|
|
203
|
+
|
|
204
|
+
`send_message_and_get_ids` returns a `MessageSendResult`, also exported from
|
|
205
|
+
`python_vk_bot.models`. Its `peer_id` identifies the recipient;
|
|
206
|
+
`conversation_message_id` is positive and belongs to that peer. The separate global
|
|
207
|
+
`message_id` is positive when available, or `None` when VK returns `0`.
|
|
208
|
+
|
|
209
|
+
```python
|
|
210
|
+
async with Bot(token) as bot:
|
|
211
|
+
sent = await bot.send_message_and_get_ids(peer_id, "Original text", random_id=12345)
|
|
212
|
+
await bot.edit_message(
|
|
213
|
+
sent.peer_id,
|
|
214
|
+
"Updated text",
|
|
215
|
+
conversation_message_id=sent.conversation_message_id,
|
|
216
|
+
)
|
|
217
|
+
forwarded = await bot.forward_message_and_get_ids(
|
|
218
|
+
destination_peer_id,
|
|
219
|
+
from_peer_id=sent.peer_id,
|
|
220
|
+
conversation_message_id=sent.conversation_message_id,
|
|
221
|
+
random_id=12346,
|
|
222
|
+
)
|
|
223
|
+
# forwarded IDs identify the new message in destination_peer_id.
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
The send method supports `text`, `keyboard`, `attachment`, `dont_parse_links` and
|
|
227
|
+
`random_id`. The forwarding method forwards one message by its source peer and
|
|
228
|
+
conversation ID, with optional `random_id`; it does not add accompanying content.
|
|
229
|
+
Both use `peer_ids` with one recipient and require one matching result with usable
|
|
230
|
+
conversation identifiers. Recipient errors raise `APIError` (`RateLimitError` for
|
|
231
|
+
code 6); malformed results raise `InvalidResponseError`. A response-validation or
|
|
232
|
+
network failure can occur after delivery: neither method sends a fallback or retries.
|
|
233
|
+
To control deduplication when retrying, retain and reuse an explicit `random_id`.
|
|
234
|
+
|
|
235
|
+
These public methods and the three result fields follow the project's alpha API
|
|
236
|
+
stability policy: changes are documented in the changelog; unknown VK response
|
|
237
|
+
fields are retained as extras but are not stable library-defined fields.
|
|
238
|
+
`forward_message(peer_id, message, random_id=...)` returns an integer result.
|
|
239
|
+
|
|
201
240
|
## Callback API
|
|
202
241
|
|
|
203
242
|
The [Callback example](examples/callback.py) extends the same base settings with its
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
python_vk_bot/__init__.py,sha256=
|
|
1
|
+
python_vk_bot/__init__.py,sha256=f6S37kGsoT0yoVT19IMpkweIMG8CyoeTxUQprFtT-LM,1631
|
|
2
2
|
python_vk_bot/_config.py,sha256=Rju-sjCKAfk70ajQbpPMzOuefBIjtgF8tKjOuSYo0AE,744
|
|
3
3
|
python_vk_bot/application.py,sha256=dkHR3F6geN8VhpnnfCb7aSEL1k8GCo-iA9nSZRoZCC4,20361
|
|
4
|
-
python_vk_bot/bot.py,sha256=
|
|
4
|
+
python_vk_bot/bot.py,sha256=1MuhW3GjKJx0HTA6SaWUioXlS6I9ZxW1-wZoNfwFsKM,18457
|
|
5
5
|
python_vk_bot/callback.py,sha256=9j11jtl4rpTnfUThvOf2g6kb7wYQXAQerHbuk3IYw3o,4726
|
|
6
6
|
python_vk_bot/context.py,sha256=yFPT1rXCUXSfbyPCu4y2kmWv2NI_XlZ2zIbzTpeWG90,504
|
|
7
7
|
python_vk_bot/errors.py,sha256=N0bBnNqc_yHUIGxQ3fTwGLk7V03nn5FLQwcpBSsA7dQ,1544
|
|
@@ -15,8 +15,8 @@ python_vk_bot/settings.py,sha256=JcbDihGRJGRGvBBSeB3j_LPaoX7dkPHfh-WviKQCrwk,915
|
|
|
15
15
|
python_vk_bot/transport.py,sha256=kdf9NRpZ2KM2yP2IGNAASfJagGvJOcgoEPpj-V9nudM,11936
|
|
16
16
|
python_vk_bot/.agents/skills/telegram-to-vk/SKILL.md,sha256=WMrNoxLkhhlu8wnS9rFpZi3OlwBlVNHCYFSNtKmkJzM,2828
|
|
17
17
|
python_vk_bot/.agents/skills/telegram-to-vk/references/migration-mapping.md,sha256=KFxmCQyKbUK7Z0JuS39FocyoFdtnslLLp9j2pNeJAds,9019
|
|
18
|
-
python_vk_bot/models/__init__.py,sha256=
|
|
19
|
-
python_vk_bot/models/api.py,sha256=
|
|
18
|
+
python_vk_bot/models/__init__.py,sha256=YhwGTUnNMchldEjw3o1nsVQEQonOhbeFhebIrr9T1q0,997
|
|
19
|
+
python_vk_bot/models/api.py,sha256=l6wVrJJyxA23hUg5cFLc7rl2RkbGdEjnzDAe1jpwqcc,2543
|
|
20
20
|
python_vk_bot/models/attachments.py,sha256=5JWHv37BtSzRI9j_z_yAHToJ61EyBxqRjYp7Y7rP-1o,3887
|
|
21
21
|
python_vk_bot/models/base.py,sha256=LYcbevtU8jq5QVLo8b1mOv70RzgEYjPtTmhMMe-JN3A,249
|
|
22
22
|
python_vk_bot/models/donut.py,sha256=qV9OyjDbmTi2UJqvzfXOL6LqYMxZvFwJo2yI1dcLSi0,1373
|
|
@@ -24,7 +24,7 @@ python_vk_bot/models/event_answers.py,sha256=BApsLwOySfcr3aLUazQo8rryEsitT3jaEGT
|
|
|
24
24
|
python_vk_bot/models/events.py,sha256=7JPZykMTRy8hiMKsX45ZZbwGiK8-sZFtzXZqprYxK18,2826
|
|
25
25
|
python_vk_bot/models/keyboards.py,sha256=p-Z7EGL5e-rOPoYVEg_uctgwSMmCVk-Z1Pgl1nZ32RY,3062
|
|
26
26
|
python_vk_bot/models/messages.py,sha256=kuQEc4iLZJvhzpKJAGvRcERJdASWYQpj0OUwNdjnn10,4215
|
|
27
|
-
python_vk_bot-0.
|
|
28
|
-
python_vk_bot-0.
|
|
29
|
-
python_vk_bot-0.
|
|
30
|
-
python_vk_bot-0.
|
|
27
|
+
python_vk_bot-0.8.1.dist-info/METADATA,sha256=ZdGFmUmK7JxA9L3z_CcEK0rcIxi8K1bJEQe75BAmzbw,15255
|
|
28
|
+
python_vk_bot-0.8.1.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
29
|
+
python_vk_bot-0.8.1.dist-info/licenses/LICENSE,sha256=F-35ycqPcYW243OjxMHCggr_AHQgZLX2zvtZgobVEvo,1072
|
|
30
|
+
python_vk_bot-0.8.1.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|