py-wecom-toolkit 3.0.2__tar.gz → 3.0.4__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.
Files changed (42) hide show
  1. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/PKG-INFO +88 -104
  2. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/README.md +87 -103
  3. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/examples/webhook/async_basic.py +6 -6
  4. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/examples/webhook/async_file.py +2 -2
  5. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/examples/webhook/async_text.py +2 -2
  6. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/examples/webhook/basic.py +6 -6
  7. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/examples/webhook/file.py +2 -2
  8. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/examples/webhook/image.py +2 -2
  9. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/examples/webhook/markdown.py +2 -2
  10. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/examples/webhook/markdown_v2.py +2 -2
  11. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/examples/webhook/news.py +2 -2
  12. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/examples/webhook/template_card.py +2 -2
  13. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/examples/webhook/text.py +2 -2
  14. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/examples/webhook/voice.py +2 -2
  15. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/pyproject.toml +1 -1
  16. py_wecom_toolkit-3.0.4/src/py_wecom_toolkit/__init__.py +67 -0
  17. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/src/py_wecom_toolkit/server/__init__.py +2 -2
  18. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/src/py_wecom_toolkit/server/client.py +19 -19
  19. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/src/py_wecom_toolkit/server/exceptions.py +6 -6
  20. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/src/py_wecom_toolkit/server/media.py +6 -6
  21. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/src/py_wecom_toolkit/server/message.py +3 -3
  22. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/src/py_wecom_toolkit/server/models.py +6 -6
  23. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/src/py_wecom_toolkit/server/token.py +10 -10
  24. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/src/py_wecom_toolkit/webhook/__init__.py +11 -11
  25. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/src/py_wecom_toolkit/webhook/client.py +54 -54
  26. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/src/py_wecom_toolkit/webhook/exceptions.py +8 -8
  27. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/src/py_wecom_toolkit/webhook/models.py +2 -2
  28. py_wecom_toolkit-3.0.2/EXAMPLES.md +0 -41
  29. py_wecom_toolkit-3.0.2/src/py_wecom_toolkit/__init__.py +0 -67
  30. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/.gitignore +0 -0
  31. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/LICENSE +0 -0
  32. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/examples/server/.env.example +0 -0
  33. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/examples/server/basic_async.py +0 -0
  34. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/examples/server/basic_sync.py +0 -0
  35. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/examples/server/config.py +0 -0
  36. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/examples/server/send_message.py +0 -0
  37. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/examples/server/upload_file.py +0 -0
  38. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/examples/webhook/.env.example +0 -0
  39. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/examples/webhook/config.py +0 -0
  40. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/examples/webhook/test_image.png +0 -0
  41. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/src/py_wecom_toolkit/server/cache.py +0 -0
  42. {py_wecom_toolkit-3.0.2 → py_wecom_toolkit-3.0.4}/src/py_wecom_toolkit/webhook/types.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: py-wecom-toolkit
3
- Version: 3.0.2
3
+ Version: 3.0.4
4
4
  Summary: 企业微信 Python 扩展包 - 支持 Webhook 与 Server 端 API
5
5
  Project-URL: Homepage, https://gitee.com/guolei19850528/py_wecom_toolkit
6
6
  Project-URL: Documentation, https://gitee.com/guolei19850528/py_wecom_toolkit#readme
@@ -52,9 +52,8 @@ Description-Content-Type: text/markdown
52
52
 
53
53
  # py-wecom-toolkit
54
54
 
55
- 企业微信 Python 扩展包 - 支持 Webhook 与 Server 端 API.
55
+ 企业微信 Python 扩展包 - 支持 Webhook 与 Server 端 API。
56
56
 
57
- - **版本**: 3.0.2
58
57
  - **作者**: 郭磊
59
58
  - **邮箱**: 174000902@qq.com
60
59
  - **项目主页**: https://gitee.com/guolei19850528/py_wecom_toolkit
@@ -72,19 +71,19 @@ py-wecom-toolkit/
72
71
  │ ├── __init__.py # 包入口,导出所有公共类
73
72
  │ ├── webhook/ # Webhook 消息发送模块(原“群机器人”)
74
73
  │ │ ├── __init__.py
