Download app.py from ABO3MR/df: direct link, hf CLI and curl.
- Browser
- Download file 3.83 kB
-
https://huggingface.co/spaces/ABO3MR/df/resolve/main/app.py
- Command line
-
hf download hf://spaces/ABO3MR/df/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ABO3MR/df/resolve/main/app.py
3.83 kB
| 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) | |
| 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, "التذكير شغال بالفعل! 🤍") | |
| 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 لاستقبال البيانات من تيليجرام | |
| 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 | |
| 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("<center><h1>🐳 البوت يعمل بنجاح باستخدام Webhooks</h1></center>") | |
| gr.Markdown("<center>تم حل مشكلة الـ Timeout للأبد.</center>") | |
| # تشغيل Gradio | |
| demo.launch() |