Update main.py
Browse files
main.py
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import time
|
| 3 |
import threading
|
|
@@ -14,12 +17,15 @@ import string
|
|
| 14 |
import uuid
|
| 15 |
import concurrent.futures
|
| 16 |
from flask import Flask
|
| 17 |
-
from rubpy.bot import BotClient, filters
|
| 18 |
|
| 19 |
from huggingface_hub import AsyncInferenceClient, HfApi, hf_hub_download
|
| 20 |
from PIL import Image
|
| 21 |
from pydub import AudioSegment
|
| 22 |
|
|
|
|
|
|
|
|
|
|
| 23 |
# --- تنظیمات آدرس سرورهای تغییر صدا ---
|
| 24 |
VC_BASE_URL = "https://sada8888-sada.hf.space"
|
| 25 |
LEGACY_BASE_URL = "https://ezmarynoori-rvc.hf.space"
|
|
@@ -42,6 +48,9 @@ STANDARD_MODELS = {
|
|
| 42 |
"12": {"name": "مریم", "ref": "https://huggingface.co/datasets/Hamed744/mp3/resolve/main/%D8%B5%D8%AF%D8%A7%DB%8C%20%D8%AE%D8%A7%D9%86%D9%85.wav?download=true"}
|
| 43 |
}
|
| 44 |
|
|
|
|
|
|
|
|
|
|
| 45 |
# --- کد مدیریت ---
|
| 46 |
ADMIN_CODE = "3011"
|
| 47 |
BOT_GUID = None
|
|
@@ -54,19 +63,13 @@ user_message_times = {}
|
|
| 54 |
def is_user_spamming(chat_id):
|
| 55 |
now = time.time()
|
| 56 |
times = user_message_times.get(chat_id,[])
|
| 57 |
-
times =
|
| 58 |
times.append(now)
|
| 59 |
user_message_times[chat_id] = times
|
| 60 |
if len(times) > 4:
|
| 61 |
return True
|
| 62 |
return False
|
| 63 |
|
| 64 |
-
# --- سیستم دیتابیس حساب کاربری מתصل به دیتاست هاگینگ فیس ---
|
| 65 |
-
DB_FILE = "users_db.json"
|
| 66 |
-
DATASET_REPO = "opera8/Karbaran-rayegan-tedad"
|
| 67 |
-
HF_TOKEN_DB = os.environ.get("HF_TOKEN")
|
| 68 |
-
db_lock = threading.Lock()
|
| 69 |
-
|
| 70 |
def gregorian_to_jalali(gy, gm, gd):
|
| 71 |
g_d_m =[0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
|
| 72 |
gy2 = (gy + 1) if (gm > 2) else gy
|
|
@@ -86,6 +89,15 @@ def gregorian_to_jalali(gy, gm, gd):
|
|
| 86 |
jd = 1 + ((days - 186) % 30)
|
| 87 |
return jy, jm, jd
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
def load_db():
|
| 90 |
print("در حال تلاش برای خواندن دیتابیس کاربران از دیتاست هاگینگ فیس...")
|
| 91 |
if not HF_TOKEN_DB:
|
|
@@ -151,6 +163,9 @@ async def background_db_uploader():
|
|
| 151 |
|
| 152 |
user_credits_db = load_db()
|
| 153 |
|
|
|
|
|
|
|
|
|
|
| 154 |
def get_or_create_referral_code(chat_id):
|
| 155 |
user_data = user_credits_db[chat_id]
|
| 156 |
if not user_data.get("referral_code"):
|
|
@@ -238,6 +253,9 @@ def to_english_digits(text):
|
|
| 238 |
translation_table = str.maketrans(persian_digits + arabic_digits, english_digits * 2)
|
| 239 |
return str(text).translate(translation_table)
|
| 240 |
|
|
|
|
|
|
|
|
|
|
| 241 |
# --- تنظیمات وب سرور ---
|
| 242 |
app = Flask(__name__)
|
| 243 |
|
|
@@ -268,6 +286,9 @@ def sync_combine_audio(current_audio, new_bytes):
|
|
| 268 |
def sync_export_audio(audio_obj, file_name):
|
| 269 |
audio_obj.export(file_name, format="mp3")
|
| 270 |
|
|
|
|
|
|
|
|
|
|
| 271 |
# --- ساختار کیبورد ---
|
| 272 |
MAIN_KEYPAD_DICT = {
|
| 273 |
"rows":[
|
|
@@ -345,6 +366,9 @@ JOIN_KEYPAD_DICT = {
|
|
| 345 |
"resize_keyboard": True
|
| 346 |
}
|
| 347 |
|
|
|
|
|
|
|
|
|
|
| 348 |
async def check_channel_membership(client, user_id):
|
| 349 |
global CHANNEL_GUID
|
| 350 |
if str(user_id) == str(BOT_GUID): return True
|
|
@@ -391,6 +415,9 @@ async def send_with_keyboard(client, chat_id, text, use_keyboard=True):
|
|
| 391 |
|
| 392 |
bot_token = os.environ.get("RUBIKA_AUTH", "").strip()
|
| 393 |
|
|
|
|
|
|
|
|
|
|
| 394 |
# ==================================================================
|
| 395 |
# تابع دانلود ضد بمب اتم (پافشاری ۳۵ بار)
|
| 396 |
# ==================================================================
|
|
@@ -491,6 +518,9 @@ async def helper_download_url_to_bytes(url):
|
|
| 491 |
await asyncio.sleep(2)
|
| 492 |
return None
|
| 493 |
|
|
|
|
|
|
|
|
|
|
| 494 |
# ==================================================================
|
| 495 |
# توکنها و کلیدها
|
| 496 |
# ==================================================================
|
|
@@ -522,17 +552,85 @@ def get_next_gemini_keys(count=100):
|
|
| 522 |
HF_TOKENS_STR = os.environ.get("HF_TOKENS", "")
|
| 523 |
HF_TOKENS =[k.strip() for k in HF_TOKENS_STR.split(",") if k.strip()]
|
| 524 |
|
| 525 |
-
# ==================================================================
|
| 526 |
-
#
|
| 527 |
-
# ==================================================================
|
| 528 |
async def helper_upload_file(client, chat_id, file_name, file_type="Image", caption=""):
|
| 529 |
abs_path = os.path.abspath(file_name)
|
| 530 |
-
error_logs =[]
|
|
|
|
|
|
|
| 531 |
|
| 532 |
-
|
| 533 |
-
if api_file_type == "Voice" and not abs_path.lower().endswith('.ogg'):
|
| 534 |
-
|
|
|
|
|
|
|
| 535 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 536 |
for attempt in range(15):
|
| 537 |
try:
|
| 538 |
url_request = f"https://botapi.rubika.ir/v3/{bot_token}/requestSendFile"
|
|
@@ -555,20 +653,34 @@ async def helper_upload_file(client, chat_id, file_name, file_type="Image", capt
|
|
| 555 |
async with session.post(upload_url, data=form, timeout=300) as up_resp:
|
| 556 |
if up_resp.status != 200:
|
| 557 |
error_logs.append(f"Upload HTTP {up_resp.status}")
|
| 558 |
-
|
|
|
|
|
|
|
|
|
|
| 559 |
continue
|
| 560 |
|
| 561 |
-
try:
|
|
|
|
| 562 |
except Exception as json_err:
|
| 563 |
error_logs.append(f"JSON Decode Error: {json_err}")
|
| 564 |
await asyncio.sleep(2)
|
| 565 |
continue
|
| 566 |
|
| 567 |
final_file_id = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 568 |
if up_data.get("status") == "OK" or up_data.get("status_det") == "OK":
|
| 569 |
if "data" in up_data and isinstance(up_data["data"], dict):
|
| 570 |
final_file_id = up_data["data"].get("file_id") or up_data["data"].get("id")
|
| 571 |
-
elif "file_id" in up_data:
|
|
|
|
| 572 |
|
| 573 |
if final_file_id:
|
| 574 |
send_payload = {
|
|
@@ -588,8 +700,8 @@ async def helper_upload_file(client, chat_id, file_name, file_type="Image", capt
|
|
| 588 |
if s_data.get("status") == "OK": return True
|
| 589 |
else: error_logs.append(f"SendFile API Error: {s_data}")
|
| 590 |
else:
|
| 591 |
-
if up_data.get("status") == "INVALID_INPUT" and api_file_type
|
| 592 |
-
api_file_type = "
|
| 593 |
error_logs.append(f"Upload API Error: {up_data}")
|
| 594 |
else: error_logs.append(f"Missing upload_url in Response: {req_data}")
|
| 595 |
else: error_logs.append(f"Request API Error: {req_data}")
|
|
@@ -597,37 +709,11 @@ async def helper_upload_file(client, chat_id, file_name, file_type="Image", capt
|
|
| 597 |
error_logs.append(f"Raw HTTP Error: {str(e)[:100]}")
|
| 598 |
await asyncio.sleep(2.5)
|
| 599 |
|
| 600 |
-
# روش دوم: استفاده از متدهای اختصاصی rubpy برای ارسال عکس/فایل (جلوگیری از ارسال عکس به صورت داکیومنت)
|
| 601 |
-
for attempt in range(10):
|
| 602 |
-
try:
|
| 603 |
-
if api_file_type == "Image" and hasattr(client, "send_photo"):
|
| 604 |
-
await client.send_photo(chat_id, photo=abs_path, caption=caption)
|
| 605 |
-
return True
|
| 606 |
-
elif api_file_type in ["Voice", "Music"]:
|
| 607 |
-
if hasattr(client, "send_voice"):
|
| 608 |
-
await client.send_voice(chat_id, voice=abs_path, caption=caption)
|
| 609 |
-
return True
|
| 610 |
-
elif hasattr(client, "send_music"):
|
| 611 |
-
await client.send_music(chat_id, music=abs_path, caption=caption)
|
| 612 |
-
return True
|
| 613 |
-
|
| 614 |
-
# در صورتی که هیچکدام از شروط بالا برقرار نبود به عنوان فایل ارسال شود
|
| 615 |
-
if hasattr(client, "send_document"):
|
| 616 |
-
await client.send_document(chat_id, document=abs_path, caption=caption)
|
| 617 |
-
return True
|
| 618 |
-
elif hasattr(client, "send_file"):
|
| 619 |
-
await client.send_file(chat_id, abs_path)
|
| 620 |
-
if caption:
|
| 621 |
-
await send_with_keyboard(client, chat_id, caption, True)
|
| 622 |
-
return True
|
| 623 |
-
except Exception as e:
|
| 624 |
-
err_msg = str(e)[:100]
|
| 625 |
-
error_logs.append(f"Rubpy Send Error: {err_msg}")
|
| 626 |
-
if "502" in err_msg or "timeout" in err_msg.lower() or "time" in err_msg.lower(): await asyncio.sleep(4)
|
| 627 |
-
else: await asyncio.sleep(2)
|
| 628 |
-
|
| 629 |
return "\n".join(error_logs[-6:])
|
| 630 |
|
|
|
|
|
|
|
|
|
|
| 631 |
# ==================================================================
|
| 632 |
# لیستهای اولیه ربات
|
| 633 |
# ==================================================================
|
|
@@ -804,6 +890,9 @@ async def process_legacy_vc_job(client, chat_id, src_bytes, model_url, pitch, mo
|
|
| 804 |
|
| 805 |
if os.path.exists(file_name_mp3): os.remove(file_name_mp3)
|
| 806 |
|
|
|
|
|
|
|
|
|
|
| 807 |
# ==================================================================
|
| 808 |
# سایر توابع پردازشی
|
| 809 |
# ==================================================================
|
|
@@ -967,6 +1056,9 @@ async def process_gemini(client, chat_id, prompt, file_bytes=None, file_name=Non
|
|
| 967 |
except Exception:
|
| 968 |
await send_with_keyboard(client, chat_id, "❌ خطایی در ارسال پیام رخ داد.", False)
|
| 969 |
|
|
|
|
|
|
|
|
|
|
| 970 |
async def process_image(client, chat_id, prompt, size_choice="1"):
|
| 971 |
str_chat_id = str(chat_id).replace("`", "").replace("'", "").replace('"', "").strip()
|
| 972 |
creds = get_user_credits(str_chat_id)
|
|
@@ -1096,6 +1188,9 @@ async def process_image(client, chat_id, prompt, size_choice="1"):
|
|
| 1096 |
except Exception as e:
|
| 1097 |
await send_with_keyboard(client, chat_id, f"❌ خطا در ذخیره و ارسال عکس:\n{str(e)[:150]}", True)
|
| 1098 |
|
|
|
|
|
|
|
|
|
|
| 1099 |
async def translate_text_aloha(prompt_text):
|
| 1100 |
session_hash = ''.join(random.choices(string.ascii_lowercase + string.digits, k=11))
|
| 1101 |
join_url = "https://hamed744-translate-tts-aloha.hf.space/gradio_api/queue/join"
|
|
@@ -1203,6 +1298,9 @@ async def process_image_edit(client, chat_id, image_bytes, prompt):
|
|
| 1203 |
except Exception as e:
|
| 1204 |
await send_with_keyboard(client, chat_id, f"❌ خطا در ذخیره عکس ویرایش شده:\n{str(e)[:150]}", True)
|
| 1205 |
|
|
|
|
|
|
|
|
|
|
| 1206 |
async def process_tts(client, chat_id, user_text, speaker_id, speaker_name):
|
| 1207 |
str_chat_id = str(chat_id).replace("`", "").replace("'", "").replace('"', "").strip()
|
| 1208 |
creds = get_user_credits(str_chat_id)
|
|
@@ -1272,6 +1370,9 @@ async def process_tts(client, chat_id, user_text, speaker_id, speaker_name):
|
|
| 1272 |
await send_with_keyboard(client, chat_id, f"❌ سرورها درگیر هستند.\nدلیل: {last_error}", True)
|
| 1273 |
except Exception: traceback.print_exc()
|
| 1274 |
|
|
|
|
|
|
|
|
|
|
| 1275 |
async def process_podcast(client, chat_id, prompt):
|
| 1276 |
str_chat_id = str(chat_id).replace("`", "").replace("'", "").replace('"', "").strip()
|
| 1277 |
creds = get_user_credits(str_chat_id)
|
|
@@ -1375,6 +1476,9 @@ async def process_podcast(client, chat_id, prompt):
|
|
| 1375 |
|
| 1376 |
if os.path.exists(file_name_mp3): os.remove(file_name_mp3)
|
| 1377 |
|
|
|
|
|
|
|
|
|
|
| 1378 |
async def process_stt(client, chat_id, audio_bytes, file_name):
|
| 1379 |
str_chat_id = str(chat_id).replace("`", "").replace("'", "").replace('"', "").strip()
|
| 1380 |
creds = get_user_credits(str_chat_id)
|
|
@@ -1519,6 +1623,9 @@ async def process_file_analysis(client, chat_id, file_bytes, file_name, prompt):
|
|
| 1519 |
else:
|
| 1520 |
await send_with_keyboard(client, chat_id, "❌ تمامی سرورها شلوغ هستند. لطفاً بعداً امتحان کنید.", True)
|
| 1521 |
|
|
|
|
|
|
|
|
|
|
| 1522 |
async def process_create_file(client, chat_id, topic):
|
| 1523 |
str_chat_id = str(chat_id).replace("`", "").replace("'", "").replace('"', "").strip()
|
| 1524 |
creds = get_user_credits(str_chat_id)
|
|
@@ -1683,6 +1790,9 @@ async def process_create_file(client, chat_id, topic):
|
|
| 1683 |
else:
|
| 1684 |
await send_with_keyboard(client, chat_id, "❌ متأسفانه پس از تلاشهای مکرر، سرور قادر به ساخت و ارسال هیچیک از فایلها نبود و اعتباری از شما کسر نشد.", True)
|
| 1685 |
|
|
|
|
|
|
|
|
|
|
| 1686 |
# ==================================================================
|
| 1687 |
# حلقه و مدیریت اصلی ربات
|
| 1688 |
# ==================================================================
|
|
|
|
| 1 |
+
# ==============================================================================
|
| 2 |
+
# 🟢 پارت 1: وارد کردن کتابخانهها و ماژولها (Imports)
|
| 3 |
+
# ==============================================================================
|
| 4 |
import os
|
| 5 |
import time
|
| 6 |
import threading
|
|
|
|
| 17 |
import uuid
|
| 18 |
import concurrent.futures
|
| 19 |
from flask import Flask
|
| 20 |
+
from rubpy.bot import BotClient, filters # بازگردانی به نسخه اصلی شما
|
| 21 |
|
| 22 |
from huggingface_hub import AsyncInferenceClient, HfApi, hf_hub_download
|
| 23 |
from PIL import Image
|
| 24 |
from pydub import AudioSegment
|
| 25 |
|
| 26 |
+
# ==============================================================================
|
| 27 |
+
# 🟢 پارت 2: تنظیمات سرورها، مدلهای تغییر صدا و کلون صدا
|
| 28 |
+
# ==============================================================================
|
| 29 |
# --- تنظیمات آدرس سرورهای تغییر صدا ---
|
| 30 |
VC_BASE_URL = "https://sada8888-sada.hf.space"
|
| 31 |
LEGACY_BASE_URL = "https://ezmarynoori-rvc.hf.space"
|
|
|
|
| 48 |
"12": {"name": "مریم", "ref": "https://huggingface.co/datasets/Hamed744/mp3/resolve/main/%D8%B5%D8%AF%D8%A7%DB%8C%20%D8%AE%D8%A7%D9%86%D9%85.wav?download=true"}
|
| 49 |
}
|
| 50 |
|
| 51 |
+
# ==============================================================================
|
| 52 |
+
# 🟢 پارت 3: متغیرهای ادمین، سیستم ضد اسپم و توابع تبدیل تاریخ
|
| 53 |
+
# ==============================================================================
|
| 54 |
# --- کد مدیریت ---
|
| 55 |
ADMIN_CODE = "3011"
|
| 56 |
BOT_GUID = None
|
|
|
|
| 63 |
def is_user_spamming(chat_id):
|
| 64 |
now = time.time()
|
| 65 |
times = user_message_times.get(chat_id,[])
|
| 66 |
+
times =[t for t in times if now - t < 3.0]
|
| 67 |
times.append(now)
|
| 68 |
user_message_times[chat_id] = times
|
| 69 |
if len(times) > 4:
|
| 70 |
return True
|
| 71 |
return False
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
def gregorian_to_jalali(gy, gm, gd):
|
| 74 |
g_d_m =[0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
|
| 75 |
gy2 = (gy + 1) if (gm > 2) else gy
|
|
|
|
| 89 |
jd = 1 + ((days - 186) % 30)
|
| 90 |
return jy, jm, jd
|
| 91 |
|
| 92 |
+
# ==============================================================================
|
| 93 |
+
# 🟢 پارت 4: راهاندازی دیتابیس و همگامسازی با دیتاست هاگینگ فیس
|
| 94 |
+
# ==============================================================================
|
| 95 |
+
# --- سیستم دیتابیس حساب کاربری מתصل به دیتاست هاگینگ فیس ---
|
| 96 |
+
DB_FILE = "users_db.json"
|
| 97 |
+
DATASET_REPO = "opera8/Karbaran-rayegan-tedad"
|
| 98 |
+
HF_TOKEN_DB = os.environ.get("HF_TOKEN")
|
| 99 |
+
db_lock = threading.Lock()
|
| 100 |
+
|
| 101 |
def load_db():
|
| 102 |
print("در حال تلاش برای خواندن دیتابیس کاربران از دیتاست هاگینگ فیس...")
|
| 103 |
if not HF_TOKEN_DB:
|
|
|
|
| 163 |
|
| 164 |
user_credits_db = load_db()
|
| 165 |
|
| 166 |
+
# ==============================================================================
|
| 167 |
+
# 🟢 پارت 5: توابع کد معرف، مدیریت سهمیه حساب کاربری و تبدیل اعداد
|
| 168 |
+
# ==============================================================================
|
| 169 |
def get_or_create_referral_code(chat_id):
|
| 170 |
user_data = user_credits_db[chat_id]
|
| 171 |
if not user_data.get("referral_code"):
|
|
|
|
| 253 |
translation_table = str.maketrans(persian_digits + arabic_digits, english_digits * 2)
|
| 254 |
return str(text).translate(translation_table)
|
| 255 |
|
| 256 |
+
# ==============================================================================
|
| 257 |
+
# 🟢 پارت 6: تنظیمات وبسرور (Flask) و توابع کمکی فایلها
|
| 258 |
+
# ==============================================================================
|
| 259 |
# --- تنظیمات وب سرور ---
|
| 260 |
app = Flask(__name__)
|
| 261 |
|
|
|
|
| 286 |
def sync_export_audio(audio_obj, file_name):
|
| 287 |
audio_obj.export(file_name, format="mp3")
|
| 288 |
|
| 289 |
+
# ==============================================================================
|
| 290 |
+
# 🟢 پارت 7: کیبوردها (دکمههای شیشهای / منوها)
|
| 291 |
+
# ==============================================================================
|
| 292 |
# --- ساختار کیبورد ---
|
| 293 |
MAIN_KEYPAD_DICT = {
|
| 294 |
"rows":[
|
|
|
|
| 366 |
"resize_keyboard": True
|
| 367 |
}
|
| 368 |
|
| 369 |
+
# ==============================================================================
|
| 370 |
+
# 🟢 پارت 8: تابع بررسی عضویت کانال، ارسال کیبورد و توکن روبیکا
|
| 371 |
+
# ==============================================================================
|
| 372 |
async def check_channel_membership(client, user_id):
|
| 373 |
global CHANNEL_GUID
|
| 374 |
if str(user_id) == str(BOT_GUID): return True
|
|
|
|
| 415 |
|
| 416 |
bot_token = os.environ.get("RUBIKA_AUTH", "").strip()
|
| 417 |
|
| 418 |
+
# ==============================================================================
|
| 419 |
+
# 🟢 پارت 9: دانلودر قدرتمند و تابع ضد قطعی روبیکا
|
| 420 |
+
# ==============================================================================
|
| 421 |
# ==================================================================
|
| 422 |
# تابع دانلود ضد بمب اتم (پافشاری ۳۵ بار)
|
| 423 |
# ==================================================================
|
|
|
|
| 518 |
await asyncio.sleep(2)
|
| 519 |
return None
|
| 520 |
|
| 521 |
+
# ==============================================================================
|
| 522 |
+
# 🟢 پارت 10: سیستم چرخش کلیدهای جیمینای و تنظیم توکنهای هاگینگ فیس
|
| 523 |
+
# ==============================================================================
|
| 524 |
# ==================================================================
|
| 525 |
# توکنها و کلیدها
|
| 526 |
# ==================================================================
|
|
|
|
| 552 |
HF_TOKENS_STR = os.environ.get("HF_TOKENS", "")
|
| 553 |
HF_TOKENS =[k.strip() for k in HF_TOKENS_STR.split(",") if k.strip()]
|
| 554 |
|
| 555 |
+
# ==============================================================================
|
| 556 |
+
# 🟢 پارت 11: آپلودر قدرتمند و تابع ارسال فایل ضد خطا (نسخه نهایی ضد بمب اتم 🚀)
|
| 557 |
+
# ==============================================================================
|
| 558 |
async def helper_upload_file(client, chat_id, file_name, file_type="Image", caption=""):
|
| 559 |
abs_path = os.path.abspath(file_name)
|
| 560 |
+
error_logs = []
|
| 561 |
+
|
| 562 |
+
api_file_type = "Image" if file_type in["photo", "Image", "image"] else "Voice" if file_type in ["voice", "Voice", "audio"] else file_type
|
| 563 |
|
| 564 |
+
# روبیکا فرمتهای خاصی را قبول میکند.
|
| 565 |
+
if api_file_type == "Voice" and not abs_path.lower().endswith('.ogg'):
|
| 566 |
+
api_file_type = "Music"
|
| 567 |
+
if api_file_type == "File" and abs_path.lower().endswith('.mp3'):
|
| 568 |
+
api_file_type = "Music"
|
| 569 |
|
| 570 |
+
# --- فاز اول (فوق سریع): استفاده از متدهای اختصاصی rubpy ---
|
| 571 |
+
for attempt in range(10):
|
| 572 |
+
try:
|
| 573 |
+
sent_success = False
|
| 574 |
+
|
| 575 |
+
# تلاش برای ارسال عکس
|
| 576 |
+
if api_file_type == "Image" and hasattr(client, "send_photo"):
|
| 577 |
+
try:
|
| 578 |
+
await client.send_photo(chat_id, photo=abs_path, caption=caption)
|
| 579 |
+
sent_success = True
|
| 580 |
+
except TypeError:
|
| 581 |
+
await client.send_photo(chat_id, abs_path, caption=caption)
|
| 582 |
+
sent_success = True
|
| 583 |
+
|
| 584 |
+
# تلاش برای ارسال ویس
|
| 585 |
+
elif api_file_type == "Voice" and hasattr(client, "send_voice"):
|
| 586 |
+
try:
|
| 587 |
+
await client.send_voice(chat_id, abs_path, caption=caption)
|
| 588 |
+
sent_success = True
|
| 589 |
+
except TypeError:
|
| 590 |
+
await client.send_voice(chat_id, file=abs_path, caption=caption)
|
| 591 |
+
sent_success = True
|
| 592 |
+
|
| 593 |
+
# تلاش برای ارسال موزیک
|
| 594 |
+
elif api_file_type == "Music" and hasattr(client, "send_music"):
|
| 595 |
+
try:
|
| 596 |
+
await client.send_music(chat_id, abs_path, caption=caption)
|
| 597 |
+
sent_success = True
|
| 598 |
+
except TypeError:
|
| 599 |
+
await client.send_music(chat_id, music=abs_path, caption=caption)
|
| 600 |
+
sent_success = True
|
| 601 |
+
|
| 602 |
+
# *** سیستم ضد بمب اتم فاز 1 ***
|
| 603 |
+
# اگر هیچکدام از متدهای بالا وجود نداشت یا به هر دلیلی ارسال نشد، حتما به عنوان فایل (سند) بفرست تا معطل نشود
|
| 604 |
+
if not sent_success:
|
| 605 |
+
if hasattr(client, "send_document"):
|
| 606 |
+
try:
|
| 607 |
+
await client.send_document(chat_id, document=abs_path, caption=caption)
|
| 608 |
+
sent_success = True
|
| 609 |
+
except TypeError:
|
| 610 |
+
await client.send_document(chat_id, abs_path, caption=caption)
|
| 611 |
+
sent_success = True
|
| 612 |
+
elif hasattr(client, "send_file"):
|
| 613 |
+
try:
|
| 614 |
+
await client.send_file(chat_id, file=abs_path)
|
| 615 |
+
except TypeError:
|
| 616 |
+
await client.send_file(chat_id, abs_path)
|
| 617 |
+
if caption:
|
| 618 |
+
await send_with_keyboard(client, chat_id, caption, True)
|
| 619 |
+
sent_success = True
|
| 620 |
+
|
| 621 |
+
if sent_success:
|
| 622 |
+
return True
|
| 623 |
+
|
| 624 |
+
except Exception as e:
|
| 625 |
+
err_msg = str(e)[:150]
|
| 626 |
+
error_logs.append(f"Rubpy Send Error: {err_msg}")
|
| 627 |
+
|
| 628 |
+
if "502" in err_msg or "timeout" in err_msg.lower() or "time" in err_msg.lower():
|
| 629 |
+
await asyncio.sleep(4)
|
| 630 |
+
else:
|
| 631 |
+
await asyncio.sleep(2)
|
| 632 |
+
|
| 633 |
+
# --- فاز دوم (سنگر آخر): آپلود دستی (Raw HTTP) در صورت مسدودی کامل روبپای ---
|
| 634 |
for attempt in range(15):
|
| 635 |
try:
|
| 636 |
url_request = f"https://botapi.rubika.ir/v3/{bot_token}/requestSendFile"
|
|
|
|
| 653 |
async with session.post(upload_url, data=form, timeout=300) as up_resp:
|
| 654 |
if up_resp.status != 200:
|
| 655 |
error_logs.append(f"Upload HTTP {up_resp.status}")
|
| 656 |
+
if up_resp.status in[502, 503, 500]:
|
| 657 |
+
await asyncio.sleep(3)
|
| 658 |
+
else:
|
| 659 |
+
await asyncio.sleep(1.5)
|
| 660 |
continue
|
| 661 |
|
| 662 |
+
try:
|
| 663 |
+
up_data = await up_resp.json()
|
| 664 |
except Exception as json_err:
|
| 665 |
error_logs.append(f"JSON Decode Error: {json_err}")
|
| 666 |
await asyncio.sleep(2)
|
| 667 |
continue
|
| 668 |
|
| 669 |
final_file_id = None
|
| 670 |
+
|
| 671 |
+
# *** سیستم ضد بمب اتم فاز 2 ***
|
| 672 |
+
# دور زدن ارور روبیکا: اگر روبیکا به فرمت فایل گیر داد، سریعاً نوع را به File تغییر بده تا بدون بررسی فرمت آپلود کند!
|
| 673 |
+
if up_data.get("status") == "INVALID_INPUT" and "format" in str(up_data).lower():
|
| 674 |
+
api_file_type = "File"
|
| 675 |
+
error_logs.append(f"Format Blocked! Downgrading type to File.")
|
| 676 |
+
await asyncio.sleep(1)
|
| 677 |
+
continue
|
| 678 |
+
|
| 679 |
if up_data.get("status") == "OK" or up_data.get("status_det") == "OK":
|
| 680 |
if "data" in up_data and isinstance(up_data["data"], dict):
|
| 681 |
final_file_id = up_data["data"].get("file_id") or up_data["data"].get("id")
|
| 682 |
+
elif "file_id" in up_data:
|
| 683 |
+
final_file_id = up_data.get("file_id")
|
| 684 |
|
| 685 |
if final_file_id:
|
| 686 |
send_payload = {
|
|
|
|
| 700 |
if s_data.get("status") == "OK": return True
|
| 701 |
else: error_logs.append(f"SendFile API Error: {s_data}")
|
| 702 |
else:
|
| 703 |
+
if up_data.get("status") == "INVALID_INPUT" and api_file_type in ["Voice", "Music"]:
|
| 704 |
+
api_file_type = "File" # دور زدن گیر دادن روبیکا
|
| 705 |
error_logs.append(f"Upload API Error: {up_data}")
|
| 706 |
else: error_logs.append(f"Missing upload_url in Response: {req_data}")
|
| 707 |
else: error_logs.append(f"Request API Error: {req_data}")
|
|
|
|
| 709 |
error_logs.append(f"Raw HTTP Error: {str(e)[:100]}")
|
| 710 |
await asyncio.sleep(2.5)
|
| 711 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 712 |
return "\n".join(error_logs[-6:])
|
| 713 |
|
| 714 |
+
# ==============================================================================
|
| 715 |
+
# 🟢 پارت 12: توابع تغییر صدا و لیست گویندگان
|
| 716 |
+
# ==============================================================================
|
| 717 |
# ==================================================================
|
| 718 |
# لیستهای اولیه ربات
|
| 719 |
# ==================================================================
|
|
|
|
| 890 |
|
| 891 |
if os.path.exists(file_name_mp3): os.remove(file_name_mp3)
|
| 892 |
|
| 893 |
+
# ==============================================================================
|
| 894 |
+
# 🟢 پارت 13: پردازش هوش مصنوعی متنی و تصویری جیمینای و هاگینگفیس
|
| 895 |
+
# ==============================================================================
|
| 896 |
# ==================================================================
|
| 897 |
# سایر توابع پردازشی
|
| 898 |
# ==================================================================
|
|
|
|
| 1056 |
except Exception:
|
| 1057 |
await send_with_keyboard(client, chat_id, "❌ خطایی در ارسال پیام رخ داد.", False)
|
| 1058 |
|
| 1059 |
+
# ==============================================================================
|
| 1060 |
+
# 🟢 پارت 14: ساخت عکس با هوش مصنوعی
|
| 1061 |
+
# ==============================================================================
|
| 1062 |
async def process_image(client, chat_id, prompt, size_choice="1"):
|
| 1063 |
str_chat_id = str(chat_id).replace("`", "").replace("'", "").replace('"', "").strip()
|
| 1064 |
creds = get_user_credits(str_chat_id)
|
|
|
|
| 1188 |
except Exception as e:
|
| 1189 |
await send_with_keyboard(client, chat_id, f"❌ خطا در ذخیره و ارسال عکس:\n{str(e)[:150]}", True)
|
| 1190 |
|
| 1191 |
+
# ==============================================================================
|
| 1192 |
+
# 🟢 پارت 15: توابع ترجمه و ویرایش عکس با هوش مصنوعی (Flux.2)
|
| 1193 |
+
# ==============================================================================
|
| 1194 |
async def translate_text_aloha(prompt_text):
|
| 1195 |
session_hash = ''.join(random.choices(string.ascii_lowercase + string.digits, k=11))
|
| 1196 |
join_url = "https://hamed744-translate-tts-aloha.hf.space/gradio_api/queue/join"
|
|
|
|
| 1298 |
except Exception as e:
|
| 1299 |
await send_with_keyboard(client, chat_id, f"❌ خطا در ذخیره عکس ویرایش شده:\n{str(e)[:150]}", True)
|
| 1300 |
|
| 1301 |
+
# ==============================================================================
|
| 1302 |
+
# 🟢 پارت 16: ساخت صدا از روی متن (تبدیل متن به صدا - TTS)
|
| 1303 |
+
# ==============================================================================
|
| 1304 |
async def process_tts(client, chat_id, user_text, speaker_id, speaker_name):
|
| 1305 |
str_chat_id = str(chat_id).replace("`", "").replace("'", "").replace('"', "").strip()
|
| 1306 |
creds = get_user_credits(str_chat_id)
|
|
|
|
| 1370 |
await send_with_keyboard(client, chat_id, f"❌ سرورها درگیر هستند.\nدلیل: {last_error}", True)
|
| 1371 |
except Exception: traceback.print_exc()
|
| 1372 |
|
| 1373 |
+
# ==============================================================================
|
| 1374 |
+
# 🟢 پارت 17: سیستم ساخت پادکست
|
| 1375 |
+
# ==============================================================================
|
| 1376 |
async def process_podcast(client, chat_id, prompt):
|
| 1377 |
str_chat_id = str(chat_id).replace("`", "").replace("'", "").replace('"', "").strip()
|
| 1378 |
creds = get_user_credits(str_chat_id)
|
|
|
|
| 1476 |
|
| 1477 |
if os.path.exists(file_name_mp3): os.remove(file_name_mp3)
|
| 1478 |
|
| 1479 |
+
# ==============================================================================
|
| 1480 |
+
# 🟢 پارت 18: استخراج متن از صدا (STT) و تحلیل انواع فایل با هوش مصنوعی
|
| 1481 |
+
# ==============================================================================
|
| 1482 |
async def process_stt(client, chat_id, audio_bytes, file_name):
|
| 1483 |
str_chat_id = str(chat_id).replace("`", "").replace("'", "").replace('"', "").strip()
|
| 1484 |
creds = get_user_credits(str_chat_id)
|
|
|
|
| 1623 |
else:
|
| 1624 |
await send_with_keyboard(client, chat_id, "❌ تمامی سرورها شلوغ هستند. لطفاً بعداً امتحان کنید.", True)
|
| 1625 |
|
| 1626 |
+
# ==============================================================================
|
| 1627 |
+
# 🟢 پارت 19: ساخت مقاله و خروجی فایل متنی (Word/PDF)
|
| 1628 |
+
# ==============================================================================
|
| 1629 |
async def process_create_file(client, chat_id, topic):
|
| 1630 |
str_chat_id = str(chat_id).replace("`", "").replace("'", "").replace('"', "").strip()
|
| 1631 |
creds = get_user_credits(str_chat_id)
|
|
|
|
| 1790 |
else:
|
| 1791 |
await send_with_keyboard(client, chat_id, "❌ متأسفانه پس از تلاشهای مکرر، سرور قادر به ساخت و ارسال هیچیک از فایلها نبود و اعتباری از شما کسر نشد.", True)
|
| 1792 |
|
| 1793 |
+
# ==============================================================================
|
| 1794 |
+
# 🟢 پارت 20: بدنه اصلی ربات (Handler)، دستورات مدیریتی، حلقه اصلی و استارت
|
| 1795 |
+
# ==============================================================================
|
| 1796 |
# ==================================================================
|
| 1797 |
# حلقه و مدیریت اصلی ربات
|
| 1798 |
# ==================================================================
|