Update main.py
Browse files
main.py
CHANGED
|
@@ -21,46 +21,46 @@ app = Flask(__name__)
|
|
| 21 |
|
| 22 |
@app.route('/')
|
| 23 |
def home():
|
| 24 |
-
return "ربات یکپارچه آلفا (نسخه پرو +
|
| 25 |
|
| 26 |
def run_flask():
|
| 27 |
app.run(host="0.0.0.0", port=7860)
|
| 28 |
|
| 29 |
|
| 30 |
-
# --- ساختار کیبورد آپدیت شده (ا
|
| 31 |
MAIN_KEYPAD_DICT = {
|
| 32 |
-
"rows":[
|
| 33 |
# ردیف اول
|
| 34 |
{
|
| 35 |
-
"buttons":[
|
| 36 |
-
{"id": "account_btn", "type": "Simple", "button_text": "حساب کاربری 👤"},
|
| 37 |
{"id": "chat_btn", "type": "Simple", "button_text": "چت با هوش مصنوعی 🤖"}
|
| 38 |
]
|
| 39 |
},
|
| 40 |
# ردیف دوم
|
| 41 |
{
|
| 42 |
-
"buttons":[
|
| 43 |
-
{"id": "
|
| 44 |
-
{"id": "
|
| 45 |
]
|
| 46 |
},
|
| 47 |
# ردیف سوم
|
| 48 |
{
|
| 49 |
-
"buttons":[
|
| 50 |
{"id": "podcast_btn", "type": "Simple", "button_text": "ساخت پادکست 🎙️"},
|
| 51 |
{"id": "tts_btn", "type": "Simple", "button_text": "تبدیل متن به صدا🗣️"}
|
| 52 |
]
|
| 53 |
},
|
| 54 |
# ردیف چهارم
|
| 55 |
{
|
| 56 |
-
"buttons":[
|
| 57 |
{"id": "stt_btn", "type": "Simple", "button_text": "فایل صوتی به متن 📝"},
|
| 58 |
{"id": "file_btn", "type": "Simple", "button_text": "تحلیل فایل 📁"}
|
| 59 |
]
|
| 60 |
},
|
| 61 |
# ردیف پنجم
|
| 62 |
{
|
| 63 |
-
"buttons":[
|
|
|
|
| 64 |
{"id": "cancel_btn", "type": "Simple", "button_text": "برگشت♻️"}
|
| 65 |
]
|
| 66 |
}
|
|
@@ -247,7 +247,7 @@ processed_message_ids = set()
|
|
| 247 |
user_last_request_time = {}
|
| 248 |
|
| 249 |
|
| 250 |
-
# --- بخش
|
| 251 |
async def process_image_edit(client, chat_id, image_bytes, prompt):
|
| 252 |
if not HF_TOKENS:
|
| 253 |
await send_with_keyboard(client, chat_id, "❌ توکنهای هاگینگ فیس تنظیم نشدهاند.", False)
|
|
@@ -299,7 +299,7 @@ async def process_image_edit(client, chat_id, image_bytes, prompt):
|
|
| 299 |
await send_with_keyboard(client, chat_id, f"❌ خطا در ذخیره عکس ویرایش شده:\n{str(e)[:150]}", True)
|
| 300 |
|
| 301 |
|
| 302 |
-
# --- پردازشهای قبلی
|
| 303 |
async def process_gemini(client, chat_id, prompt, file_bytes=None, file_name=None):
|
| 304 |
if not GEMINI_KEYS: return await send_with_keyboard(client, chat_id, "❌ کلیدهای API جیمینای تنظیم نشدهاند.", False)
|
| 305 |
proc_msg = await send_with_keyboard(client, chat_id, "🧠 در حال پردازش...", False)
|
|
@@ -638,26 +638,42 @@ else:
|
|
| 638 |
async def main_handler(client, update):
|
| 639 |
try:
|
| 640 |
current_time = time.time()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 641 |
msg_obj = getattr(update, "message", None) or getattr(update, "new_message", None)
|
|
|
|
|
|
|
| 642 |
msg_id = getattr(update, "message_id", None)
|
| 643 |
if not msg_id and msg_obj:
|
| 644 |
msg_id = msg_obj.get("message_id") if isinstance(msg_obj, dict) else getattr(msg_obj, "message_id", None)
|
| 645 |
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
|
|
|
|
|
|
| 649 |
if len(processed_message_ids) > 5000: processed_message_ids.clear()
|
| 650 |
|
| 651 |
-
|
| 652 |
-
if not chat_id: return
|
| 653 |
-
|
| 654 |
-
last_req_time = user_last_request_time.get(chat_id, 0)
|
| 655 |
-
if current_time - last_req_time < 1.0: return
|
| 656 |
-
user_last_request_time[chat_id] = current_time
|
| 657 |
-
|
| 658 |
user_text = getattr(update, "text", "") or getattr(msg_obj, "text", "")
|
| 659 |
user_text_str = str(user_text).strip() if user_text else ""
|
| 660 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 661 |
is_file = False
|
| 662 |
file_name = "unknown_file.jpg"
|
| 663 |
|
|
@@ -683,15 +699,13 @@ else:
|
|
| 683 |
|
| 684 |
if user_text_str in ["/start", "سلام", "لغو", "/cancel", "❌ لغو", "برگشت♻️"]:
|
| 685 |
user_states[chat_id] = {"mode": None, "text": "", "history": [], "file_bytes": None, "file_name": None}
|
| 686 |
-
await send_with_keyboard(client, chat_id, "سلام! به ربات هوشمند آلفا خوش آمدید 🤖\n\nلطفاً برای شروع، یکی از بخشهای زیر را انتخاب کنید:", True)
|
| 687 |
return
|
| 688 |
|
| 689 |
-
# ---
|
| 690 |
if user_text_str in ["/account", "حساب کاربری 👤"]:
|
| 691 |
-
|
| 692 |
-
account_profile = f"""👤 **اطلاعات حساب کاربری**
|
| 693 |
|
| 694 |
-
🔹 **نام:** حامد نوری
|
| 695 |
🔹 **شناسه یکتا:** `{chat_id}`
|
| 696 |
🔹 **وضعیت اشتراک:** 🥉 نسخه رایگان (آزمایشی)
|
| 697 |
|
|
@@ -716,7 +730,6 @@ else:
|
|
| 716 |
await send_with_keyboard(client, chat_id, "🎨 وارد بخش **ساخت عکس** شدید.\nمتن خود را بفرستید:", True)
|
| 717 |
return
|
| 718 |
|
| 719 |
-
# --- بخش جدید: ویرایش عکس ---
|
| 720 |
if user_text_str in ["/edit_image", "ویرایش تصاویر 🪄"]:
|
| 721 |
user_states[chat_id] = {"mode": "image_edit_waiting_for_image", "text": "", "history": [], "file_bytes": None, "file_name": None}
|
| 722 |
await send_with_keyboard(client, chat_id, "🪄 به بخش **ویرایش عکس (Flux.2)** خوش آمدید.\n\nلطفاً ابتدا عکسی که میخواهید ویرایش کنید را بفرستید:", True)
|
|
@@ -763,7 +776,6 @@ else:
|
|
| 763 |
if user_text_str: asyncio.create_task(process_image(client, chat_id, user_text_str))
|
| 764 |
return
|
| 765 |
|
| 766 |
-
# --- مسیریابی بخش ویرایش تصویر ---
|
| 767 |
elif current_mode == "image_edit_waiting_for_image":
|
| 768 |
if not is_file: return await send_with_keyboard(client, chat_id, "⚠️ لطفاً ابتدا یک عکس ارسال کنید.", False)
|
| 769 |
await send_with_keyboard(client, chat_id, "📥 در حال دانلود عکس...", False)
|
|
|
|
| 21 |
|
| 22 |
@app.route('/')
|
| 23 |
def home():
|
| 24 |
+
return "ربات یکپارچه آلفا (نسخه پرو + چت چندرسانهای + پرامپتنویس + پادکست + ویرایش عکس) روشن است! 🚀"
|
| 25 |
|
| 26 |
def run_flask():
|
| 27 |
app.run(host="0.0.0.0", port=7860)
|
| 28 |
|
| 29 |
|
| 30 |
+
# --- ساختار کیبورد آپدیت شده (دقیقاً طبق چیدمان درخواستی شما) ---
|
| 31 |
MAIN_KEYPAD_DICT = {
|
| 32 |
+
"rows": [
|
| 33 |
# ردیف اول
|
| 34 |
{
|
| 35 |
+
"buttons": [
|
|
|
|
| 36 |
{"id": "chat_btn", "type": "Simple", "button_text": "چت با هوش مصنوعی 🤖"}
|
| 37 |
]
|
| 38 |
},
|
| 39 |
# ردیف دوم
|
| 40 |
{
|
| 41 |
+
"buttons": [
|
| 42 |
+
{"id": "img_btn", "type": "Simple", "button_text": "ساخت تصاویر🎨"},
|
| 43 |
+
{"id": "edit_img_btn", "type": "Simple", "button_text": "ویرایش تصاویر 🪄"}
|
| 44 |
]
|
| 45 |
},
|
| 46 |
# ردیف سوم
|
| 47 |
{
|
| 48 |
+
"buttons": [
|
| 49 |
{"id": "podcast_btn", "type": "Simple", "button_text": "ساخت پادکست 🎙️"},
|
| 50 |
{"id": "tts_btn", "type": "Simple", "button_text": "تبدیل متن به صدا🗣️"}
|
| 51 |
]
|
| 52 |
},
|
| 53 |
# ردیف چهارم
|
| 54 |
{
|
| 55 |
+
"buttons": [
|
| 56 |
{"id": "stt_btn", "type": "Simple", "button_text": "فایل صوتی به متن 📝"},
|
| 57 |
{"id": "file_btn", "type": "Simple", "button_text": "تحلیل فایل 📁"}
|
| 58 |
]
|
| 59 |
},
|
| 60 |
# ردیف پنجم
|
| 61 |
{
|
| 62 |
+
"buttons": [
|
| 63 |
+
{"id": "account_btn", "type": "Simple", "button_text": "حساب کاربری 👤"},
|
| 64 |
{"id": "cancel_btn", "type": "Simple", "button_text": "برگشت♻️"}
|
| 65 |
]
|
| 66 |
}
|
|
|
|
| 247 |
user_last_request_time = {}
|
| 248 |
|
| 249 |
|
| 250 |
+
# --- بخش پردازش ویرایش عکس با FLUX.2-dev ---
|
| 251 |
async def process_image_edit(client, chat_id, image_bytes, prompt):
|
| 252 |
if not HF_TOKENS:
|
| 253 |
await send_with_keyboard(client, chat_id, "❌ توکنهای هاگینگ فیس تنظیم نشدهاند.", False)
|
|
|
|
| 299 |
await send_with_keyboard(client, chat_id, f"❌ خطا در ذخیره عکس ویرایش شده:\n{str(e)[:150]}", True)
|
| 300 |
|
| 301 |
|
| 302 |
+
# --- پردازشهای قبلی ---
|
| 303 |
async def process_gemini(client, chat_id, prompt, file_bytes=None, file_name=None):
|
| 304 |
if not GEMINI_KEYS: return await send_with_keyboard(client, chat_id, "❌ کلیدهای API جیمینای تنظیم نشدهاند.", False)
|
| 305 |
proc_msg = await send_with_keyboard(client, chat_id, "🧠 در حال پردازش...", False)
|
|
|
|
| 638 |
async def main_handler(client, update):
|
| 639 |
try:
|
| 640 |
current_time = time.time()
|
| 641 |
+
|
| 642 |
+
# --- 🛡 استخراج دقیق شناسه چت 🛡 ---
|
| 643 |
+
chat_id = None
|
| 644 |
+
if hasattr(update, 'object_guid') and update.object_guid:
|
| 645 |
+
chat_id = update.object_guid
|
| 646 |
+
elif hasattr(update, 'author_guid') and update.author_guid:
|
| 647 |
+
chat_id = update.author_guid
|
| 648 |
+
else:
|
| 649 |
+
chat_id = getattr(update, "chat_id", None)
|
| 650 |
+
|
| 651 |
+
if not chat_id: return
|
| 652 |
+
|
| 653 |
msg_obj = getattr(update, "message", None) or getattr(update, "new_message", None)
|
| 654 |
+
|
| 655 |
+
# استخراج آیدی پیام
|
| 656 |
msg_id = getattr(update, "message_id", None)
|
| 657 |
if not msg_id and msg_obj:
|
| 658 |
msg_id = msg_obj.get("message_id") if isinstance(msg_obj, dict) else getattr(msg_obj, "message_id", None)
|
| 659 |
|
| 660 |
+
# 🛡 کلید ترکیبی ضد تداخل: آیدی چت + آیدی پیام 🛡
|
| 661 |
+
unique_msg_key = f"{chat_id}_{msg_id}" if msg_id else None
|
| 662 |
+
if unique_msg_key:
|
| 663 |
+
if unique_msg_key in processed_message_ids: return
|
| 664 |
+
processed_message_ids.add(unique_msg_key)
|
| 665 |
if len(processed_message_ids) > 5000: processed_message_ids.clear()
|
| 666 |
|
| 667 |
+
# سیستم محدودکننده سرعت کاربر (در صورتی که فرمان start/ نباشد)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 668 |
user_text = getattr(update, "text", "") or getattr(msg_obj, "text", "")
|
| 669 |
user_text_str = str(user_text).strip() if user_text else ""
|
| 670 |
|
| 671 |
+
if user_text_str not in ["/start", "سلام"]:
|
| 672 |
+
last_req_time = user_last_request_time.get(chat_id, 0)
|
| 673 |
+
if current_time - last_req_time < 1.0: return
|
| 674 |
+
user_last_request_time[chat_id] = current_time
|
| 675 |
+
|
| 676 |
+
# تشخیص فایل
|
| 677 |
is_file = False
|
| 678 |
file_name = "unknown_file.jpg"
|
| 679 |
|
|
|
|
| 699 |
|
| 700 |
if user_text_str in ["/start", "سلام", "لغو", "/cancel", "❌ لغو", "برگشت♻️"]:
|
| 701 |
user_states[chat_id] = {"mode": None, "text": "", "history": [], "file_bytes": None, "file_name": None}
|
| 702 |
+
await send_with_keyboard(client, chat_id, "سلام کاربر عزیز! به ربات هوشمند آلفا خوش آمدید 🤖\n\nلطفاً برای شروع، یکی از بخشهای زیر را انتخاب کنید:", True)
|
| 703 |
return
|
| 704 |
|
| 705 |
+
# --- شخصیسازی حساب کاربری با شناسه چت (داینامیک) ---
|
| 706 |
if user_text_str in ["/account", "حساب کاربری 👤"]:
|
| 707 |
+
account_profile = f"""👤 **اطلاعات حساب کاربری شما**
|
|
|
|
| 708 |
|
|
|
|
| 709 |
🔹 **شناسه یکتا:** `{chat_id}`
|
| 710 |
🔹 **وضعیت اشتراک:** 🥉 نسخه رایگان (آزمایشی)
|
| 711 |
|
|
|
|
| 730 |
await send_with_keyboard(client, chat_id, "🎨 وارد بخش **ساخت عکس** شدید.\nمتن خود را بفرستید:", True)
|
| 731 |
return
|
| 732 |
|
|
|
|
| 733 |
if user_text_str in ["/edit_image", "ویرایش تصاویر 🪄"]:
|
| 734 |
user_states[chat_id] = {"mode": "image_edit_waiting_for_image", "text": "", "history": [], "file_bytes": None, "file_name": None}
|
| 735 |
await send_with_keyboard(client, chat_id, "🪄 به بخش **ویرایش عکس (Flux.2)** خوش آمدید.\n\nلطفاً ابتدا عکسی که میخواهید ویرایش کنید را بفرستید:", True)
|
|
|
|
| 776 |
if user_text_str: asyncio.create_task(process_image(client, chat_id, user_text_str))
|
| 777 |
return
|
| 778 |
|
|
|
|
| 779 |
elif current_mode == "image_edit_waiting_for_image":
|
| 780 |
if not is_file: return await send_with_keyboard(client, chat_id, "⚠️ لطفاً ابتدا یک عکس ارسال کنید.", False)
|
| 781 |
await send_with_keyboard(client, chat_id, "📥 در حال دانلود عکس...", False)
|