File size: 3,828 Bytes
b763e52
 
 
 
b2ddb51
b763e52
b2ddb51
b234e6c
b763e52
 
 
b2ddb51
 
 
 
3834c7a
b2ddb51
 
 
 
b763e52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b234e6c
b763e52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2ddb51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b763e52
b2ddb51
 
b763e52
b2ddb51
b763e52
b2ddb51
 
b763e52
b2ddb51
b763e52
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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("<center><h1>🐳 البوت يعمل بنجاح باستخدام Webhooks</h1></center>")
    gr.Markdown("<center>تم حل مشكلة الـ Timeout للأبد.</center>")

# تشغيل Gradio
demo.launch()