WechatPush
基于 企业微信 API 的微信消息推送, 无需自建服务器的 Python 库解决方案.
前言
很多时候编写程序时希望能将程序的运行结果实时告知开发者, 需要点对点的推送服务.
之前组内一直使用的QQ机器人,直到某天大佬提桶跑路无人维护,机器人挂了就再也起不来了.
没办法只有自己动手丰衣足食,github上搜到了这个库,对企业微信API简单封装了一层,简单修改了一下,使用起来也算方便.
开始
而此 Python 库有以下优点:
- 无需自建服务器, 不使用第三方服务器, 无其他费用
- 第一次需要配置(只需要个人微信), 之后可以直接调用, 简单方便
- 无需安装企业微信客户端, 可以直接在微信接收推送
- 调用次数充足, 限制少
- 可以推送 文本, 超链接, 图片, 文件, Markdown 多种格式
配置(本步内容参考了 WecomChan 的相关文档)
配置过程中, 你需要记录下 corp_id secret 以及 agent_id 三个值.
-
第一步,注册企业
用电脑打开企业微信官网,注册一个企业
-
第二步,创建应用
注册成功后,点「管理企业」进入管理界面,选择「应用管理」 → 「自建」 → 「创建应用」
应用名称可以随意填入,应用logo自己上传一张图片,可见范围选择公司名。
创建完成后进入应用详情页,可以得到
agent_id,应用Secret(secret)。注意:
secret推送到手机端时,只能在企业微信客户端中查看。 -
第三步,获取企业ID
进入「我的企业」页面,拉到最下边,可以看到企业ID(
corp_id)③,复制并填到上方。推送UID直接填
@all,推送给公司全员。 -
第四步,推送消息到微信
进入「我的企业」 → 「微信插件」,拉到下边扫描二维码,关注以后即可收到推送的消息。
PS:如果出现
接口请求正常,企业微信接受消息正常,个人微信无法收到消息的情况:进入「我的企业」 → 「微信插件」,拉到最下方,勾选 “允许成员在微信插件中接收和回复聊天消息”
安装
pip install wechatpush-fix
示例
>>> from wechatpush import WechatPush
>>> push = WechatPush("你的corp_id", "你的secret", "你的agent_id")
>>> push.send_text('Hello!\n文本支持换行\n<a href="https://github.com">文本支持超链接</a>')
{'errcode': 0, 'errmsg': 'ok', 'msgid': 'xxx'}
>>> push.send_markdown("**Markdown here!**")
{'errcode': 0, 'errmsg': 'ok', 'msgid': 'xxx'}
>>> push.send_file(open("test.txt", "rb"), "微信中显示的文件名称")
{'errcode': 0, 'errmsg': 'ok', 'msgid': 'xxx'}
>>> push.send_image(open("test.png", "rb"))
{'errcode': 0, 'errmsg': 'ok', 'msgid': 'xxx'}
Metadata
Release files for wechatpush-fix 1.0.3
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 |
|---|---|---|---|---|
| wechatpush_fix-1.0.3.tar.gz | 5.5 kB | Without provenance | Details for wechatpush_fix-1.0.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| wechatpush_fix-1.0.3-py3-none-any.whl | Python 3 | none | any | Details for wechatpush_fix-1.0.3-py3-none-any.whl |
Total release size: 11.0 kB
Release files / wechatpush_fix-1.0.3.tar.gz
| Download URL | wechatpush_fix-1.0.3.tar.gz |
|---|---|
| Size | 5.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ad1f78b17ae9261c6aa212b70ff1e7a1eaffc3298f3c212b8c57f054b1700026
|
|
BLAKE2b-256 checksum How to use checksums |
986cc2b593c9e6a42b04a70d7ea7af4bed237ee225763648826448f291bd32cd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Release files / wechatpush_fix-1.0.3-py3-none-any.whl
| Download URL | wechatpush_fix-1.0.3-py3-none-any.whl |
|---|---|
| Size | 5.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e64cc0bc323956cfc327a85783169e3721c8331ef1dfc99138cba13301168224
|
|
BLAKE2b-256 checksum How to use checksums |
28626e9e626fb607aaf7f924800eb5429f07efa27520d930bbf95b651dc44a6d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Security for wechatpush-fix 1.0.3
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.