75
- │ │ ├── client.py # WebhookClient / AsyncWebhookClient
74
+ │ │ ├── client.py # WeComWebhookClient / AsyncWeComWebhookClient
76
75
  │ │ ├── exceptions.py # Webhook 异常定义
77
76
  │ │ ├── models.py # pydantic v2 消息模型(8种消息类型)
78
77
  │ │ └── types.py # 类型别名
79
78
  │ └── server/ # Server 端 API 模块(企业微信服务端接口)
80
79
  │ ├── __init__.py
81
80
  │ ├── client.py # WeComServerClient / AsyncWeComServerClient
82
- │ ├── token.py # AccessToken 管理(支持缓存)
83
- │ ├── cache.py # MemoryCache / DiskCache / RedisCache
81
+ │ ├── token.py # WeComServerAccessTokenManager
82
+ │ ├── cache.py # WeComServerMemoryCache / WeComServerDiskCache / WeComServerRedisCache
84
83
  │ ├── models.py # Server API 响应模型
85
84
  │ ├── exceptions.py # Server 异常定义
86
- │ ├── message.py # 应用消息发送(MessageSendMixin)
87
- │ └── media.py # 素材上传/下载(MediaMixin)
85
+ │ ├── message.py # 应用消息发送(WeComServerMessageSendMixin)
86
+ │ └── media.py # 素材上传/下载(WeComServerMediaMixin)
88
87
  ├── tests/ # 测试用例
89
88
  │ ├── __init__.py
90
89
  │ ├── webhook/ # Webhook 测试
@@ -100,13 +99,13 @@ py-wecom-toolkit/
100
99
  │ ├── test_token.py # AccessToken 管理器测试
101
100
  │ ├── test_cache.py # 缓存测试
102
101
  │ ├── test_integration_real.py # 真实集成测试
103
- │ └── test_upload_readme_to_guolei.py # 上传README并发送测试
102
+ │ └── test_upload_readme_to_guolei.py # 上传 README 并发送测试
104
103
  └── examples/ # 演示代码
105
104
  ├── webhook/ # Webhook 示例
106
105
  │ ├── README.md
107
106
  │ ├── .env.example # 环境变量模板
108
107
  │ ├── config.py # 配置读取
109
- │ ├── basic.py # 基础用法(上下文管理器、手动连接、自定义配置、原始dict)
108
+ │ ├── basic.py # 基础用法(上下文管理器、手动连接、自定义配置、原始 dict)
110
109
  │ ├── text.py # 文本消息示例
111
110
  │ ├── markdown.py # Markdown 消息示例
112
111
  │ ├── markdown_v2.py # Markdown V2 消息示例
@@ -130,17 +129,19 @@ py-wecom-toolkit/
130
129
 
131
130
  ## 安装
132
131
 
132
+ ### 基础安装
133
+
133
134
  ```bash
134
135
  pip install py-wecom-toolkit
135
136
  ```
136
137
 
137
- 安装包含 Server 端支持 (diskcache / redis 缓存):
138
+ ### 包含 Server 端支持(diskcache / redis 缓存)
138
139
 
139
140
  ```bash
140
141
  pip install py-wecom-toolkit[server]
141
142
  ```
142
143
 
143
- 开发环境(包含测试、代码检查、打包、上传工具):
144
+ ### 开发环境(包含测试、代码检查、打包、上传工具)
144
145
 
