init commit 2
This commit is contained in:
39
bot/bot.py
Normal file
39
bot/bot.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from telegram import Update
|
||||
from telegram.ext import Updater, CommandHandler, CallbackContext
|
||||
from users.models import User, UserConfirmation
|
||||
import uuid
|
||||
|
||||
def start(update: Update, context: CallbackContext):
|
||||
user_id = update.message.from_user.id
|
||||
chat_id = update.message.chat_id
|
||||
|
||||
user, created = User.objects.get_or_create(telegram_id=user_id, defaults={'chat_id': chat_id})
|
||||
if not user.confirmed:
|
||||
confirmation_code = str(uuid.uuid4())
|
||||
UserConfirmation.objects.create(user=user, confirmation_code=confirmation_code)
|
||||
update.message.reply_text(f"Ваш код подтверждения: {confirmation_code}")
|
||||
else:
|
||||
update.message.reply_text("Вы уже зарегистрированы!")
|
||||
|
||||
def confirm(update: Update, context: CallbackContext):
|
||||
user_id = update.message.from_user.id
|
||||
code = ' '.join(context.args)
|
||||
|
||||
try:
|
||||
confirmation = UserConfirmation.objects.get(user__telegram_id=user_id, confirmation_code=code)
|
||||
confirmation.user.confirmed = True
|
||||
confirmation.user.save()
|
||||
confirmation.delete()
|
||||
update.message.reply_text("Регистрация подтверждена!")
|
||||
except UserConfirmation.DoesNotExist:
|
||||
update.message.reply_text("Неверный код подтверждения!")
|
||||
|
||||
def main():
|
||||
updater = Updater("YOUR_TELEGRAM_BOT_TOKEN")
|
||||
dispatcher = updater.dispatcher
|
||||
|
||||
dispatcher.add_handler(CommandHandler("start", start))
|
||||
dispatcher.add_handler(CommandHandler("confirm", confirm))
|
||||
|
||||
updater.start_polling()
|
||||
updater.idle()
|
||||
Reference in New Issue
Block a user