import os from fastapi import APIRouter, Request, HTTPException from app.services.auth import AuthService from telebot import TeleBot, types import asyncio router = APIRouter() auth_service = AuthService() BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN") WEBHOOK_SECRET = os.getenv("TELEGRAM_WEBHOOK_SECRET", "") # опционально bot = TeleBot(BOT_TOKEN, threaded=False) # threaded=False обычно проще в async-сервисах API_URL = os.getenv("API_URL") user_states = {} # ⚠️ см. примечание ниже про хранение @bot.message_handler(commands=['start']) def start(message): if len(message.text.split()) > 1: username = message.text.split()[1] user_states[message.chat.id] = {"username": username} bot.reply_to(message, "📋 Введите код из лаунчера:") else: bot.reply_to(message, "🔑 Введите ваш игровой никнейм:") bot.register_next_step_handler(message, process_username) def process_username(message): user_states[message.chat.id] = {"username": message.text.strip()} bot.reply_to(message, "📋 Теперь введите код из лаунчера:") @bot.message_handler(func=lambda m: m.chat.id in user_states) def verify_code(message): username = user_states[message.chat.id]["username"] code = message.text.strip() try: asyncio.run(auth_service.verify_code(username, code, message.chat.id)) bot.reply_to(message, "✅ Аккаунт подтвержден!") except Exception as e: bot.reply_to(message, "❌ Ошибка: " + str(e)) user_states.pop(message.chat.id, None) # ====== WEBHOOK ENDPOINT ====== @router.post("/telegram/webhook") async def telegram_webhook(request: Request): # простой секрет, чтобы никто кроме Telegram не слал апдейты if WEBHOOK_SECRET: header = request.headers.get("X-Telegram-Bot-Api-Secret-Token") if header != WEBHOOK_SECRET: raise HTTPException(status_code=403, detail="forbidden") update_json = await request.json() update = types.Update.de_json(update_json) bot.process_new_updates([update]) return {"ok": True}