add: verify code in telegram
All checks were successful
Build and Deploy / deploy (push) Successful in 41s
All checks were successful
Build and Deploy / deploy (push) Successful in 41s
This commit is contained in:
46
telegram_bot.py
Normal file
46
telegram_bot.py
Normal file
@ -0,0 +1,46 @@
|
||||
from telebot import TeleBot
|
||||
import httpx
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
bot = TeleBot(os.getenv("TELEGRAM_BOT_TOKEN"))
|
||||
API_URL = os.getenv("API_URL")
|
||||
|
||||
user_states = {} # {"chat_id": {"username": "DIKER0K"}}
|
||||
|
||||
@bot.message_handler(commands=['start'])
|
||||
def start(message):
|
||||
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()
|
||||
print(username, code, message.chat.id)
|
||||
|
||||
try:
|
||||
response = httpx.post(
|
||||
f"{API_URL}/auth/verify_code",
|
||||
json={"username": username, "code": code, "telegram_chat_id": message.chat.id}, # JSON-сериализация автоматически
|
||||
headers={"Content-Type": "application/json"} # Необязательно, httpx добавляет сам
|
||||
)
|
||||
print(response.json())
|
||||
if response.status_code == 200:
|
||||
bot.reply_to(message, "✅ Аккаунт подтвержден!")
|
||||
else:
|
||||
bot.reply_to(message, f"❌ Ошибка: {response.json().get('detail')}")
|
||||
except Exception:
|
||||
print(API_URL)
|
||||
bot.reply_to(message, "⚠️ Сервер недоступен")
|
||||
|
||||
del user_states[message.chat.id]
|
||||
|
||||
if __name__ == "__main__":
|
||||
bot.polling(none_stop=True)
|
Reference in New Issue
Block a user