Files
TG_autoposter/app/handlers/callbacks.py
2025-12-18 05:55:32 +09:00

147 lines
5.2 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, InlineKeyboardMarkup, InlineKeyboardButton
from telegram.ext import ContextTypes, ConversationHandler
from app.database import AsyncSessionLocal
from app.database.repository import GroupRepository, MessageRepository, MessageGroupRepository
from app.utils.keyboards import (
get_main_keyboard, get_back_keyboard, get_message_actions_keyboard,
get_group_actions_keyboard, CallbackType
)
import logging
logger = logging.getLogger(__name__)
# Состояния для ConversationHandler
WAITING_MESSAGE_TEXT = 1
WAITING_MESSAGE_TITLE = 2
WAITING_GROUP_SELECTION = 3
WAITING_FOR_GROUP = 4
async def start_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Главное меню"""
query = update.callback_query
await query.answer()
text = """🤖 <b>Автопостер - Главное меню</b>
Выберите, что вы хотите делать:"""
await query.edit_message_text(
text,
parse_mode='HTML',
reply_markup=get_main_keyboard()
)
async def manage_messages(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Меню управления сообщениями"""
query = update.callback_query
await query.answer()
text = """📨 <b>Управление сообщениями</b>
Выберите действие:"""
keyboard = [
[InlineKeyboardButton(" Новое сообщение", callback_data=CallbackType.CREATE_MESSAGE)],
[InlineKeyboardButton("📜 Список сообщений", callback_data=CallbackType.LIST_MESSAGES)],
[InlineKeyboardButton("⬅️ Назад", callback_data=CallbackType.MAIN_MENU)],
]
await query.edit_message_text(
text,
parse_mode='HTML',
reply_markup=InlineKeyboardMarkup(keyboard)
)
async def manage_groups(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Меню управления группами"""
query = update.callback_query
await query.answer()
text = """👥 <b>Управление группами</b>
Выберите действие:"""
keyboard = [
[InlineKeyboardButton("📜 Список групп", callback_data=CallbackType.LIST_GROUPS)],
[InlineKeyboardButton("⬅️ Назад", callback_data=CallbackType.MAIN_MENU)],
]
await query.edit_message_text(
text,
parse_mode='HTML',
reply_markup=InlineKeyboardMarkup(keyboard)
)
async def list_messages(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Список всех сообщений"""
query = update.callback_query
await query.answer()
async with AsyncSessionLocal() as session:
repo = MessageRepository(session)
messages = await repo.get_all_messages()
if not messages:
text = "📭 Нет сообщений"
keyboard = [[InlineKeyboardButton("⬅️ Назад", callback_data=CallbackType.MANAGE_MESSAGES)]]
else:
text = "📨 <b>Ваши сообщения:</b>\n\n"
keyboard = []
for msg in messages:
status = "" if msg.is_active else ""
text += f"{status} <b>{msg.title}</b> (ID: {msg.id})\n"
keyboard.append([
InlineKeyboardButton(f"📤 {msg.title}", callback_data=f"send_msg_{msg.id}"),
InlineKeyboardButton("🗑️", callback_data=f"delete_msg_{msg.id}")
])
keyboard.append([InlineKeyboardButton("⬅️ Назад", callback_data=CallbackType.MANAGE_MESSAGES)])
await query.edit_message_text(
text,
parse_mode='HTML',
reply_markup=InlineKeyboardMarkup(keyboard)
)
async def list_groups(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Список всех групп"""
query = update.callback_query
await query.answer()
async with AsyncSessionLocal() as session:
repo = GroupRepository(session)
groups = await repo.get_all_active_groups()
if not groups:
text = "👥 Нет групп в базе данных\n\nДобавьте бота в группы - они автоматически появятся здесь."
keyboard = [[InlineKeyboardButton("⬅️ Назад", callback_data=CallbackType.MANAGE_GROUPS)]]
else:
text = "👥 <b>Группы в базе данных:</b>\n\n"
keyboard = []
for group in groups:
status = "" if group.is_active else ""
delay = f"⏱️ {group.slow_mode_delay}s" if group.slow_mode_delay > 0 else "🚀 нет"
text += f"{status} <b>{group.title}</b>\n"
text += f" ID: {group.chat_id}\n"
text += f" {delay}\n\n"
keyboard.append([
InlineKeyboardButton(f"📝 {group.title}", callback_data=f"group_messages_{group.id}"),
InlineKeyboardButton("🗑️", callback_data=f"delete_group_{group.id}")
])
keyboard.append([InlineKeyboardButton("⬅️ Назад", callback_data=CallbackType.MANAGE_GROUPS)])
await query.edit_message_text(
text,
parse_mode='HTML',
reply_markup=InlineKeyboardMarkup(keyboard)
)