Files
Touchh/bot/handlers.py

30 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

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.

from telegram import Update
from telegram.ext import ContextTypes
from users.models import User
from hotels.models import Hotel
from asgiref.sync import sync_to_async # Импортируем sync_to_async
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Обработчик команды /start"""
await update.message.reply_text("Привет! Я бот, работающий с Django. Используй /users или /hotels для проверки базы данных.")
async def list_users(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Обработчик команды /users"""
# Выполняем запрос к базе данных через sync_to_async
users = await sync_to_async(list)(User.objects.all()) # Преобразуем QuerySet в список
if users:
user_list = "\n".join([f"{user.id}: {user.username}" for user in users])
await update.message.reply_text(f"Список пользователей:\n{user_list}")
else:
await update.message.reply_text("В базе данных нет пользователей.")
async def list_hotels(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Обработчик команды /hotels"""
# Выполняем запрос к базе данных через sync_to_async
hotels = await sync_to_async(list)(Hotel.objects.all()) # Преобразуем QuerySet в список
if hotels:
hotel_list = "\n".join([f"{hotel.id}: {hotel.name}" for hotel in hotels])
await update.message.reply_text(f"Список отелей:\n{hotel_list}")
else:
await update.message.reply_text("В базе данных нет отелей.")