File size: 4,333 Bytes
b4e5fe0
f37220a
910d767
 
95a621e
f00bd22
01bd9f0
 
8e59a13
 
910d767
8e59a13
910d767
8e59a13
 
f37220a
8e59a13
 
 
 
 
 
 
 
 
 
 
 
 
 
f37220a
8e59a13
 
 
 
 
 
 
f37220a
8e59a13
f37220a
 
 
910d767
8e59a13
910d767
8e59a13
 
 
 
f37220a
910d767
8e59a13
910d767
8e59a13
 
910d767
8e59a13
 
 
910d767
 
8e59a13
910d767
8e59a13
 
 
 
 
 
 
 
 
 
f37220a
 
 
 
8e59a13
910d767
8e59a13
 
 
 
 
 
 
 
 
f37220a
8e59a13
 
 
f37220a
8e59a13
f37220a
8e59a13
 
 
f37220a
8e59a13
 
f37220a
 
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 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)