Update main.py
Browse files
main.py
CHANGED
|
@@ -172,7 +172,6 @@ def home():
|
|
| 172 |
def run_flask():
|
| 173 |
app.run(host="0.0.0.0", port=7860)
|
| 174 |
|
| 175 |
-
|
| 176 |
# --- ساختار کیبورد استاندارد بله/تلگرام ---
|
| 177 |
def get_main_keyboard():
|
| 178 |
markup = ReplyKeyboardMarkup(resize_keyboard=True)
|
|
@@ -191,7 +190,6 @@ def get_join_keyboard():
|
|
| 191 |
markup.row(KeyboardButton("✅ عضو شدم"))
|
| 192 |
return markup
|
| 193 |
|
| 194 |
-
|
| 195 |
# --- تابع ارسال منحصراً برای کیبورد پایین صفحه ---
|
| 196 |
async def send_with_keyboard(client, chat_id, text, use_keyboard=True):
|
| 197 |
try:
|
|
@@ -203,7 +201,6 @@ async def send_with_keyboard(client, chat_id, text, use_keyboard=True):
|
|
| 203 |
except Exception:
|
| 204 |
return None
|
| 205 |
|
| 206 |
-
|
| 207 |
# --- 🚨 تابع هوشمند دانلود فایل از بله ---
|
| 208 |
async def helper_download_file(client, message):
|
| 209 |
file_id = None
|
|
@@ -225,7 +222,6 @@ async def helper_download_file(client, message):
|
|
| 225 |
downloaded_bytes = await client.download_file(file_info.file_path)
|
| 226 |
return downloaded_bytes
|
| 227 |
|
| 228 |
-
|
| 229 |
# --- تنظیمات کلیدها ---
|
| 230 |
GEMINI_KEYS_STR1 = os.environ.get("GEMINI_API_KEYS1", "")
|
| 231 |
GEMINI_KEYS_STR2 = os.environ.get("GEMINI_API_KEYS2", "")
|
|
@@ -259,9 +255,8 @@ def get_next_gemini_keys(count=100):
|
|
| 259 |
HF_TOKENS_STR = os.environ.get("HF_TOKENS", "")
|
| 260 |
HF_TOKENS =[k.strip() for k in HF_TOKENS_STR.split(",") if k.strip()]
|
| 261 |
|
| 262 |
-
# استفاده از توکن بله
|
| 263 |
-
bot_token = os.environ.get("BALE_TOKEN", "").strip()
|
| 264 |
-
|
| 265 |
|
| 266 |
# --- 🚨 تابع اختصاصی آپلود فایل به بله ---
|
| 267 |
async def helper_upload_file(client, chat_id, file_name, file_type="Image", caption=""):
|
|
@@ -284,7 +279,6 @@ async def helper_upload_file(client, chat_id, file_name, file_type="Image", capt
|
|
| 284 |
except Exception as e:
|
| 285 |
return f"Bale Send Error: {str(e)[:100]}"
|
| 286 |
|
| 287 |
-
|
| 288 |
WORKER_URLS = ["https://opera8-ttspro.hf.space/generate"]
|
| 289 |
|
| 290 |
SPEAKERS = {
|
|
@@ -424,7 +418,6 @@ async def process_gemini(client, chat_id, prompt, file_bytes=None, file_name=Non
|
|
| 424 |
except Exception:
|
| 425 |
await send_with_keyboard(client, chat_id, "❌ خطایی در ارسال پیام رخ داد.", False)
|
| 426 |
|
| 427 |
-
|
| 428 |
# --- ۲. پردازش ساخت عکس ---
|
| 429 |
async def process_image(client, chat_id, prompt):
|
| 430 |
str_chat_id = str(chat_id)
|
|
@@ -606,7 +599,6 @@ async def process_image_edit(client, chat_id, image_bytes, prompt):
|
|
| 606 |
except Exception as e:
|
| 607 |
await send_with_keyboard(client, chat_id, f"❌ خطا در ذخیره عکس ویرایش شده:\n{str(e)[:150]}", True)
|
| 608 |
|
| 609 |
-
|
| 610 |
# --- ۳. پردازش ساخت صدا ---
|
| 611 |
async def process_tts(client, chat_id, user_text, speaker_id, speaker_name):
|
| 612 |
str_chat_id = str(chat_id)
|
|
@@ -662,7 +654,6 @@ async def process_tts(client, chat_id, user_text, speaker_id, speaker_name):
|
|
| 662 |
else: await send_with_keyboard(client, chat_id, f"❌ سرورها درگیر هستند.\nدلیل: {last_error}", True)
|
| 663 |
except Exception: traceback.print_exc()
|
| 664 |
|
| 665 |
-
|
| 666 |
# --- ۳.۵. پردازش پادکست ---
|
| 667 |
async def process_podcast(client, chat_id, prompt):
|
| 668 |
str_chat_id = str(chat_id)
|
|
@@ -754,7 +745,6 @@ async def process_podcast(client, chat_id, prompt):
|
|
| 754 |
|
| 755 |
if os.path.exists(file_name_mp3): os.remove(file_name_mp3)
|
| 756 |
|
| 757 |
-
|
| 758 |
# --- ۴. پردازش تبدیل فایل صوتی/تصویری به متن ---
|
| 759 |
async def process_stt(client, chat_id, audio_bytes, file_name):
|
| 760 |
str_chat_id = str(chat_id)
|
|
@@ -799,7 +789,6 @@ async def process_stt(client, chat_id, audio_bytes, file_name):
|
|
| 799 |
else:
|
| 800 |
await send_with_keyboard(client, chat_id, "❌ سرور شلوغ است فعلا بعدا امتحان کنید.", True)
|
| 801 |
|
| 802 |
-
|
| 803 |
# --- ۵. پردازش تحلیل فایل مستقل ---
|
| 804 |
async def process_file_analysis(client, chat_id, file_bytes, file_name, prompt):
|
| 805 |
str_chat_id = str(chat_id)
|
|
@@ -843,7 +832,6 @@ async def process_file_analysis(client, chat_id, file_bytes, file_name, prompt):
|
|
| 843 |
else:
|
| 844 |
await send_with_keyboard(client, chat_id, "❌ سرور شلوغ است فعلا بعدا امتحان کنید.", True)
|
| 845 |
|
| 846 |
-
|
| 847 |
# --- ۵.۵. پردازش ساخت مقاله و تبدیل به فایل ---
|
| 848 |
async def process_create_file(client, chat_id, topic):
|
| 849 |
str_chat_id = str(chat_id)
|
|
@@ -994,7 +982,6 @@ async def task_file_analysis(client, chat_id, message, file_name):
|
|
| 994 |
await send_with_keyboard(client, chat_id, "✅ فایل با موفقیت دریافت شد.\n\nحالا لطفاً متنی بگویید **چگونه تحلیل شود؟**", False)
|
| 995 |
except Exception as e: await send_with_keyboard(client, chat_id, f"❌ خطا در دریافت فایل!\n{str(e)}", False)
|
| 996 |
|
| 997 |
-
|
| 998 |
# --- راهاندازی و اتصال هندلرها به بله ---
|
| 999 |
bot = AsyncTeleBot(bot_token) if bot_token else None
|
| 1000 |
|
|
@@ -1225,6 +1212,21 @@ if bot:
|
|
| 1225 |
|
| 1226 |
current_mode = user_states[chat_id].get("mode")
|
| 1227 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1228 |
if current_mode is None:
|
| 1229 |
if is_file: pass
|
| 1230 |
elif user_text_str: asyncio.create_task(send_with_keyboard(bot, chat_id, "⚠️ لطفاً ابتدا از کیبورد پایین، بخش مورد نظرتان را انتخاب کنید:", True))
|
|
@@ -1320,4 +1322,4 @@ if __name__ == "__main__":
|
|
| 1320 |
else:
|
| 1321 |
print("ربات آلفا پرو با سیستم اشتراک نامحدود + سپر امنیتی متصل به پیامرسان «بله» روشن شد... 🚀")
|
| 1322 |
# کلید حل مشکل اسپم: skip_pending=True
|
| 1323 |
-
asyncio.run(bot.polling(non_stop=True, skip_pending=True, timeout=20, request_timeout=30))
|
|
|
|
| 172 |
def run_flask():
|
| 173 |
app.run(host="0.0.0.0", port=7860)
|
| 174 |
|
|
|
|
| 175 |
# --- ساختار کیبورد استاندارد بله/تلگرام ---
|
| 176 |
def get_main_keyboard():
|
| 177 |
markup = ReplyKeyboardMarkup(resize_keyboard=True)
|
|
|
|
| 190 |
markup.row(KeyboardButton("✅ عضو شدم"))
|
| 191 |
return markup
|
| 192 |
|
|
|
|
| 193 |
# --- تابع ارسال منحصراً برای کیبورد پایین صفحه ---
|
| 194 |
async def send_with_keyboard(client, chat_id, text, use_keyboard=True):
|
| 195 |
try:
|
|
|
|
| 201 |
except Exception:
|
| 202 |
return None
|
| 203 |
|
|
|
|
| 204 |
# --- 🚨 تابع هوشمند دانلود فایل از بله ---
|
| 205 |
async def helper_download_file(client, message):
|
| 206 |
file_id = None
|
|
|
|
| 222 |
downloaded_bytes = await client.download_file(file_info.file_path)
|
| 223 |
return downloaded_bytes
|
| 224 |
|
|
|
|
| 225 |
# --- تنظیمات کلیدها ---
|
| 226 |
GEMINI_KEYS_STR1 = os.environ.get("GEMINI_API_KEYS1", "")
|
| 227 |
GEMINI_KEYS_STR2 = os.environ.get("GEMINI_API_KEYS2", "")
|
|
|
|
| 255 |
HF_TOKENS_STR = os.environ.get("HF_TOKENS", "")
|
| 256 |
HF_TOKENS =[k.strip() for k in HF_TOKENS_STR.split(",") if k.strip()]
|
| 257 |
|
| 258 |
+
# استفاده از توکن بله تنظیم شده توسط کاربر
|
| 259 |
+
bot_token = os.environ.get("BALE_TOKEN", "2030030646:ioHMqrCsw0PwK5hTg_1Av1rsXZMiu-LETBI").strip()
|
|
|
|
| 260 |
|
| 261 |
# --- 🚨 تابع اختصاصی آپلود فایل به بله ---
|
| 262 |
async def helper_upload_file(client, chat_id, file_name, file_type="Image", caption=""):
|
|
|
|
| 279 |
except Exception as e:
|
| 280 |
return f"Bale Send Error: {str(e)[:100]}"
|
| 281 |
|
|
|
|
| 282 |
WORKER_URLS = ["https://opera8-ttspro.hf.space/generate"]
|
| 283 |
|
| 284 |
SPEAKERS = {
|
|
|
|
| 418 |
except Exception:
|
| 419 |
await send_with_keyboard(client, chat_id, "❌ خطایی در ارسال پیام رخ داد.", False)
|
| 420 |
|
|
|
|
| 421 |
# --- ۲. پردازش ساخت عکس ---
|
| 422 |
async def process_image(client, chat_id, prompt):
|
| 423 |
str_chat_id = str(chat_id)
|
|
|
|
| 599 |
except Exception as e:
|
| 600 |
await send_with_keyboard(client, chat_id, f"❌ خطا در ذخیره عکس ویرایش شده:\n{str(e)[:150]}", True)
|
| 601 |
|
|
|
|
| 602 |
# --- ۳. پردازش ساخت صدا ---
|
| 603 |
async def process_tts(client, chat_id, user_text, speaker_id, speaker_name):
|
| 604 |
str_chat_id = str(chat_id)
|
|
|
|
| 654 |
else: await send_with_keyboard(client, chat_id, f"❌ سرورها درگیر هستند.\nدلیل: {last_error}", True)
|
| 655 |
except Exception: traceback.print_exc()
|
| 656 |
|
|
|
|
| 657 |
# --- ۳.۵. پردازش پادکست ---
|
| 658 |
async def process_podcast(client, chat_id, prompt):
|
| 659 |
str_chat_id = str(chat_id)
|
|
|
|
| 745 |
|
| 746 |
if os.path.exists(file_name_mp3): os.remove(file_name_mp3)
|
| 747 |
|
|
|
|
| 748 |
# --- ۴. پردازش تبدیل فایل صوتی/تصویری به متن ---
|
| 749 |
async def process_stt(client, chat_id, audio_bytes, file_name):
|
| 750 |
str_chat_id = str(chat_id)
|
|
|
|
| 789 |
else:
|
| 790 |
await send_with_keyboard(client, chat_id, "❌ سرور شلوغ است فعلا بعدا امتحان کنید.", True)
|
| 791 |
|
|
|
|
| 792 |
# --- ۵. پردازش تحلیل فایل مستقل ---
|
| 793 |
async def process_file_analysis(client, chat_id, file_bytes, file_name, prompt):
|
| 794 |
str_chat_id = str(chat_id)
|
|
|
|
| 832 |
else:
|
| 833 |
await send_with_keyboard(client, chat_id, "❌ سرور شلوغ است فعلا بعدا امتحان کنید.", True)
|
| 834 |
|
|
|
|
| 835 |
# --- ۵.۵. پردازش ساخت مقاله و تبدیل به فایل ---
|
| 836 |
async def process_create_file(client, chat_id, topic):
|
| 837 |
str_chat_id = str(chat_id)
|
|
|
|
| 982 |
await send_with_keyboard(client, chat_id, "✅ فایل با موفقیت دریافت شد.\n\nحالا لطفاً متنی بگویید **چگونه تحلیل شود؟**", False)
|
| 983 |
except Exception as e: await send_with_keyboard(client, chat_id, f"❌ خطا در دریافت فایل!\n{str(e)}", False)
|
| 984 |
|
|
|
|
| 985 |
# --- راهاندازی و اتصال هندلرها به بله ---
|
| 986 |
bot = AsyncTeleBot(bot_token) if bot_token else None
|
| 987 |
|
|
|
|
| 1212 |
|
| 1213 |
current_mode = user_states[chat_id].get("mode")
|
| 1214 |
|
| 1215 |
+
# --- دریافت درخواست از طریق وب سایت اختصاصی (وب به بله) ---
|
| 1216 |
+
if user_text_str.startswith("/webtts"):
|
| 1217 |
+
parts = user_text_str.split(" ", 2)
|
| 1218 |
+
if len(parts) >= 3:
|
| 1219 |
+
spk_key = parts[1]
|
| 1220 |
+
txt = parts[2]
|
| 1221 |
+
|
| 1222 |
+
if spk_key in SPEAKERS:
|
| 1223 |
+
spk_name, spk_id = SPEAKERS[spk_key]
|
| 1224 |
+
asyncio.create_task(send_with_keyboard(bot, chat_id, f"🌐 درخواست شما از طریق سایت دریافت شد. در حال پردازش...", False))
|
| 1225 |
+
asyncio.create_task(process_tts(bot, chat_id, txt, spk_id, spk_name))
|
| 1226 |
+
else:
|
| 1227 |
+
asyncio.create_task(send_with_keyboard(bot, chat_id, "❌ خطای وب: گوینده یافت نشد.", False))
|
| 1228 |
+
return
|
| 1229 |
+
|
| 1230 |
if current_mode is None:
|
| 1231 |
if is_file: pass
|
| 1232 |
elif user_text_str: asyncio.create_task(send_with_keyboard(bot, chat_id, "⚠️ لطفاً ابتدا از کیبورد پایین، بخش مورد نظرتان را انتخاب کنید:", True))
|
|
|
|
| 1322 |
else:
|
| 1323 |
print("ربات آلفا پرو با سیستم اشتراک نامحدود + سپر امنیتی متصل به پیامرسان «بله» روشن شد... 🚀")
|
| 1324 |
# کلید حل مشکل اسپم: skip_pending=True
|
| 1325 |
+
asyncio.run(bot.polling(non_stop=True, skip_pending=True, timeout=20, request_timeout=30))
|