py-tjbrhk-toolkit
功能
- 博瑞云音箱 notify 接口:提交通知语音播报,支持同步和异步客户端,均支持上下文管理器以自动关闭 HTTP 连接。
- 类型模型:基于 Pydantic 定义响应模型。
安装
pip install py-tjbrhk-toolkit
使用 uv:
uv add py-tjbrhk-toolkit
需要开发与测试工具时,从仓库安装:
uv sync --extra dev --extra test
包结构
py_tjbrhk_toolkit/
├── pyproject.toml
├── README.md
├── LICENSE
├── src/
│ └── py_tjbrhk_toolkit/
│ ├── __init__.py
│ ├── py.typed
│ └── speaker/
│ ├── __init__.py
│ ├── _clients.py # 内部实现:同步与异步通知客户端
│ └── _models.py # 内部实现:通知接口响应模型
├── examples/
│ └── speaker/
│ ├── .env.example
│ ├── notify_async.py
│ └── notify_sync.py
└── tests/
└── speaker/
├── .env.example
├── conftest.py
├── test_notify_async.py
└── test_notify_sync.py
包根导出 speaker 模块。各模块对外公开的名称带有 Speaker 前缀,避免名称冲突;_ 开头的文件是内部实现模块。
使用
1. speaker 模块 - 云音箱通知
博瑞云音箱模块通过 Token 和 ID 初始化客户端。notify 支持发送语音播报通知。
1.1 同步发送通知
from py_tjbrhk_toolkit.speaker import SpeakerClient
with SpeakerClient(
token="你的云音箱 Token",
id="你的云音箱 ID",
) as client:
response = client.notify(
message="您好,欢迎光临",
vol=80,
)
print(response.model_dump())
print("发送成功:", response.ok)
1.2 异步发送通知
import asyncio
from py_tjbrhk_toolkit.speaker import SpeakerAsyncClient
async def main() -> None:
async with SpeakerAsyncClient(
token="你的云音箱 Token",
id="你的云音箱 ID",
) as client:
response = await client.notify(
message="收到一笔新的付款",
vol=80,
)
print(response.model_dump())
print("发送成功:", response.ok)
asyncio.run(main())
同步客户端可用 with 管理连接;异步客户端可用 async with 管理连接。也可以分别调用 SpeakerClient.close() 和 SpeakerAsyncClient.aclose() 手动关闭。
1.3 客户端配置
同步客户端 SpeakerClient 和异步客户端 SpeakerAsyncClient 使用相同的构造参数:
| 参数 | 必填 | 默认值 | 说明 |
|---|---|---|---|
token |
是 | — | 云音箱 Token。 |
id |
是 | — | 云音箱 ID。 |
base_url |
否 | https://speaker.17laimai.cn |
云音箱服务接口地址。 |
version |
否 | 1 |
接口版本。 |
timeout |
否 | 30.0 |
HTTP 请求超时时间,单位为秒。 |
1.4 通知发送参数及响应
notify(message, *, seq=None, vol=None, speed=None, trace_no=None, id=None, token=None, version=None) 支持以下参数:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
message |
str |
是 | 播报内容,最长 128 个 UTF-8 字节 |
seq |
int | str |
否 | 通讯去重顺序号 |
vol |
int | str |
否 | 音量设置 |
speed |
int | str |
否 | 语速设置 |
trace_no |
str |
否 | 请求追踪字符串 |
id |
str |
否 | 覆盖客户端配置的云音箱 ID |
token |
str |
否 | 覆盖客户端配置的 Token |
version |
str |
否 | 覆盖客户端配置的接口版本 |
接口返回 SpeakerNotifyResponse,其属性 errcode、errmsg、detail 与接口响应字段一致;response.ok 在 errcode 等于 0 或 "0" 时为 True。
消息中的数字默认按金额播报;如需按号码或编号播报,可按接口约定使用方括号,例如 "[123]"。音箱型号和接口版本对 speed 等参数的支持可能不同,请以官方接口文档为准。
1.5 使用环境变量运行示例
示例脚本从仓库根目录加载 .env 文件,且系统中已有的环境变量优先。复制配置模板并填写云音箱 ID 和 Token:
cp examples/speaker/.env.example .env
完成配置后运行同步或异步示例:
python examples/speaker/notify_sync.py
python examples/speaker/notify_async.py
.env 文件包含设备凭据,请勿提交到版本控制。
1.6 异常处理
客户端配置无效或请求参数不符合校验时会产生 ValueError;HTTP 网络请求失败时会产生 ConnectionError。调用方可根据需要显式处理:
try:
# 在此处初始化客户端并调用 notify。
...
except (ValueError, ConnectionError) as exc:
print(f"云音箱请求失败:{exc}")
配置示例与测试
示例脚本读取环境变量;仓库提供 examples/speaker/.env.example 作为配置模板。将所需变量设置在仓库根目录的 .env 中,或在运行脚本前导出环境变量。
测试配置位于 tests/speaker/.env.example。复制到对应测试目录的 .env 并填入有效凭证。配置包含 BASE_URL、ID、TOKEN 和 VERSION。
pytest
测试配置在缺少必需环境变量时会跳过相应测试。pytest 默认配置会输出终端、HTML 和 XML 覆盖率报告。
**注意:**默认测试会向真实云音箱发送请求并触发语音播报。只有在
.env中配置有效凭据时才会运行真实请求;否则 pytest 会跳过测试。请勿在不适合播报的设备或环境中执行。
开发
ruff check .
ruff format --check .
python -m compileall -q src examples tests
项目支持 Python 3.9 及以上版本。核心依赖包括 httpx 和 Pydantic;开发、测试、文档工具可通过 pyproject.toml 中的可选依赖组安装。
作者信息
- 作者:郭磊
- 邮箱:174000902@qq.com
许可证
本项目基于 MIT 许可证 开源。
Metadata
Release files for py-tjbrhk-toolkit 4.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| py_tjbrhk_toolkit-4.1.1.tar.gz | 9.0 kB | Without provenance | Details for py_tjbrhk_toolkit-4.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| py_tjbrhk_toolkit-4.1.1-py3-none-any.whl | Python 3 | none | any | Details for py_tjbrhk_toolkit-4.1.1-py3-none-any.whl |
Total release size: 17.7 kB
Release files / py_tjbrhk_toolkit-4.1.1.tar.gz
| Download URL | py_tjbrhk_toolkit-4.1.1.tar.gz |
|---|---|
| Size | 9.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
96608b7ee2b195b24803e2a8e91f57a58281005af57ec2c2456b07055174d0d0
|
|
BLAKE2b-256 checksum How to use checksums |
f9b3cb95579b33fab7eac8232bea53333d7be740a8980d36bd699c86d5b2e7b7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.11
|
Release files / py_tjbrhk_toolkit-4.1.1-py3-none-any.whl
| Download URL | py_tjbrhk_toolkit-4.1.1-py3-none-any.whl |
|---|---|
| Size | 8.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d6532297a3031cfe5721375f91245565760785a52a4bc985157ea37b84badd07
|
|
BLAKE2b-256 checksum How to use checksums |
72d8d160069811560f6bd946924afdfcd2461e8ef5b396aba07d1e244179faa3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.11
|
Security for py-tjbrhk-toolkit 4.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.