Files
HavkaTrain/main.py

75 lines
2.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.

import os
import random
from aiogram import Bot, Dispatcher, types, F
from aiogram.filters import Command
from aiogram.types import ReplyKeyboardMarkup, KeyboardButton
from fastapi import FastAPI
from contextlib import asynccontextmanager
# Загрузка токена из переменных окружения
BOT_TOKEN = os.getenv("BOT_TOKEN")
if not BOT_TOKEN:
raise ValueError("Не задан токен бота. Укажите его в переменной окружения BOT_TOKEN")
# Инициализация бота и диспетчера
bot = Bot(token=BOT_TOKEN)
dp = Dispatcher()
# Создаем клавиатуру
keyboard = ReplyKeyboardMarkup(
keyboard=[
[KeyboardButton(text="Завтрак"), KeyboardButton(text="Обед")],
[KeyboardButton(text="Ужин"), KeyboardButton(text="Прочее")]
],
resize_keyboard=True,
input_field_placeholder="Выберите тип блюда"
)
# Функция для получения случайного блюда из файла
def get_random_dish(meal_type: str) -> str:
filename = f"{meal_type.lower()}.txt"
try:
with open(filename, 'r', encoding='utf-8') as file:
dishes = file.read().splitlines()
if dishes:
return random.choice(dishes)
return f"Файл {filename} пуст"
except FileNotFoundError:
return f"Файл с блюдами для {meal_type} не найден"
# Обработчик команды /start
@dp.message(Command("start"))
async def start_handler(message: types.Message):
await message.answer(
"Привет! Я помогу выбрать блюдо. Выбери тип:",
reply_markup=keyboard
)
# Обработчик кнопок
@dp.message(F.text.in_(["Завтрак", "Обед", "Ужин", "Прочее"]))
async def meal_handler(message: types.Message):
meal_type = message.text
dish = get_random_dish(meal_type)
await message.answer(f"📌 Для {meal_type}а рекомендую:\n\n<b>{dish}</b>", parse_mode="HTML")
# Инициализация FastAPI
@asynccontextmanager
async def lifespan(app: FastAPI):
await bot.delete_webhook()
await bot.set_webhook(f"{BASE_URL}/webhook")
yield
await bot.session.close()
app = FastAPI(lifespan=lifespan)
@app.post("/webhook")
async def webhook(update: dict):
telegram_update = types.Update(**update)
await dp.feed_update(bot=bot, update=telegram_update)
@app.get("/")
async def index():
return {"status": "Bot is running"}
# Конфигурация (замените на ваш публичный URL)
BASE_URL = "https://bubu.frps.botforge.biz"