145
146
  ```bash
146
147
  pip install py-wecom-toolkit[dev]
@@ -148,76 +149,34 @@ pip install py-wecom-toolkit[dev]
148
149
 
149
150
  ## 功能特性
150
151
 
151
- ### Webhook 模块
152
+ ### Webhook 消息发送模块
153
+
152
154
  - 支持全部 8 种 Webhook 消息类型: text, markdown, markdown_v2, image, news, file, voice, template_card
153
155
  - 基于 pydantic v2 的消息模型验证
154
156
  - 基于 httpx 的异步 HTTP 客户端
155
- - 同步与异步客户端双模式支持 (`WebhookClient` / `AsyncWebhookClient`)
157
+ - 同步与异步客户端双模式支持 (`WeComWebhookClient` / `AsyncWeComWebhookClient`)
156
158
  - 消息发送支持 @ 成员列表合并与去重
157
159
  - 文件上传接口 (base64 编码 + md5 校验)
158
160
 
159
- ### Server 端模块
160
- - **access_token 管理**: 自动获取与缓存 (支持 memory/diskcache/redis 三种后端)
161
+ ### Server 端 API 模块
162
+
163
+ - **access_token 管理**: 自动获取与缓存(支持 memory/diskcache/redis 三种后端)
161
164
  - **接口 IP 段**: 获取企业微信服务器 IP 段
162
165
  - **回调 IP 段**: 获取企业微信回调服务器 IP 段
163
- - **应用消息发送**: 文本、Markdown、图片、文件、语音、图文、模版卡片等
166
+ - **应用消息发送**: 文本、markdown、图片、文件、语音、图文、模版卡片等
164
167
  - **素材管理**: 上传临时素材、下载临时素材
165
168
  - 同步与异步客户端双模式支持 (`WeComServerClient` / `AsyncWeComServerClient`)
166
169
  - agentid 支持:可在客户端初始化时指定,发送消息时未传则自动使用客户端 agentid
167
170
 
168
171
  ## 快速开始
169
172
 
170
- ### Webhook 消息发送
173
+ ### Webhook 消息发送(同步)
171
174
 
172
175
  ```python
173
- import asyncio
174
- from py_wecom_toolkit import WebhookClient
175
-
176
- async def main():
177
- client = WebhookClient(key="你的webhook_key")
178
-
179
- # 发送文本消息
180
- await client.send_text("Hello, World!")
181
-
182
- # 发送文本消息并 @某人
183
- await client.send_text(
184
- "你好, 请查看",
185
- mentioned_list=["zhangsan"],
186
- mentioned_mobile_list=["13800001111"],
187
- )
188
-
189
- # 发送 Markdown 消息
190
- await client.send_markdown(
191
- '实时新增用户反馈<font color="warning">132例</font>'
192
- )
193
-
194
- # 发送图文消息
195
- await client.send_news([{
196
- "title": "中秋节礼品领取",
197
- "description": "今年中秋节公司有豪礼相送",
198
- "url": "https://work.weixin.qq.com",
199
- "picurl": "https://example.com/pic.png",
200
- }])
201
-
202
- # 发送图片
203
- await client.send_image("/path/to/image.png")
204
-
205
- # 上传文件并发送
206
- media_id = await client.upload_media("/path/to/file.pdf")
207
- await client.send_file(media_id)
208
-
209
- await client.close()
210
-
211
- asyncio.run(main())
212
- ```
213
-
214
- ### Webhook 消息发送 (同步)
215
-
216
- ```python
217
- from py_wecom_toolkit import WebhookClient
176
+ from py_wecom_toolkit import WeComWebhookClient
218
177
 
219
178
  # 创建同步客户端,支持上下文管理器自动管理连接池
220
- with WebhookClient(key="你的webhook_key") as client:
179
+ with WeComWebhookClient(key="你的webhook_key") as client:
221
180
 
222
181
  # 发送文本消息
223
182
  resp = client.send_text("Hello, World!")
@@ -256,7 +215,34 @@ with WebhookClient(key="你的webhook_key") as client:
256
215
  print(f"发送结果: {resp.errmsg}")
257
216
  ```
258
217
 
259
- ### Server 端 API (同步)
218
+ ### Webhook 消息发送(异步)
219
+
220
+ ```python
221
+ import asyncio
222
+ from py_wecom_toolkit import AsyncWeComWebhookClient
223
+
224
+ async def main():
225
+ async with AsyncWeComWebhookClient(key="你的webhook_key") as client:
226
+
227
+ # 发送文本消息
228
+ resp = await client.send_text("Hello, World!")
229
+ print(f"发送结果: {resp.errmsg}")
230
+
231
+ # 发送 Markdown 消息
232
+ resp = await client.send_markdown(
233
+ '实时新增用户反馈<font color="warning">132例</font>'
234
+ )
235
+ print(f"发送结果: {resp.errmsg}")
236
+
237
+ # 上传文件并发送
238
+ media_id = await client.upload_media("/path/to/file.pdf")
239
+ resp = await client.send_file(media_id)
240
+ print(f"发送结果: {resp.errmsg}")
241
+
242
+ asyncio.run(main())
243
+ ```
244
+
245
+ ### Server 端 API(同步)
260
246
 
261
247
  ```python
