nvd commited on
Commit
9bcb9fd
Β·
verified Β·
1 Parent(s): 8d6acba

Update bot.py

Browse files
Files changed (1) hide show
  1. bot.py +22 -27
bot.py CHANGED
@@ -247,46 +247,42 @@ async def handle_callback(callback: dict):
247
  if data.startswith("dl:"):
248
  url = data[3:]
249
 
250
- # Extract real filename from the URL (preserving extension)
251
  try:
252
  raw_filename = url.split("/")[-1].split("?")[0]
253
  filename = unquote(raw_filename)
254
  except:
255
  filename = "asset.tmp"
256
 
257
- logger.info(f"⬇️ Preparing to download: {url} as '{filename}'")
 
 
 
 
 
 
 
 
 
 
 
 
258
 
259
  # Send status message
260
  status_msg = await send_message(chat_id, "⏳ Downloading file from GitHub...")
261
  status_message_id = status_msg.get("result", {}).get("message_id") if status_msg.get("ok") else None
262
 
263
- # Temp file with real name
264
  tmp = tempfile.NamedTemporaryFile(delete=False, suffix="_" + filename)
265
  tmp_path = tmp.name
266
  try:
267
  await download_file(url, tmp_path)
268
 
269
- # Inside handle_callback, right after await download_file(url, tmp_path)
270
-
271
- # ─── Detect APK and rename if needed ───
272
- if filename.lower().endswith('.apk'):
273
- new_path = tmp_path + '.bin'
274
- os.rename(tmp_path, new_path)
275
- original_tmp = tmp_path
276
- tmp_path = new_path
277
- filename_for_upload = filename + '.bin'
278
- caption = "Here you go!\n\nπŸ“ Rename to .apk after downloading."
279
- else:
280
- filename_for_upload = filename
281
- caption = "Here you go!"
282
-
283
- # Now pass the (possibly renamed) file to send_document_file
284
- result = await send_document_file(
285
- chat_id,
286
- tmp_path,
287
- caption=caption,
288
- display_filename=filename_for_upload
289
- )
290
 
291
  file_size = os.path.getsize(tmp_path)
292
  size_mb = file_size / (1024 * 1024)
@@ -301,8 +297,8 @@ result = await send_document_file(
301
  else:
302
  await send_message(chat_id, f"πŸ“₯ Downloaded {size_mb:.2f} MB – uploading to Bale...")
303
 
304
- # Upload with correct filename (display_filename argument)
305
- result = await send_document_file(chat_id, tmp_path, caption="Here you go!", display_filename=filename)
306
  if result.get("ok"):
307
  if status_message_id:
308
  await bale_request("deleteMessage", {
@@ -311,7 +307,6 @@ result = await send_document_file(
311
  })
312
  await answer_callback(callback_id, "Sent!")
313
  else:
314
- # Send the full raw Bale response to the chat for debugging
315
  raw_error = result.get("description", str(result))
316
  await send_message(chat_id, f"❌ Upload failed. Bale response:\n{raw_error}")
317
  await answer_callback(callback_id, "Failed to send.")
 
247
  if data.startswith("dl:"):
248
  url = data[3:]
249
 
250
+ # Extract real filename
251
  try:
252
  raw_filename = url.split("/")[-1].split("?")[0]
253
  filename = unquote(raw_filename)
254
  except:
255
  filename = "asset.tmp"
256
 
257
+ logger.info(f"⬇️ Requested asset: {filename}")
258
+
259
+ # Prepare caption
260
+ caption = "Here you go!"
261
+
262
+ # If APK, remove .apk extension from the filename and add a note
263
+ if filename.lower().endswith('.apk'):
264
+ # Remove the last 4 characters (".apk")
265
+ base_name = filename[:-4]
266
+ filename_for_upload = base_name
267
+ caption = "Here you go!\n\nπŸ“ Rename to .apk after downloading."
268
+ else:
269
+ filename_for_upload = filename
270
 
271
  # Send status message
272
  status_msg = await send_message(chat_id, "⏳ Downloading file from GitHub...")
273
  status_message_id = status_msg.get("result", {}).get("message_id") if status_msg.get("ok") else None
274
 
275
+ # Temp file (keep original suffix for download, we'll rename later if needed)
276
  tmp = tempfile.NamedTemporaryFile(delete=False, suffix="_" + filename)
277
  tmp_path = tmp.name
278
  try:
279
  await download_file(url, tmp_path)
280
 
281
+ # If APK, rename the temp file to remove .apk extension
282
+ if filename.lower().endswith('.apk'):
283
+ new_tmp_path = tmp_path[:-4] # remove ".apk" from the temp filename
284
+ os.rename(tmp_path, new_tmp_path)
285
+ tmp_path = new_tmp_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
286
 
287
  file_size = os.path.getsize(tmp_path)
288
  size_mb = file_size / (1024 * 1024)
 
297
  else:
298
  await send_message(chat_id, f"πŸ“₯ Downloaded {size_mb:.2f} MB – uploading to Bale...")
299
 
300
+ # Upload with the (possibly modified) filename
301
+ result = await send_document_file(chat_id, tmp_path, caption=caption, display_filename=filename_for_upload)
302
  if result.get("ok"):
303
  if status_message_id:
304
  await bale_request("deleteMessage", {
 
307
  })
308
  await answer_callback(callback_id, "Sent!")
309
  else:
 
310
  raw_error = result.get("description", str(result))
311
  await send_message(chat_id, f"❌ Upload failed. Bale response:\n{raw_error}")
312
  await answer_callback(callback_id, "Failed to send.")