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,47 @@
|
||||
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 Channel, Button
|
||||
|
||||
SELECT_CHANNEL, MANAGE_BUTTONS = range(2)
|
||||
|
||||
async def channel_buttons_start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
session = SessionLocal()
|
||||
channels = session.query(Channel).all()
|
||||
session.close()
|
||||
keyboard = [[InlineKeyboardButton(c.name, callback_data=str(c.id))] for c in channels]
|
||||
await update.message.reply_text(
|
||||
"Выберите канал для настройки клавиатуры:",
|
||||
reply_markup=InlineKeyboardMarkup(keyboard)
|
||||
)
|
||||
return SELECT_CHANNEL
|
||||
from sqlalchemy import select
|
||||
session = AsyncSessionLocal()
|
||||
try:
|
||||
channels_result = await session.execute(select(Channel))
|
||||
channels = channels_result.scalars().all()
|
||||
keyboard = [[InlineKeyboardButton(f'{getattr(c, "name", str(c.name))}', callback_data=str(getattr(c, "id", str(c.id))))] for c in channels]
|
||||
if update.message:
|
||||
await update.message.reply_text(
|
||||
"Выберите канал для настройки клавиатуры:",
|
||||
reply_markup=InlineKeyboardMarkup(keyboard)
|
||||
)
|
||||
return SELECT_CHANNEL
|
||||
return ConversationHandler.END
|
||||
finally:
|
||||
await session.close()
|
||||
|
||||
async def select_channel(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
query = update.callback_query
|
||||
await query.answer()
|
||||
channel_id = int(query.data)
|
||||
context.user_data['channel_id'] = channel_id
|
||||
session = SessionLocal()
|
||||
buttons = session.query(Button).filter_by(channel_id=channel_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()
|
||||
channel_id = int(query.data)
|
||||
if context.user_data is None:
|
||||
context.user_data = {}
|
||||
context.user_data['channel_id'] = channel_id
|
||||
from sqlalchemy import select
|
||||
session = AsyncSessionLocal()
|
||||
try:
|
||||
buttons_result = await session.execute(select(Button).where(Button.channel_id == channel_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
|
||||
|
||||
channel_buttons_conv = ConversationHandler(
|
||||
@@ -37,4 +50,4 @@ channel_buttons_conv = ConversationHandler(
|
||||
SELECT_CHANNEL: [CallbackQueryHandler(select_channel)],
|
||||
},
|
||||
fallbacks=[]
|
||||
)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user