pywecom-webhook 1.0.0__py3-none-any.whl
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.
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pywecom-webhook
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: 一个简化企业微信群机器人 webhook 调用的 Python 工具包,提供同步和异步发送消息的功能。
|
|
5
|
+
Home-page: https://gitee.com/guolei19850528/pywecom-webhook
|
|
6
|
+
Author: guolei
|
|
7
|
+
Author-email: 174000902@qq.com
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.6
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: httpx
|
|
15
|
+
Requires-Dist: jsonschema
|
|
16
|
+
Requires-Dist: decorator
|
|
17
|
+
Requires-Dist: jsonpath-ng
|
|
18
|
+
Dynamic: author
|
|
19
|
+
Dynamic: author-email
|
|
20
|
+
Dynamic: classifier
|
|
21
|
+
Dynamic: description
|
|
22
|
+
Dynamic: description-content-type
|
|
23
|
+
Dynamic: home-page
|
|
24
|
+
Dynamic: license-file
|
|
25
|
+
Dynamic: requires-dist
|
|
26
|
+
Dynamic: requires-python
|
|
27
|
+
Dynamic: summary
|
|
28
|
+
|
|
29
|
+
# pywecom-webhook
|
|
30
|
+
|
|
31
|
+
企业微信机器人 Webhook API 的 Python SDK,支持发送文本、Markdown、图片、图文、文件、语音等消息类型,同时提供同步和异步两种调用方式。
|
|
32
|
+
|
|
33
|
+
[](https://opensource.org/licenses/MIT)
|
|
34
|
+
[](https://www.python.org/)
|
|
35
|
+
[](https://github.com/psf/black)
|
|
36
|
+
|
|
37
|
+
## 功能特性
|
|
38
|
+
|
|
39
|
+
- ✅ 支持多种消息类型:文本、Markdown、MarkdownV2、图片、图文、文件、语音
|
|
40
|
+
- ✅ 基于 Pydantic 2.0 实现数据模型验证
|
|
41
|
+
- ✅ 支持 @ 指定用户或 @all
|
|
42
|
+
- ✅ 支持文件上传(upload_media)
|
|
43
|
+
- ✅ 支持模板卡片消息
|
|
44
|
+
- ✅ 同步发送器(Sender)
|
|
45
|
+
- ✅ 异步发送器(AsyncSender),支持 async with 语法
|
|
46
|
+
- ✅ 基于 httpx 实现,支持同步/异步请求
|
|
47
|
+
|
|
48
|
+
## 安装
|
|
49
|
+
|
|
50
|
+
使用 pip 安装:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
pip install pywecom-webhook
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
或从源码安装:
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
git clone https://gitee.com/guolei19850528/pywecom_webhook.git
|
|
60
|
+
cd pywecom-webhook
|
|
61
|
+
pip install .
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## 快速开始
|
|
65
|
+
|
|
66
|
+
### 同步发送
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
from pywecom_webhook import Sender, TextContent
|
|
70
|
+
|
|
71
|
+
# 创建 Sender 实例
|
|
72
|
+
sender = Sender(key="your_webhook_key")
|
|
73
|
+
|
|
74
|
+
# 发送文本消息
|
|
75
|
+
result = sender.send_text(TextContent(content="Hello, World!"))
|
|
76
|
+
print(result)
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### 异步发送
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
import asyncio
|
|
83
|
+
from pywecom_webhook import AsyncSender, TextContent
|
|
84
|
+
|
|
85
|
+
async def main():
|
|
86
|
+
# 使用 async with 语法
|
|
87
|
+
async with AsyncSender(key="your_webhook_key") as sender:
|
|
88
|
+
result = await sender.send_text(TextContent(content="Hello, Async World!"))
|
|
89
|
+
print(result)
|
|
90
|
+
|
|
91
|
+
asyncio.run(main())
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### 发送带 @ 的消息
|
|
95
|
+
|
|
96
|
+
```python
|
|
97
|
+
from pywecom_webhook import Sender, TextContent
|
|
98
|
+
|
|
99
|
+
sender = Sender(
|
|
100
|
+
key="your_webhook_key",
|
|
101
|
+
mentioned_list=["user1", "user2"], # 默认 @ 用户列表
|
|
102
|
+
mentioned_mobile_list=["13800000000"] # 默认 @ 手机号列表
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
# 发送带 @ 的文本消息
|
|
106
|
+
result = sender.send_text(
|
|
107
|
+
TextContent(
|
|
108
|
+
content="重要通知:系统即将维护",
|
|
109
|
+
mentioned_list=["@all"], # @ 所有人
|
|
110
|
+
mentioned_mobile_list=["13900000000"]
|
|
111
|
+
)
|
|
112
|
+
)
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## 消息类型
|
|
116
|
+
|
|
117
|
+
### 1. 文本消息
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
from pywecom_webhook import TextContent
|
|
121
|
+
|
|
122
|
+
content = TextContent(
|
|
123
|
+
content="这是一条文本消息",
|
|
124
|
+
mentioned_list=["user_id"],
|
|
125
|
+
mentioned_mobile_list=["13800000000"]
|
|
126
|
+
)
|
|
127
|
+
sender.send_text(content)
|
|
128
|
+
|
|
129
|
+
# 异步方式
|
|
130
|
+
await async_sender.send_text(content)
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### 2. Markdown 消息
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
from pywecom_webhook import MarkdownContent
|
|
137
|
+
|
|
138
|
+
content = MarkdownContent(
|
|
139
|
+
content="""## 标题
|
|
140
|
+
|
|
141
|
+
**加粗文本**
|
|
142
|
+
|
|
143
|
+
*斜体文本*
|
|
144
|
+
|
|
145
|
+
> 引用文本
|
|
146
|
+
|
|
147
|
+
[链接](https://example.com)
|
|
148
|
+
"""
|
|
149
|
+
)
|
|
150
|
+
sender.send_markdown(content)
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### 3. MarkdownV2 消息
|
|
154
|
+
|
|
155
|
+
```python
|
|
156
|
+
from pywecom_webhook import MarkdownV2Content
|
|
157
|
+
|
|
158
|
+
content = MarkdownV2Content(
|
|
159
|
+
content="<font color=\"red\">红色文本</font>\n\n<b>加粗文本</b>"
|
|
160
|
+
)
|
|
161
|
+
sender.send_markdown_v2(content)
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### 4. 图片消息
|
|
165
|
+
|
|
166
|
+
```python
|
|
167
|
+
import base64
|
|
168
|
+
import hashlib
|
|
169
|
+
from pywecom_webhook import ImageContent
|
|
170
|
+
|
|
171
|
+
with open("image.png", "rb") as f:
|
|
172
|
+
image_data = f.read()
|
|
173
|
+
|
|
174
|
+
content = ImageContent(
|
|
175
|
+
base64=base64.b64encode(image_data).decode("utf-8"),
|
|
176
|
+
md5=hashlib.md5(image_data).hexdigest()
|
|
177
|
+
)
|
|
178
|
+
sender.send_image(content)
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### 5. 图文消息
|
|
182
|
+
|
|
183
|
+
```python
|
|
184
|
+
from pywecom_webhook import NewsArticleContent, NewsArticlesContent
|
|
185
|
+
|
|
186
|
+
article = NewsArticlesContent(
|
|
187
|
+
articles=[
|
|
188
|
+
NewsArticleContent(
|
|
189
|
+
title="文章标题",
|
|
190
|
+
description="文章描述",
|
|
191
|
+
url="https://example.com",
|
|
192
|
+
pic_url="https://example.com/image.jpg"
|
|
193
|
+
)
|
|
194
|
+
]
|
|
195
|
+
)
|
|
196
|
+
sender.send_news([article])
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
### 6. 文件消息
|
|
200
|
+
|
|
201
|
+
```python
|
|
202
|
+
from pywecom_webhook import FileContent
|
|
203
|
+
|
|
204
|
+
# 先上传文件获取 media_id
|
|
205
|
+
media_id = sender.upload_media(
|
|
206
|
+
file_type="file",
|
|
207
|
+
files={"file": ("test.txt", open("test.txt", "rb"), "text/plain")}
|
|
208
|
+
)
|
|
209
|
+
|
|
210
|
+
# 发送文件消息
|
|
211
|
+
content = FileContent(media_id=media_id)
|
|
212
|
+
sender.send_file(content)
|
|
213
|
+
|
|
214
|
+
# 异步方式
|
|
215
|
+
media_id = await async_sender.upload_media(
|
|
216
|
+
file_type="file",
|
|
217
|
+
files={"file": ("test.txt", open("test.txt", "rb"), "text/plain")}
|
|
218
|
+
)
|
|
219
|
+
await async_sender.send_file(FileContent(media_id=media_id))
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### 7. 语音消息
|
|
223
|
+
|
|
224
|
+
```python
|
|
225
|
+
from pywecom_webhook import VoiceContent
|
|
226
|
+
|
|
227
|
+
# 先上传语音文件获取 media_id
|
|
228
|
+
media_id = sender.upload_media(
|
|
229
|
+
file_type="voice",
|
|
230
|
+
files={"file": ("voice.amr", open("voice.amr", "rb"), "audio/amr")}
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
# 发送语音消息
|
|
234
|
+
content = VoiceContent(media_id=media_id)
|
|
235
|
+
sender.send_voice(content)
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
### 8. 模板卡片消息
|
|
239
|
+
|
|
240
|
+
```python
|
|
241
|
+
template_card = {
|
|
242
|
+
"msgtype": "template_card",
|
|
243
|
+
"template_card": {
|
|
244
|
+
"card_type": "text_notice",
|
|
245
|
+
"source": {
|
|
246
|
+
"icon_url": "https://example.com/icon.png",
|
|
247
|
+
"desc": "通知来源"
|
|
248
|
+
},
|
|
249
|
+
"main_title": {
|
|
250
|
+
"title": "标题",
|
|
251
|
+
"desc": "描述"
|
|
252
|
+
},
|
|
253
|
+
"card_action": {
|
|
254
|
+
"type": 1,
|
|
255
|
+
"url": "https://example.com"
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
sender.send_template_card(template_card)
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
## API 文档
|
|
263
|
+
|
|
264
|
+
### Sender 类(同步)
|
|
265
|
+
|
|
266
|
+
```python
|
|
267
|
+
class Sender(
|
|
268
|
+
base_url: str = "https://qyapi.weixin.qq.com/cgi-bin/webhook",
|
|
269
|
+
key: str = None,
|
|
270
|
+
mentioned_list: list = None,
|
|
271
|
+
mentioned_mobile_list: list = None,
|
|
272
|
+
client_kwargs: dict = None
|
|
273
|
+
)
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
### AsyncSender 类(异步)
|
|
277
|
+
|
|
278
|
+
```python
|
|
279
|
+
class AsyncSender(
|
|
280
|
+
base_url: str = "https://qyapi.weixin.qq.com/cgi-bin/webhook",
|
|
281
|
+
key: str = None,
|
|
282
|
+
mentioned_list: list = None,
|
|
283
|
+
mentioned_mobile_list: list = None,
|
|
284
|
+
client_kwargs: dict = None
|
|
285
|
+
)
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
**参数说明**:
|
|
289
|
+
- `base_url`: Webhook 接口地址,默认为官方地址
|
|
290
|
+
- `key`: 群机器人的 Webhook key
|
|
291
|
+
- `mentioned_list`: 默认 @ 用户列表
|
|
292
|
+
- `mentioned_mobile_list`: 默认 @ 手机号列表
|
|
293
|
+
- `client_kwargs`: 传递给 httpx.Client / httpx.AsyncClient 的额外参数
|
|
294
|
+
|
|
295
|
+
### 发送方法
|
|
296
|
+
|
|
297
|
+
| 同步方法 | 异步方法 | 说明 |
|
|
298
|
+
|---------|---------|------|
|
|
299
|
+
| `send_text(content)` | `async send_text(content)` | 发送文本消息 |
|
|
300
|
+
| `send_markdown(content)` | `async send_markdown(content)` | 发送 Markdown 消息 |
|
|
301
|
+
| `send_markdown_v2(content)` | `async send_markdown_v2(content)` | 发送 MarkdownV2 消息 |
|
|
302
|
+
| `send_image(content)` | `async send_image(content)` | 发送图片消息 |
|
|
303
|
+
| `send_news(content)` | `async send_news(content)` | 发送图文消息 |
|
|
304
|
+
| `send_file(content)` | `async send_file(content)` | 发送文件消息 |
|
|
305
|
+
| `send_voice(content)` | `async send_voice(content)` | 发送语音消息 |
|
|
306
|
+
| `send_template_card(content)` | `async send_template_card(content)` | 发送模板卡片消息 |
|
|
307
|
+
| `upload_media(file_type, **kwargs)` | `async upload_media(file_type, **kwargs)` | 上传媒体文件 |
|
|
308
|
+
| `send(**kwargs)` | `async send(**kwargs)` | 直接发送消息(底层方法) |
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
## 企业微信官方文档
|
|
312
|
+
|
|
313
|
+
参考企业微信官方文档:[群机器人配置说明](https://developer.work.weixin.qq.com/document/path/91770)
|
|
314
|
+
|
|
315
|
+
## 许可证
|
|
316
|
+
|
|
317
|
+
MIT License
|
|
318
|
+
|
|
319
|
+
## 作者
|
|
320
|
+
|
|
321
|
+
郭磊 <174000902@qq.com>
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
pywecom_webhook-1.0.0.dist-info/licenses/LICENSE,sha256=HazFJNaQP3rbY1dvg3hAmO-FZ6v7DwM0YHwXvWsGG3A,1063
|
|
2
|
+
pywecom_webhook-1.0.0.dist-info/METADATA,sha256=wlJUV2e_LO-rgEoa-h4YVlC71Cc0_EetERVtZMGc_8k,7982
|
|
3
|
+
pywecom_webhook-1.0.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
4
|
+
pywecom_webhook-1.0.0.dist-info/top_level.txt,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
|
|
5
|
+
pywecom_webhook-1.0.0.dist-info/RECORD,,
|
|
@@ -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.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|