262
248
  from py_wecom_toolkit import WeComServerClient
@@ -268,7 +254,7 @@ client = WeComServerClient(
268
254
  agentid="你的AgentID",
269
255
  )
270
256
 
271
- # 获取 access_token (自动缓存)
257
+ # 获取 access_token(自动缓存)
272
258
  token = client.get_access_token()
273
259
  print(f"access_token: {token}")
274
260
 
@@ -295,40 +281,38 @@ print(f"发送文件结果: {resp.errmsg}")
295
281
  client.close()
296
282
  ```
297
283
 
298
- ### Server 端 API (异步)
284
+ ### Server 端 API(异步)
299
285
 
300
286
  ```python
301
287
  import asyncio
302
288
  from py_wecom_toolkit import AsyncWeComServerClient
303
289
 
304
290
  async def main():
305
- client = AsyncWeComServerClient(
291
+ async with AsyncWeComServerClient(
306
292
  corpid="你的CorpID",
307
293
  corpsecret="你的CorpSecret",
308
294
  agentid="你的AgentID",
309
- )
310
-
311
- # 获取 access_token (自动缓存)
312
- token = await client.async_get_access_token()
313
- print(f"access_token: {token}")
295
+ ) as client:
314
296
 
315
- # 获取接口 IP 段
316
- iplist = await client.async_get_iplist()
317
- print(f"接口 IP 段: {iplist.ip_list}")
297
+ # 获取 access_token(自动缓存)
298
+ token = await client.async_get_access_token()
299
+ print(f"access_token: {token}")
318
300
 
319
- # 发送文本应用消息
320
- resp = await client.async_send_text(
321
- content="Hello, WeCom Async!",
322
- touser="Guolei",
323
- )
324
- print(f"发送结果: {resp.errmsg}")
301
+ # 获取接口 IP 段
302
+ iplist = await client.async_get_iplist()
303
+ print(f"接口 IP 段: {iplist.ip_list}")
325
304
 
326
- # 上传素材并发送文件消息
327
- media = await client.async_upload_media("/path/to/README.md", media_type="file")
328
- resp = await client.async_send_file(media.media_id, touser="Guolei")
329
- print(f"发送文件结果: {resp.errmsg}")
305
+ # 发送文本应用消息
306
+ resp = await client.async_send_text(
307
+ content="Hello, WeCom Async!",
308
+ touser="Guolei",
309
+ )
310
+ print(f"发送结果: {resp.errmsg}")
330
311
 
331
- await client.close()
312
+ # 上传素材并发送文件消息
313
+ media = await client.async_upload_media("/path/to/README.md", media_type="file")
314
+ resp = await client.async_send_file(media.media_id, touser="Guolei")
315
+ print(f"发送文件结果: {resp.errmsg}")
332
316
 
333
317
  asyncio.run(main())
334
318
  ```
@@ -337,39 +321,38 @@ asyncio.run(main())
337
321
 
338
322
  ```python
339
323
  import httpx
340
- from py_wecom_toolkit import WebhookClient
324
+ from py_wecom_toolkit import WeComWebhookClient, AsyncWeComWebhookClient
325
+ from py_wecom_toolkit.server.cache import WeComServerDiskCache, WeComServerRedisCache
341
326
 
342
- # 使用自定义 httpx 客户端
327
+ # 使用自定义 httpx 客户端(Webhook 异步客户端)
343
328
  async with httpx.AsyncClient(timeout=30.0) as http_client:
344
- client = WebhookClient(key="xxx", client=http_client)
329
+ client = AsyncWeComWebhookClient(key="xxx", client=http_client)
345
330
  await client.send_text("使用自定义客户端")
346
331
 
347
- # 使用上下文管理器
348
- async with WebhookClient(key="xxx") as client:
349
- await client.send_text("自动管理连接池")
332
+ # 使用上下文管理器(Webhook 同步客户端)
333
+ with WeComWebhookClient(key="xxx") as client:
334
+ client.send_text("自动管理连接池")
350
335
 
351
336
  # 使用 diskcache 缓存 access_token
