async refactor & docker deploy environment
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
@@ -1,34 +1,45 @@
|
||||
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
|
||||
from telegram.ext import CommandHandler, CallbackQueryHandler, ConversationHandler, ContextTypes
|
||||
from db import SessionLocal
|
||||
from db import AsyncSessionLocal
|
||||
from models import Group, Button
|
||||
|
||||
SELECT_GROUP, MANAGE_BUTTONS = range(2)
|
||||
|
||||
async def group_buttons_start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
session = SessionLocal()
|
||||
groups = session.query(Group).all()
|
||||
session.close()
|
||||
keyboard = [[InlineKeyboardButton(g.name, callback_data=str(g.id))] for g in groups]
|
||||
await update.message.reply_text(
|
||||
"Выберите группу для настройки клавиатуры:",
|
||||
reply_markup=InlineKeyboardMarkup(keyboard)
|
||||
)
|
||||
return SELECT_GROUP
|
||||
session = AsyncSessionLocal()
|
||||
try:
|
||||
groups_result = await session.execute(Group.__table__.select())
|
||||
groups = groups_result.scalars().all()
|
||||
keyboard = [[InlineKeyboardButton(f'{getattr(g, "name", str(g.name))}', callback_data=str(getattr(g, "id", str(g.id))))] for g in groups]
|
||||
if update.message:
|
||||
await update.message.reply_text(
|
||||
"Выберите группу для настройки клавиатуры:",
|
||||
reply_markup=InlineKeyboardMarkup(keyboard)
|
||||
)
|
||||
return SELECT_GROUP
|
||||
return ConversationHandler.END
|
||||
finally:
|
||||
await session.close()
|
||||
|
||||
async def select_group(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
query = update.callback_query
|
||||
await query.answer()
|
||||
group_id = int(query.data)
|
||||
context.user_data['group_id'] = group_id
|
||||
session = SessionLocal()
|
||||
buttons = session.query(Button).filter_by(group_id=group_id).all()
|
||||
session.close()
|
||||
text = "Кнопки этой группы:\n"
|
||||
for b in buttons:
|
||||
text += f"- {b.name}: {b.url}\n"
|
||||
text += "\nДобавить новую кнопку: /add_button\nУдалить: /del_button <название>"
|
||||
await query.edit_message_text(text)
|
||||
if query and query.data:
|
||||
await query.answer()
|
||||
group_id = int(query.data)
|
||||
if context.user_data is None:
|
||||
context.user_data = {}
|
||||
context.user_data['group_id'] = group_id
|
||||
session = AsyncSessionLocal()
|
||||
try:
|
||||
buttons_result = await session.execute(Button.__table__.select().where(Button.group_id == group_id))
|
||||
buttons = buttons_result.scalars().all()
|
||||
text = "Кнопки этой группы:\n"
|
||||
for b in buttons:
|
||||
text += f"- {b.name}: {b.url}\n"
|
||||
text += "\nДобавить новую кнопку: /add_button\nУдалить: /del_button <название>"
|
||||
await query.edit_message_text(text)
|
||||
finally:
|
||||
await session.close()
|
||||
return ConversationHandler.END
|
||||
|
||||
group_buttons_conv = ConversationHandler(
|
||||
@@ -37,4 +48,4 @@ group_buttons_conv = ConversationHandler(
|
||||
SELECT_GROUP: [CallbackQueryHandler(select_group)],
|
||||
},
|
||||
fallbacks=[]
|
||||
)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user