nvd commited on
Update bot.py
Browse files
bot.py
CHANGED
|
@@ -13,6 +13,7 @@ from urllib.parse import unquote
|
|
| 13 |
# ---------- Logging ----------
|
| 14 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
| 15 |
logger = logging.getLogger(__name__)
|
|
|
|
| 16 |
|
| 17 |
# ---------- Import tokens ----------
|
| 18 |
try:
|
|
@@ -123,18 +124,23 @@ async def send_message(chat_id, text, reply_markup=None):
|
|
| 123 |
params["reply_markup"] = json.dumps(reply_markup)
|
| 124 |
return await bale_request("sendMessage", params)
|
| 125 |
|
| 126 |
-
async def send_document_file(chat_id, file_path, caption=""):
|
| 127 |
"""
|
| 128 |
Upload a local file to Bale using sendDocument.
|
| 129 |
-
Uses httpx
|
| 130 |
"""
|
|
|
|
|
|
|
|
|
|
| 131 |
url = BALE_API + "sendDocument"
|
| 132 |
data = {"chat_id": str(chat_id)}
|
| 133 |
if caption:
|
| 134 |
data["caption"] = caption
|
| 135 |
|
|
|
|
| 136 |
with open(file_path, "rb") as f:
|
| 137 |
-
files = {"document": f}
|
|
|
|
| 138 |
async with httpx.AsyncClient() as client:
|
| 139 |
resp = await client.post(url, data=data, files=files, timeout=60)
|
| 140 |
if resp.status_code != 200:
|
|
@@ -273,7 +279,7 @@ async def handle_callback(callback: dict):
|
|
| 273 |
else:
|
| 274 |
await send_message(chat_id, f"π₯ Downloaded {size_mb:.2f} MB β uploading to Bale...")
|
| 275 |
|
| 276 |
-
# Upload with correct filename
|
| 277 |
result = await send_document_file(chat_id, tmp_path, caption="Here you go!", display_filename=filename)
|
| 278 |
if result.get("ok"):
|
| 279 |
if status_message_id:
|
|
@@ -283,10 +289,9 @@ async def handle_callback(callback: dict):
|
|
| 283 |
})
|
| 284 |
await answer_callback(callback_id, "Sent!")
|
| 285 |
else:
|
| 286 |
-
#
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
await send_message(chat_id, f"β Upload failed. Bale response:\n{result.get('description')}")
|
| 290 |
await answer_callback(callback_id, "Failed to send.")
|
| 291 |
except Exception as e:
|
| 292 |
logger.error(f"β Download/Upload error: {e}", exc_info=True)
|
|
|
|
| 13 |
# ---------- Logging ----------
|
| 14 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
| 15 |
logger = logging.getLogger(__name__)
|
| 16 |
+
logging.getLogger("httpx").setLevel(logging.DEBUG) # show all HTTP requests in logs
|
| 17 |
|
| 18 |
# ---------- Import tokens ----------
|
| 19 |
try:
|
|
|
|
| 124 |
params["reply_markup"] = json.dumps(reply_markup)
|
| 125 |
return await bale_request("sendMessage", params)
|
| 126 |
|
| 127 |
+
async def send_document_file(chat_id, file_path, caption="", display_filename=None):
|
| 128 |
"""
|
| 129 |
Upload a local file to Bale using sendDocument.
|
| 130 |
+
Uses httpx and explicitly sets a clean display_filename for the upload.
|
| 131 |
"""
|
| 132 |
+
if display_filename is None:
|
| 133 |
+
display_filename = os.path.basename(file_path)
|
| 134 |
+
|
| 135 |
url = BALE_API + "sendDocument"
|
| 136 |
data = {"chat_id": str(chat_id)}
|
| 137 |
if caption:
|
| 138 |
data["caption"] = caption
|
| 139 |
|
| 140 |
+
# Open file and use tuple (filename, file, content_type) for correct multipart header
|
| 141 |
with open(file_path, "rb") as f:
|
| 142 |
+
files = {"document": (display_filename, f, "application/octet-stream")}
|
| 143 |
+
logger.debug(f"Uploading file as: {display_filename}, local path: {file_path}")
|
| 144 |
async with httpx.AsyncClient() as client:
|
| 145 |
resp = await client.post(url, data=data, files=files, timeout=60)
|
| 146 |
if resp.status_code != 200:
|
|
|
|
| 279 |
else:
|
| 280 |
await send_message(chat_id, f"π₯ Downloaded {size_mb:.2f} MB β uploading to Bale...")
|
| 281 |
|
| 282 |
+
# Upload with correct filename (display_filename argument)
|
| 283 |
result = await send_document_file(chat_id, tmp_path, caption="Here you go!", display_filename=filename)
|
| 284 |
if result.get("ok"):
|
| 285 |
if status_message_id:
|
|
|
|
| 289 |
})
|
| 290 |
await answer_callback(callback_id, "Sent!")
|
| 291 |
else:
|
| 292 |
+
# Send the full raw Bale response to the chat for debugging
|
| 293 |
+
raw_error = result.get("description", str(result))
|
| 294 |
+
await send_message(chat_id, f"β Upload failed. Bale response:\n{raw_error}")
|
|
|
|
| 295 |
await answer_callback(callback_id, "Failed to send.")
|
| 296 |
except Exception as e:
|
| 297 |
logger.error(f"β Download/Upload error: {e}", exc_info=True)
|