wechatapi-python
WeChatAPI Labs 接口的 Python SDK。用代码收发微信和企业微信消息、管理好友和群、校验回调签名。
安装
pip install wechatapilabs
需要 Python 3.8 或更高版本,唯一依赖是 requests。
快速开始
import os
from wechatapilabs import WeChatAPI
client = WeChatAPI(os.environ["WECHATAPI_KEY"])
client.message.send_text(
account_id="acc_123",
to="123456@chatroom", # 好友 ID 或群 ID
content="今日报表已生成",
at=["wxid_alice"], # 可选,群里 @ 成员
)
API Key 和测试账号请在联系我们申请。每个方法都和接口文档里的接口一一对应。
方法列表(公开接口)
| 方法 | 接口 |
|---|---|
account.login_qrcode(type) / account.status(account_id) |
/v1/account/* |
message.send_text / send_image / send_file / send_link / revoke |
/v1/message/* |
contact.friends / iter_friends / accept / remark |
/v1/contact/* |
group.create / invite / remove / notice / members |
/v1/group/* |
moment.post(...) |
/v1/moment/post |
wecom.group_send / customers |
/v1/wecom/* |
request(method, path, params) |
直接调用任意接口 |
说明: 上表是已公开的 18 个常用接口,SDK 已全部封装。完整版 API 还包括更多接口,以及详细的参数、返回字段和回调说明。如需获取,请到官网联系我们。
完整版 API 还覆盖这些能力
| 能力 | 内容 |
|---|---|
| 更多消息类型 | 除文本、图片、文件、链接卡片外,还支持语音、视频、小程序卡片的发送和接收 |
| 好友管理 | 搜索并添加好友、自动通过好友申请、获取好友资料 |
| 群管理 | 修改群名,以及入群、退群事件回调 |
| 朋友圈 | 获取好友朋友圈、点赞与评论 |
| 企业微信客户管理 | 添加客户、打标签、修改备注,与 CRM / SCRM 系统打通 |
| 企业微信外部群管理 | 创建外部群、邀请客户进群、修改群公告、移出成员 |
| 客户朋友圈 | 发布企业微信客户朋友圈 |
| 多成员托管 | 一个控制台管理多个账号,按成员分配回调地址和权限 |
| 其他 | 绝大部分功能都是存在的 |
已有的接口都可以通过 request(method, path, params) 直接调用,不需要等 SDK 更新。有列表里没有的需求,大部分都是存在的,欢迎联系我们。
方法返回响应里的 data 字段。code 非 0 时抛出 WeChatAPIError,带 .code、.request_id 和 .retry_after。遇到限流(45009)和 5xx 会自动重试,默认最多 2 次(max_retries)。
回调
事件以 JSON POST 到你的回调地址,请求头 X-Signature 是用回调密钥对原始请求体做 HMAC-SHA256 的十六进制结果。请对原始字节校验,5 秒内返回 200,再处理业务。
from wechatapilabs import verify_webhook
if not verify_webhook(request.get_data(), request.headers.get("X-Signature"), SECRET):
abort(401)
完整的 Flask 示例见 examples/flask_webhook.py。要接入大模型,请看 wechat-ai-bot-template。
错误处理
from wechatapilabs import WeChatAPIError
try:
client.message.send_text("acc_123", "wxid_bob", "你好")
except WeChatAPIError as e:
if e.code == 42001: # 账号离线
...
elif e.code == 46001: # 触发风控:暂停主动操作 24 小时
...
完整错误码见接口文档。
测试
python -m unittest discover -s tests -t .
合规使用
请只发送对方预期会收到的消息,比如客服回复、通知和用户主动加入的群。批量发送骚扰消息违反微信的使用规范,会导致账号被限制。详见安全与合规。
许可证
MIT
Metadata
Release files for wechatapilabs 0.1.0
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 |
|---|---|---|---|---|
| wechatapilabs-0.1.0.tar.gz | 7.6 kB | Without provenance | Details for wechatapilabs-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| wechatapilabs-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for wechatapilabs-0.1.0-py3-none-any.whl |
Total release size: 15.1 kB
Release files / wechatapilabs-0.1.0.tar.gz
| Download URL | wechatapilabs-0.1.0.tar.gz |
|---|---|
| Size | 7.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9be2afa0bb2f3142a65456cd7cd68b044cc35ed3d4bcb4dea35ca7d7b4f096d0
|
|
BLAKE2b-256 checksum How to use checksums |
049522784f87eea1da4bc6ac9ba182409158ffc973e5569e60f2f393fd89df51
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.16
|
Release files / wechatapilabs-0.1.0-py3-none-any.whl
| Download URL | wechatapilabs-0.1.0-py3-none-any.whl |
|---|---|
| Size | 7.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
eb22caae283becdd6e7fdf6dce51d3459e408d6bbdb32c79f6393985db33a18f
|
|
BLAKE2b-256 checksum How to use checksums |
a848c65e3ba6cfd546165d90013b767535d257647aab637b7ad5939ab85016fa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.16
|
Security for wechatapilabs 0.1.0
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.