qrpa 1.0.8__py3-none-any.whl → 1.0.10__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.

Potentially problematic release.


This version of qrpa might be problematic. Click here for more details.

qrpa/wxwork.py CHANGED
@@ -1,274 +1,272 @@
1
- # -*- coding: utf-8 -*-
2
- """
3
- -------------------------------------------------
4
- @version : v1.0
5
- @author : qsir
6
- @contact : qsir@vxnote.com
7
- @software : PyCharm
8
- @filename : wxwork.py
9
- @create time: 2025/08/03
10
- @modify time: 2025/08/03
11
- @describe :
12
- -------------------------------------------------
13
- """
14
- import json
15
- import os
16
- import hashlib
17
- import base64
18
- import requests
19
- from requests_toolbelt import MultipartEncoder
20
-
21
-
22
- # 通过企微群机器人发送消息
23
- class WxWorkBot:
24
- def __init__(self, key):
25
- self.key = key
26
-
27
- def upload_media(self, filepath):
28
- """
29
- 上传临时素材,给企微群里发文件消息时需要先将文件上传至企微临时素材中
30
- :param filepath:
31
- :return: 临时素材的media_id
32
- """
33
- try:
34
- headers = {
35
- 'Content-Type': 'multipart/form-data',
36
- }
37
- with open(filepath, 'rb') as f:
38
- files = {
39
- 'media': (os.path.basename(filepath), f.read())
40
- }
41
- response = requests.post(
42
- f'https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key={self.key}&type=file',
43
- headers=headers, files=files)
44
- if response.status_code == 200:
45
- result = json.loads(response.text)
46
- return result['media_id']
47
- else:
48
- print("HTTP Error:", response.status_code)
49
- return None
50
- except Exception as err:
51
- raise Exception("upload_media error", err)
52
-
53
- def send_file(self, file_path):
54
- """
55
- 发送文件到群里
56
- :param file_path:
57
- :return:
58
- """
59
- media_id = self.upload_media(file_path)
60
- data = {
61
- "msgtype": "file",
62
- "file": {
63
- "media_id": media_id
64
- }
65
- }
66
- return self.send_msg(data)
67
-
68
- def send_text(self, content, mentioned_list=None, mentioned_mobile_list=None):
69
- """
70
- 发送文本消息
71
- :param content:
72
- :param mentioned_list: 需要@的人userid
73
- :param mentioned_mobile_list: 需要@的人手机号
74
- :return:
75
- """
76
- data = {
77
- "msgtype": "text",
78
- "text": {
79
- "content": content
80
- }
81
- }
82
- if mentioned_list is not None and mentioned_list:
83
- data['text'].update({"mentioned_list": mentioned_list})
84
- if mentioned_mobile_list is not None and mentioned_mobile_list:
85
- data['text'].update({"mentioned_mobile_list": mentioned_mobile_list})
86
-
87
- self.send_msg(data)
88
-
89
- def send_markdown(self, content):
90
- """
91
- 发送Markdown消息
92
- :param content:
93
- :return:
94
- """
95
- data = {
96
- "msgtype": "markdown",
97
- "markdown": {
98
- "content": content
99
- }
100
- }
101
- self.send_msg(data)
102
-
103
- def send_img(self, img_path):
104
- """
105
- 发送图片消息
106
- 图片(base64编码前)最大不能超过2M,支持JPG,PNG格式
107
- :param img_path:
108
- :return:
109
- """
110
- data = {
111
- "msgtype": "image",
112
- "image": {
113
- "base64": self.img_to_base64(img_path),
114
- "md5": self.img_to_md5(img_path)
115
- }
116
- }
117
- self.send_msg(data)
118
-
119
- def send_news(self, title, description, url, picurl):
120
- """
121
- 发送图文消息
122
- :param title: 标题
123
- :param description: 描述
124
- :param url: 跳转URL
125
- :param picurl: 图文图片地址
126
- :return:
127
- """
128
- data = {
129
- "msgtype": "news",
130
- "news": {
131
- "articles": [
132
- {
133
- "title": title,
134
- "description": description,
135
- "url": url,
136
- "picurl": picurl
137
- }
138
- ]
139
- }
140
- }
141
- self.send_msg(data)
142
-
143
- def send_msg(self, data):
144
- """
145
- 发送机器人通用消息到企微群
146
- :param data: 消息内容json数据
147
- :return:
148
- """
149
- try:
150
- header = {
151
- "Content-Type": "application/json"
152
- }
153
- response = requests.post(f"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={self.key}", headers=header, data=json.dumps(data))
154
- if response.status_code == 200:
155
- result = json.loads(response.text)
156
- return result
157
- else:
158
- print("HTTP Error:", response.status_code)
159
- return None
160
- except Exception as err:
161
- raise Exception("Send Chat Message error", err)
162
-
163
- def img_to_md5(self, img_path):
164
- # 读取图片文件并计算MD5值
165
- with open(img_path, 'rb') as image_file:
166
- image_data = image_file.read()
167
- return hashlib.md5(image_data).hexdigest()
168
-
169
- def img_to_base64(self, img_path):
170
- # 读取图片文件并转换为Base64编码
171
- with open(img_path, 'rb') as image_file:
172
- image_data = image_file.read()
173
- return base64.b64encode(image_data).decode('utf-8')
174
-
175
-
176
- # 通过企微应用发送消息
177
- class WxWorkAppBot:
178
- def __init__(self, corpid, corpsecret, agentid):
179
- self.corpid = corpid
180
- self.corpsecret = corpsecret
181
- self.agentid = agentid
182
- self.access_token = self._getToken()
183
-
184
- def _getToken(self):
185
- try:
186
- if all([self.corpid, self.corpsecret]):
187
- url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecret}".format(
188
- corpid=self.corpid, corpsecret=self.corpsecret)
189
- response = requests.get(url)
190
- if response.status_code == 200:
191
- result = json.loads(response.text)
192
- return result['access_token']
193
- else:
194
- print("HTTP Error:", response.status_code)
195
- return None
196
- except Exception as err:
197
- raise Exception("get WeChat access Token error", err)
198
-
199
- def _send_msg(self, data):
200
- self._check_token()
201
- try:
202
- send_url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}".format(
203
- access_token=self.access_token)
204
- response = requests.post(send_url, json.dumps(data))
205
- if response.status_code == 200:
206
- result = json.loads(response.text)
207
- return result
208
- else:
209
- print("HTTP Error:", response.status_code)
210
- return None
211
- except Exception as err:
212
- raise Exception("send WeChat Message error", err)
213
-
214
- def _check_token(self):
215
- if self.access_token is None:
216
- self._getToken()
217
-
218
- def send_msg(self, data):
219
- return self._send_msg(data)
220
-
221
- def upload_media(self, filetype, filepath, filename):
222
- """
223
- 上传临时素材到企微并获取media_id
224
- :param filetype: 图片(image)、语音(voice)、视频(video),普通文件(file)
225
- :param filepath:
226
- :param filename:
227
- :return: media_id
228
- """
229
- try:
230
- self._check_token()
231
- post_file_url = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={access_token}&type={filetype}".format(
232
- filetype=filetype,
233
- access_token=self.access_token)
234
-
235
- m = MultipartEncoder(
236
- fields={filename: (filename, open(filepath + filename, 'rb'), 'text/plain')},
237
- )
238
- response = requests.post(url=post_file_url, data=m, headers={'Content-Type': m.content_type})
239
- if response.status_code == 200:
240
- result = json.loads(response.text)
241
- return result['media_id']
242
- else:
243
- print("HTTP Error:", response.status_code)
244
- return None
245
- except Exception as err:
246
- raise Exception("upload media error", err)
247
-
248
- def get_media(self, media_id):
249
- """
250
- 获取临时素材
251
- :param media_id:
252
- :return: 返回二进制形式
253
- """
254
- try:
255
- self._check_token()
256
- url = "https://qyapi.weixin.qq.com/cgi-bin/media/get"
257
- params = {
258
- "access_token": self.access_token,
259
- "media_id": media_id
260
- }
261
- response = requests.get(url=url, params=params)
262
- if response.status_code == 200:
263
- content_type = response.headers.get('Content-Type')
264
- if content_type == 'application/json':
265
- response_data = json.loads(response.text)
266
- print("Error:", response_data.get("errmsg"))
267
- return None
268
- else:
269
- return response.content
270
- else:
271
- print("HTTP Error:", response.status_code)
272
- return None
273
- except Exception as err:
274
- raise Exception("get media error", err)
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ -------------------------------------------------
4
+ @version : v1.0
5
+ @author : qsir
6
+ @contact : qsir@vxnote.com
7
+ @software : PyCharm
8
+ @filename : wxwork.py
9
+ @create time: 2025/08/03
10
+ @modify time: 2025/08/03
11
+ @describe :
12
+ -------------------------------------------------
13
+ """
14
+ import json
15
+ import os
16
+ import hashlib
17
+ import base64
18
+ import requests
19
+ from requests_toolbelt import MultipartEncoder
20
+
21
+ # 通过企微群机器人发送消息
22
+ class WxWorkBot:
23
+ def __init__(self, key):
24
+ self.key = key
25
+
26
+ def upload_media(self, filepath):
27
+ """
28
+ 上传临时素材,给企微群里发文件消息时需要先将文件上传至企微临时素材中
29
+ :param filepath:
30
+ :return: 临时素材的media_id
31
+ """
32
+ try:
33
+ headers = {
34
+ 'Content-Type': 'multipart/form-data',
35
+ }
36
+ with open(filepath, 'rb') as f:
37
+ files = {
38
+ 'media': (os.path.basename(filepath), f.read())
39
+ }
40
+ response = requests.post(
41
+ f'https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key={self.key}&type=file',
42
+ headers=headers, files=files)
43
+ if response.status_code == 200:
44
+ result = json.loads(response.text)
45
+ return result['media_id']
46
+ else:
47
+ print("HTTP Error:", response.status_code)
48
+ return None
49
+ except Exception as err:
50
+ raise Exception("upload_media error", err)
51
+
52
+ def send_file(self, file_path):
53
+ """
54
+ 发送文件到群里
55
+ :param file_path:
56
+ :return:
57
+ """
58
+ media_id = self.upload_media(file_path)
59
+ data = {
60
+ "msgtype": "file",
61
+ "file" : {
62
+ "media_id": media_id
63
+ }
64
+ }
65
+ return self.send_msg(data)
66
+
67
+ def send_text(self, content, mentioned_list=None, mentioned_mobile_list=None):
68
+ """
69
+ 发送文本消息
70
+ :param content:
71
+ :param mentioned_list: 需要@的人userid
72
+ :param mentioned_mobile_list: 需要@的人手机号
73
+ :return:
74
+ """
75
+ data = {
76
+ "msgtype": "text",
77
+ "text" : {
78
+ "content": content
79
+ }
80
+ }
81
+ if mentioned_list is not None and mentioned_list:
82
+ data['text'].update({"mentioned_list": mentioned_list})
83
+ if mentioned_mobile_list is not None and mentioned_mobile_list:
84
+ data['text'].update({"mentioned_mobile_list": mentioned_mobile_list})
85
+
86
+ self.send_msg(data)
87
+
88
+ def send_markdown(self, content):
89
+ """
90
+ 发送Markdown消息
91
+ :param content:
92
+ :return:
93
+ """
94
+ data = {
95
+ "msgtype" : "markdown",
96
+ "markdown": {
97
+ "content": content
98
+ }
99
+ }
100
+ self.send_msg(data)
101
+
102
+ def send_img(self, img_path):
103
+ """
104
+ 发送图片消息
105
+ 图片(base64编码前)最大不能超过2M,支持JPG,PNG格式
106
+ :param img_path:
107
+ :return:
108
+ """
109
+ data = {
110
+ "msgtype": "image",
111
+ "image" : {
112
+ "base64": self.img_to_base64(img_path),
113
+ "md5" : self.img_to_md5(img_path)
114
+ }
115
+ }
116
+ self.send_msg(data)
117
+
118
+ def send_news(self, title, description, url, picurl):
119
+ """
120
+ 发送图文消息
121
+ :param title: 标题
122
+ :param description: 描述
123
+ :param url: 跳转URL
124
+ :param picurl: 图文图片地址
125
+ :return:
126
+ """
127
+ data = {
128
+ "msgtype": "news",
129
+ "news" : {
130
+ "articles": [
131
+ {
132
+ "title" : title,
133
+ "description": description,
134
+ "url" : url,
135
+ "picurl" : picurl
136
+ }
137
+ ]
138
+ }
139
+ }
140
+ self.send_msg(data)
141
+
142
+ def send_msg(self, data):
143
+ """
144
+ 发送机器人通用消息到企微群
145
+ :param data: 消息内容json数据
146
+ :return:
147
+ """
148
+ try:
149
+ header = {
150
+ "Content-Type": "application/json"
151
+ }
152
+ response = requests.post(f"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={self.key}", headers=header, data=json.dumps(data))
153
+ if response.status_code == 200:
154
+ result = json.loads(response.text)
155
+ return result
156
+ else:
157
+ print("HTTP Error:", response.status_code)
158
+ return None
159
+ except Exception as err:
160
+ raise Exception("Send Chat Message error", err)
161
+
162
+ def img_to_md5(self, img_path):
163
+ # 读取图片文件并计算MD5值
164
+ with open(img_path, 'rb') as image_file:
165
+ image_data = image_file.read()
166
+ return hashlib.md5(image_data).hexdigest()
167
+
168
+ def img_to_base64(self, img_path):
169
+ # 读取图片文件并转换为Base64编码
170
+ with open(img_path, 'rb') as image_file:
171
+ image_data = image_file.read()
172
+ return base64.b64encode(image_data).decode('utf-8')
173
+
174
+ # 通过企微应用发送消息
175
+ class WxWorkAppBot:
176
+ def __init__(self, corpid, corpsecret, agentid):
177
+ self.corpid = corpid
178
+ self.corpsecret = corpsecret
179
+ self.agentid = agentid
180
+ self.access_token = self._getToken()
181
+
182
+ def _getToken(self):
183
+ try:
184
+ if all([self.corpid, self.corpsecret]):
185
+ url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecret}".format(
186
+ corpid=self.corpid, corpsecret=self.corpsecret)
187
+ response = requests.get(url)
188
+ if response.status_code == 200:
189
+ result = json.loads(response.text)
190
+ return result['access_token']
191
+ else:
192
+ print("HTTP Error:", response.status_code)
193
+ return None
194
+ except Exception as err:
195
+ raise Exception("get WeChat access Token error", err)
196
+
197
+ def _send_msg(self, data):
198
+ self._check_token()
199
+ try:
200
+ send_url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}".format(
201
+ access_token=self.access_token)
202
+ response = requests.post(send_url, json.dumps(data))
203
+ if response.status_code == 200:
204
+ result = json.loads(response.text)
205
+ return result
206
+ else:
207
+ print("HTTP Error:", response.status_code)
208
+ return None
209
+ except Exception as err:
210
+ raise Exception("send WeChat Message error", err)
211
+
212
+ def _check_token(self):
213
+ if self.access_token is None:
214
+ self._getToken()
215
+
216
+ def send_msg(self, data):
217
+ return self._send_msg(data)
218
+
219
+ def upload_media(self, filetype, filepath, filename):
220
+ """
221
+ 上传临时素材到企微并获取media_id
222
+ :param filetype: 图片(image)、语音(voice)、视频(video),普通文件(file)
223
+ :param filepath:
224
+ :param filename:
225
+ :return: media_id
226
+ """
227
+ try:
228
+ self._check_token()
229
+ post_file_url = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={access_token}&type={filetype}".format(
230
+ filetype=filetype,
231
+ access_token=self.access_token)
232
+
233
+ m = MultipartEncoder(
234
+ fields={filename: (filename, open(filepath + filename, 'rb'), 'text/plain')},
235
+ )
236
+ response = requests.post(url=post_file_url, data=m, headers={'Content-Type': m.content_type})
237
+ if response.status_code == 200:
238
+ result = json.loads(response.text)
239
+ return result['media_id']
240
+ else:
241
+ print("HTTP Error:", response.status_code)
242
+ return None
243
+ except Exception as err:
244
+ raise Exception("upload media error", err)
245
+
246
+ def get_media(self, media_id):
247
+ """
248
+ 获取临时素材
249
+ :param media_id:
250
+ :return: 返回二进制形式
251
+ """
252
+ try:
253
+ self._check_token()
254
+ url = "https://qyapi.weixin.qq.com/cgi-bin/media/get"
255
+ params = {
256
+ "access_token": self.access_token,
257
+ "media_id" : media_id
258
+ }
259
+ response = requests.get(url=url, params=params)
260
+ if response.status_code == 200:
261
+ content_type = response.headers.get('Content-Type')
262
+ if content_type == 'application/json':
263
+ response_data = json.loads(response.text)
264
+ print("Error:", response_data.get("errmsg"))
265
+ return None
266
+ else:
267
+ return response.content
268
+ else:
269
+ print("HTTP Error:", response.status_code)
270
+ return None
271
+ except Exception as err:
272
+ raise Exception("get media error", err)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: qrpa
3
- Version: 1.0.8
3
+ Version: 1.0.10
4
4
  Summary: qsir's rpa library
5
5
  Author: QSir
6
6
  Author-email: QSir <1171725650@qq.com>
@@ -0,0 +1,16 @@
1
+ qrpa/RateLimitedSender.py,sha256=7-wDoVJbv-y3Z3dX85conBYNdUfpidZ7QgssdAKNlFU,1698
2
+ qrpa/__init__.py,sha256=HW_Aqi4nHtb-tnPlPRq4vx5M8yr-hKGxxItGRS1Zn5A,608
3
+ qrpa/db_migrator.py,sha256=2VmhzcMsU0MKpl-mNCwKyV8tLTqyEysSpP27-S_rQZ8,21862
4
+ qrpa/fun_base.py,sha256=j5EcC-1l6n9ZEb-xGj1sTGgQ-1zCaoSHl919iDku3Wo,3363
5
+ qrpa/fun_excel.py,sha256=aVSLcE3UJYnxvGi1XULM9qTbziUT7xlOcd2grnP6F3I,103615
6
+ qrpa/fun_file.py,sha256=BInN-Iuxi8sYiJ031gXI_DzO_n170RsOf7fnpkl_etM,7100
7
+ qrpa/fun_web.py,sha256=5QLQorAhRzMOGMRh4eCJ2UH8ZhVHvxkHwobWhmgU5qM,6286
8
+ qrpa/fun_win.py,sha256=mPvojBYYvAcoXIGh_IXaNcA6x1qZAPGTRzwRvDb-WSU,1223
9
+ qrpa/shein_ziniao.py,sha256=nSqqcEPh4nVQtUxUnIRzeZfTLyXywGPjPZn5pP-w57U,18309
10
+ qrpa/time_utils.py,sha256=ef0hhbN_6b-gcnz5ETIVOoxemIMvcxGVGGIRnHnGaBo,29564
11
+ qrpa/time_utils_example.py,sha256=shHOXKKF3QSzb0SHsNc34M61wEkkLuM30U9X1THKNS8,8053
12
+ qrpa/wxwork.py,sha256=IeJq-1LwKCsCt_AdMSLvYrQJWNoQp_wqVtk6iqwJqf4,9200
13
+ qrpa-1.0.10.dist-info/METADATA,sha256=wRmrV9leWSw8ap06Z7aIQ4ynlUte1xVm2m8B6XOhs_Q,231
14
+ qrpa-1.0.10.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
15
+ qrpa-1.0.10.dist-info/top_level.txt,sha256=F6T5igi0fhXDucPPUbmmSC0qFCDEsH5eVijfVF48OFU,5
16
+ qrpa-1.0.10.dist-info/RECORD,,
@@ -1,13 +0,0 @@
1
- qrpa/__init__.py,sha256=a-B3yFHm2MGn1R-CQQQheJXgM_C8eWqcfVqmNYk1ytE,249
2
- qrpa/db_migrator.py,sha256=2VmhzcMsU0MKpl-mNCwKyV8tLTqyEysSpP27-S_rQZ8,21862
3
- qrpa/fun_base.py,sha256=9bbtyfQsqyBXF-AU4wR5wPvr3qeqKU1ml5KS1ZODpLA,1229
4
- qrpa/fun_file.py,sha256=BInN-Iuxi8sYiJ031gXI_DzO_n170RsOf7fnpkl_etM,7100
5
- qrpa/fun_win.py,sha256=mPvojBYYvAcoXIGh_IXaNcA6x1qZAPGTRzwRvDb-WSU,1223
6
- qrpa/shein_ziniao.py,sha256=LjOCHUB5nPDnGta0Nq2sdy7_gtbwiFquLS2r7wh6J4c,17979
7
- qrpa/time_utils.py,sha256=NJHDoEiEv8oM7hIQAXZJ9YpbupS3WMSIwrzsytblMAg,29707
8
- qrpa/time_utils_example.py,sha256=shHOXKKF3QSzb0SHsNc34M61wEkkLuM30U9X1THKNS8,8053
9
- qrpa/wxwork.py,sha256=3Aycpjnr9UPwekMh_0ngCJCVqhemARK741HziLx3d44,9436
10
- qrpa-1.0.8.dist-info/METADATA,sha256=PGTrj1-GwY8NAojhL_CziNbQAKgMHxWWmeIp4aughPs,230
11
- qrpa-1.0.8.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
12
- qrpa-1.0.8.dist-info/top_level.txt,sha256=F6T5igi0fhXDucPPUbmmSC0qFCDEsH5eVijfVF48OFU,5
13
- qrpa-1.0.8.dist-info/RECORD,,
File without changes