Update main.py
Browse files
main.py
CHANGED
|
@@ -18,13 +18,13 @@ app = Flask(__name__)
|
|
| 18 |
|
| 19 |
@app.route('/')
|
| 20 |
def home():
|
| 21 |
-
return "ربات یکپارچه آلفا (نسخه پرو + د
|
| 22 |
|
| 23 |
def run_flask():
|
| 24 |
app.run(host="0.0.0.0", port=7860)
|
| 25 |
|
| 26 |
|
| 27 |
-
# --- ساختار ک
|
| 28 |
MAIN_KEYPAD_DICT = {
|
| 29 |
"rows": [
|
| 30 |
{
|
|
@@ -62,7 +62,7 @@ MAIN_KEYPAD_DICT = {
|
|
| 62 |
}
|
| 63 |
|
| 64 |
|
| 65 |
-
# --- تابع ارسال کیبورد پایین صفحه ---
|
| 66 |
async def send_with_keyboard(client, chat_id, text, use_keyboard=True):
|
| 67 |
if not use_keyboard:
|
| 68 |
msg = await client.send_message(chat_id, text)
|
|
@@ -88,64 +88,73 @@ async def send_with_keyboard(client, chat_id, text, use_keyboard=True):
|
|
| 88 |
return msg
|
| 89 |
|
| 90 |
|
| 91 |
-
# --- 🚨 تابع هوشمند
|
| 92 |
async def helper_download_file(client, msg_obj):
|
| 93 |
errors = []
|
| 94 |
|
| 95 |
-
# استخراج آیدی فایل
|
| 96 |
file_id = None
|
|
|
|
| 97 |
for attr in ['file', 'file_inline', 'photo', 'voice', 'audio', 'document', 'video']:
|
| 98 |
val = getattr(msg_obj, attr, None)
|
| 99 |
if val:
|
|
|
|
| 100 |
if hasattr(val, 'file_id'): file_id = val.file_id
|
| 101 |
elif isinstance(val, dict) and 'file_id' in val: file_id = val['file_id']
|
| 102 |
if file_id: break
|
| 103 |
|
| 104 |
-
#
|
| 105 |
-
|
| 106 |
-
if hasattr(client, "download"):
|
| 107 |
-
res = await client.download(msg_obj)
|
| 108 |
-
if isinstance(res, bytes): return res
|
| 109 |
-
if isinstance(res, str) and os.path.exists(res):
|
| 110 |
-
with open(res, 'rb') as f: data = f.read()
|
| 111 |
-
os.remove(res)
|
| 112 |
-
return data
|
| 113 |
-
else:
|
| 114 |
-
errors.append("BotClient has no 'download' attribute")
|
| 115 |
-
except Exception as e:
|
| 116 |
-
errors.append(f"client.download error: {e}")
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
if
|
| 124 |
-
with open(
|
| 125 |
-
os.remove(
|
| 126 |
return data
|
| 127 |
-
|
| 128 |
-
errors.append("
|
| 129 |
-
except Exception as e:
|
| 130 |
-
errors.append(f"msg_obj.download error: {e}")
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
with open(res, 'rb') as f: data = f.read()
|
| 140 |
-
os.remove(res)
|
| 141 |
return data
|
| 142 |
-
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
-
|
|
|
|
| 146 |
|
| 147 |
|
| 148 |
-
# --- لیست سرورهای تولید صدا ---
|
| 149 |
WORKER_URLS =[
|
| 150 |
"https://hamed744-ttspro.hf.space/generate",
|
| 151 |
"https://hamed744-ttspro2.hf.space/generate",
|
|
@@ -482,7 +491,7 @@ async def process_file_analysis(client, chat_id, file_bytes, file_name, prompt):
|
|
| 482 |
if final_answer:
|
| 483 |
await send_with_keyboard(client, chat_id, f"💡 **نتیجه تحلیل:**\n\n{final_answer}", True)
|
| 484 |
else:
|
| 485 |
-
await send_with_keyboard(client, chat_id, "❌ در حال حاضر پاسخی برای این فایل دریافت نشد.", True)
|
| 486 |
|
| 487 |
|
| 488 |
# --- تنظیمات ربات روبیکا ---
|
|
@@ -621,7 +630,7 @@ else:
|
|
| 621 |
asyncio.create_task(process_stt(client, chat_id, audio_bytes))
|
| 622 |
except Exception as dl_err:
|
| 623 |
err_trace = traceback.format_exc()
|
| 624 |
-
await send_with_keyboard(client, chat_id, f"❌ خطا در دانلود فایل!\n\n🔴 لاگ برنامهنویس:\n`{err_trace[-
|
| 625 |
return
|
| 626 |
|
| 627 |
# --- بخش تحلیل فایل ---
|
|
@@ -639,7 +648,7 @@ else:
|
|
| 639 |
await send_with_keyboard(client, chat_id, "✅ فایل با موفقیت دریافت شد.\n\nحالا لطفاً به صورت متنی بگویید **چگونه تحلیل شود؟**", False)
|
| 640 |
except Exception as dl_err:
|
| 641 |
err_trace = traceback.format_exc()
|
| 642 |
-
await send_with_keyboard(client, chat_id, f"❌ خطا در دریافت فایل!\n\n🔴 لاگ برنامهنویس:\n`{err_trace[-
|
| 643 |
return
|
| 644 |
|
| 645 |
elif current_mode == "file_waiting_for_prompt":
|
|
|
|
| 18 |
|
| 19 |
@app.route('/')
|
| 20 |
def home():
|
| 21 |
+
return "ربات یکپارچه آلفا (نسخه پرو + دانلود فایل ۱۰۰٪ تضمینی) روشن است! 🚀"
|
| 22 |
|
| 23 |
def run_flask():
|
| 24 |
app.run(host="0.0.0.0", port=7860)
|
| 25 |
|
| 26 |
|
| 27 |
+
# --- ساختار کیبورد پایین صفحه (Chat Keypad) ---
|
| 28 |
MAIN_KEYPAD_DICT = {
|
| 29 |
"rows": [
|
| 30 |
{
|
|
|
|
| 62 |
}
|
| 63 |
|
| 64 |
|
| 65 |
+
# --- تابع ارسال منحصراً برای کیبورد پایین صفحه ---
|
| 66 |
async def send_with_keyboard(client, chat_id, text, use_keyboard=True):
|
| 67 |
if not use_keyboard:
|
| 68 |
msg = await client.send_message(chat_id, text)
|
|
|
|
| 88 |
return msg
|
| 89 |
|
| 90 |
|
| 91 |
+
# --- 🚨 تابع هوشمند دانلود فایل (آپدیت و رفع باگ مسیر ذخیره) 🚨 ---
|
| 92 |
async def helper_download_file(client, msg_obj):
|
| 93 |
errors = []
|
| 94 |
|
|
|
|
| 95 |
file_id = None
|
| 96 |
+
file_obj = None
|
| 97 |
for attr in ['file', 'file_inline', 'photo', 'voice', 'audio', 'document', 'video']:
|
| 98 |
val = getattr(msg_obj, attr, None)
|
| 99 |
if val:
|
| 100 |
+
file_obj = val
|
| 101 |
if hasattr(val, 'file_id'): file_id = val.file_id
|
| 102 |
elif isinstance(val, dict) and 'file_id' in val: file_id = val['file_id']
|
| 103 |
if file_id: break
|
| 104 |
|
| 105 |
+
# ساخت یک نام رندوم تا کتابخانه rubpy ارور NoneType برای نام فایل ندهد
|
| 106 |
+
temp_name = f"temp_dl_{random.randint(1000, 999999)}.tmp"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
+
if hasattr(client, "download_file"):
|
| 109 |
+
|
| 110 |
+
# تلاش ۱: ارسال مستقیم آبجکت پیام و نام فایل
|
| 111 |
+
try:
|
| 112 |
+
await client.download_file(msg_obj, temp_name)
|
| 113 |
+
if os.path.exists(temp_name):
|
| 114 |
+
with open(temp_name, 'rb') as f: data = f.read()
|
| 115 |
+
os.remove(temp_name)
|
| 116 |
return data
|
| 117 |
+
except Exception as e:
|
| 118 |
+
errors.append(f"تلاش ۱ (msg_obj): {e}")
|
|
|
|
|
|
|
| 119 |
|
| 120 |
+
# تلاش ۲: ارسال آبجکت فایل اختصاصی
|
| 121 |
+
if file_obj:
|
| 122 |
+
try:
|
| 123 |
+
await client.download_file(file_obj, temp_name)
|
| 124 |
+
if os.path.exists(temp_name):
|
| 125 |
+
with open(temp_name, 'rb') as f: data = f.read()
|
| 126 |
+
os.remove(temp_name)
|
|
|
|
|
|
|
| 127 |
return data
|
| 128 |
+
except Exception as e:
|
| 129 |
+
errors.append(f"تلاش ۲ (file_obj): {e}")
|
| 130 |
+
|
| 131 |
+
# تلاش ۳: ارسال آیدی با آرگومان file_name
|
| 132 |
+
if file_id:
|
| 133 |
+
try:
|
| 134 |
+
await client.download_file(file_id, file_name=temp_name)
|
| 135 |
+
if os.path.exists(temp_name):
|
| 136 |
+
with open(temp_name, 'rb') as f: data = f.read()
|
| 137 |
+
os.remove(temp_name)
|
| 138 |
+
return data
|
| 139 |
+
except Exception as e:
|
| 140 |
+
errors.append(f"تلاش ۳ (file_id, file_name): {e}")
|
| 141 |
+
|
| 142 |
+
# تلاش ۴: ارسال آیدی ��ا آرگومان save_as
|
| 143 |
+
if file_id:
|
| 144 |
+
try:
|
| 145 |
+
await client.download_file(file_id, save_as=temp_name)
|
| 146 |
+
if os.path.exists(temp_name):
|
| 147 |
+
with open(temp_name, 'rb') as f: data = f.read()
|
| 148 |
+
os.remove(temp_name)
|
| 149 |
+
return data
|
| 150 |
+
except Exception as e:
|
| 151 |
+
errors.append(f"تلاش ۴ (file_id, save_as): {e}")
|
| 152 |
|
| 153 |
+
# در صورت شکست تمام حالتها
|
| 154 |
+
raise Exception("دانلود ناموفق. لاگ:\n" + "\n".join(errors))
|
| 155 |
|
| 156 |
|
| 157 |
+
# --- لیست تمام سرورهای تولید صدای شما ---
|
| 158 |
WORKER_URLS =[
|
| 159 |
"https://hamed744-ttspro.hf.space/generate",
|
| 160 |
"https://hamed744-ttspro2.hf.space/generate",
|
|
|
|
| 491 |
if final_answer:
|
| 492 |
await send_with_keyboard(client, chat_id, f"💡 **نتیجه تحلیل:**\n\n{final_answer}", True)
|
| 493 |
else:
|
| 494 |
+
await send_with_keyboard(client, chat_id, "❌ در حال حاضر پاسخی برای این فایل دریافت نشد (شاید فرمت فایل پشتیبانی نمیشود).", True)
|
| 495 |
|
| 496 |
|
| 497 |
# --- تنظیمات ربات روبیکا ---
|
|
|
|
| 630 |
asyncio.create_task(process_stt(client, chat_id, audio_bytes))
|
| 631 |
except Exception as dl_err:
|
| 632 |
err_trace = traceback.format_exc()
|
| 633 |
+
await send_with_keyboard(client, chat_id, f"❌ خطا در دانلود فایل!\n\n🔴 لاگ برنامهنویس:\n`{err_trace[-300:]}`\n{str(dl_err)}", False)
|
| 634 |
return
|
| 635 |
|
| 636 |
# --- بخش تحلیل فایل ---
|
|
|
|
| 648 |
await send_with_keyboard(client, chat_id, "✅ فایل با موفقیت دریافت شد.\n\nحالا لطفاً به صورت متنی بگویید **چگونه تحلیل شود؟**", False)
|
| 649 |
except Exception as dl_err:
|
| 650 |
err_trace = traceback.format_exc()
|
| 651 |
+
await send_with_keyboard(client, chat_id, f"❌ خطا در دریافت فایل!\n\n🔴 لاگ برنامهنویس:\n`{err_trace[-300:]}`\n{str(dl_err)}", False)
|
| 652 |
return
|
| 653 |
|
| 654 |
elif current_mode == "file_waiting_for_prompt":
|