main commit. bot added, admin is functional
This commit is contained in:
29
bot/handlers.py
Normal file
29
bot/handlers.py
Normal file
@@ -0,0 +1,29 @@
|
||||
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("В базе данных нет отелей.")
|
||||
Reference in New Issue
Block a user