fix: validate file reply business result and add s3 fallback
This commit is contained in:
parent
1b9982e743
commit
f7ffdb4233
@ -2210,11 +2210,44 @@ class OpenClawDingTalkBot(ChatbotHandler):
|
|||||||
)
|
)
|
||||||
response_text = response.text
|
response_text = response.text
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
|
payload = {}
|
||||||
|
try:
|
||||||
|
payload = response.json()
|
||||||
|
except Exception:
|
||||||
|
payload = {}
|
||||||
|
if isinstance(payload, dict):
|
||||||
|
errcode = payload.get("errcode")
|
||||||
|
success = payload.get("success")
|
||||||
|
if (errcode is not None and str(errcode) not in {"0", ""}) or success is False:
|
||||||
|
self.logger.error(
|
||||||
|
"reply file business failed, errcode=%s payload=%s",
|
||||||
|
errcode,
|
||||||
|
str(payload)[:500],
|
||||||
|
)
|
||||||
|
return False
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
self.logger.error("reply file failed, error=%s, response.text=%s", exc, response_text)
|
self.logger.error("reply file failed, error=%s, response.text=%s", exc, response_text)
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def _upload_excel_output_to_s3(self, file_path: str, filename: str) -> str:
|
||||||
|
if boto3 is None or BotoConfig is None:
|
||||||
|
return ""
|
||||||
|
if not (self.s3_bucket and self.s3_access_key and self.s3_secret_key):
|
||||||
|
return ""
|
||||||
|
try:
|
||||||
|
safe_name = self._normalize_file_name(filename or f"excel_{int(time.time())}.xlsx")
|
||||||
|
object_key = f"{self.s3_key_prefix}excel-output/{safe_name}"
|
||||||
|
self._upload_to_s3(
|
||||||
|
file_path,
|
||||||
|
object_key,
|
||||||
|
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||||
|
)
|
||||||
|
return self._build_public_s3_url(object_key)
|
||||||
|
except Exception as exc:
|
||||||
|
self.logger.exception("upload excel output to s3 failed: %s", exc)
|
||||||
|
return ""
|
||||||
|
|
||||||
def _run_excel_image_link_job(self, msg: ChatbotMessage, file_payload: dict) -> tuple[str, bool]:
|
def _run_excel_image_link_job(self, msg: ChatbotMessage, file_payload: dict) -> tuple[str, bool]:
|
||||||
if not self.excel_link_feature_enabled:
|
if not self.excel_link_feature_enabled:
|
||||||
return "Excel 图片链接转图片功能未启用。", False
|
return "Excel 图片链接转图片功能未启用。", False
|
||||||
@ -2289,6 +2322,7 @@ class OpenClawDingTalkBot(ChatbotHandler):
|
|||||||
summary_line = line.replace("SUMMARY:", "").strip()
|
summary_line = line.replace("SUMMARY:", "").strip()
|
||||||
break
|
break
|
||||||
|
|
||||||
|
media_id = ""
|
||||||
try:
|
try:
|
||||||
with open(output_path, "rb") as fh:
|
with open(output_path, "rb") as fh:
|
||||||
media_content = fh.read()
|
media_content = fh.read()
|
||||||
@ -2297,16 +2331,15 @@ class OpenClawDingTalkBot(ChatbotHandler):
|
|||||||
filetype="file",
|
filetype="file",
|
||||||
filename=output_name,
|
filename=output_name,
|
||||||
mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||||
)
|
) or ""
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
self.logger.exception("upload converted excel failed")
|
self.logger.exception("upload converted excel failed")
|
||||||
return f"处理完成但上传失败:{exc}", False
|
|
||||||
|
|
||||||
if not media_id:
|
sent = self._reply_file_by_media_id(msg, media_id) if media_id else False
|
||||||
return "处理完成但上传媒体失败,请稍后重试。", False
|
|
||||||
|
|
||||||
sent = self._reply_file_by_media_id(msg, media_id)
|
|
||||||
if not sent:
|
if not sent:
|
||||||
|
s3_url = self._upload_excel_output_to_s3(output_path, output_name)
|
||||||
|
if s3_url:
|
||||||
|
return f"钉钉文件回传受限,已生成下载链接:{s3_url}", True
|
||||||
return "处理完成但回传文件失败,请稍后重试。", False
|
return "处理完成但回传文件失败,请稍后重试。", False
|
||||||
|
|
||||||
summary_text = summary_line or "已完成图片回填。"
|
summary_text = summary_line or "已完成图片回填。"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user