qrpa 1.1.32__py3-none-any.whl → 1.1.34__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/RateLimitedSender.py +45 -45
- qrpa/__init__.py +31 -31
- qrpa/db_migrator.py +600 -600
- qrpa/feishu_bot_app.py +267 -267
- qrpa/fun_base.py +339 -339
- qrpa/fun_excel.py +3059 -3059
- qrpa/fun_file.py +318 -318
- qrpa/fun_web.py +258 -258
- qrpa/fun_win.py +198 -198
- qrpa/mysql_module/new_product_analysis_model.py +429 -0
- qrpa/mysql_module/shein_ledger_model.py +468 -468
- qrpa/mysql_module/shein_product_model.py +484 -484
- qrpa/mysql_module/shein_return_order_model.py +569 -569
- qrpa/shein_daily_report_model.py +375 -375
- qrpa/shein_excel.py +3125 -3125
- qrpa/shein_lib.py +3932 -3609
- qrpa/shein_mysql.py +22 -0
- qrpa/shein_sqlite.py +153 -153
- qrpa/shein_ziniao.py +529 -529
- qrpa/temu_chrome.py +56 -56
- qrpa/temu_excel.py +139 -139
- qrpa/temu_lib.py +154 -154
- qrpa/time_utils.py +882 -882
- qrpa/time_utils_example.py +243 -243
- qrpa/wxwork.py +318 -318
- {qrpa-1.1.32.dist-info → qrpa-1.1.34.dist-info}/METADATA +1 -1
- qrpa-1.1.34.dist-info/RECORD +32 -0
- qrpa-1.1.32.dist-info/RECORD +0 -31
- {qrpa-1.1.32.dist-info → qrpa-1.1.34.dist-info}/WHEEL +0 -0
- {qrpa-1.1.32.dist-info → qrpa-1.1.34.dist-info}/top_level.txt +0 -0
qrpa/feishu_bot_app.py
CHANGED
|
@@ -1,268 +1,268 @@
|
|
|
1
|
-
# pip install lark-oapi -U
|
|
2
|
-
import json
|
|
3
|
-
import os
|
|
4
|
-
import uuid
|
|
5
|
-
from typing import Optional
|
|
6
|
-
|
|
7
|
-
import lark_oapi as lark
|
|
8
|
-
from lark_oapi.api.im.v1 import *
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
class FeishuBot:
|
|
12
|
-
"""飞书机器人类,封装所有机器人相关功能"""
|
|
13
|
-
|
|
14
|
-
def __init__(self, config):
|
|
15
|
-
"""
|
|
16
|
-
初始化飞书机器人
|
|
17
|
-
|
|
18
|
-
Args:
|
|
19
|
-
config: 配置对象,包含应用ID、应用密钥和群组信息
|
|
20
|
-
"""
|
|
21
|
-
self.config = config
|
|
22
|
-
self._client = None
|
|
23
|
-
|
|
24
|
-
@property
|
|
25
|
-
def client(self):
|
|
26
|
-
"""获取飞书客户端,使用懒加载模式"""
|
|
27
|
-
if self._client is None:
|
|
28
|
-
self._client = lark.Client.builder() \
|
|
29
|
-
.app_id(self.config.feishu_bot.app_id) \
|
|
30
|
-
.app_secret(self.config.feishu_bot.app_secret) \
|
|
31
|
-
.log_level(lark.LogLevel.INFO) \
|
|
32
|
-
.build()
|
|
33
|
-
return self._client
|
|
34
|
-
|
|
35
|
-
def _get_chat_id(self, bot_name: str) -> Optional[str]:
|
|
36
|
-
"""
|
|
37
|
-
根据群组别名获取群组ID
|
|
38
|
-
|
|
39
|
-
Args:
|
|
40
|
-
bot_name: 群组别名
|
|
41
|
-
|
|
42
|
-
Returns:
|
|
43
|
-
群组ID,如果别名不存在则返回None
|
|
44
|
-
"""
|
|
45
|
-
return self.config.dict_feishu_group.get(bot_name)
|
|
46
|
-
|
|
47
|
-
def _handle_response_error(self, response, operation_name: str):
|
|
48
|
-
"""
|
|
49
|
-
处理API响应错误
|
|
50
|
-
|
|
51
|
-
Args:
|
|
52
|
-
response: API响应对象
|
|
53
|
-
operation_name: 操作名称,用于错误日志
|
|
54
|
-
"""
|
|
55
|
-
if not response.success():
|
|
56
|
-
lark.logger.error(
|
|
57
|
-
f"{operation_name} failed, code: {response.code}, "
|
|
58
|
-
f"msg: {response.msg}, log_id: {response.get_log_id()}, "
|
|
59
|
-
f"resp: \n{json.dumps(json.loads(response.raw.content), indent=4, ensure_ascii=False)}"
|
|
60
|
-
)
|
|
61
|
-
return True
|
|
62
|
-
return False
|
|
63
|
-
|
|
64
|
-
def send_text(self, content: str, bot_name: str = 'test') -> bool:
|
|
65
|
-
"""
|
|
66
|
-
发送文本消息
|
|
67
|
-
|
|
68
|
-
Args:
|
|
69
|
-
content: 文本内容
|
|
70
|
-
bot_name: 群组别名,默认为'test'
|
|
71
|
-
|
|
72
|
-
Returns:
|
|
73
|
-
发送是否成功
|
|
74
|
-
"""
|
|
75
|
-
chat_id = self._get_chat_id(bot_name)
|
|
76
|
-
if not chat_id:
|
|
77
|
-
lark.logger.error(f"未找到群组别名 '{bot_name}' 对应的群组ID")
|
|
78
|
-
return False
|
|
79
|
-
|
|
80
|
-
message_content = {"text": content}
|
|
81
|
-
|
|
82
|
-
# 构造请求对象
|
|
83
|
-
request: CreateMessageRequest = CreateMessageRequest.builder() \
|
|
84
|
-
.receive_id_type("chat_id") \
|
|
85
|
-
.request_body(CreateMessageRequestBody.builder()
|
|
86
|
-
.receive_id(chat_id)
|
|
87
|
-
.msg_type("text")
|
|
88
|
-
.content(json.dumps(message_content))
|
|
89
|
-
.uuid(str(uuid.uuid4()))
|
|
90
|
-
.build()) \
|
|
91
|
-
.build()
|
|
92
|
-
|
|
93
|
-
# 发起请求
|
|
94
|
-
response: CreateMessageResponse = self.client.im.v1.message.create(request)
|
|
95
|
-
|
|
96
|
-
# 处理失败返回
|
|
97
|
-
if self._handle_response_error(response, "send_text"):
|
|
98
|
-
return False
|
|
99
|
-
|
|
100
|
-
# 处理业务结果
|
|
101
|
-
lark.logger.info(lark.JSON.marshal(response.data, indent=4))
|
|
102
|
-
return True
|
|
103
|
-
|
|
104
|
-
def send_image(self, file_path: str, bot_name: str = 'test') -> bool:
|
|
105
|
-
"""
|
|
106
|
-
发送图片消息
|
|
107
|
-
|
|
108
|
-
Args:
|
|
109
|
-
file_path: 图片文件路径
|
|
110
|
-
bot_name: 群组别名,默认为'test'
|
|
111
|
-
|
|
112
|
-
Returns:
|
|
113
|
-
发送是否成功
|
|
114
|
-
"""
|
|
115
|
-
# 先上传图片获取image_key
|
|
116
|
-
image_key = self.upload_image(file_path)
|
|
117
|
-
if not image_key:
|
|
118
|
-
return False
|
|
119
|
-
|
|
120
|
-
chat_id = self._get_chat_id(bot_name)
|
|
121
|
-
if not chat_id:
|
|
122
|
-
lark.logger.error(f"未找到群组别名 '{bot_name}' 对应的群组ID")
|
|
123
|
-
return False
|
|
124
|
-
|
|
125
|
-
message_content = {"image_key": image_key}
|
|
126
|
-
|
|
127
|
-
# 构造请求对象
|
|
128
|
-
request: CreateMessageRequest = CreateMessageRequest.builder() \
|
|
129
|
-
.receive_id_type("chat_id") \
|
|
130
|
-
.request_body(CreateMessageRequestBody.builder()
|
|
131
|
-
.receive_id(chat_id)
|
|
132
|
-
.msg_type("image")
|
|
133
|
-
.content(json.dumps(message_content))
|
|
134
|
-
.uuid(str(uuid.uuid4()))
|
|
135
|
-
.build()) \
|
|
136
|
-
.build()
|
|
137
|
-
|
|
138
|
-
# 发起请求
|
|
139
|
-
response: CreateMessageResponse = self.client.im.v1.message.create(request)
|
|
140
|
-
|
|
141
|
-
# 处理失败返回
|
|
142
|
-
if self._handle_response_error(response, "send_image"):
|
|
143
|
-
return False
|
|
144
|
-
|
|
145
|
-
# 处理业务结果
|
|
146
|
-
lark.logger.info(lark.JSON.marshal(response.data, indent=4))
|
|
147
|
-
return True
|
|
148
|
-
|
|
149
|
-
def send_excel(self, file_path: str, bot_name: str = 'test') -> bool:
|
|
150
|
-
"""
|
|
151
|
-
发送Excel文件
|
|
152
|
-
|
|
153
|
-
Args:
|
|
154
|
-
file_path: Excel文件路径
|
|
155
|
-
bot_name: 群组别名,默认为'test'
|
|
156
|
-
|
|
157
|
-
Returns:
|
|
158
|
-
发送是否成功
|
|
159
|
-
"""
|
|
160
|
-
# 先上传文件获取file_key
|
|
161
|
-
file_key = self.upload_excel(file_path)
|
|
162
|
-
if not file_key:
|
|
163
|
-
return False
|
|
164
|
-
|
|
165
|
-
chat_id = self._get_chat_id(bot_name)
|
|
166
|
-
if not chat_id:
|
|
167
|
-
lark.logger.error(f"未找到群组别名 '{bot_name}' 对应的群组ID")
|
|
168
|
-
return False
|
|
169
|
-
|
|
170
|
-
message_content = {"file_key": file_key}
|
|
171
|
-
|
|
172
|
-
# 构造请求对象
|
|
173
|
-
request: CreateMessageRequest = CreateMessageRequest.builder() \
|
|
174
|
-
.receive_id_type("chat_id") \
|
|
175
|
-
.request_body(CreateMessageRequestBody.builder()
|
|
176
|
-
.receive_id(chat_id)
|
|
177
|
-
.msg_type("file")
|
|
178
|
-
.content(json.dumps(message_content))
|
|
179
|
-
.uuid(str(uuid.uuid4()))
|
|
180
|
-
.build()) \
|
|
181
|
-
.build()
|
|
182
|
-
|
|
183
|
-
# 发起请求
|
|
184
|
-
response: CreateMessageResponse = self.client.im.v1.message.create(request)
|
|
185
|
-
|
|
186
|
-
# 处理失败返回
|
|
187
|
-
if self._handle_response_error(response, "send_excel"):
|
|
188
|
-
return False
|
|
189
|
-
|
|
190
|
-
# 处理业务结果
|
|
191
|
-
lark.logger.info(lark.JSON.marshal(response.data, indent=4))
|
|
192
|
-
return True
|
|
193
|
-
|
|
194
|
-
def upload_excel(self, file_path: str) -> Optional[str]:
|
|
195
|
-
"""
|
|
196
|
-
上传Excel文件
|
|
197
|
-
|
|
198
|
-
Args:
|
|
199
|
-
file_path: 文件路径
|
|
200
|
-
|
|
201
|
-
Returns:
|
|
202
|
-
文件key,上传失败返回None
|
|
203
|
-
"""
|
|
204
|
-
if not os.path.exists(file_path):
|
|
205
|
-
lark.logger.error(f"文件不存在: {file_path}")
|
|
206
|
-
return None
|
|
207
|
-
|
|
208
|
-
try:
|
|
209
|
-
with open(file_path, "rb") as file:
|
|
210
|
-
file_name = os.path.basename(file_path)
|
|
211
|
-
request: CreateFileRequest = CreateFileRequest.builder() \
|
|
212
|
-
.request_body(CreateFileRequestBody.builder()
|
|
213
|
-
.file_type("xls")
|
|
214
|
-
.file_name(file_name)
|
|
215
|
-
.file(file)
|
|
216
|
-
.build()) \
|
|
217
|
-
.build()
|
|
218
|
-
|
|
219
|
-
# 发起请求
|
|
220
|
-
response: CreateFileResponse = self.client.im.v1.file.create(request)
|
|
221
|
-
|
|
222
|
-
# 处理失败返回
|
|
223
|
-
if self._handle_response_error(response, "upload_excel"):
|
|
224
|
-
return None
|
|
225
|
-
|
|
226
|
-
# 处理业务结果
|
|
227
|
-
lark.logger.info(lark.JSON.marshal(response.data, indent=4))
|
|
228
|
-
return response.data.file_key
|
|
229
|
-
except Exception as e:
|
|
230
|
-
lark.logger.error(f"上传Excel文件时发生错误: {e}")
|
|
231
|
-
return None
|
|
232
|
-
|
|
233
|
-
def upload_image(self, file_path: str) -> Optional[str]:
|
|
234
|
-
"""
|
|
235
|
-
上传图片文件
|
|
236
|
-
|
|
237
|
-
Args:
|
|
238
|
-
file_path: 图片文件路径
|
|
239
|
-
|
|
240
|
-
Returns:
|
|
241
|
-
图片key,上传失败返回None
|
|
242
|
-
"""
|
|
243
|
-
if not os.path.exists(file_path):
|
|
244
|
-
lark.logger.error(f"文件不存在: {file_path}")
|
|
245
|
-
return None
|
|
246
|
-
|
|
247
|
-
try:
|
|
248
|
-
with open(file_path, "rb") as file:
|
|
249
|
-
request: CreateImageRequest = CreateImageRequest.builder() \
|
|
250
|
-
.request_body(CreateImageRequestBody.builder()
|
|
251
|
-
.image_type("message")
|
|
252
|
-
.image(file)
|
|
253
|
-
.build()) \
|
|
254
|
-
.build()
|
|
255
|
-
|
|
256
|
-
# 发起请求
|
|
257
|
-
response: CreateImageResponse = self.client.im.v1.image.create(request)
|
|
258
|
-
|
|
259
|
-
# 处理失败返回
|
|
260
|
-
if self._handle_response_error(response, "upload_image"):
|
|
261
|
-
return None
|
|
262
|
-
|
|
263
|
-
# 处理业务结果
|
|
264
|
-
lark.logger.info(lark.JSON.marshal(response.data, indent=4))
|
|
265
|
-
return response.data.image_key
|
|
266
|
-
except Exception as e:
|
|
267
|
-
lark.logger.error(f"上传图片文件时发生错误: {e}")
|
|
1
|
+
# pip install lark-oapi -U
|
|
2
|
+
import json
|
|
3
|
+
import os
|
|
4
|
+
import uuid
|
|
5
|
+
from typing import Optional
|
|
6
|
+
|
|
7
|
+
import lark_oapi as lark
|
|
8
|
+
from lark_oapi.api.im.v1 import *
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class FeishuBot:
|
|
12
|
+
"""飞书机器人类,封装所有机器人相关功能"""
|
|
13
|
+
|
|
14
|
+
def __init__(self, config):
|
|
15
|
+
"""
|
|
16
|
+
初始化飞书机器人
|
|
17
|
+
|
|
18
|
+
Args:
|
|
19
|
+
config: 配置对象,包含应用ID、应用密钥和群组信息
|
|
20
|
+
"""
|
|
21
|
+
self.config = config
|
|
22
|
+
self._client = None
|
|
23
|
+
|
|
24
|
+
@property
|
|
25
|
+
def client(self):
|
|
26
|
+
"""获取飞书客户端,使用懒加载模式"""
|
|
27
|
+
if self._client is None:
|
|
28
|
+
self._client = lark.Client.builder() \
|
|
29
|
+
.app_id(self.config.feishu_bot.app_id) \
|
|
30
|
+
.app_secret(self.config.feishu_bot.app_secret) \
|
|
31
|
+
.log_level(lark.LogLevel.INFO) \
|
|
32
|
+
.build()
|
|
33
|
+
return self._client
|
|
34
|
+
|
|
35
|
+
def _get_chat_id(self, bot_name: str) -> Optional[str]:
|
|
36
|
+
"""
|
|
37
|
+
根据群组别名获取群组ID
|
|
38
|
+
|
|
39
|
+
Args:
|
|
40
|
+
bot_name: 群组别名
|
|
41
|
+
|
|
42
|
+
Returns:
|
|
43
|
+
群组ID,如果别名不存在则返回None
|
|
44
|
+
"""
|
|
45
|
+
return self.config.dict_feishu_group.get(bot_name)
|
|
46
|
+
|
|
47
|
+
def _handle_response_error(self, response, operation_name: str):
|
|
48
|
+
"""
|
|
49
|
+
处理API响应错误
|
|
50
|
+
|
|
51
|
+
Args:
|
|
52
|
+
response: API响应对象
|
|
53
|
+
operation_name: 操作名称,用于错误日志
|
|
54
|
+
"""
|
|
55
|
+
if not response.success():
|
|
56
|
+
lark.logger.error(
|
|
57
|
+
f"{operation_name} failed, code: {response.code}, "
|
|
58
|
+
f"msg: {response.msg}, log_id: {response.get_log_id()}, "
|
|
59
|
+
f"resp: \n{json.dumps(json.loads(response.raw.content), indent=4, ensure_ascii=False)}"
|
|
60
|
+
)
|
|
61
|
+
return True
|
|
62
|
+
return False
|
|
63
|
+
|
|
64
|
+
def send_text(self, content: str, bot_name: str = 'test') -> bool:
|
|
65
|
+
"""
|
|
66
|
+
发送文本消息
|
|
67
|
+
|
|
68
|
+
Args:
|
|
69
|
+
content: 文本内容
|
|
70
|
+
bot_name: 群组别名,默认为'test'
|
|
71
|
+
|
|
72
|
+
Returns:
|
|
73
|
+
发送是否成功
|
|
74
|
+
"""
|
|
75
|
+
chat_id = self._get_chat_id(bot_name)
|
|
76
|
+
if not chat_id:
|
|
77
|
+
lark.logger.error(f"未找到群组别名 '{bot_name}' 对应的群组ID")
|
|
78
|
+
return False
|
|
79
|
+
|
|
80
|
+
message_content = {"text": content}
|
|
81
|
+
|
|
82
|
+
# 构造请求对象
|
|
83
|
+
request: CreateMessageRequest = CreateMessageRequest.builder() \
|
|
84
|
+
.receive_id_type("chat_id") \
|
|
85
|
+
.request_body(CreateMessageRequestBody.builder()
|
|
86
|
+
.receive_id(chat_id)
|
|
87
|
+
.msg_type("text")
|
|
88
|
+
.content(json.dumps(message_content))
|
|
89
|
+
.uuid(str(uuid.uuid4()))
|
|
90
|
+
.build()) \
|
|
91
|
+
.build()
|
|
92
|
+
|
|
93
|
+
# 发起请求
|
|
94
|
+
response: CreateMessageResponse = self.client.im.v1.message.create(request)
|
|
95
|
+
|
|
96
|
+
# 处理失败返回
|
|
97
|
+
if self._handle_response_error(response, "send_text"):
|
|
98
|
+
return False
|
|
99
|
+
|
|
100
|
+
# 处理业务结果
|
|
101
|
+
lark.logger.info(lark.JSON.marshal(response.data, indent=4))
|
|
102
|
+
return True
|
|
103
|
+
|
|
104
|
+
def send_image(self, file_path: str, bot_name: str = 'test') -> bool:
|
|
105
|
+
"""
|
|
106
|
+
发送图片消息
|
|
107
|
+
|
|
108
|
+
Args:
|
|
109
|
+
file_path: 图片文件路径
|
|
110
|
+
bot_name: 群组别名,默认为'test'
|
|
111
|
+
|
|
112
|
+
Returns:
|
|
113
|
+
发送是否成功
|
|
114
|
+
"""
|
|
115
|
+
# 先上传图片获取image_key
|
|
116
|
+
image_key = self.upload_image(file_path)
|
|
117
|
+
if not image_key:
|
|
118
|
+
return False
|
|
119
|
+
|
|
120
|
+
chat_id = self._get_chat_id(bot_name)
|
|
121
|
+
if not chat_id:
|
|
122
|
+
lark.logger.error(f"未找到群组别名 '{bot_name}' 对应的群组ID")
|
|
123
|
+
return False
|
|
124
|
+
|
|
125
|
+
message_content = {"image_key": image_key}
|
|
126
|
+
|
|
127
|
+
# 构造请求对象
|
|
128
|
+
request: CreateMessageRequest = CreateMessageRequest.builder() \
|
|
129
|
+
.receive_id_type("chat_id") \
|
|
130
|
+
.request_body(CreateMessageRequestBody.builder()
|
|
131
|
+
.receive_id(chat_id)
|
|
132
|
+
.msg_type("image")
|
|
133
|
+
.content(json.dumps(message_content))
|
|
134
|
+
.uuid(str(uuid.uuid4()))
|
|
135
|
+
.build()) \
|
|
136
|
+
.build()
|
|
137
|
+
|
|
138
|
+
# 发起请求
|
|
139
|
+
response: CreateMessageResponse = self.client.im.v1.message.create(request)
|
|
140
|
+
|
|
141
|
+
# 处理失败返回
|
|
142
|
+
if self._handle_response_error(response, "send_image"):
|
|
143
|
+
return False
|
|
144
|
+
|
|
145
|
+
# 处理业务结果
|
|
146
|
+
lark.logger.info(lark.JSON.marshal(response.data, indent=4))
|
|
147
|
+
return True
|
|
148
|
+
|
|
149
|
+
def send_excel(self, file_path: str, bot_name: str = 'test') -> bool:
|
|
150
|
+
"""
|
|
151
|
+
发送Excel文件
|
|
152
|
+
|
|
153
|
+
Args:
|
|
154
|
+
file_path: Excel文件路径
|
|
155
|
+
bot_name: 群组别名,默认为'test'
|
|
156
|
+
|
|
157
|
+
Returns:
|
|
158
|
+
发送是否成功
|
|
159
|
+
"""
|
|
160
|
+
# 先上传文件获取file_key
|
|
161
|
+
file_key = self.upload_excel(file_path)
|
|
162
|
+
if not file_key:
|
|
163
|
+
return False
|
|
164
|
+
|
|
165
|
+
chat_id = self._get_chat_id(bot_name)
|
|
166
|
+
if not chat_id:
|
|
167
|
+
lark.logger.error(f"未找到群组别名 '{bot_name}' 对应的群组ID")
|
|
168
|
+
return False
|
|
169
|
+
|
|
170
|
+
message_content = {"file_key": file_key}
|
|
171
|
+
|
|
172
|
+
# 构造请求对象
|
|
173
|
+
request: CreateMessageRequest = CreateMessageRequest.builder() \
|
|
174
|
+
.receive_id_type("chat_id") \
|
|
175
|
+
.request_body(CreateMessageRequestBody.builder()
|
|
176
|
+
.receive_id(chat_id)
|
|
177
|
+
.msg_type("file")
|
|
178
|
+
.content(json.dumps(message_content))
|
|
179
|
+
.uuid(str(uuid.uuid4()))
|
|
180
|
+
.build()) \
|
|
181
|
+
.build()
|
|
182
|
+
|
|
183
|
+
# 发起请求
|
|
184
|
+
response: CreateMessageResponse = self.client.im.v1.message.create(request)
|
|
185
|
+
|
|
186
|
+
# 处理失败返回
|
|
187
|
+
if self._handle_response_error(response, "send_excel"):
|
|
188
|
+
return False
|
|
189
|
+
|
|
190
|
+
# 处理业务结果
|
|
191
|
+
lark.logger.info(lark.JSON.marshal(response.data, indent=4))
|
|
192
|
+
return True
|
|
193
|
+
|
|
194
|
+
def upload_excel(self, file_path: str) -> Optional[str]:
|
|
195
|
+
"""
|
|
196
|
+
上传Excel文件
|
|
197
|
+
|
|
198
|
+
Args:
|
|
199
|
+
file_path: 文件路径
|
|
200
|
+
|
|
201
|
+
Returns:
|
|
202
|
+
文件key,上传失败返回None
|
|
203
|
+
"""
|
|
204
|
+
if not os.path.exists(file_path):
|
|
205
|
+
lark.logger.error(f"文件不存在: {file_path}")
|
|
206
|
+
return None
|
|
207
|
+
|
|
208
|
+
try:
|
|
209
|
+
with open(file_path, "rb") as file:
|
|
210
|
+
file_name = os.path.basename(file_path)
|
|
211
|
+
request: CreateFileRequest = CreateFileRequest.builder() \
|
|
212
|
+
.request_body(CreateFileRequestBody.builder()
|
|
213
|
+
.file_type("xls")
|
|
214
|
+
.file_name(file_name)
|
|
215
|
+
.file(file)
|
|
216
|
+
.build()) \
|
|
217
|
+
.build()
|
|
218
|
+
|
|
219
|
+
# 发起请求
|
|
220
|
+
response: CreateFileResponse = self.client.im.v1.file.create(request)
|
|
221
|
+
|
|
222
|
+
# 处理失败返回
|
|
223
|
+
if self._handle_response_error(response, "upload_excel"):
|
|
224
|
+
return None
|
|
225
|
+
|
|
226
|
+
# 处理业务结果
|
|
227
|
+
lark.logger.info(lark.JSON.marshal(response.data, indent=4))
|
|
228
|
+
return response.data.file_key
|
|
229
|
+
except Exception as e:
|
|
230
|
+
lark.logger.error(f"上传Excel文件时发生错误: {e}")
|
|
231
|
+
return None
|
|
232
|
+
|
|
233
|
+
def upload_image(self, file_path: str) -> Optional[str]:
|
|
234
|
+
"""
|
|
235
|
+
上传图片文件
|
|
236
|
+
|
|
237
|
+
Args:
|
|
238
|
+
file_path: 图片文件路径
|
|
239
|
+
|
|
240
|
+
Returns:
|
|
241
|
+
图片key,上传失败返回None
|
|
242
|
+
"""
|
|
243
|
+
if not os.path.exists(file_path):
|
|
244
|
+
lark.logger.error(f"文件不存在: {file_path}")
|
|
245
|
+
return None
|
|
246
|
+
|
|
247
|
+
try:
|
|
248
|
+
with open(file_path, "rb") as file:
|
|
249
|
+
request: CreateImageRequest = CreateImageRequest.builder() \
|
|
250
|
+
.request_body(CreateImageRequestBody.builder()
|
|
251
|
+
.image_type("message")
|
|
252
|
+
.image(file)
|
|
253
|
+
.build()) \
|
|
254
|
+
.build()
|
|
255
|
+
|
|
256
|
+
# 发起请求
|
|
257
|
+
response: CreateImageResponse = self.client.im.v1.image.create(request)
|
|
258
|
+
|
|
259
|
+
# 处理失败返回
|
|
260
|
+
if self._handle_response_error(response, "upload_image"):
|
|
261
|
+
return None
|
|
262
|
+
|
|
263
|
+
# 处理业务结果
|
|
264
|
+
lark.logger.info(lark.JSON.marshal(response.data, indent=4))
|
|
265
|
+
return response.data.image_key
|
|
266
|
+
except Exception as e:
|
|
267
|
+
lark.logger.error(f"上传图片文件时发生错误: {e}")
|
|
268
268
|
return None
|