Skip to main content

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/*

Metadata

Release files for luke-qywechat 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for luke-qywechat 1.1.0
File Size Uploaded Provenance
luke_qywechat-1.1.0.tar.gz 10.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for luke-qywechat 1.1.0
File Interpreter ABI Platform
luke_qywechat-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 22.3 kB

Release files / luke_qywechat-1.1.0.tar.gz

Download URL luke_qywechat-1.1.0.tar.gz
Size 10.2 kB
Tags Source
SHA-256 checksum
How to use checksums
efb68159cfcadd6c30b354fdec568c632e9226c36ef4369104c075ce5533afe1
BLAKE2b-256 checksum
How to use checksums
98ec9326a28d73a4a53bd309aab54aa93bd37a577be73d6409ce36d0060d842d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Release files / luke_qywechat-1.1.0-py3-none-any.whl

Download URL luke_qywechat-1.1.0-py3-none-any.whl
Size 12.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
667f9c7e61b327fa88e648d36500f309acdcef2ab6d1adef60859904b873cbef
BLAKE2b-256 checksum
How to use checksums
b6b8698e94caf0453daf8c18f4e98bbbe015695fc3c412989b80d6fec97283d7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Security for luke-qywechat 1.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.

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page