A simple SDK for sending messages through Luke's WeCom application.
Project description
luke_qywechat
luke 的企业微信 SDK。包含两个独立的客户端类:
| 类 | 用途 | 后端 |
|---|---|---|
WeChatPub(已有) |
直接调企业微信 API 发文本/图片 | https://qyapi.weixin.qq.com(或反代) |
WeChatHub(新增) |
通过 qywechat_hub 服务收发消息,不感知企业微信细节 | hub 的 HTTP/SSE API |
两个类并存,旧代码 from qy_wechat import WeChatPub 不变。新代码推荐用 WeChatHub:业务方完全不用管 access_token / media_id / 加解密 / IP 白名单。
安装
pip install luke_qywechat
WeChatPub:直接调企业微信(兼容老用法)
只需要传入企业微信应用的 agentId 和 secret:
from qy_wechat import WeChatPub
wechat = WeChatPub(agentId=1000001, secret="your-secret")
wechat.send_msg("hello")
wechat.send_image_msg("/path/to/image.jpg")
wechat.send_image_msg("https://example.com/image.jpg")
兼容旧写法:
wechat = WeChatPub(1000001, "your-secret")
wechat.send_msg("hello", d_ids="5")
命令行测试
不要把 agentId 和 secret 写进代码或提交到仓库。命令行测试可以使用环境变量:
export QY_WECHAT_AGENT_ID=1000001
export QY_WECHAT_SECRET=your-secret
qy-wechat "测试消息"
WeChatHub:通过 qywechat_hub 服务(推荐)
业务方代码完全不接触企业微信——所有发送/接收都通过 hub 中转,由 hub 处理鉴权、加解密、IP 白名单。
配置(环境变量)
export QYWECHAT_HUB_URL=https://qywechat.luke9012.cn # 或 dev 域名
export QYWECHAT_HUB_TOKEN=tok_xxxxxxxx # 找 hub 管理员发
export QYWECHAT_AGENT_ID=1000006 # 默认 agent,可调用时覆盖
发消息
from wechat_hub import WeChatHub
hub = WeChatHub.from_env()
hub.send_text("hello", touser="luke")
hub.send_markdown("# 标题\n正文", touser="luke")
hub.send_image("/path/to/qrcode.png", touser="luke")
hub.send_image(png_bytes, touser="luke") # bytes 也行
收消息(SSE 阻塞迭代器)
for msg in hub.messages(user="luke", types="text"):
if msg.content and msg.content.startswith("登录 "):
platform = msg.content.split(" ", 1)[1]
...
break
迭代器是真的"流式"——hub 推一条 yield 一条。break 出循环时连接会自动断开。
异常处理
from wechat_hub import HubError
try:
hub.send_text("hi", touser="not-a-real-user")
except HubError as e:
print(e.status_code, e.detail)
HubError.status_code 是 HTTP 状态,detail 是 hub 返回的 body(502 时包含企业微信原始 errcode/errmsg)。
发布到 PyPI
本项目不会在包内保存 agentId、secret 或 PyPI token。发布前请使用环境变量或本机 keyring 管理令牌。
python -m build
python -m twine check dist/*
python -m twine upload dist/*
如果使用 token:
export TWINE_USERNAME=__token__
export TWINE_PASSWORD=<your API token>
python -m twine upload dist/*
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file luke_qywechat-1.1.0.tar.gz.
File metadata
- Download URL: luke_qywechat-1.1.0.tar.gz
- Upload date:
- Size: 10.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
efb68159cfcadd6c30b354fdec568c632e9226c36ef4369104c075ce5533afe1
|
|
| MD5 |
036ac241d7fe429e69ee73c79e52ad1e
|
|
| BLAKE2b-256 |
98ec9326a28d73a4a53bd309aab54aa93bd37a577be73d6409ce36d0060d842d
|
File details
Details for the file luke_qywechat-1.1.0-py3-none-any.whl.
File metadata
- Download URL: luke_qywechat-1.1.0-py3-none-any.whl
- Upload date:
- Size: 12.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
667f9c7e61b327fa88e648d36500f309acdcef2ab6d1adef60859904b873cbef
|
|
| MD5 |
52bdb1280c7a12f8838ba28f86c6d9bd
|
|
| BLAKE2b-256 |
b6b8698e94caf0453daf8c18f4e98bbbe015695fc3c412989b80d6fec97283d7
|