352
- from py_wecom_toolkit.server.cache import DiskCache
353
337
  client = WeComServerClient(
354
338
  corpid="xxx",
355
339
  corpsecret="yyy",
356
340
  agentid="zzz",
357
- cache=DiskCache(directory=".wecom_cache"),
341
+ cache=WeComServerDiskCache(directory=".wecom_cache"),
358
342
  )
359
343
 
360
344
  # 使用 redis 缓存 access_token
361
- from py_wecom_toolkit.server.cache import RedisCache
362
345
  client = WeComServerClient(
363
346
  corpid="xxx",
364
347
  corpsecret="yyy",
365
348
  agentid="zzz",
366
- cache=RedisCache(host="localhost", port=6379),
349
+ cache=WeComServerRedisCache(host="localhost", port=6379),
367
350
  )
368
351
  ```
369
352
 
370
353
  ## API 参考
371
354
 
372
- ### WebhookClient / AsyncWebhookClient
355
+ ### WeComWebhookClient / AsyncWeComWebhookClient
373
356
 
374
357
  | 方法 | 说明 |
375
358
  |------|------|
@@ -388,7 +371,7 @@ client = WeComServerClient(
388
371
 
389
372
  | 方法 | 说明 |
390
373
  |------|------|
391
- | `get_access_token()` / `async_get_access_token()` | 获取 access_token (自动缓存) |
374
+ | `get_access_token()` / `async_get_access_token()` | 获取 access_token(自动缓存) |
392
375
  | `get_iplist()` / `async_get_iplist()` | 获取企业微信接口 IP 段 |
393
376
  | `get_callback_ip()` / `async_get_callback_ip()` | 获取企业微信回调 IP 段 |
394
377
  | `send_text(content, touser, ...)` / `async_send_text(...)` | 发送文本应用消息 |
@@ -417,6 +400,7 @@ pytest tests/server/test_integration_real.py
417
400
  ```
418
401
 
419
402
  测试覆盖:
403
+
420
404
  - `tests/webhook/test_client.py` - 同步 Webhook 客户端测试
421
405
  - `tests/webhook/test_async_client.py` - 异步 Webhook 客户端测试
422
406
  - `tests/webhook/test_models.py` - Webhook 消息模型测试
@@ -424,7 +408,7 @@ pytest tests/server/test_integration_real.py
424
408
  - `tests/server/test_client.py` - 同步 Server 客户端测试
425
409
  - `tests/server/test_async_client.py` - 异步 Server 客户端测试
426
410
  - `tests/server/test_token.py` - AccessToken 管理器测试
427
- - `tests/server/test_cache.py` - 缓存测试 (memory/diskcache/redis)
411
+ - `tests/server/test_cache.py` - 缓存测试(memory/diskcache/redis)
428
412
  - `tests/server/test_integration_real.py` - Server 端真实集成测试
429
413
  - `tests/server/test_upload_readme_to_guolei.py` - 上传 README 并发送测试
430
414
 
@@ -434,7 +418,7 @@ pytest tests/server/test_integration_real.py
434
418
 
435
419
  | 文件 | 说明 |
436
420
  |------|------|
437
- | `basic.py` | 基础用法(上下文管理器、手动连接、自定义配置、原始dict) |
421
+ | `basic.py` | 基础用法(上下文管理器、手动连接、自定义配置、原始 dict) |
438
422
  | `text.py` | 文本消息示例 |
439
423
  | `markdown.py` | Markdown 消息示例 |
440
424
  | `markdown_v2.py` | Markdown V2 消息示例 |
@@ -483,10 +467,10 @@ python -m twine upload dist/*
483
467
 
484
468
  ## 消息类型参考
485
469
 
486
- 参考 [企业微信文档](https://developer.work.weixin.qq.com/document/path/91770) 获取 Webhook 消息格式说明.
470
+ 参考 [企业微信文档](https://developer.work.weixin.qq.com/document/path/91770) 获取 Webhook 消息格式说明。
487
471
 
488
- 参考 [企业微信服务端API](https://developer.work.weixin.qq.com/document/path/90664) 获取 Server 端 API 说明.
472
+ 参考 [企业微信服务端 API](https://developer.work.weixin.qq.com/document/path/90664) 获取 Server 端 API 说明。
489
473
 
490
474
  ## License
491
475
 
492
- MIT
476
+ MIT