Spaces:
Paused
Paused
Update main.py
Browse files
main.py
CHANGED
|
@@ -610,89 +610,99 @@ HF_TOKENS_STR = os.environ.get("HF_TOKENS", "")
|
|
| 610 |
HF_TOKENS =[k.strip() for k in HF_TOKENS_STR.split(",") if k.strip()]
|
| 611 |
|
| 612 |
# ==============================================================================
|
| 613 |
-
# 🟢 پارت 11: آپلودر
|
| 614 |
# ==============================================================================
|
| 615 |
-
# فقط توکن ربات بله نیاز است (آیدی چت از روی همین توکن استخراج میشود)
|
| 616 |
BALE_BOT_TOKEN = os.environ.get("BALE_BOT_TOKEN", "").strip()
|
| 617 |
|
| 618 |
async def helper_upload_file(client, chat_id, file_name, file_type="Image", caption=""):
|
| 619 |
abs_path = os.path.abspath(file_name)
|
| 620 |
error_logs =[]
|
| 621 |
|
| 622 |
-
|
| 623 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 624 |
|
| 625 |
-
#
|
| 626 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 627 |
|
| 628 |
for attempt in range(5):
|
| 629 |
try:
|
| 630 |
-
|
| 631 |
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 636 |
|
| 637 |
-
async with aiohttp.ClientSession() as session:
|
| 638 |
-
async with session.post(bale_url, data=form, timeout=120) as resp:
|
| 639 |
-
if resp.status == 200:
|
| 640 |
-
bale_data = await resp.json()
|
| 641 |
-
if bale_data.get("ok"):
|
| 642 |
-
# استخراج file_id از پاسخ بله
|
| 643 |
-
try:
|
| 644 |
-
file_id = bale_data['result']['document']['file_id']
|
| 645 |
-
except KeyError:
|
| 646 |
-
if 'audio' in bale_data['result']:
|
| 647 |
-
file_id = bale_data['result']['audio']['file_id']
|
| 648 |
-
elif 'video' in bale_data['result']:
|
| 649 |
-
file_id = bale_data['result']['video']['file_id']
|
| 650 |
-
elif 'voice' in bale_data['result']:
|
| 651 |
-
file_id = bale_data['result']['voice']['file_id']
|
| 652 |
-
elif 'photo' in bale_data['result']:
|
| 653 |
-
file_id = bale_data['result']['photo'][-1]['file_id']
|
| 654 |
-
else:
|
| 655 |
-
error_logs.append("Format not recognized in Bale response.")
|
| 656 |
-
await asyncio.sleep(2)
|
| 657 |
-
continue
|
| 658 |
-
|
| 659 |
-
# ساخت لینک دانلود بله
|
| 660 |
-
download_url = f"https://tapi.bale.ai/file/bot{BALE_BOT_TOKEN}/{file_id}"
|
| 661 |
-
|
| 662 |
-
# ترکیب کپشن اصلی شما با متن درخواستی برای لینک
|
| 663 |
-
final_text = f"""{caption}
|
| 664 |
-
|
| 665 |
-
━━━━━━━━━━━━━━━━━━━
|
| 666 |
-
🌐 لینک دانلود فایل شما:
|
| 667 |
-
|
| 668 |
-
{download_url}
|
| 669 |
-
|
| 670 |
-
━━━━━━━━━━━━━━━━━━━
|
| 671 |
-
⚠️ **راهنمای دانلود:**
|
| 672 |
-
لطفاً لینک بالا را کپی کرده و در **مرورگر گوشی خود (مثل کروم یا فایرفاکس)** باز کنید تا فایلتان دانلود شود.
|
| 673 |
-
*(لطفاً مستقیماً از اینجا روی لینک کلیک نکنید، چون روبیکا اجازه باز کردن و دانلود این نوع فایلها را نمیدهد)*.
|
| 674 |
-
|
| 675 |
-
🇮🇷 این لینک با دامنه سایت «بله» ایجاد شده و میتوانید فایل خود را به راحتی با **اینترنت داخلی (ملی)** دانلود کنید.
|
| 676 |
-
|
| 677 |
-
💡 **نکته:** در صورتی که محدودیتهای سرور روبیکا در دریافت فا��ل برطرف شود، سیستم را تغییر میدهیم تا در آینده بتوانید فایلها را مستقیماً داخل همین ربات دریافت کنید."""
|
| 678 |
-
|
| 679 |
-
# ارسال متن حاوی لینک به کاربر در روبیکا
|
| 680 |
-
send_res = await send_with_keyboard(client, chat_id, final_text, True)
|
| 681 |
-
if send_res:
|
| 682 |
-
return True
|
| 683 |
-
else:
|
| 684 |
-
error_logs.append("Failed to send text to Rubika.")
|
| 685 |
-
else:
|
| 686 |
-
error_logs.append(f"Bale API Error: {bale_data.get('description')}")
|
| 687 |
-
else:
|
| 688 |
-
error_logs.append(f"Bale HTTP Error: {resp.status}")
|
| 689 |
-
if resp.status in [500, 502, 503]:
|
| 690 |
-
await asyncio.sleep(3)
|
| 691 |
except Exception as e:
|
| 692 |
-
error_logs.append(f"
|
| 693 |
-
await asyncio.sleep(
|
| 694 |
|
| 695 |
-
return "\n".join(error_logs[-
|
| 696 |
|
| 697 |
# ==============================================================================
|
| 698 |
# 🟢 پارت 12: توابع تغییر صدا و لیست گویندگان
|
|
|
|
| 610 |
HF_TOKENS =[k.strip() for k in HF_TOKENS_STR.split(",") if k.strip()]
|
| 611 |
|
| 612 |
# ==============================================================================
|
| 613 |
+
# 🟢 پارت 11: آپلودر دوگانه هوشمند (آپلود در بله + بکاپ خودکار در روبیکا)
|
| 614 |
# ==============================================================================
|
|
|
|
| 615 |
BALE_BOT_TOKEN = os.environ.get("BALE_BOT_TOKEN", "").strip()
|
| 616 |
|
| 617 |
async def helper_upload_file(client, chat_id, file_name, file_type="Image", caption=""):
|
| 618 |
abs_path = os.path.abspath(file_name)
|
| 619 |
error_logs =[]
|
| 620 |
|
| 621 |
+
# ==========================================
|
| 622 |
+
# فاز 1: تلاش برای آپلود در بله (ساخت لینک)
|
| 623 |
+
# ==========================================
|
| 624 |
+
if BALE_BOT_TOKEN:
|
| 625 |
+
bale_chat_id = BALE_BOT_TOKEN.split(":")[0]
|
| 626 |
+
for attempt in range(3): # ۳ بار تلاش در سرور بله
|
| 627 |
+
try:
|
| 628 |
+
bale_url = f"https://tapi.bale.ai/bot{BALE_BOT_TOKEN}/sendDocument"
|
| 629 |
+
with open(abs_path, "rb") as f:
|
| 630 |
+
form = aiohttp.FormData()
|
| 631 |
+
form.add_field('chat_id', bale_chat_id)
|
| 632 |
+
form.add_field('document', f, filename=os.path.basename(abs_path))
|
| 633 |
+
|
| 634 |
+
async with aiohttp.ClientSession() as session:
|
| 635 |
+
async with session.post(bale_url, data=form, timeout=120) as resp:
|
| 636 |
+
if resp.status == 200:
|
| 637 |
+
bale_data = await resp.json()
|
| 638 |
+
if bale_data.get("ok"):
|
| 639 |
+
try:
|
| 640 |
+
file_id = bale_data['result']['document']['file_id']
|
| 641 |
+
except KeyError:
|
| 642 |
+
if 'audio' in bale_data['result']: file_id = bale_data['result']['audio']['file_id']
|
| 643 |
+
elif 'video' in bale_data['result']: file_id = bale_data['result']['video']['file_id']
|
| 644 |
+
elif 'voice' in bale_data['result']: file_id = bale_data['result']['voice']['file_id']
|
| 645 |
+
elif 'photo' in bale_data['result']: file_id = bale_data['result']['photo'][-1]['file_id']
|
| 646 |
+
else:
|
| 647 |
+
error_logs.append("فرمت در بله شناخته نشد.")
|
| 648 |
+
continue
|
| 649 |
+
|
| 650 |
+
download_url = f"https://tapi.bale.ai/file/bot{BALE_BOT_TOKEN}/{file_id}"
|
| 651 |
+
final_text = f"""{caption}\n\n━━━━━━━━━━━━━━━━━━━\n🌐 لینک دانلود فایل شما:\n\n{download_url}\n\n━━━━━━━━━━━━━━━━━━━\n⚠️ **راهنمای دانلود:**\nلطفاً لینک بالا را کپی کرده و در **مرورگر گوشی خود** باز کنید.\n*(مستقیماً کلیک نکنید)*"""
|
| 652 |
+
|
| 653 |
+
send_res = await send_with_keyboard(client, chat_id, final_text, True)
|
| 654 |
+
if send_res: return True
|
| 655 |
+
else:
|
| 656 |
+
error_logs.append(f"Bale API Err: {bale_data.get('description')}")
|
| 657 |
+
else:
|
| 658 |
+
error_logs.append(f"Bale HTTP Error: {resp.status}")
|
| 659 |
+
if resp.status in[500, 502, 503]: await asyncio.sleep(2)
|
| 660 |
+
except Exception as e:
|
| 661 |
+
error_logs.append(f"Bale Err: {str(e)[:100]}")
|
| 662 |
+
await asyncio.sleep(2)
|
| 663 |
|
| 664 |
+
# ==========================================
|
| 665 |
+
# فاز 2: سیستم نجات! (آپلود مستقیم در روبیکا)
|
| 666 |
+
# (اگر بله قطع بود، فایل مستقیما تو روبیکا ارسال میشه)
|
| 667 |
+
# ==========================================
|
| 668 |
+
api_file_type = "Image" if file_type in["photo", "Image", "image"] else "Voice" if file_type in ["voice", "Voice", "audio"] else file_type
|
| 669 |
+
if api_file_type == "Voice" and not abs_path.lower().endswith('.ogg'): api_file_type = "Music"
|
| 670 |
+
if api_file_type == "File" and abs_path.lower().endswith('.mp3'): api_file_type = "Music"
|
| 671 |
+
|
| 672 |
+
fallback_caption = f"{caption}\n\n⚠️ (به دلیل اختلال سرورهای بله، فایل موقتاً به صورت مستقیم در روبیکا ارسال شد)"
|
| 673 |
|
| 674 |
for attempt in range(5):
|
| 675 |
try:
|
| 676 |
+
sent_success = False
|
| 677 |
|
| 678 |
+
if api_file_type == "Image" and hasattr(client, "send_photo"):
|
| 679 |
+
try: await client.send_photo(chat_id, photo=abs_path, caption=fallback_caption); sent_success = True
|
| 680 |
+
except: await client.send_photo(chat_id, abs_path, caption=fallback_caption); sent_success = True
|
| 681 |
+
elif api_file_type == "Voice" and hasattr(client, "send_voice"):
|
| 682 |
+
try: await client.send_voice(chat_id, abs_path, caption=fallback_caption); sent_success = True
|
| 683 |
+
except: await client.send_voice(chat_id, file=abs_path, caption=fallback_caption); sent_success = True
|
| 684 |
+
elif api_file_type == "Music" and hasattr(client, "send_music"):
|
| 685 |
+
try: await client.send_music(chat_id, abs_path, caption=fallback_caption); sent_success = True
|
| 686 |
+
except: await client.send_music(chat_id, music=abs_path, caption=fallback_caption); sent_success = True
|
| 687 |
+
|
| 688 |
+
if not sent_success:
|
| 689 |
+
if hasattr(client, "send_document"):
|
| 690 |
+
try: await client.send_document(chat_id, document=abs_path, caption=fallback_caption); sent_success = True
|
| 691 |
+
except: await client.send_document(chat_id, abs_path, caption=fallback_caption); sent_success = True
|
| 692 |
+
elif hasattr(client, "send_file"):
|
| 693 |
+
try: await client.send_file(chat_id, file=abs_path)
|
| 694 |
+
except: await client.send_file(chat_id, abs_path)
|
| 695 |
+
if fallback_caption: await send_with_keyboard(client, chat_id, fallback_caption, True)
|
| 696 |
+
sent_success = True
|
| 697 |
+
|
| 698 |
+
if sent_success:
|
| 699 |
+
return True
|
| 700 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 701 |
except Exception as e:
|
| 702 |
+
error_logs.append(f"Rubpy Send Err: {str(e)[:100]}")
|
| 703 |
+
await asyncio.sleep(3)
|
| 704 |
|
| 705 |
+
return "\n".join(error_logs[-5:])
|
| 706 |
|
| 707 |
# ==============================================================================
|
| 708 |
# 🟢 پارت 12: توابع تغییر صدا و لیست گویندگان
|