import telebot import time import random import threading import os import gradio as gr from flask import Flask, request, abort TOKEN = "8238023143:AAHLMQFTBSjdusQvvsat9FNdCx968neiaj0" bot = telebot.TeleBot(TOKEN) # ============================================================== # ⚠️ هام جداً: ضع رابط الـ Space الخاص بك هنا # ابحث في إعدادات הـ Space أو انسخ الرابط الذي يظهر فيه الـ App # مثال: "https://abo3mr-df.hf.space" WEBHOOK_URL = "https://abo3mr-df.hf.space" # ============================================================== app = Flask(__name__) azkar = [ "اللهم صل وسلم على نبينا محمد 🤍", "اللهم صل على سيدنا محمد وعلى آله وصحبه وسلم ✨", "صلوا على من بكى شوقاً لرؤيتنا، اللهم صل وسلم على سيدنا محمد 🌸", "اللهم صل على محمد وآل محمد 📿", "عليه أفضل الصلاة والسلام 🌿" ] active_users = set() def send_azkar(chat_id): while chat_id in active_users: try: msg = random.choice(azkar) bot.send_message(chat_id, msg) time.sleep(3) except Exception as e: print(f"Error sending to {chat_id}: {e}") time.sleep(5) @bot.message_handler(commands=['start']) def start_message(message): chat_id = message.chat.id if chat_id not in active_users: active_users.add(chat_id) bot.reply_to(message, "تم تفعيل التذكير بالصلاة على النبي كل 3 ثواني 🤍\nلإيقاف التذكير ارسل /stop") threading.Thread(target=send_azkar, args=(chat_id,), daemon=True).start() else: bot.reply_to(message, "التذكير شغال بالفعل! 🤍") @bot.message_handler(commands=['stop']) def stop_message(message): chat_id = message.chat.id if chat_id in active_users: active_users.remove(chat_id) bot.reply_to(message, "تم إيقاف التذكير 🛑") else: bot.reply_to(message, "التذكير متوقف أصلاً.") # مسار Flask لاستقبال البيانات من تيليجرام @app.route(f"/{TOKEN}", methods=['POST']) def webhook(): if request.headers.get('content-type') == 'application/json': json_string = request.get_data().decode('utf-8') update = telebot.types.Update.de_json(json_string) bot.process_new_updates([update]) return '', 200 else: abort(403) # مسار رئيسي وهمي لـ Flask @app.route("/") def index(): return "Bot is running with Webhooks!" # وظيفة لإعداد الـ Webhook وتشغيل سيرفر Flask def run_flask_app(): # إزالة أي ويب هوك قديم أولاً bot.remove_webhook() time.sleep(1) # ربط تيليجرام بسيرفرك الجديد # إذا لم تقم بتعديل WEBHOOK_URL بشكل صحيح، فلن يعمل البوت webhook_target = f"{WEBHOOK_URL.rstrip('/')}/{TOKEN}" success = bot.set_webhook(url=webhook_target) if success: print(f"Webhook set successfully to {webhook_target}") else: print("Failed to set webhook!") # تشغيل Flask على بورت 8080 (Gradio سيعمل على 7860) app.run(host="0.0.0.0", port=8080) # تشغيل السيرفر في الخلفية threading.Thread(target=run_flask_app, daemon=True).start() # واجهة Gradio لإبقاء الـ Space نشطاً with gr.Blocks() as demo: gr.Markdown("

🐳 البوت يعمل بنجاح باستخدام Webhooks

") gr.Markdown("
تم حل مشكلة الـ Timeout للأبد.
") # تشغيل Gradio demo.launch()