Opera8 commited on
Commit
c0e40af
·
verified ·
1 Parent(s): 981f66b

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +69 -56
main.py CHANGED
@@ -142,54 +142,78 @@ async def helper_download_file(client, msg_obj):
142
  raise Exception("دانلود ناموفق. لاگ:\n" + "\n".join(errors))
143
 
144
 
145
- # --- 🚨 تابع هوشمند آپلود فایل (نسخه فوق‌پیشرفته + دیباگر) 🚨 ---
146
- async def helper_upload_file(client, chat_id, file_name, file_type="photo", caption=""):
147
- abs_path = os.path.abspath(file_name) # پیدا کردن مسیر قطعی در سرور
148
- error_logs =[]
149
-
150
- # 1. تلاش اول: ارسال پیام با پارامتر فایل
151
- try:
152
- await client.send_message(chat_id, text=caption, file=abs_path)
153
- return True
154
- except Exception as e:
155
- error_logs.append(f"MsgFile: {e}")
156
 
157
- # 2. تلاش دوم: استفاده مستقیم بدون نام پارامتر
158
- try:
159
- if file_type == "photo":
160
- await client.send_photo(chat_id, abs_path, caption=caption)
161
- else:
162
- await client.send_voice(chat_id, abs_path)
163
- return True
164
- except Exception as e:
165
- error_logs.append(f"DirectArg: {e}")
166
 
167
- # 3. تلاش سوم: استفاده با نام پارامتر
168
- try:
169
- if file_type == "photo":
170
- await client.send_photo(chat_id, photo=abs_path, caption=caption)
171
- else:
172
- await client.send_voice(chat_id, voice=abs_path)
173
- return True
174
- except Exception as e:
175
- error_logs.append(f"Kwarg: {e}")
176
 
177
- # 4. تلاش چهارم: ارسال به عنوان سند (Document)
 
 
 
 
 
 
 
 
178
  try:
179
- await client.send_document(chat_id, document=abs_path, caption=caption)
180
- return True
181
- except Exception as e:
182
- error_logs.append(f"Doc: {e}")
183
 
184
- # 5. تلاش پنجم: تبدیل به بایت و ارسال مستقیم حافظه
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
  try:
186
- with open(abs_path, 'rb') as f:
187
- await client.send_message(chat_id, text=caption, file=f)
188
- return True
189
  except Exception as e:
190
- error_logs.append(f"ByteObj: {e}")
191
 
192
- # در صورت شکست، لیست ارورها را خروجی می‌دهد
193
  return "\n".join(error_logs)
194
 
195
 
@@ -219,13 +243,6 @@ SPEAKERS = {
219
  "28": ("مهتاب (زن)", "Kore"), "29": ("سام (مرد)", "Fenrir"), "30": ("لیدا (زن)", "Leda")
220
  }
221
 
222
- # --- تنظیمات کلیدها ---
223
- GEMINI_KEYS_STR = os.environ.get("GEMINI_API_KEYS", "")
224
- GEMINI_KEYS =[k.strip() for k in GEMINI_KEYS_STR.split(",") if k.strip()]
225
-
226
- HF_TOKENS_STR = os.environ.get("HF_TOKENS", "")
227
- HF_TOKENS =[k.strip() for k in HF_TOKENS_STR.split(",") if k.strip()]
228
-
229
  user_states = {}
230
 
231
 
@@ -357,12 +374,10 @@ async def process_image(client, chat_id, prompt):
357
  await asyncio.sleep(1)
358
  caption_text = "🎨 تصویر شما آماده شد!\n(مدل: Z-Image-Turbo)"
359
 
360
- # استفاده از تابع هوشمند برای آپلود
361
- upload_result = await helper_upload_file(client, chat_id, file_name, "photo", caption_text)
362
 
363
- if upload_result is True:
364
- await send_with_keyboard(client, chat_id, "✅ تصویر با موفقیت ارسال شد.", True)
365
- else:
366
  err_msg = str(upload_result)[:800]
367
  await send_with_keyboard(client, chat_id, f"❌ تصویر در سرور ساخته شد، اما روبیکا در آپلود آن ارور داد.\n\n🔴 لاگ ارور برنامه‌نویس:\n`{err_msg}`", True)
368
 
@@ -418,8 +433,8 @@ async def process_tts(client, chat_id, user_text, speaker_id, speaker_name):
418
 
419
  await asyncio.sleep(1)
420
 
421
- # استفاده از تابع هوشمند برای آپلود
422
- upload_result = await helper_upload_file(client, chat_id, file_name, "voice", "")
423
 
424
  if upload_result is True:
425
  await send_with_keyboard(client, chat_id, "✅ صدای شما با موفقیت ارسال شد.", True)
@@ -525,8 +540,6 @@ async def process_file_analysis(client, chat_id, file_bytes, file_name, prompt):
525
 
526
 
527
  # --- تنظیمات ربات روبیکا ---
528
- bot_token = os.environ.get("RUBIKA_AUTH", "").strip()
529
-
530
  if not bot_token:
531
  print("خطا: توکن ربات روبیکا وارد نشده است!")
532
  else:
 
142
  raise Exception("دانلود ناموفق. لاگ:\n" + "\n".join(errors))
143
 
144
 
145
+ # --- تنظیمات کلیدها ---
146
+ GEMINI_KEYS_STR = os.environ.get("GEMINI_API_KEYS", "")
147
+ GEMINI_KEYS =[k.strip() for k in GEMINI_KEYS_STR.split(",") if k.strip()]
 
 
 
 
 
 
 
 
148
 
