py-wecom 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- py_wecom-0.1.0/LICENSE +21 -0
- py_wecom-0.1.0/PKG-INFO +295 -0
- py_wecom-0.1.0/README.md +240 -0
- py_wecom-0.1.0/pyproject.toml +47 -0
- py_wecom-0.1.0/setup.cfg +4 -0
- py_wecom-0.1.0/src/py_wecom/__init__.py +2 -0
- py_wecom-0.1.0/src/py_wecom/server/__init__.py +352 -0
- py_wecom-0.1.0/src/py_wecom/server/materials.py +160 -0
- py_wecom-0.1.0/src/py_wecom/server/messages.py +659 -0
- py_wecom-0.1.0/src/py_wecom/server/responses.py +28 -0
- py_wecom-0.1.0/src/py_wecom/server/utils.py +76 -0
- py_wecom-0.1.0/src/py_wecom/webhook/__init__.py +644 -0
- py_wecom-0.1.0/src/py_wecom/webhook/messages.py +131 -0
- py_wecom-0.1.0/src/py_wecom/webhook/responses.py +49 -0
- py_wecom-0.1.0/src/py_wecom/webhook/utils.py +118 -0
- py_wecom-0.1.0/src/py_wecom.egg-info/PKG-INFO +295 -0
- py_wecom-0.1.0/src/py_wecom.egg-info/SOURCES.txt +18 -0
- py_wecom-0.1.0/src/py_wecom.egg-info/dependency_links.txt +1 -0
- py_wecom-0.1.0/src/py_wecom.egg-info/requires.txt +12 -0
- py_wecom-0.1.0/src/py_wecom.egg-info/top_level.txt +1 -0
py_wecom-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 郭磊
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
py_wecom-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: py-wecom
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: 企业微信 SDK,提供企业微信 API 的 Python 封装,支持同步和异步调用。
|
|
5
|
+
Author-email: Guolei <174000902@qq.com>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2026 郭磊
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Homepage, https://gitee.com/guolei19850528/py_wecom
|
|
29
|
+
Project-URL: Repository, https://gitee.com/guolei19850528/py_wecom.git
|
|
30
|
+
Keywords: wecom,python,client,api,企业微信,异步调用,server,消息推送,文件上传
|
|
31
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
32
|
+
Classifier: Development Status :: 4 - Beta
|
|
33
|
+
Classifier: Intended Audience :: Developers
|
|
34
|
+
Classifier: Programming Language :: Python :: 3
|
|
35
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
36
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
37
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
38
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
39
|
+
Classifier: Operating System :: OS Independent
|
|
40
|
+
Requires-Python: >=3.10
|
|
41
|
+
Description-Content-Type: text/markdown
|
|
42
|
+
License-File: LICENSE
|
|
43
|
+
Requires-Dist: httpx>=0.27.0
|
|
44
|
+
Requires-Dist: pydantic>=2.0
|
|
45
|
+
Requires-Dist: jsonpath-ng>=1.5.3
|
|
46
|
+
Requires-Dist: jsonschema>=4.21.0
|
|
47
|
+
Requires-Dist: diskcache>=5.6.3
|
|
48
|
+
Requires-Dist: redis>=4.6.0
|
|
49
|
+
Provides-Extra: dev
|
|
50
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
51
|
+
Requires-Dist: pytest-cov>=4.0; extra == "dev"
|
|
52
|
+
Requires-Dist: setuptools>=61.0; extra == "dev"
|
|
53
|
+
Requires-Dist: twine>=4.0; extra == "dev"
|
|
54
|
+
Dynamic: license-file
|
|
55
|
+
|
|
56
|
+
# py_wecom
|
|
57
|
+
|
|
58
|
+
企业微信 SDK,提供企业微信 API 的 Python 封装,支持同步和异步调用。
|
|
59
|
+
|
|
60
|
+
## 功能特性
|
|
61
|
+
|
|
62
|
+
- **Server API**: 企业微信服务端 API,支持消息发送、素材上传等
|
|
63
|
+
- **Webhook API**: 企业微信机器人 Webhook 接口,支持多种消息类型
|
|
64
|
+
- **同步/异步支持**: 所有接口均提供同步和异步版本
|
|
65
|
+
- **类型安全**: 使用 Pydantic 进行数据验证,提供完整的类型提示
|
|
66
|
+
- **缓存支持**: 支持 Redis 和 diskcache 缓存 access_token
|
|
67
|
+
|
|
68
|
+
## 安装
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
pip install py_wecom
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## 快速开始
|
|
75
|
+
|
|
76
|
+
### Server API
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
from py_wecom.server import Base, Sender, Uploader, Text, TextContent
|
|
80
|
+
|
|
81
|
+
# 初始化客户端
|
|
82
|
+
server = Base(
|
|
83
|
+
corpid="your_corp_id",
|
|
84
|
+
corpsecret="your_corp_secret",
|
|
85
|
+
agentid="your_agent_id"
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
# 刷新 access_token
|
|
89
|
+
server.refresh_access_token()
|
|
90
|
+
|
|
91
|
+
# 发送文本消息
|
|
92
|
+
sender = Sender()
|
|
93
|
+
sender.agentid = server.agentid
|
|
94
|
+
sender.access_token = server.access_token
|
|
95
|
+
|
|
96
|
+
content = Text(
|
|
97
|
+
touser="@all",
|
|
98
|
+
text=TextContent(content="Hello from py_wecom!")
|
|
99
|
+
)
|
|
100
|
+
response = sender.send_text(content=content)
|
|
101
|
+
print(response.json())
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Webhook API
|
|
105
|
+
|
|
106
|
+
```python
|
|
107
|
+
from py_wecom.webhook import Webhook, Text, TextContent
|
|
108
|
+
|
|
109
|
+
# 初始化 Webhook 客户端
|
|
110
|
+
webhook = Webhook(key="your_webhook_key")
|
|
111
|
+
|
|
112
|
+
# 发送文本消息
|
|
113
|
+
content = Text(
|
|
114
|
+
text=TextContent(
|
|
115
|
+
content="Hello from py_wecom!",
|
|
116
|
+
mentioned_list=["@all"]
|
|
117
|
+
)
|
|
118
|
+
)
|
|
119
|
+
response = webhook.send_text(content=content)
|
|
120
|
+
print(response.json())
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## 支持的消息类型
|
|
124
|
+
|
|
125
|
+
### Server API 支持的消息类型
|
|
126
|
+
|
|
127
|
+
| 消息类型 | 说明 |
|
|
128
|
+
|---------|------|
|
|
129
|
+
| Text | 文本消息 |
|
|
130
|
+
| Image | 图片消息 |
|
|
131
|
+
| Voice | 语音消息 |
|
|
132
|
+
| Video | 视频消息 |
|
|
133
|
+
| File | 文件消息 |
|
|
134
|
+
| TextCard | 文本卡片消息 |
|
|
135
|
+
| News | 图文消息 |
|
|
136
|
+
| MpNews | 图文消息(mpnews) |
|
|
137
|
+
| Markdown | Markdown消息 |
|
|
138
|
+
| MiniprogramNotice | 小程序通知 |
|
|
139
|
+
| TemplateCard | 模板卡片消息 |
|
|
140
|
+
|
|
141
|
+
### Webhook API 支持的消息类型
|
|
142
|
+
|
|
143
|
+
| 消息类型 | 说明 |
|
|
144
|
+
|---------|------|
|
|
145
|
+
| Text | 文本消息 |
|
|
146
|
+
| Markdown | Markdown消息 |
|
|
147
|
+
| MarkdownV2 | MarkdownV2消息(支持更多样式) |
|
|
148
|
+
| Image | 图片消息 |
|
|
149
|
+
| News | 图文消息 |
|
|
150
|
+
| Voice | 语音消息 |
|
|
151
|
+
| File | 文件消息 |
|
|
152
|
+
| TemplateCard | 模板卡片消息 |
|
|
153
|
+
|
|
154
|
+
## 使用示例
|
|
155
|
+
|
|
156
|
+
### 发送图片消息 (Server API)
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
from py_wecom.server import Base, Sender, Image, ImageContent
|
|
160
|
+
|
|
161
|
+
server = Base(corpid="xxx", corpsecret="xxx", agentid="xxx")
|
|
162
|
+
server.refresh_access_token()
|
|
163
|
+
|
|
164
|
+
sender = Sender()
|
|
165
|
+
sender.agentid = server.agentid
|
|
166
|
+
sender.access_token = server.access_token
|
|
167
|
+
|
|
168
|
+
# 先上传图片获取 media_id
|
|
169
|
+
uploader = Uploader()
|
|
170
|
+
uploader.access_token = server.access_token
|
|
171
|
+
|
|
172
|
+
# 上传图片
|
|
173
|
+
with open("image.jpg", "rb") as f:
|
|
174
|
+
upload_response = uploader.upload(
|
|
175
|
+
file_type="image",
|
|
176
|
+
files={"media": f}
|
|
177
|
+
)
|
|
178
|
+
media_id = upload_response.json()["media_id"]
|
|
179
|
+
|
|
180
|
+
# 发送图片消息
|
|
181
|
+
content = Image(
|
|
182
|
+
touser="user_id",
|
|
183
|
+
image=ImageContent(media_id=media_id)
|
|
184
|
+
)
|
|
185
|
+
response = sender.send_image(content=content)
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### 发送 Markdown 消息 (Webhook)
|
|
189
|
+
|
|
190
|
+
```python
|
|
191
|
+
from py_wecom.webhook import Webhook, Markdown, MarkdownContent
|
|
192
|
+
|
|
193
|
+
webhook = Webhook(key="your_webhook_key")
|
|
194
|
+
|
|
195
|
+
content = Markdown(
|
|
196
|
+
markdown=MarkdownContent(
|
|
197
|
+
content="""
|
|
198
|
+
# 标题
|
|
199
|
+
|
|
200
|
+
**加粗文本**
|
|
201
|
+
|
|
202
|
+
*斜体文本*
|
|
203
|
+
|
|
204
|
+
[链接](https://example.com)
|
|
205
|
+
"""
|
|
206
|
+
)
|
|
207
|
+
)
|
|
208
|
+
response = webhook.send_markdown(content=content)
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
### 使用缓存 (Server API)
|
|
212
|
+
|
|
213
|
+
```python
|
|
214
|
+
import redis
|
|
215
|
+
from py_wecom.server import Base
|
|
216
|
+
|
|
217
|
+
# 使用 Redis 缓存 access_token
|
|
218
|
+
redis_client = redis.Redis(host="localhost", port=6379, db=0)
|
|
219
|
+
|
|
220
|
+
server = Base(
|
|
221
|
+
corpid="xxx",
|
|
222
|
+
corpsecret="xxx",
|
|
223
|
+
agentid="xxx",
|
|
224
|
+
cache_config={
|
|
225
|
+
"instance": redis_client,
|
|
226
|
+
"key": "pywecom_access_token",
|
|
227
|
+
"expire": 7100
|
|
228
|
+
}
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
# 刷新 access_token(会自动使用缓存)
|
|
232
|
+
server.refresh_access_token()
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
## 异步支持
|
|
236
|
+
|
|
237
|
+
所有方法均提供异步版本,方法名以 `async_` 开头:
|
|
238
|
+
|
|
239
|
+
```python
|
|
240
|
+
import asyncio
|
|
241
|
+
from py_wecom.server import Base, Sender, Text, TextContent
|
|
242
|
+
|
|
243
|
+
async def main():
|
|
244
|
+
server = Base(corpid="xxx", corpsecret="xxx", agentid="xxx")
|
|
245
|
+
await server.async_refresh_access_token()
|
|
246
|
+
|
|
247
|
+
sender = Sender()
|
|
248
|
+
sender.agentid = server.agentid
|
|
249
|
+
sender.access_token = server.access_token
|
|
250
|
+
|
|
251
|
+
content = Text(
|
|
252
|
+
touser="@all",
|
|
253
|
+
text=TextContent(content="Hello!")
|
|
254
|
+
)
|
|
255
|
+
response = await sender.async_send_text(content=content)
|
|
256
|
+
print(response.json())
|
|
257
|
+
|
|
258
|
+
asyncio.run(main())
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
## 项目结构
|
|
262
|
+
|
|
263
|
+
```
|
|
264
|
+
py_wecom/
|
|
265
|
+
├── src/
|
|
266
|
+
│ └── py_wecom/
|
|
267
|
+
│ ├── __init__.py
|
|
268
|
+
│ ├── server/ # Server API
|
|
269
|
+
│ │ ├── __init__.py # Base 类,access_token 管理
|
|
270
|
+
│ │ ├── materials.py # 素材上传
|
|
271
|
+
│ │ ├── messages.py # 消息类型和发送
|
|
272
|
+
│ │ ├── responses.py # 响应模型
|
|
273
|
+
│ │ └── utils.py # 工具函数
|
|
274
|
+
│ └── webhook/ # Webhook API
|
|
275
|
+
│ ├── __init__.py # Webhook 客户端
|
|
276
|
+
│ ├── messages.py # 消息类型
|
|
277
|
+
│ ├── responses.py # 响应模型
|
|
278
|
+
│ └── utils.py # 工具函数
|
|
279
|
+
└── README.md
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
## 参考文档
|
|
283
|
+
|
|
284
|
+
- [企业微信开发者文档](https://developer.work.weixin.qq.com/document/path/90238)
|
|
285
|
+
- [企业微信机器人文档](https://developer.work.weixin.qq.com/document/path/91770)
|
|
286
|
+
|
|
287
|
+
## 主页
|
|
288
|
+
|
|
289
|
+
[https://gitee.com/guolei19850528/py_wecom](https://gitee.com/guolei19850528/py_wecom)
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
## 许可证
|
|
294
|
+
|
|
295
|
+
MIT License
|
py_wecom-0.1.0/README.md
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
# py_wecom
|
|
2
|
+
|
|
3
|
+
企业微信 SDK,提供企业微信 API 的 Python 封装,支持同步和异步调用。
|
|
4
|
+
|
|
5
|
+
## 功能特性
|
|
6
|
+
|
|
7
|
+
- **Server API**: 企业微信服务端 API,支持消息发送、素材上传等
|
|
8
|
+
- **Webhook API**: 企业微信机器人 Webhook 接口,支持多种消息类型
|
|
9
|
+
- **同步/异步支持**: 所有接口均提供同步和异步版本
|
|
10
|
+
- **类型安全**: 使用 Pydantic 进行数据验证,提供完整的类型提示
|
|
11
|
+
- **缓存支持**: 支持 Redis 和 diskcache 缓存 access_token
|
|
12
|
+
|
|
13
|
+
## 安装
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pip install py_wecom
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## 快速开始
|
|
20
|
+
|
|
21
|
+
### Server API
|
|
22
|
+
|
|
23
|
+
```python
|
|
24
|
+
from py_wecom.server import Base, Sender, Uploader, Text, TextContent
|
|
25
|
+
|
|
26
|
+
# 初始化客户端
|
|
27
|
+
server = Base(
|
|
28
|
+
corpid="your_corp_id",
|
|
29
|
+
corpsecret="your_corp_secret",
|
|
30
|
+
agentid="your_agent_id"
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
# 刷新 access_token
|
|
34
|
+
server.refresh_access_token()
|
|
35
|
+
|
|
36
|
+
# 发送文本消息
|
|
37
|
+
sender = Sender()
|
|
38
|
+
sender.agentid = server.agentid
|
|
39
|
+
sender.access_token = server.access_token
|
|
40
|
+
|
|
41
|
+
content = Text(
|
|
42
|
+
touser="@all",
|
|
43
|
+
text=TextContent(content="Hello from py_wecom!")
|
|
44
|
+
)
|
|
45
|
+
response = sender.send_text(content=content)
|
|
46
|
+
print(response.json())
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Webhook API
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
from py_wecom.webhook import Webhook, Text, TextContent
|
|
53
|
+
|
|
54
|
+
# 初始化 Webhook 客户端
|
|
55
|
+
webhook = Webhook(key="your_webhook_key")
|
|
56
|
+
|
|
57
|
+
# 发送文本消息
|
|
58
|
+
content = Text(
|
|
59
|
+
text=TextContent(
|
|
60
|
+
content="Hello from py_wecom!",
|
|
61
|
+
mentioned_list=["@all"]
|
|
62
|
+
)
|
|
63
|
+
)
|
|
64
|
+
response = webhook.send_text(content=content)
|
|
65
|
+
print(response.json())
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## 支持的消息类型
|
|
69
|
+
|
|
70
|
+
### Server API 支持的消息类型
|
|
71
|
+
|
|
72
|
+
| 消息类型 | 说明 |
|
|
73
|
+
|---------|------|
|
|
74
|
+
| Text | 文本消息 |
|
|
75
|
+
| Image | 图片消息 |
|
|
76
|
+
| Voice | 语音消息 |
|
|
77
|
+
| Video | 视频消息 |
|
|
78
|
+
| File | 文件消息 |
|
|
79
|
+
| TextCard | 文本卡片消息 |
|
|
80
|
+
| News | 图文消息 |
|
|
81
|
+
| MpNews | 图文消息(mpnews) |
|
|
82
|
+
| Markdown | Markdown消息 |
|
|
83
|
+
| MiniprogramNotice | 小程序通知 |
|
|
84
|
+
| TemplateCard | 模板卡片消息 |
|
|
85
|
+
|
|
86
|
+
### Webhook API 支持的消息类型
|
|
87
|
+
|
|
88
|
+
| 消息类型 | 说明 |
|
|
89
|
+
|---------|------|
|
|
90
|
+
| Text | 文本消息 |
|
|
91
|
+
| Markdown | Markdown消息 |
|
|
92
|
+
| MarkdownV2 | MarkdownV2消息(支持更多样式) |
|
|
93
|
+
| Image | 图片消息 |
|
|
94
|
+
| News | 图文消息 |
|
|
95
|
+
| Voice | 语音消息 |
|
|
96
|
+
| File | 文件消息 |
|
|
97
|
+
| TemplateCard | 模板卡片消息 |
|
|
98
|
+
|
|
99
|
+
## 使用示例
|
|
100
|
+
|
|
101
|
+
### 发送图片消息 (Server API)
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
from py_wecom.server import Base, Sender, Image, ImageContent
|
|
105
|
+
|
|
106
|
+
server = Base(corpid="xxx", corpsecret="xxx", agentid="xxx")
|
|
107
|
+
server.refresh_access_token()
|
|
108
|
+
|
|
109
|
+
sender = Sender()
|
|
110
|
+
sender.agentid = server.agentid
|
|
111
|
+
sender.access_token = server.access_token
|
|
112
|
+
|
|
113
|
+
# 先上传图片获取 media_id
|
|
114
|
+
uploader = Uploader()
|
|
115
|
+
uploader.access_token = server.access_token
|
|
116
|
+
|
|
117
|
+
# 上传图片
|
|
118
|
+
with open("image.jpg", "rb") as f:
|
|
119
|
+
upload_response = uploader.upload(
|
|
120
|
+
file_type="image",
|
|
121
|
+
files={"media": f}
|
|
122
|
+
)
|
|
123
|
+
media_id = upload_response.json()["media_id"]
|
|
124
|
+
|
|
125
|
+
# 发送图片消息
|
|
126
|
+
content = Image(
|
|
127
|
+
touser="user_id",
|
|
128
|
+
image=ImageContent(media_id=media_id)
|
|
129
|
+
)
|
|
130
|
+
response = sender.send_image(content=content)
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### 发送 Markdown 消息 (Webhook)
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
from py_wecom.webhook import Webhook, Markdown, MarkdownContent
|
|
137
|
+
|
|
138
|
+
webhook = Webhook(key="your_webhook_key")
|
|
139
|
+
|
|
140
|
+
content = Markdown(
|
|
141
|
+
markdown=MarkdownContent(
|
|
142
|
+
content="""
|
|
143
|
+
# 标题
|
|
144
|
+
|
|
145
|
+
**加粗文本**
|
|
146
|
+
|
|
147
|
+
*斜体文本*
|
|
148
|
+
|
|
149
|
+
[链接](https://example.com)
|
|
150
|
+
"""
|
|
151
|
+
)
|
|
152
|
+
)
|
|
153
|
+
response = webhook.send_markdown(content=content)
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### 使用缓存 (Server API)
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
import redis
|
|
160
|
+
from py_wecom.server import Base
|
|
161
|
+
|
|
162
|
+
# 使用 Redis 缓存 access_token
|
|
163
|
+
redis_client = redis.Redis(host="localhost", port=6379, db=0)
|
|
164
|
+
|
|
165
|
+
server = Base(
|
|
166
|
+
corpid="xxx",
|
|
167
|
+
corpsecret="xxx",
|
|
168
|
+
agentid="xxx",
|
|
169
|
+
cache_config={
|
|
170
|
+
"instance": redis_client,
|
|
171
|
+
"key": "pywecom_access_token",
|
|
172
|
+
"expire": 7100
|
|
173
|
+
}
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
# 刷新 access_token(会自动使用缓存)
|
|
177
|
+
server.refresh_access_token()
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## 异步支持
|
|
181
|
+
|
|
182
|
+
所有方法均提供异步版本,方法名以 `async_` 开头:
|
|
183
|
+
|
|
184
|
+
```python
|
|
185
|
+
import asyncio
|
|
186
|
+
from py_wecom.server import Base, Sender, Text, TextContent
|
|
187
|
+
|
|
188
|
+
async def main():
|
|
189
|
+
server = Base(corpid="xxx", corpsecret="xxx", agentid="xxx")
|
|
190
|
+
await server.async_refresh_access_token()
|
|
191
|
+
|
|
192
|
+
sender = Sender()
|
|
193
|
+
sender.agentid = server.agentid
|
|
194
|
+
sender.access_token = server.access_token
|
|
195
|
+
|
|
196
|
+
content = Text(
|
|
197
|
+
touser="@all",
|
|
198
|
+
text=TextContent(content="Hello!")
|
|
199
|
+
)
|
|
200
|
+
response = await sender.async_send_text(content=content)
|
|
201
|
+
print(response.json())
|
|
202
|
+
|
|
203
|
+
asyncio.run(main())
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
## 项目结构
|
|
207
|
+
|
|
208
|
+
```
|
|
209
|
+
py_wecom/
|
|
210
|
+
├── src/
|
|
211
|
+
│ └── py_wecom/
|
|
212
|
+
│ ├── __init__.py
|
|
213
|
+
│ ├── server/ # Server API
|
|
214
|
+
│ │ ├── __init__.py # Base 类,access_token 管理
|
|
215
|
+
│ │ ├── materials.py # 素材上传
|
|
216
|
+
│ │ ├── messages.py # 消息类型和发送
|
|
217
|
+
│ │ ├── responses.py # 响应模型
|
|
218
|
+
│ │ └── utils.py # 工具函数
|
|
219
|
+
│ └── webhook/ # Webhook API
|
|
220
|
+
│ ├── __init__.py # Webhook 客户端
|
|
221
|
+
│ ├── messages.py # 消息类型
|
|
222
|
+
│ ├── responses.py # 响应模型
|
|
223
|
+
│ └── utils.py # 工具函数
|
|
224
|
+
└── README.md
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
## 参考文档
|
|
228
|
+
|
|
229
|
+
- [企业微信开发者文档](https://developer.work.weixin.qq.com/document/path/90238)
|
|
230
|
+
- [企业微信机器人文档](https://developer.work.weixin.qq.com/document/path/91770)
|
|
231
|
+
|
|
232
|
+
## 主页
|
|
233
|
+
|
|
234
|
+
[https://gitee.com/guolei19850528/py_wecom](https://gitee.com/guolei19850528/py_wecom)
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
## 许可证
|
|
239
|
+
|
|
240
|
+
MIT License
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
[project]
|
|
5
|
+
name = "py-wecom"
|
|
6
|
+
version = "0.1.0"
|
|
7
|
+
requires-python = ">=3.10"
|
|
8
|
+
authors = [
|
|
9
|
+
{ name = "Guolei", email = "174000902@qq.com" }
|
|
10
|
+
]
|
|
11
|
+
description = "企业微信 SDK,提供企业微信 API 的 Python 封装,支持同步和异步调用。"
|
|
12
|
+
keywords = ["wecom", "python", "client", "api", "企业微信", "异步调用", "server", "消息推送", "文件上传"]
|
|
13
|
+
license = { file = "LICENSE" }
|
|
14
|
+
readme = "README.md"
|
|
15
|
+
classifiers = [
|
|
16
|
+
"License :: OSI Approved :: MIT License",
|
|
17
|
+
"Development Status :: 4 - Beta",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3.10",
|
|
21
|
+
"Programming Language :: Python :: 3.11",
|
|
22
|
+
"Programming Language :: Python :: 3.12",
|
|
23
|
+
"Programming Language :: Python :: 3.13",
|
|
24
|
+
"Operating System :: OS Independent",
|
|
25
|
+
]
|
|
26
|
+
dependencies = [
|
|
27
|
+
"httpx>=0.27.0",
|
|
28
|
+
"pydantic>=2.0",
|
|
29
|
+
"jsonpath-ng>=1.5.3",
|
|
30
|
+
"jsonschema>=4.21.0",
|
|
31
|
+
"diskcache>=5.6.3",
|
|
32
|
+
"redis>=4.6.0",
|
|
33
|
+
]
|
|
34
|
+
[project.optional-dependencies]
|
|
35
|
+
dev = [
|
|
36
|
+
"pytest>=7.0",
|
|
37
|
+
"pytest-cov>=4.0",
|
|
38
|
+
"setuptools>=61.0",
|
|
39
|
+
"twine>=4.0",
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
[project.urls]
|
|
43
|
+
"Homepage" = "https://gitee.com/guolei19850528/py_wecom"
|
|
44
|
+
"Repository" = "https://gitee.com/guolei19850528/py_wecom.git"
|
|
45
|
+
|
|
46
|
+
[tool.setuptools.packages.find]
|
|
47
|
+
where = ["src"]
|
py_wecom-0.1.0/setup.cfg
ADDED