py-wecom-toolkit
介绍
企业微信 Python 扩展包,支持 Webhook(群机器人)与 Server 端 API。提供同步/异步双模客户端、基于 Pydantic v2 的消息模型,以及自动缓存的 access_token 管理。
py-wecom-toolkit 让你极其简单地与企业微信 API 交互。无需手动拼接 URL,无需手动构建 JSON 负载,自动缓存 access_token,支持同步和异步客户端。
软件架构
py-wecom-toolkit/
├── pyproject.toml # 项目构建配置
├── README.md # 项目说明文档
├── README.en.md # 项目说明文档(英文)
├── LICENSE # MIT 许可证
├── uv.lock # 锁定依赖版本
├── src/
│ └── py_wecom_toolkit/ # 源代码包
│ ├── __init__.py # 包入口
│ ├── py.typed # PEP 561 类型标记
│ ├── webhook/ # Webhook 消息发送模块(群机器人)
│ │ ├── __init__.py
│ │ ├── client.py # WeComWebhookClient / AsyncWeComWebhookClient / WeComWebhookClientConfig
│ │ ├── models.py # pydantic v2 消息模型与异常定义
│ │ └── types.py # 类型别名
│ └── server/ # Server 端 API 模块(企业微信服务端接口)
│ ├── __init__.py
│ ├── client.py # WeComServerClient / AsyncWeComServerClient / WeComServerClientConfig
│ ├── token.py # WeComServerAccessTokenManager / AsyncWeComServerAccessTokenManager
│ ├── cache.py # WeComServerMemoryCache / WeComServerDiskCache / WeComServerRedisCache
│ ├── models.py # Server API 响应模型与异常定义
│ ├── message.py # 应用消息发送(WeComServerMessageSendMixin / AsyncWeComServerMessageSendMixin)
│ └── media.py # 素材上传/下载(WeComServerMediaMixin / AsyncWeComServerMediaMixin)
├── tests/ # 测试用例
│ ├── webhook/ # Webhook 测试(按消息类型组织)
│ └── server/ # Server 端测试(按功能模块组织)
└── examples/ # 演示代码
├── webhook/ # Webhook 示例
└── server/ # Server 端示例
安装教程
- 基础安装(推荐 uv):
uv pip install py-wecom-toolkit - 带缓存依赖安装(diskcache / redis):
uv pip install py-wecom-toolkit[cache] - 开发环境安装(包含测试、代码检查工具):
uv pip install py-wecom-toolkit[dev]
使用说明
- Webhook 消息发送(同步):
from py_wecom_toolkit.webhook.client import WeComWebhookClient
with WeComWebhookClient(key="你的webhook_key") as client:
resp = client.send_text("Hello, World!")
print(f"发送结果: {resp.errmsg}")
- Webhook 消息发送(异步):
import asyncio
from py_wecom_toolkit.webhook.client import AsyncWeComWebhookClient
async def main():
async with AsyncWeComWebhookClient(key="你的webhook_key") as client:
resp = await client.send_text("Hello, World!")
print(f"发送结果: {resp.errmsg}")
asyncio.run(main())
- Server 端 API(同步):
from py_wecom_toolkit.server.client import WeComServerClient
from py_wecom_toolkit.server.cache import WeComServerDiskCache
client = WeComServerClient(
corpid="你的CorpID",
corpsecret="你的CorpSecret",
agentid="你的AgentID",
cache=WeComServerDiskCache(directory=".runtime.cache.diskcache"),
)
token = client.get_access_token()
print(f"access_token: {token}")
resp = client.send_text(content="Hello, WeCom!", touser="Guolei")
print(f"发送结果: {resp.errmsg}")
client.close()
参与贡献
- Fork 本仓库
- 新建 Feat_xxx 分支
- 提交代码
- 新建 Pull Request
特技
- 同步与异步双模客户端,方法名完全一致
- 基于 Pydantic v2 的消息模型,字段自动校验
- 自动缓存 access_token,支持内存、diskcache、Redis 三种后端
Metadata
Release files for py-wecom-toolkit 3.0.13
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 | |
|---|---|---|---|---|
| py_wecom_toolkit-3.0.13.tar.gz | 20.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| py_wecom_toolkit-3.0.13-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 44.9 kB
Release files / py_wecom_toolkit-3.0.13.tar.gz
| Download URL | py_wecom_toolkit-3.0.13.tar.gz |
|---|---|
| Size | 20.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7bf329a03e700f45ceeab8c28c2cca5aff5d40ddb3a2644334555111d1415ad0
|
|
BLAKE2b-256 checksum How to use checksums |
4cbc3401be5e75a70367960822cfddf70b1387acfaeb87bf83b52ba1f39dbd28
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.11
|
Release files / py_wecom_toolkit-3.0.13-py3-none-any.whl
| Download URL | py_wecom_toolkit-3.0.13-py3-none-any.whl |
|---|---|
| Size | 24.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
990ea079ed3ac2ffa32a995431a17c21cc6d2eff727013d5aef2fb5ae652dd97
|
|
BLAKE2b-256 checksum How to use checksums |
8ee542ddb1d53fca85c2b45a9e6f8baf98da6ff7241d8a5e85bbd8bcd709d974
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.11
|
Security for py-wecom-toolkit 3.0.13
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.