Opera8 commited on
Commit
76d1865
·
verified ·
1 Parent(s): 0cf1ae0

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +90 -4
main.py CHANGED
@@ -311,16 +311,99 @@ def to_english_digits(text):
311
  return str(text).translate(translation_table)
312
 
313
  # ==============================================================================
314
- # 🟢 پارت 6: تنظیمات وب‌سرور (Flask) و توابع کمکی فایل‌ها
315
  # ==============================================================================
316
- # --- تنظیمات وب سرور ---
 
 
 
 
317
  app = Flask(__name__)
318
 
319
- @app.route('/')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
320
  def home():
321
- return "ربات یکپارچه آلفا (نسخه پرو + مدیریت اشتراک نامحدود + دعوت دوستان + سیستم تغییر صدا) روشن است! 🚀"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
322
 
323
  def run_flask():
 
 
 
 
324
  app.run(host="0.0.0.0", port=7860, threaded=True)
325
 
326
  # --- توابع کمکی ---
@@ -337,12 +420,15 @@ def sync_read_file(file_name):
337
  return f.read()
338
 
339
  def sync_combine_audio(current_audio, new_bytes):
 
 
340
  audio_segment = AudioSegment.from_file(io.BytesIO(new_bytes))
341
  return current_audio + audio_segment
342
 
343
  def sync_export_audio(audio_obj, file_name):
344
  audio_obj.export(file_name, format="mp3")
345
 
 
346
  # ==============================================================================
347
  # 🟢 پارت 7: کیبوردها (دکمه���های شیشه‌ای / منوها)
348
  # ==============================================================================
 
311
  return str(text).translate(translation_table)
312
 
313
  # ==============================================================================
314
+ # 🟢 پارت 6: تنظیمات وب‌سرور (Flask)، دریافت وب‌هوک و توابع کمکی فایل‌ها
315
  # ==============================================================================
316
+ import requests
317
+ import asyncio
318
+ from flask import Flask, request, jsonify
319
+
320
+ # --- تنظیمات وب سرور و وب‌هوک ---
321
  app = Flask(__name__)
322
 
323
+ # آدرس وب‌هوک (اگر از Cloudflare Worker استفاده کردید، آدرس آن را اینجا بگذارید)
324
+ WEBHOOK_URL = "https://opera8-sadarubikabot.hf.space/"
325
+
326
+ def set_webhook_on_rubika():
327
+ bot_token = os.environ.get("RUBIKA_AUTH", "").strip()
328
+ if not bot_token:
329
+ print("❌ توکن روبیکا یافت نشد!")
330
+ return
331
+
332
+ url = f"https://botapi.rubika.ir/v3/{bot_token}/updateBotEndpoints"
333
+
334
+ payload_update = {"url": WEBHOOK_URL, "type": "ReceiveUpdate"}
335
+ payload_inline = {"url": WEBHOOK_URL, "type": "ReceiveInlineMessage"}
336
+
337
+ try:
338
+ r1 = requests.post(url, json=payload_update, timeout=15)
339
+ print("🟢 Webhook Setup (ReceiveUpdate):", r1.json())
340
+
341
+ r2 = requests.post(url, json=payload_inline, timeout=15)
342
+ print("🟢 Webhook Setup (InlineMessage):", r2.json())
343
+ except Exception as e:
344
+ print("❌ Webhook Setup Failed:", e)
345
+
346
+ class UpdateWrapper:
347
+ def __init__(self, d):
348
+ self._d = d
349
+ for k, v in d.items():
350
+ if isinstance(v, dict):
351
+ setattr(self, k, UpdateWrapper(v))
352
+ elif isinstance(v, list):
353
+ setattr(self, k, [UpdateWrapper(i) if isinstance(i, dict) else i for i in v])
354
+ else:
355
+ setattr(self, k, v)
356
+
357
+ # 🟢 جادوی تطبیق متغیرهای وب‌هوک با استانداردهای ربات شما (رفع باگ خوانده نشدن پیام‌ها)
358
+ if hasattr(self, 'new_message'):
359
+ self.message = self.new_message
360
+
361
+ if hasattr(self, 'message') and isinstance(self.message, UpdateWrapper):
362
+ if hasattr(self.message, 'sender_id') and not hasattr(self.message, 'author_object_guid'):
363
+ self.message.author_object_guid = self.message.sender_id
364
+
365
+ if hasattr(self, 'chat_id') and not hasattr(self, 'object_guid'):
366
+ self.object_guid = self.chat_id
367
+ self.author_guid = self.chat_id
368
+
369
+ def to_dict(self):
370
+ return self._d
371
+
372
+ def get(self, key, default=None):
373
+ return getattr(self, key, default)
374
+
375
+ @app.route('/', methods=['GET', 'POST'])
376
  def home():
377
+ if request.method == 'GET':
378
+ return "ربات یکپارچه آلفا (نسخه پرو + وب‌هوک روبیکا) روشن است! 🚀"
379
+
380
+ if request.method == 'POST':
381
+ try:
382
+ data = request.json
383
+ if data:
384
+ inner_data = data
385
+ if "update" in data:
386
+ inner_data = data["update"]
387
+ elif "inline_message" in data:
388
+ inner_data = data["inline_message"]
389
+
390
+ wrapped_update = UpdateWrapper(inner_data)
391
+
392
+ # تسک به سرعت به حلقه اصلی پاس داده می‌شود تا سرور منتظر نماند
393
+ if 'loop' in globals() and 'main_handler' in globals():
394
+ asyncio.run_coroutine_threadsafe(main_handler(bot, wrapped_update), loop)
395
+
396
+ except Exception as e:
397
+ print("❌ Error in webhook processing:", e)
398
+
399
+ # همیشه و در کسری از ثانیه 200 OK برمی‌گردانیم تا روبیکا ارتباط را قطع نکند
400
+ return jsonify({"status": "ok"})
401
 
402
  def run_flask():
403
+ set_webhook_on_rubika()
404
+ import logging
405
+ log = logging.getLogger('werkzeug')
406
+ log.setLevel(logging.ERROR)
407
  app.run(host="0.0.0.0", port=7860, threaded=True)
408
 
409
  # --- توابع کمکی ---
 
420
  return f.read()
421
 
422
  def sync_combine_audio(current_audio, new_bytes):
423
+ from pydub import AudioSegment
424
+ import io
425
  audio_segment = AudioSegment.from_file(io.BytesIO(new_bytes))
426
  return current_audio + audio_segment
427
 
428
  def sync_export_audio(audio_obj, file_name):
429
  audio_obj.export(file_name, format="mp3")
430
 
431
+
432
  # ==============================================================================
433
  # 🟢 پارت 7: کیبوردها (دکمه���های شیشه‌ای / منوها)
434
  # ==============================================================================