uploaderbot / app.py
neop3's picture
Update app.py
8e59a13 verified
import logging
import os
import time
import asyncio
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
BOT_TOKEN = os.getenv("BOT_TOKEN")
# ═══════════════════════════════════════════════
# 1) Γ–NCE HEALTH CHECK - SENKRON, HEMEN
# ═══════════════════════════════════════════════
from http.server import HTTPServer, BaseHTTPRequestHandler
import threading
class H(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write(b"OK")
def do_HEAD(self):
self.send_response(200)
self.end_headers()
def log_message(self, *a): pass
server = HTTPServer(("0.0.0.0", 7860), H)
t = threading.Thread(target=server.serve_forever, daemon=True)
t.start()
logger.info("βœ… HEALTH SERVER UP ON 7860")
# 3 saniye bekle - HF'nin health check'i gΓΆrmesi iΓ§in
time.sleep(3)
logger.info("βœ… WAITED 3 SEC")
# ═══════════════════════════════════════════════
# 2) TOKEN KONTROL
# ═══════════════════════════════════════════════
if not BOT_TOKEN:
logger.error("❌ NO BOT_TOKEN - sleeping forever")
while True:
time.sleep(3600)
# ═══════════════════════════════════════════════
# 3) TELEGRAM - GECΔ°KMELΔ° IMPORT
# ═══════════════════════════════════════════════
logger.info("Importing telegram...")
from telegram import Update
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
logger.info("βœ… Telegram imported")
# ═══════════════════════════════════════════════
# 4) HANDLERS
# ═══════════════════════════════════════════════
async def cmd_start(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("Γ‡alışıyorum! πŸŽ‰")
async def on_msg(update: Update, context: ContextTypes.DEFAULT_TYPE):
txt = update.message.text[:50] if update.message.text else ""
await update.message.reply_text(f"AldΔ±m: {txt}")
# ═══════════════════════════════════════════════
# 5) ASYNC MAIN
# ═══════════════════════════════════════════════
async def run_bot():
logger.info("Creating application...")
app = (
Application.builder()
.token(BOT_TOKEN)
.connect_timeout(30)
.read_timeout(30)
.build()
)
app.add_handler(CommandHandler("start", cmd_start))
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, on_msg))
logger.info("Starting bot polling...")
# Bloklamayan şekilde çalıştır
async with app:
await app.start()
await app.updater.start_polling(drop_pending_updates=True)
logger.info("βœ… BOT POLLING STARTED")
# Sonsuza kadar çalış
while True:
await asyncio.sleep(3600)
# ═══════════════════════════════════════════════
# 6) ENTRY POINT
# ═══════════════════════════════════════════════
if __name__ == "__main__":
logger.info("Starting async loop...")
try:
asyncio.run(run_bot())
except KeyboardInterrupt:
logger.info("Interrupted")
except Exception as e:
logger.error(f"πŸ’€ FATAL: {e}", exc_info=True)
# Γ–lme, health check devam etsin
while True:
time.sleep(3600)