diff --git a/plugin/tools.py b/plugin/tools.py index 465bd0b..392edb0 100644 --- a/plugin/tools.py +++ b/plugin/tools.py @@ -224,7 +224,28 @@ class RocketChatAPI: req = urllib.request.Request(url, data=body, headers=headers, method="POST") try: with urllib.request.urlopen(req, timeout=60) as resp: - return json.loads(resp.read().decode("utf-8")) + upload_result = json.loads(resp.read().decode("utf-8")) + if not upload_result.get("success"): + return upload_result + # Step 2: Post message with the uploaded file as attachment + file_info = upload_result.get("file") or {} + file_id = file_info.get("_id") + if file_id: + attachment: dict = { + "title": filename, + "title_link": f"/file-upload/{file_id}/{filename}", + "title_link_download": True, + "type": "file", + } + if description: + attachment["description"] = description + post_result = self.send_message( + room_id=room_id, + text=message or "", + attachments=[attachment], + ) + return {"success": True, "upload": upload_result, "post_message": post_result} + return upload_result except urllib.error.HTTPError as exc: msg = f"Upload HTTP {exc.code}" try: @@ -674,19 +695,10 @@ def _handle_rocketchat_upload(args: dict, **kw) -> str: if not room_id or not file_path: return tool_error("room_id and file_path are required for rocketchat_upload") + # RocketChatAPI.upload_file() already handles rooms.media + chat.postMessage try: result = client.upload_file(room_id, file_path, message, description) - if result.get("success"): - return tool_result({ - "success": True, - "file_id": result.get("file_id"), - "filename": result.get("filename"), - "file_type": result.get("file_type"), - "message": result.get("message"), - "result": result, - }) - else: - return tool_result({"success": False, "error": result.get("error", "Upload failed")}) + return tool_result(result) except Exception as exc: return tool_error(f"rocketchat_upload failed: {type(exc).__name__}: {exc}")