| import os |
| import telebot |
| import google.generativeai as genai |
| from flask import Flask |
| from threading import Thread |
| import time |
|
|
| |
| app = Flask('') |
|
|
| @app.route('/') |
| def home(): |
| return "البوت يعمل بنجاح وهو مستيقظ 24/7!" |
|
|
| def run_flask(): |
| |
| try: |
| app.run(host='0.0.0.0', port=7860) |
| except Exception as e: |
| print(f"Flask Error: {e}") |
|
|
| def keep_alive(): |
| t = Thread(target=run_flask) |
| t.daemon = True |
| t.start() |
|
|
| |
| TELEGRAM_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN", "").strip() |
| GEMINI_KEY = os.getenv("GEMINI_API_KEY", "").strip() |
|
|
| |
| if not TELEGRAM_TOKEN or ":" not in TELEGRAM_TOKEN: |
| print("خطأ: يرجى التأكد من إضافة TELEGRAM_BOT_TOKEN في Secrets بشكل صحيح.") |
| elif not GEMINI_KEY: |
| print("خطأ: يرجى إضافة GEMINI_API_KEY في Secrets.") |
| else: |
| |
| genai.configure(api_key=GEMINI_KEY) |
| model = genai.GenerativeModel('gemini-pro') |
| |
| |
| bot = telebot.TeleBot(TELEGRAM_TOKEN, threaded=False) |
|
|
| |
| @bot.message_handler(commands=['start']) |
| def welcome(message): |
| bot.reply_to(message, "مرحباً! أنا بوت ذكاء اصطناعي مطور. أرسل لي أي شيء وسأجيبك باستخدام Gemini.") |
|
|
| @bot.message_handler(func=lambda message: True) |
| def handle_message(message): |
| try: |
| response = model.generate_content(message.text) |
| bot.reply_to(message, response.text) |
| except Exception as e: |
| print(f"Gemini Error: {e}") |
| bot.reply_to(message, "عذراً، حدث خطأ بسيط. جرب إرسال الرسالة مرة أخرى.") |
|
|
| |
| if __name__ == "__main__": |
| keep_alive() |
| print("جاري تشغيل النظام...") |
| |
| while True: |
| try: |
| if TELEGRAM_TOKEN and ":" in TELEGRAM_TOKEN: |
| print("تم بدء تشغيل بوت تليجرام...") |
| |
| bot.infinity_polling(timeout=20, long_polling_timeout=10) |
| else: |
| break |
| except Exception as e: |
| print(f"Connection Error: {e}. سأحاول إعادة الاتصال خلال 5 ثوانٍ...") |
| time.sleep(5) |