Files
post_bot/handlers/add_group.py
Andrey K. Choi 9793648ee3
Some checks failed
continuous-integration/drone/push Build is failing
security, audit, fom features
2025-09-06 05:03:45 +09:00

64 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.

from telegram import Update
from telegram.ext import ContextTypes, ConversationHandler, CommandHandler, MessageHandler, filters
from db import AsyncSessionLocal
from models import Group
INPUT_NAME, INPUT_LINK = range(2)
async def add_group_start(update: Update, context: ContextTypes.DEFAULT_TYPE):
if context.user_data is None:
context.user_data = {}
if update.message:
await update.message.reply_text('Введите имя группы:')
return INPUT_NAME
async def input_group_name(update: Update, context: ContextTypes.DEFAULT_TYPE):
if context.user_data is None:
context.user_data = {}
text = update.message.text.strip() if update.message and update.message.text else ''
context.user_data['group_name'] = text
if update.message:
await update.message.reply_text('Теперь отправьте chat_id группы (например, -1001234567890):')
return INPUT_LINK
async def input_group_link(update: Update, context: ContextTypes.DEFAULT_TYPE):
if context.user_data is None:
context.user_data = {}
link = update.message.text.strip() if update.message and update.message.text else ''
if not link.startswith('-100'):
if update.message:
await update.message.reply_text('Ошибка: chat_id группы должен начинаться с -100. Попробуйте снова.')
return INPUT_LINK
context.user_data['group_link'] = link
return await save_group(update, context)
async def save_group(update: Update, context: ContextTypes.DEFAULT_TYPE):
if context.user_data is None:
context.user_data = {}
name = context.user_data.get('group_name')
link = context.user_data.get('group_link')
if not name or not link:
if update.message:
await update.message.reply_text('Ошибка: не указано название или ссылка.')
return ConversationHandler.END
async with AsyncSessionLocal() as session:
group = Group(name=name, link=link)
session.add(group)
await session.commit()
from db import log_action
user_id = update.effective_user.id if update.effective_user else None
log_action(user_id, "add_group", f"name={name}, link={link}")
if update.message:
await update.message.reply_text(f'Группа "{name}" добавлена.')
return ConversationHandler.END
add_group_conv = ConversationHandler(
entry_points=[CommandHandler('add_group', add_group_start)],
states={
INPUT_NAME: [MessageHandler(filters.TEXT & ~filters.COMMAND, input_group_name)],
INPUT_LINK: [MessageHandler(filters.TEXT & ~filters.COMMAND, input_group_link)],
},
fallbacks=[]
)