gusvoice-bot 0.1.0__tar.gz
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.
- gusvoice_bot-0.1.0/LICENSE +21 -0
- gusvoice_bot-0.1.0/PKG-INFO +89 -0
- gusvoice_bot-0.1.0/README.md +70 -0
- gusvoice_bot-0.1.0/gusvoice_bot/__init__.py +50 -0
- gusvoice_bot-0.1.0/gusvoice_bot/_generated.py +502 -0
- gusvoice_bot-0.1.0/gusvoice_bot/client.py +273 -0
- gusvoice_bot-0.1.0/gusvoice_bot/errors.py +24 -0
- gusvoice_bot-0.1.0/gusvoice_bot/keyboard.py +81 -0
- gusvoice_bot-0.1.0/gusvoice_bot/polling.py +103 -0
- gusvoice_bot-0.1.0/gusvoice_bot/py.typed +0 -0
- gusvoice_bot-0.1.0/gusvoice_bot/webhook.py +133 -0
- gusvoice_bot-0.1.0/gusvoice_bot.egg-info/PKG-INFO +89 -0
- gusvoice_bot-0.1.0/gusvoice_bot.egg-info/SOURCES.txt +19 -0
- gusvoice_bot-0.1.0/gusvoice_bot.egg-info/dependency_links.txt +1 -0
- gusvoice_bot-0.1.0/gusvoice_bot.egg-info/top_level.txt +1 -0
- gusvoice_bot-0.1.0/pyproject.toml +32 -0
- gusvoice_bot-0.1.0/setup.cfg +4 -0
- gusvoice_bot-0.1.0/tests/test_client.py +181 -0
- gusvoice_bot-0.1.0/tests/test_keyboard_generated.py +145 -0
- gusvoice_bot-0.1.0/tests/test_polling.py +249 -0
- gusvoice_bot-0.1.0/tests/test_webhook.py +69 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 The GusVoice authors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: gusvoice-bot
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: GusVoice Bot API client for Python: long polling, webhooks with signature check, inline buttons. Standard library only.
|
|
5
|
+
Author: The GusVoice authors
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Keywords: gusvoice,bot,chat,api
|
|
8
|
+
Classifier: Development Status :: 4 - Beta
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
13
|
+
Classifier: Topic :: Communications :: Chat
|
|
14
|
+
Classifier: Typing :: Typed
|
|
15
|
+
Requires-Python: >=3.9
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Dynamic: license-file
|
|
19
|
+
|
|
20
|
+
# gusvoice-bot — GusVoice Bot API for Python
|
|
21
|
+
|
|
22
|
+
A thin client of the [GusVoice](../../README.md) Bot API on the **standard library only** (Python 3.9+, no
|
|
23
|
+
dependencies): every API method, long polling, webhook deliveries with signature check, inline buttons, retry on 429.
|
|
24
|
+
The full API reference is [`docs/bots.md`](../../docs/bots.md); the machine-readable schema is
|
|
25
|
+
[`docs/bot-api/openapi.json`](../../docs/bot-api/openapi.json) — the method wrappers here are generated from it.
|
|
26
|
+
|
|
27
|
+
Install: `pip install gusvoice-bot` (or from the source tree: `pip install ./packages/bot-sdk-py`, or copy the
|
|
28
|
+
`gusvoice_bot/` folder next to your bot — it has no dependencies).
|
|
29
|
+
|
|
30
|
+
## Quick start
|
|
31
|
+
|
|
32
|
+
```python
|
|
33
|
+
import os
|
|
34
|
+
from gusvoice_bot import Client
|
|
35
|
+
|
|
36
|
+
bot = Client(os.environ["BOT_TOKEN"], os.environ.get("GUSVOICE_URL", "https://voice.example.com"))
|
|
37
|
+
|
|
38
|
+
def on_update(update):
|
|
39
|
+
msg = update.get("message")
|
|
40
|
+
if msg and msg.get("text"):
|
|
41
|
+
bot.send_message(msg["chat"]["id"], "You said: " + msg["text"])
|
|
42
|
+
|
|
43
|
+
bot.run_polling(on_update)
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
`base_url` is the instance site: the API address is read from `<site>/config.json` (`apiUrl`), like the web client
|
|
47
|
+
does (`discover=False` uses `base_url` as is). The token is the `gvb_…` string from the bot settings — keep it in the
|
|
48
|
+
environment, never in the code.
|
|
49
|
+
|
|
50
|
+
## What is inside
|
|
51
|
+
|
|
52
|
+
| | |
|
|
53
|
+
|---|---|
|
|
54
|
+
| `Client(token, base_url)` | One method per API call, generated: `send_message(chat_id, text, reply_markup=…, files=[…])`, `get_updates(…)`, `answer_callback_query(…)`, … `None` arguments are not sent; `**extra` passes parameters newer than the SDK. Any method by name: `call("sendMessage", {...})`. |
|
|
55
|
+
| 429 | Retried after `retry_after` up to `max_retries` (3) times if the wait is ≤ `max_retry_after` (60 s). Other errors raise `GusVoiceApiError(code, description, retry_after)`; network errors raise as they are. |
|
|
56
|
+
| `run_polling(handler)` | Long polling loop. The offset moves past an update only after `handler(update)` returned; 401/403/404/409 are fatal, everything else is retried (1 → 30 s). Stop with a `threading.Event` (`stop_event=`) or Ctrl+C. |
|
|
57
|
+
| `verify_signature(secret, header, raw_body)` | Webhook signature check: HMAC-SHA256, 5-minute window, constant-time comparison. `webhook_handler(secret, on_update)` is a ready request handler for `http.server`. |
|
|
58
|
+
| `InlineKeyboard` | Button builder with the server limits (`callback_data` ≤ 64 **bytes**, ≤ 8 per row, ≤ 100 total, http(s) links only). |
|
|
59
|
+
| `InputFile` | Attachment for `send_message(files=[…])`: a path, bytes or a binary file object (sent as `multipart/form-data`). |
|
|
60
|
+
| `AsyncClient` | `await`-able wrapper: each call runs in a worker thread (`asyncio.to_thread`). It is threads, not native async I/O. |
|
|
61
|
+
| `METHODS`, `UPDATE_TYPES`, `API_VERSION` | Method names → API level they appeared in; update kinds; API level of this SDK. |
|
|
62
|
+
|
|
63
|
+
Not included: WebSocket updates and voice — they need third-party packages; use polling or a webhook, or the
|
|
64
|
+
TypeScript SDK `@gusvoice/bot`.
|
|
65
|
+
|
|
66
|
+
## Webhook
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
from http.server import ThreadingHTTPServer
|
|
70
|
+
from gusvoice_bot import webhook_handler
|
|
71
|
+
|
|
72
|
+
server = ThreadingHTTPServer(("127.0.0.1", 8081), webhook_handler(os.environ["WEBHOOK_SECRET"], on_update, path="/hook"))
|
|
73
|
+
server.serve_forever()
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Register the address once with `bot.set_webhook(url, secret_token)`; put the server behind an HTTPS reverse proxy.
|
|
77
|
+
The handler answers 200 right away and then calls `on_update` — the server waits for the answer, keep it fast.
|
|
78
|
+
|
|
79
|
+
## Security
|
|
80
|
+
|
|
81
|
+
Check `update["message"]["from"]["id"]` (or `callback_query.from.id`) before doing anything privileged: anyone who can
|
|
82
|
+
write to the bot can send it a command. `callback_data` is visible to the person who presses the button — no secrets.
|
|
83
|
+
|
|
84
|
+
## Development
|
|
85
|
+
|
|
86
|
+
`_generated.py` is produced by `node scripts/gen-bot-api.mjs` from the OpenAPI document; CI fails if it is out of date.
|
|
87
|
+
Tests: `python -m unittest discover -s packages/bot-sdk-py/tests`.
|
|
88
|
+
|
|
89
|
+
License: MIT (see `LICENSE`).
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# gusvoice-bot — GusVoice Bot API for Python
|
|
2
|
+
|
|
3
|
+
A thin client of the [GusVoice](../../README.md) Bot API on the **standard library only** (Python 3.9+, no
|
|
4
|
+
dependencies): every API method, long polling, webhook deliveries with signature check, inline buttons, retry on 429.
|
|
5
|
+
The full API reference is [`docs/bots.md`](../../docs/bots.md); the machine-readable schema is
|
|
6
|
+
[`docs/bot-api/openapi.json`](../../docs/bot-api/openapi.json) — the method wrappers here are generated from it.
|
|
7
|
+
|
|
8
|
+
Install: `pip install gusvoice-bot` (or from the source tree: `pip install ./packages/bot-sdk-py`, or copy the
|
|
9
|
+
`gusvoice_bot/` folder next to your bot — it has no dependencies).
|
|
10
|
+
|
|
11
|
+
## Quick start
|
|
12
|
+
|
|
13
|
+
```python
|
|
14
|
+
import os
|
|
15
|
+
from gusvoice_bot import Client
|
|
16
|
+
|
|
17
|
+
bot = Client(os.environ["BOT_TOKEN"], os.environ.get("GUSVOICE_URL", "https://voice.example.com"))
|
|
18
|
+
|
|
19
|
+
def on_update(update):
|
|
20
|
+
msg = update.get("message")
|
|
21
|
+
if msg and msg.get("text"):
|
|
22
|
+
bot.send_message(msg["chat"]["id"], "You said: " + msg["text"])
|
|
23
|
+
|
|
24
|
+
bot.run_polling(on_update)
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
`base_url` is the instance site: the API address is read from `<site>/config.json` (`apiUrl`), like the web client
|
|
28
|
+
does (`discover=False` uses `base_url` as is). The token is the `gvb_…` string from the bot settings — keep it in the
|
|
29
|
+
environment, never in the code.
|
|
30
|
+
|
|
31
|
+
## What is inside
|
|
32
|
+
|
|
33
|
+
| | |
|
|
34
|
+
|---|---|
|
|
35
|
+
| `Client(token, base_url)` | One method per API call, generated: `send_message(chat_id, text, reply_markup=…, files=[…])`, `get_updates(…)`, `answer_callback_query(…)`, … `None` arguments are not sent; `**extra` passes parameters newer than the SDK. Any method by name: `call("sendMessage", {...})`. |
|
|
36
|
+
| 429 | Retried after `retry_after` up to `max_retries` (3) times if the wait is ≤ `max_retry_after` (60 s). Other errors raise `GusVoiceApiError(code, description, retry_after)`; network errors raise as they are. |
|
|
37
|
+
| `run_polling(handler)` | Long polling loop. The offset moves past an update only after `handler(update)` returned; 401/403/404/409 are fatal, everything else is retried (1 → 30 s). Stop with a `threading.Event` (`stop_event=`) or Ctrl+C. |
|
|
38
|
+
| `verify_signature(secret, header, raw_body)` | Webhook signature check: HMAC-SHA256, 5-minute window, constant-time comparison. `webhook_handler(secret, on_update)` is a ready request handler for `http.server`. |
|
|
39
|
+
| `InlineKeyboard` | Button builder with the server limits (`callback_data` ≤ 64 **bytes**, ≤ 8 per row, ≤ 100 total, http(s) links only). |
|
|
40
|
+
| `InputFile` | Attachment for `send_message(files=[…])`: a path, bytes or a binary file object (sent as `multipart/form-data`). |
|
|
41
|
+
| `AsyncClient` | `await`-able wrapper: each call runs in a worker thread (`asyncio.to_thread`). It is threads, not native async I/O. |
|
|
42
|
+
| `METHODS`, `UPDATE_TYPES`, `API_VERSION` | Method names → API level they appeared in; update kinds; API level of this SDK. |
|
|
43
|
+
|
|
44
|
+
Not included: WebSocket updates and voice — they need third-party packages; use polling or a webhook, or the
|
|
45
|
+
TypeScript SDK `@gusvoice/bot`.
|
|
46
|
+
|
|
47
|
+
## Webhook
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
from http.server import ThreadingHTTPServer
|
|
51
|
+
from gusvoice_bot import webhook_handler
|
|
52
|
+
|
|
53
|
+
server = ThreadingHTTPServer(("127.0.0.1", 8081), webhook_handler(os.environ["WEBHOOK_SECRET"], on_update, path="/hook"))
|
|
54
|
+
server.serve_forever()
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Register the address once with `bot.set_webhook(url, secret_token)`; put the server behind an HTTPS reverse proxy.
|
|
58
|
+
The handler answers 200 right away and then calls `on_update` — the server waits for the answer, keep it fast.
|
|
59
|
+
|
|
60
|
+
## Security
|
|
61
|
+
|
|
62
|
+
Check `update["message"]["from"]["id"]` (or `callback_query.from.id`) before doing anything privileged: anyone who can
|
|
63
|
+
write to the bot can send it a command. `callback_data` is visible to the person who presses the button — no secrets.
|
|
64
|
+
|
|
65
|
+
## Development
|
|
66
|
+
|
|
67
|
+
`_generated.py` is produced by `node scripts/gen-bot-api.mjs` from the OpenAPI document; CI fails if it is out of date.
|
|
68
|
+
Tests: `python -m unittest discover -s packages/bot-sdk-py/tests`.
|
|
69
|
+
|
|
70
|
+
License: MIT (see `LICENSE`).
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
"""GusVoice Bot API for Python — a thin client on the standard library only.
|
|
3
|
+
|
|
4
|
+
from gusvoice_bot import Client
|
|
5
|
+
bot = Client(os.environ["BOT_TOKEN"], "https://voice.example.com")
|
|
6
|
+
bot.send_message(chat_id, "Hello")
|
|
7
|
+
|
|
8
|
+
Long polling (`run_polling`), webhook deliveries with signature check (`verify_signature`, `webhook_handler`), inline
|
|
9
|
+
buttons (`InlineKeyboard`), retry on 429. No WebSocket and no voice: for those use the TypeScript SDK `@gusvoice/bot`.
|
|
10
|
+
"""
|
|
11
|
+
from ._generated import API_VERSION, METHODS, SCHEMA_VERSION, UPDATE_TYPES
|
|
12
|
+
from .client import AsyncClient, Client, InputFile, normalize_base
|
|
13
|
+
from .errors import GusVoiceApiError, GusVoiceError
|
|
14
|
+
from .keyboard import (
|
|
15
|
+
CALLBACK_DATA_MAX_BYTES,
|
|
16
|
+
KEYBOARD_BUTTON_TEXT_MAX,
|
|
17
|
+
KEYBOARD_BUTTONS_MAX,
|
|
18
|
+
KEYBOARD_ROW_BUTTONS_MAX,
|
|
19
|
+
InlineKeyboard,
|
|
20
|
+
)
|
|
21
|
+
from .polling import run_polling
|
|
22
|
+
from .webhook import DEFAULT_TOLERANCE_S, SIGNATURE_HEADER, parse_signature_header, sign, verify_signature, webhook_handler
|
|
23
|
+
|
|
24
|
+
__version__ = "0.1.0"
|
|
25
|
+
|
|
26
|
+
__all__ = [
|
|
27
|
+
"API_VERSION",
|
|
28
|
+
"METHODS",
|
|
29
|
+
"SCHEMA_VERSION",
|
|
30
|
+
"UPDATE_TYPES",
|
|
31
|
+
"AsyncClient",
|
|
32
|
+
"Client",
|
|
33
|
+
"InputFile",
|
|
34
|
+
"normalize_base",
|
|
35
|
+
"GusVoiceApiError",
|
|
36
|
+
"GusVoiceError",
|
|
37
|
+
"CALLBACK_DATA_MAX_BYTES",
|
|
38
|
+
"KEYBOARD_BUTTON_TEXT_MAX",
|
|
39
|
+
"KEYBOARD_BUTTONS_MAX",
|
|
40
|
+
"KEYBOARD_ROW_BUTTONS_MAX",
|
|
41
|
+
"InlineKeyboard",
|
|
42
|
+
"run_polling",
|
|
43
|
+
"DEFAULT_TOLERANCE_S",
|
|
44
|
+
"SIGNATURE_HEADER",
|
|
45
|
+
"parse_signature_header",
|
|
46
|
+
"sign",
|
|
47
|
+
"verify_signature",
|
|
48
|
+
"webhook_handler",
|
|
49
|
+
"__version__",
|
|
50
|
+
]
|