import telebot import json import os import threading import time import random import logging from flask import Flask # إعداد السجلات logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') # ============ خادم الويب لضمان عمل البوت 24 ساعة ============ app = Flask('') @app.route('/') def home(): return "Bot is running and alive!" def run_web(): # Hugging Face Spaces تستخدم المنفذ 7860 افتراضياً app.run(host='0.0.0.0', port=7860) # تشغيل السيرفر في الخلفية threading.Thread(target=run_web, daemon=True).start() # ============ الإعدادات ============ TOKEN = os.getenv("TOKEN", "8615712682:AAFYFQM7NYxVhAuKdA-4XcK6o_irjX32UzA") if not TOKEN or TOKEN == "1234567890:ABCdefGHIjklMNOpqrSTUvwxYZ_example": logging.error("❌ لم يتم العثور على التوكن! أضف TOKEN في Environment Variables") exit(1) DATA_FILE = "user_data.json" # ============ المحتوى ============ MESSAGES = [ {"text": "﴿ إِنَّ مَعَ الْعُسْرِ يُسْرًا ﴾\n\n[سورة الشرح: 6]", "source": "قرآن كريم"}, {"text": "﴿ وَقُل رَّبِّ زِدْنِي عِلْمًا ﴾\n\n[سورة طه: 114]", "source": "قرآن كريم"}, {"text": "﴿ أَلَا بِذِكْرِ اللَّهِ تَطْمَئِنُّ الْقُلُوبُ ﴾\n\n[سورة الرعد: 28]", "source": "قرآن كريم"}, {"text": "﴿ وَمَن يَتَوَكَّلْ عَلَى اللَّهِ فَهُوَ حَسْبُهُ ﴾\n\n[سورة الطلاق: 3]", "source": "قرآن كريم"}, {"text": "﴿ فَاذْكُرُونِي أَذْكُرْكُمْ وَاشْكُرُوا لِي وَلَا تَكْفُرُونِ ﴾\n\n[سورة البقرة: 152]", "source": "قرآن كريم"}, {"text": "اللَّهُمَّ إِنِّي أَسْأَلُكَ الْهُدَى وَالتُّقَى وَالْعَفَافَ وَالْغِنَى", "source": "دعاء"}, {"text": "اللَّهُمَّ أَعِنِّي عَلَى ذِكْرِكَ وَشُكْرِكَ وَحُسْنِ عِبَادَتِكَ", "source": "دعاء"}, {"text": "رَبَّنَا آتِنَا فِي الدُّنْيَا حَسَنَةً وَفِي الآخِرَةِ حَسَنَةً وَقِنَا عَذَابَ النَّارِ", "source": "دعاء"}, {"text": "اللَّهُمَّ صَلِّ وَسَلِّمْ عَلَى نَبِيِّنَا مُحَمَّدٍ", "source": "صلاة على النبي"}, {"text": "سُبْحَانَ اللَّهِ وَبِحَمْدِهِ، سُبْحَانَ اللَّهِ الْعَظِيمِ", "source": "ذكر"}, {"text": "لَا إِلَهَ إِلَّا اللَّهُ وَحْدَهُ لَا شَرِيكَ لَهُ، لَهُ الْمُلْكُ وَلَهُ الْحَمْدُ وَهُوَ عَلَى كُلِّ شَيْءٍ قَدِيرٌ", "source": "ذكر"}, {"text": "أَسْتَغْفِرُ اللَّهَ الْعَظِيمَ وَأَتُوبُ إِلَيْهِ", "source": "استغفار"}, ] INTERVAL = 900 # 15 دقيقة bot = telebot.TeleBot(TOKEN) # ============ حفظ/تحميل المجموعات ============ def load_groups() -> list: if os.path.exists(DATA_FILE): try: with open(DATA_FILE, "r", encoding="utf-8") as f: return json.load(f) except json.JSONDecodeError: return [] return [] def save_groups(groups: list): try: with open(DATA_FILE, "w", encoding="utf-8") as f: json.dump(groups, f, ensure_ascii=False, indent=4) except Exception as e: logging.error(f"خطأ في حفظ البيانات: {e}") def add_group(chat_id: int, title: str): groups = load_groups() for g in groups: if g["chat_id"] == chat_id: return groups.append({"chat_id": chat_id, "title": title}) save_groups(groups) logging.info(f"✅ تمت إضافة المجموعة: {title} ({chat_id})") def remove_group(chat_id: int): groups = load_groups() groups = [g for g in groups if g["chat_id"] != chat_id] save_groups(groups) logging.info(f"🗑️ تمت إزالة المجموعة: {chat_id}") # ============ الإرسال التلقائي كل 15 دقيقة ============ def auto_send_loop(): time.sleep(10) while True: logging.info("⏰ بدء دورة الإرسال التلقائي...") groups = load_groups() logging.info(f"عدد المجموعات: {len(groups)}") for g in groups: chat_id = g["chat_id"] title = g.get("title", "مجموعة") msg = random.choice(MESSAGES) try: text = f"{msg['text']}\n\n— {msg['source']} 🌙" bot.send_message(chat_id=chat_id, text=text) logging.info(f"✅ تم الإرسال إلى {title}") except Exception as e: logging.error(f"❌ فشل الإرسال إلى {title}: {e}") logging.info(f"😴 الانتظار {INTERVAL} ثانية (15 دقيقة)...") time.sleep(INTERVAL) threading.Thread(target=auto_send_loop, daemon=True).start() # ============ معالجات تيليجرام ============ @bot.message_handler(commands=['start']) def start(message): if message.chat.type == "private": bot.reply_to( message, "🤖 بوت الأدعية والقرآن\n\n" "أضفني إلى مجموعتك وسأرسل آية أو دعاء كل 15 دقيقة تلقائياً.\n\n" "شكراً لاستخدامك البوت." ) @bot.message_handler(content_types=['new_chat_members']) def on_new_chat_members(message): """عند إضافة البوت لمجموعة جديدة، يسجلها تلقائياً.""" for member in message.new_chat_members: if member.id == bot.get_me().id: add_group(message.chat.id, message.chat.title or "مجموعة") try: bot.send_message( message.chat.id, "🤖 أهلاً بكم!\n\nسأرسل لكم آية أو دعاء كل 15 دقيقة تلقائياً بإذن الله.\n\n" "— البوت أجر لي ولكم 🌙" ) except Exception as e: logging.error(f"فشل إرسال رسالة الترحيب: {e}") @bot.message_handler(content_types=['left_chat_member']) def on_left_chat_member(message): """عند إزالة البوت من المجموعة، يحذفها من القائمة.""" if message.left_chat_member.id == bot.get_me().id: remove_group(message.chat.id) @bot.message_handler(commands=['stop']) def stop_cmd(message): """أمر /stop داخل المجموعة لإيقاف الإرسال لهذي المجموعة.""" if message.chat.type in ["group", "supergroup"]: try: member = bot.get_chat_member(message.chat.id, message.from_user.id) if member.status not in ["administrator", "creator"]: return remove_group(message.chat.id) bot.reply_to(message, "🛑 تم إيقاف الإرسال في هذي المجموعة.") except Exception as e: logging.error(f"خطأ في /stop: {e}") @bot.message_handler(commands=['resume']) def resume_cmd(message): """أمر /resume داخل المجموعة لاستئناف الإرسال.""" if message.chat.type in ["group", "supergroup"]: try: member = bot.get_chat_member(message.chat.id, message.from_user.id) if member.status not in ["administrator", "creator"]: return add_group(message.chat.id, message.chat.title or "مجموعة") bot.reply_to(message, "✅ تم تشغيل الإرسال من جديد.") except Exception as e: logging.error(f"خطأ في /resume: {e}") if __name__ == "__main__": if not os.path.exists(DATA_FILE): save_groups([]) logging.info("🚀 بدء تشغيل البوت...") while True: try: bot.polling(none_stop=True, interval=0, timeout=20) except Exception as e: logging.error(f"⚠️ خطأ: {e}") time.sleep(5) logging.info("إعادة تشغيل البوت...")