Practica2025/bot/handlers.py
2025-12-29 04:26:49 +03:00

85 lines
3.5 KiB
Python
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# bot/handlers.py
from telegram import Update
from telegram.ext import ContextTypes
from django.utils import translation
from django.utils.translation import gettext as _
from .utils import render_keyboard, render_message
from .router import router
# 1. Сценарий: Меню
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
# Установка языка по умолчанию
context.user_data['lang'] = 'ru'
keyboard = render_keyboard('bot/menu.html')
text = _("Добро пожаловать! Выберите действие:")
await update.message.reply_text(text, reply_markup=keyboard)
# Обработчики callback-ов через наш роутер
@router.route('set_lang')
async def set_language_menu(update: Update, context: ContextTypes.DEFAULT_TYPE):
# Показываем меню выбора языка
keyboard = render_keyboard('bot/lang_menu.html')
await update.callback_query.edit_message_text(
_("Выберите язык интерфейса:"),
reply_markup=keyboard
)
@router.route('lang_ru')
async def lang_ru(update: Update, context: ContextTypes.DEFAULT_TYPE):
context.user_data['lang'] = 'ru'
translation.activate('ru')
await show_main_menu(update, _("Язык сменен на Русский"))
@router.route('lang_en')
async def lang_en(update: Update, context: ContextTypes.DEFAULT_TYPE):
context.user_data['lang'] = 'en'
translation.activate('en')
await show_main_menu(update, _("Language changed to English"))
@router.route('lang_fr')
async def lang_fr(update: Update, context: ContextTypes.DEFAULT_TYPE):
context.user_data['lang'] = 'fr'
translation.activate('fr')
await show_main_menu(update, _("Язык сменен на Французский"))
@router.route('lang_de')
async def lang_de(update: Update, context: ContextTypes.DEFAULT_TYPE):
context.user_data['lang'] = 'de'
translation.activate('de')
await show_main_menu(update, _("Язык сменен на Немецкий"))
@router.route('profile')
async def profile(update: Update, context: ContextTypes.DEFAULT_TYPE):
user = update.effective_user
text = _("Ваш профиль:\nID: {id}\nИмя: {name}").format(id=user.id, name=user.first_name)
keyboard = render_keyboard('bot/back.html')
await update.callback_query.edit_message_text(text, reply_markup=keyboard)
@router.route('help')
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
# Текст помощи (можно тоже вынести в HTML, но для простоты тут текст)
text = _(" *Справка*\n\n"
"Это демонстрационный бот на Django.\n"
"Вы можете менять язык интерфейса и смотреть профиль.\n\n"
"Версия: 1.0")
# Используем кнопку "Назад", которая у нас уже есть
keyboard = render_keyboard('bot/back.html')
await update.callback_query.edit_message_text(
text,
reply_markup=keyboard,
parse_mode='Markdown'
)
@router.route('main_menu')
async def back_to_main(update: Update, context: ContextTypes.DEFAULT_TYPE):
await show_main_menu(update, _("Главное меню"))
async def show_main_menu(update, text):
keyboard = render_keyboard('bot/menu.html')
await update.callback_query.edit_message_text(text, reply_markup=keyboard)