Files
duty-teller/handlers/commands.py
Nikolay Tatarinov d60a4fdf3f Enhance Telegram bot with database integration and API features
- Added SQLite database support with Alembic for migrations.
- Implemented FastAPI for HTTP API to manage duties.
- Updated configuration to include database URL and HTTP port.
- Created entrypoint script for Docker to handle migrations and permissions.
- Expanded command handlers to register users and display duties.
- Developed a web application for calendar display of duties.
- Included necessary Pydantic schemas and SQLAlchemy models for data handling.
- Updated requirements.txt to include new dependencies for FastAPI and SQLAlchemy.
2026-02-17 12:51:01 +03:00

62 lines
2.1 KiB
Python

"""Command handlers: /start, /help; /start registers user and shows Calendar button."""
import asyncio
import config
from telegram import Update, WebAppInfo, KeyboardButton, ReplyKeyboardMarkup
from telegram.ext import CommandHandler, ContextTypes
from db.session import get_session
from db.repository import get_or_create_user
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if not update.message:
return
user = update.effective_user
if not user:
return
full_name = " ".join(filter(None, [user.first_name or "", user.last_name or ""])).strip() or "User"
telegram_user_id = user.id
username = user.username
first_name = user.first_name
last_name = user.last_name
def do_get_or_create() -> None:
session = get_session(config.DATABASE_URL)
try:
get_or_create_user(
session,
telegram_user_id=telegram_user_id,
full_name=full_name,
username=username,
first_name=first_name,
last_name=last_name,
)
finally:
session.close()
await asyncio.get_event_loop().run_in_executor(None, do_get_or_create)
text = "Привет! Я бот календаря дежурств. Используй /help для списка команд."
if config.MINI_APP_BASE_URL:
keyboard = ReplyKeyboardMarkup(
[[KeyboardButton("📅 Календарь", web_app=WebAppInfo(url=config.MINI_APP_BASE_URL + "/app/"))]],
resize_keyboard=True,
)
await update.message.reply_text(text, reply_markup=keyboard)
else:
await update.message.reply_text(text)
async def help_cmd(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if update.message:
await update.message.reply_text(
"Доступные команды:\n"
"/start — Начать и открыть календарь\n"
"/help — Показать эту справку"
)
start_handler = CommandHandler("start", start)
help_handler = CommandHandler("help", help_cmd)