Opera8 commited on
Commit
37cda30
·
verified ·
1 Parent(s): 8293167

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +111 -26
main.py CHANGED
@@ -16,16 +16,36 @@ app = Flask(__name__)
16
 
17
  @app.route('/')
18
  def home():
19
- return "ربات یکپارچه آلفا (نسخه کیبورد شیشهای/استاندارد) روشن است! 🚀"
20
 
21
  def run_flask():
22
  app.run(host="0.0.0.0", port=7860)
23
 
24
 
25
  # --- ساختار کیبورد تضمینی روبیکا ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  try:
27
- from rubpy.models import Keypad, KeypadRow, Button
28
- MAIN_KEYPAD = Keypad(
29
  rows=[
30
  KeypadRow(buttons=[
31
  Button(id="chat_btn", type="Simple", button_text="💬 چت"),
@@ -38,21 +58,74 @@ try:
38
  ]
39
  )
40
  except ImportError:
41
- MAIN_KEYPAD = None
42
-
43
-
44
- # --- تابع ارسال کیبورد ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  async def send_with_keyboard(client, chat_id, text, use_keyboard=True):
46
- if not use_keyboard or not MAIN_KEYPAD:
47
  await client.send_message(chat_id, text)
48
  return
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  try:
51
- # استانداردترین پارامتر کتابخانه برای دکمه‌ها
52
- await client.send_message(chat_id, text, reply_markup=MAIN_KEYPAD)
53
- except Exception as e:
54
- print(f"خطای نمایش کیبورد: {e}")
55
- await client.send_message(chat_id, text)
 
 
 
 
56
 
57
 
58
  # --- لیست تمام سرورهای تولید صدای شما ---
@@ -68,7 +141,6 @@ WORKER_URLS =[
68
  "https://hamed744-ttspro9.hf.space/generate",
69
  ]
70
 
71
- # --- دیکشنری ۳۰ گوینده ---
72
  SPEAKERS = {
73
  "1": ("شهاب (مرد)", "Charon"), "2": ("آوا (زن)", "Zephyr"), "3": ("نوید (مرد)", "Achird"),
74
  "4": ("آرمان (مرد)", "Zubenelgenubi"), "5": ("مهسا (زن)", "Vindemiatrix"), "6": ("دانا (مرد)", "Rasalgethi"),
@@ -90,7 +162,6 @@ HF_TOKENS_STR = os.environ.get("HF_TOKENS", "")
90
  HF_TOKENS = [k.strip() for k in HF_TOKENS_STR.split(",") if k.strip()]
91
 
92
  user_states = {}
93
-
94
  FOOTER_MENU = "\n\n➖➖➖➖➖➖\n👇 از دکمه‌های پایین برای تغییر بخش استفاده کنید."
95
 
96
 
@@ -158,11 +229,9 @@ async def process_gemini(client, chat_id, prompt):
158
  await send_with_keyboard(client, chat_id, chunk, use_kb)
159
  await asyncio.sleep(2.5)
160
  except Exception as send_err:
161
- print(f"خطا در ارسال قطعه {idx}: {send_err}")
162
  await asyncio.sleep(2.5)
163
 
164
  except Exception as e:
165
- print(f"خطای سیستم پردازش متن جیمینای: {e}")
166
  await send_with_keyboard(client, chat_id, "❌ خطایی در ارسال پیام رخ داد." + FOOTER_MENU, True)
167
 
168
 
@@ -197,8 +266,8 @@ async def process_image(client, chat_id, prompt):
197
  try:
198
  file_name = f"image_{random.randint(1000, 999999)}.jpg"
199
  rgb_im = generated_image.convert('RGB')
200
-
201
  rgb_im.save(file_name, format="JPEG", quality=100)
 
202
  await asyncio.sleep(1)
203
  caption_text = "🎨 تصویر شما آماده شد!\n(مدل: Z-Image-Turbo)" + FOOTER_MENU
204
 
@@ -243,7 +312,6 @@ async def process_image(client, chat_id, prompt):
243
  os.remove(file_name)
244
 
245
  except Exception as e:
246
- print(f"خطای پردازش تصویر: {e}")
247
  await send_with_keyboard(client, chat_id, "❌ خطایی در فرآیند ذخیره عکس رخ داد." + FOOTER_MENU, True)
248
 
249
 
@@ -337,23 +405,41 @@ else:
337
  @bot.on_update(filters.private)
338
  async def main_handler(client, update):
339
  try:
 
340
  user_text = getattr(update, "text", "") or getattr(getattr(update, "message", None), "text", "") or getattr(getattr(update, "new_message", None), "text", "")
341
- if not user_text: return
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
342
 
343
  chat_id = getattr(update, "chat_id", None) or getattr(update, "author_guid", None) or getattr(update, "object_guid", None)
344
- if not chat_id: return
345
-
346
- user_text_str = str(user_text).strip()
347
 
348
  if chat_id not in user_states:
349
  user_states[chat_id] = {"mode": None, "text": ""}
350
 
351
- # مدیریت کیبورد و کامندهای لغو/شروع
352
  if user_text_str in ["/start", "سلام", "لغو", "/cancel", "❌ لغو"]:
353
  user_states[chat_id] = {"mode": None, "text": ""}
354
  menu = (
355
  "سلام! به ربات هوشمند آلفا خوش آمدید 🤖\n\n"
356
- "لطفاً برای شروع، از دکمه‌های پایین یکی از بخش‌ها را انتخاب کنید:\n\n"
357
  "💬 چت با هوش مصنوعی\n"
358
  "🎨 طراحی عکس\n"
359
  "🎙️ تبدیل متن به صدا"
@@ -428,7 +514,6 @@ else:
428
  return
429
 
430
  except Exception as e:
431
- print(f"خطای کلی سیستم: {e}")
432
  traceback.print_exc()
433
 
434
  if __name__ == "__main__":
 
16
 
17
  @app.route('/')
18
  def home():
19
+ return "ربات یکپارچه آلفا (نسخه کیبورد تهاجمی) روشن است! 🚀"
20
 
21
  def run_flask():
22
  app.run(host="0.0.0.0", port=7860)
23
 
24
 
25
  # --- ساختار کیبورد تضمینی روبیکا ---
26
+ # حالت خام (پشتیبان)
27
+ MAIN_KEYPAD_DICT = {
28
+ "rows": [
29
+ {
30
+ "buttons": [
31
+ {"id": "chat_btn", "type": "Simple", "button_text": "💬 چت"},
32
+ {"id": "img_btn", "type": "Simple", "button_text": "🎨 عکس"}
33
+ ]
34
+ },
35
+ {
36
+ "buttons": [
37
+ {"id": "tts_btn", "type": "Simple", "button_text": "🎙️ صدا"},
38
+ {"id": "cancel_btn", "type": "Simple", "button_text": "❌ لغو"}
39
+ ]
40
+ }
41
+ ]
42
+ }
43
+
44
+ # حالت کلاسیک (برای نسخه‌های جدید rubpy)
45
+ KEYPAD_OBJ = None
46
  try:
47
+ from rubpy.bot.models import Keypad, KeypadRow, Button
48
+ KEYPAD_OBJ = Keypad(
49
  rows=[
50
  KeypadRow(buttons=[
51
  Button(id="chat_btn", type="Simple", button_text="💬 چت"),
 
58
  ]
59
  )
60
  except ImportError:
61
+ try:
62
+ from rubpy.models import Keypad, KeypadRow, Button
63
+ KEYPAD_OBJ = Keypad(
64
+ rows=[
65
+ KeypadRow(buttons=[
66
+ Button(id="chat_btn", type="Simple", button_text="💬 چت"),
67
+ Button(id="img_btn", type="Simple", button_text="🎨 عکس")
68
+ ]),
69
+ KeypadRow(buttons=[
70
+ Button(id="tts_btn", type="Simple", button_text="🎙️ صدا"),
71
+ Button(id="cancel_btn", type="Simple", button_text="❌ لغو")
72
+ ])
73
+ ]
74
+ )
75
+ except Exception:
76
+ pass
77
+
78
+
79
+ # --- تابع ارسال کیبورد هوشمند (امتحان تمام متدها) ---
80
  async def send_with_keyboard(client, chat_id, text, use_keyboard=True):
81
+ if not use_keyboard:
82
  await client.send_message(chat_id, text)
83
  return
84
 
85
+ # لیست تمام قالب‌های ممکن برای ارسال
86
+ payloads = []
87
+ if KEYPAD_OBJ:
88
+ payloads.append(KEYPAD_OBJ)
89
+ payloads.append(MAIN_KEYPAD_DICT)
90
+
91
+ for payload in payloads:
92
+ # تست ১: کیبورد شیشه‌ای (قوی‌ترین روش روبیکا)
93
+ try:
94
+ await client.send_message(chat_id, text, inline_keypad=payload)
95
+ return
96
+ except Exception:
97
+ pass
98
+
99
+ # تست ۲: کیبورد پایین صفحه
100
+ try:
101
+ await client.send_message(chat_id, text, chat_keypad=payload, chat_keypad_type="New")
102
+ return
103
+ except Exception:
104
+ pass
105
+
106
+ # تست ۳: ریپلای مارکاپ عمومی
107
+ try:
108
+ await client.send_message(chat_id, text, reply_markup=payload)
109
+ return
110
+ except Exception:
111
+ pass
112
+
113
+ # اگر به هیچ وجه نسخه کتابخانه شما از کیبورد پشتیبانی نکرد!
114
+ await client.send_message(chat_id, text)
115
+
116
+
117
+ # --- تابع کمکی برای استخراج آیدی کلیک ---
118
+ def extract_button_id(update):
119
  try:
120
+ if hasattr(update, 'aux_data') and update.aux_data:
121
+ return getattr(update.aux_data, 'button_id', None)
122
+ if hasattr(update, 'new_message') and update.new_message and hasattr(update.new_message, 'aux_data') and update.new_message.aux_data:
123
+ return getattr(update.new_message.aux_data, 'button_id', None)
124
+ if hasattr(update, 'message') and update.message and hasattr(update.message, 'aux_data') and update.message.aux_data:
125
+ return getattr(update.message.aux_data, 'button_id', None)
126
+ except Exception:
127
+ pass
128
+ return None
129
 
130
 
131
  # --- لیست تمام سرورهای تولید صدای شما ---
 
141
  "https://hamed744-ttspro9.hf.space/generate",
142
  ]
143
 
 
144
  SPEAKERS = {
145
  "1": ("شهاب (مرد)", "Charon"), "2": ("آوا (زن)", "Zephyr"), "3": ("نوید (مرد)", "Achird"),
146
  "4": ("آرمان (مرد)", "Zubenelgenubi"), "5": ("مهسا (زن)", "Vindemiatrix"), "6": ("دانا (مرد)", "Rasalgethi"),
 
162
  HF_TOKENS = [k.strip() for k in HF_TOKENS_STR.split(",") if k.strip()]
163
 
164
  user_states = {}
 
165
  FOOTER_MENU = "\n\n➖➖➖➖➖➖\n👇 از دکمه‌های پایین برای تغییر بخش استفاده کنید."
166
 
167
 
 
229
  await send_with_keyboard(client, chat_id, chunk, use_kb)
230
  await asyncio.sleep(2.5)
231
  except Exception as send_err:
 
232
  await asyncio.sleep(2.5)
233
 
234
  except Exception as e:
 
235
  await send_with_keyboard(client, chat_id, "❌ خطایی در ارسال پیام رخ داد." + FOOTER_MENU, True)
236
 
237
 
 
266
  try:
267
  file_name = f"image_{random.randint(1000, 999999)}.jpg"
268
  rgb_im = generated_image.convert('RGB')
 
269
  rgb_im.save(file_name, format="JPEG", quality=100)
270
+
271
  await asyncio.sleep(1)
272
  caption_text = "🎨 تصویر شما آماده شد!\n(مدل: Z-Image-Turbo)" + FOOTER_MENU
273
 
 
312
  os.remove(file_name)
313
 
314
  except Exception as e:
 
315
  await send_with_keyboard(client, chat_id, "❌ خطایی در فرآیند ذخیره عکس رخ داد." + FOOTER_MENU, True)
316
 
317
 
 
405
  @bot.on_update(filters.private)
406
  async def main_handler(client, update):
407
  try:
408
+ # ۱. استخراج متن پیام کاربر
409
  user_text = getattr(update, "text", "") or getattr(getattr(update, "message", None), "text", "") or getattr(getattr(update, "new_message", None), "text", "")
410
+
411
+ # ۲. استخراج آیدی دکمه در صورتی که کاربر روی کیبورد شیشه‌ای کلیک کرده باشد
412
+ button_id = extract_button_id(update)
413
+
414
+ user_text_str = str(user_text).strip() if user_text else ""
415
+
416
+ # ۳. اگر دکمه فشرده شد، متن به طور خودکار تنظیم شود!
417
+ if button_id == "chat_btn":
418
+ user_text_str = "/chat"
419
+ elif button_id == "img_btn":
420
+ user_text_str = "/image"
421
+ elif button_id == "tts_btn":
422
+ user_text_str = "/tts"
423
+ elif button_id == "cancel_btn":
424
+ user_text_str = "/cancel"
425
+
426
+ # جلوگیری از پردازش رویدادهای خالی
427
+ if not user_text_str and not button_id:
428
+ return
429
 
430
  chat_id = getattr(update, "chat_id", None) or getattr(update, "author_guid", None) or getattr(update, "object_guid", None)
431
+ if not chat_id:
432
+ return
 
433
 
434
  if chat_id not in user_states:
435
  user_states[chat_id] = {"mode": None, "text": ""}
436
 
437
+ # مدیریت کامندهای لغو/شروع
438
  if user_text_str in ["/start", "سلام", "لغو", "/cancel", "❌ لغو"]:
439
  user_states[chat_id] = {"mode": None, "text": ""}
440
  menu = (
441
  "سلام! به ربات هوشمند آلفا خوش آمدید 🤖\n\n"
442
+ "لطفاً برای شروع، از دکمه‌های شیشه‌ای زیر یکی از بخش‌ها را انتخاب کنید:\n\n"
443
  "💬 چت با هوش مصنوعی\n"
444
  "🎨 طراحی عکس\n"
445
  "🎙️ تبدیل متن به صدا"
 
514
  return
515
 
516
  except Exception as e:
 
517
  traceback.print_exc()
518
 
519
  if __name__ == "__main__":