Update main.py
Browse files
main.py
CHANGED
|
@@ -2,6 +2,7 @@ import os
|
|
| 2 |
import threading
|
| 3 |
import random
|
| 4 |
import aiohttp
|
|
|
|
| 5 |
from flask import Flask
|
| 6 |
from rubpy.bot import BotClient, filters
|
| 7 |
|
|
@@ -33,26 +34,39 @@ if not bot_token:
|
|
| 33 |
else:
|
| 34 |
bot = BotClient(bot_token)
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
if len(user_text) > 500:
|
| 48 |
-
await message.reply("⚠️ کاربر گرامی، لطفاً متنی کوتاهتر از ۵۰۰ کاراکتر بفرستید.")
|
| 49 |
-
return
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
worker_url = random.choice(WORKER_URLS)
|
| 57 |
|
| 58 |
# تنظیمات ویس (در اینجا گوینده 'Charon' یا همان 'شهاب' تنظیم شده است)
|
|
@@ -62,29 +76,42 @@ else:
|
|
| 62 |
"use_live_model": True
|
| 63 |
}
|
| 64 |
|
| 65 |
-
# ا
|
| 66 |
async with aiohttp.ClientSession() as session:
|
| 67 |
async with session.post(worker_url, json=payload, timeout=120) as response:
|
| 68 |
if response.status == 200:
|
| 69 |
audio_bytes = await response.read()
|
| 70 |
|
| 71 |
-
#
|
| 72 |
-
file_name = f"audio_{
|
| 73 |
with open(file_name, "wb") as f:
|
| 74 |
f.write(audio_bytes)
|
| 75 |
|
| 76 |
-
# ارسال فایل صوتی
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
# پاک کردن فایل از روی سرور
|
| 80 |
-
os.
|
|
|
|
| 81 |
|
| 82 |
else:
|
| 83 |
-
await
|
| 84 |
|
| 85 |
except Exception as e:
|
| 86 |
-
|
| 87 |
-
|
|
|
|
| 88 |
|
| 89 |
if __name__ == "__main__":
|
| 90 |
threading.Thread(target=run_flask, daemon=True).start()
|
|
|
|
| 2 |
import threading
|
| 3 |
import random
|
| 4 |
import aiohttp
|
| 5 |
+
import traceback
|
| 6 |
from flask import Flask
|
| 7 |
from rubpy.bot import BotClient, filters
|
| 8 |
|
|
|
|
| 34 |
else:
|
| 35 |
bot = BotClient(bot_token)
|
| 36 |
|
| 37 |
+
# استفاده از فیلتر private برای دریافت پیامهای پیوی
|
| 38 |
+
@bot.on_update(filters.private)
|
| 39 |
+
async def handle_messages(client, update):
|
| 40 |
+
try:
|
| 41 |
+
# 1. استخراج امن متن پیام (ضد خطا برای نسخههای مختلف rubpy)
|
| 42 |
+
user_text = ""
|
| 43 |
+
if hasattr(update, "text") and update.text:
|
| 44 |
+
user_text = update.text
|
| 45 |
+
elif hasattr(update, "message") and hasattr(update.message, "text") and update.message.text:
|
| 46 |
+
user_text = update.message.text
|
| 47 |
+
elif hasattr(update, "new_message") and hasattr(update.new_message, "text") and update.new_message.text:
|
| 48 |
+
user_text = update.new_message.text
|
| 49 |
+
|
| 50 |
+
if not user_text:
|
| 51 |
+
return
|
| 52 |
|
| 53 |
+
print(f"پیام جدید دریافت شد: {user_text}")
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
# 2. پاسخ به دستور استارت
|
| 56 |
+
if user_text == "/start" or user_text == "سلام":
|
| 57 |
+
welcome_text = "سلام! 🎙️\nمن ربات هوش مصنوعی تبدیل متن به صدا هستم.\n\nهر متنی که دوست داری رو برام بفرست تا با کیفیت استودیویی برات بخونمش!"
|
| 58 |
+
await update.reply(welcome_text)
|
| 59 |
+
return
|
| 60 |
|
| 61 |
+
# محدودیت طول متن
|
| 62 |
+
if len(user_text) > 500:
|
| 63 |
+
await update.reply("⚠️ کاربر گرامی، لطفاً متنی کوتاهتر از ۵۰۰ کاراکتر بفرستید.")
|
| 64 |
+
return
|
| 65 |
+
|
| 66 |
+
# پیام در حال پردازش
|
| 67 |
+
await update.reply("⏳ در حال ساخت صدا...\n(ممکن است چند ثانیه طول بکشد)")
|
| 68 |
+
|
| 69 |
+
# 3. انتخاب تصادفی یکی از سرورهای هوش مصنوعی
|
| 70 |
worker_url = random.choice(WORKER_URLS)
|
| 71 |
|
| 72 |
# تنظیمات ویس (در اینجا گوینده 'Charon' یا همان 'شهاب' تنظیم شده است)
|
|
|
|
| 76 |
"use_live_model": True
|
| 77 |
}
|
| 78 |
|
| 79 |
+
# 4. اتصال به سرور شما و دریافت صدا
|
| 80 |
async with aiohttp.ClientSession() as session:
|
| 81 |
async with session.post(worker_url, json=payload, timeout=120) as response:
|
| 82 |
if response.status == 200:
|
| 83 |
audio_bytes = await response.read()
|
| 84 |
|
| 85 |
+
# نام فایل رندوم برای جلوگیری از تداخل
|
| 86 |
+
file_name = f"audio_{random.randint(1000, 999999)}.wav"
|
| 87 |
with open(file_name, "wb") as f:
|
| 88 |
f.write(audio_bytes)
|
| 89 |
|
| 90 |
+
# 5. ارسال فایل صوتی ساخته شده
|
| 91 |
+
try:
|
| 92 |
+
# روش اول: استخراج آیدی کاربر و ارسال ویس
|
| 93 |
+
chat_id = getattr(update, "chat_id", None) or getattr(update, "object_guid", None) or getattr(update, "author_guid", None)
|
| 94 |
+
await client.send_voice(chat_id, file_name)
|
| 95 |
+
except Exception as e_send:
|
| 96 |
+
print(f"Send Voice Error: {e_send}")
|
| 97 |
+
try:
|
| 98 |
+
# روش جایگزین در صورتی که روش اول جواب نداد
|
| 99 |
+
await update.reply(file=file_name)
|
| 100 |
+
except Exception as e_reply:
|
| 101 |
+
print(f"Reply File Error: {e_reply}")
|
| 102 |
+
await update.reply("✅ فایل با موفقیت ساخته شد اما ربات اجازه ارسال ویس را ندارد.")
|
| 103 |
|
| 104 |
+
# پاک کردن فایل از روی سرور پس از ارسال
|
| 105 |
+
if os.path.exists(file_name):
|
| 106 |
+
os.remove(file_name)
|
| 107 |
|
| 108 |
else:
|
| 109 |
+
await update.reply("❌ متاسفانه سرور هوش مصنوعی در حال حاضر شلوغ است. لطفاً چند دقیقه دیگر تلاش کنید.")
|
| 110 |
|
| 111 |
except Exception as e:
|
| 112 |
+
# چاپ کامل خطا در لاگهای هاگینگ فیس
|
| 113 |
+
print(f"یک خطای کلی در ربات رخ داد: {e}")
|
| 114 |
+
traceback.print_exc()
|
| 115 |
|
| 116 |
if __name__ == "__main__":
|
| 117 |
threading.Thread(target=run_flask, daemon=True).start()
|