149
+ HF_TOKENS_STR = os.environ.get("HF_TOKENS", "")
150
+ HF_TOKENS =[k.strip() for k in HF_TOKENS_STR.split(",") if k.strip()]
 
 
 
 
 
 
 
151
 
152
+ # 🔴 تنظیم کردن توکن ربات به صورت سراسری برای متد آپلود 🔴
153
+ bot_token = os.environ.get("RUBIKA_AUTH", "").strip()
 
 
 
 
 
 
 
154
 
155
+
156
+ # --- 🚨 تابع اختصاصی آپلود مستقیم به هسته روبیکا (دور زدن محدودیت‌های Rubpy) 🚨 ---
157
+ async def helper_upload_file(client, chat_id, file_name, file_type="Image", caption=""):
158
+ abs_path = os.path.abspath(file_name)
159
+ error_logs =[]
160
+
161
+ # تنظیم نوع فایل برای API رسمی روبیکا
162
+ api_file_type = "Image" if file_type in ["photo", "Image", "image"] else "Voice" if file_type in["voice", "Voice", "audio"] else "File"
163
+
164
  try:
165
+ # روش تضمینی: استفاده از HTTP Request خام به Bot API روبیکا
166
+ url_request = f"https://botapi.rubika.ir/v3/{bot_token}/requestSendFile"
167
+ url_send = f"https://botapi.rubika.ir/v3/{bot_token}/sendFile"
 
168
 
169
+ async with aiohttp.ClientSession() as session:
170
+ # مرحله 1: درخواست ایجاد فضای آپلود
171
+ async with session.post(url_request, json={"type": api_file_type}, timeout=20) as resp:
172
+ req_data = await resp.json()
173
+
174
+ if req_data.get("status") == "OK":
175
+ upload_url = req_data["data"]["upload_url"]
176
+ file_id = req_data["data"]["file_id"]
177
+
178
+ # مرحله 2: آپلود بایت‌های فایل
179
+ with open(abs_path, "rb") as f:
180
+ form = aiohttp.FormData()
181
+ form.add_field('file', f, filename=os.path.basename(abs_path))
182
+ headers = {"file-id": str(file_id)}
183
+
184
+ async with session.post(upload_url, data=form, headers=headers, timeout=60) as up_resp:
185
+ up_data = await up_resp.json()
186
+
187
+ if up_data.get("status") == "OK":
188
+ # مرحله 3: ارسال فایل آپلود شده به چت کاربر به همراه کیبورد
189
+ send_payload = {
190
+ "chat_id": chat_id,
191
+ "file_id": file_id,
192
+ "text": caption,
193
+ "chat_keypad_type": "New",
194
+ "chat_keypad": MAIN_KEYPAD_DICT
195
+ }
196
+ async with session.post(url_send, json=send_payload, timeout=20) as send_resp:
197
+ s_data = await send_resp.json()
198
+ if s_data.get("status") == "OK":
199
+ return True
200
+ else:
201
+ error_logs.append(f"SendFile API Error: {s_data}")
202
+ else:
203
+ error_logs.append(f"Upload API Error: {up_data}")
204
+ else:
205
+ error_logs.append(f"Request API Error: {req_data}")
206
+ except Exception as e:
207
+ error_logs.append(f"Raw HTTP Exception: {e}")
208
+
209
+ # پشتیبان: اگر API بالا به هر دلیلی محدود شده بود
210
  try:
211
+ if hasattr(client, "send_file"):
212
+ await client.send_file(chat_id, abs_path, caption=caption)
213
+ return True
214
  except Exception as e:
215
+ error_logs.append(f"Rubpy Send_file: {e}")
216
 
 
217
  return "\n".join(error_logs)
218
 
219
 
 
243
  "28": ("مهتاب (زن)", "Kore"), "29": ("سام (مرد)", "Fenrir"), "30": ("لیدا (زن)", "Leda")
244
  }
245
 
 
 
 
 
 
 
 
246
  user_states = {}
247
 
248
 
 
374
  await asyncio.sleep(1)
375
  caption_text = "🎨 تصویر شما آماده شد!\n(مدل: Z-Image-Turbo)"
376
 
377
+ # آپلود مستقیم به سرور اصلی روبیکا
378
+ upload_result = await helper_upload_file(client, chat_id, file_name, "Image", caption_text)
379
 
380
+ if upload_result is not True:
 
 
381
  err_msg = str(upload_result)[:800]
382
  await send_with_keyboard(client, chat_id, f"❌ تصویر در سرور ساخته شد، اما روبیکا در آپلود آن ارور داد.\n\n🔴 لاگ ارور برنامه‌نویس:\n`{err_msg}`", True)
383
 
 
433
 
434
  await asyncio.sleep(1)
435
 
436
+ # آپلود مستقیم به سرور اصلی روبیکا
437
+ upload_result = await helper_upload_file(client, chat_id, file_name, "Voice", "")
438
 
439
  if upload_result is True:
440
  await send_with_keyboard(client, chat_id, "✅ صدای شما با موفقیت ارسال شد.", True)
 
540
 
541
 
542
  # --- تنظیمات ربات روبیکا ---
 
 
543
  if not bot_token:
544
  print("خطا: توکن ربات روبیکا وارد نشده است!")
545
  else: