S
S
SergeyYatsenko2022-02-09 17:42:32
Python
SergeyYatsenko, 2022-02-09 17:42:32

Is it possible to reduce this code?

@dp.message_handler(text_startswith=['text', 'Text', 'TEXT'])
async def file1(message: types.Message):
    for ret in cur.execute("SELECT id FROM names WHERE name = 'test1'").fetchall():
        await bot.send_photo(message.from_user.id, ret[0],\
                             reply_markup=InlineKeyboardMarkup(row_width=1).add(InlineKeyboardButton(text='ТЕКСТ НА КНОПКЕ 1', callback_data='test1'),\
                                                                                InlineKeyboardButton(text='ЧТЕКСТ НА КНОПКЕ 2', callback_data='test2'),\
                                                                                InlineKeyboardButton(text='ТЕКСТ НА КНОПКЕ 3', callback_data='test3')))
@dp.callback_query_handler(text_contains="test")
async def test(callback: types.CallbackQuery):
    if callback.data == 'test1':
        for ret in cur.execute("SELECT id, title, score FROM names WHERE name = 'test_1'").fetchall():
            await bot.send_video(callback.from_user.id, ret[0], caption=f'Текст под постом1: {ret[1]}\n Текст под постом2: {ret[-1]}\n\n Текст под постом3 \n\nТекст под постом4, reply_markup=underpost_butt)
    elif callback.data == 'test2':
        for ret in cur.execute("SELECT id, title, score FROM names WHERE name = 'test_2'").fetchall():
            await bot.send_video(callback.from_user.id, ret[0], caption=f'Текст под постом1: {ret[1]}\n Текст под постом2: {ret[-1]}\n\n Текст под постом3 \n\nТекст под постом4, reply_markup=underpost_butt)
    elif callback.data == 'test3':
        for ret in cur.execute("SELECT id, title, score FROM names WHERE name = 'test_3'").fetchall():
            await bot.send_video(callback.from_user.id, ret[0], caption=f'Текст под постом1: {ret[1]}\n Текст под постом2: {ret[-1]}\n\n Текст под постом3 \n\nТекст под постом4, reply_markup=underpost_butt)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2022-02-09
@Vindicar

if callback.data in ('test1', 'test2', 'test3'):
    for ret in cur.execute("SELECT id, title, score FROM names WHERE name = ?", (callback.data, )):
        await bot.send_video(callback.from_user.id, ret[0], caption=f'Текст под постом1: {ret[1]}\n Текст под постом2: {ret[-1]}\n\n Текст под постом3 \n\nТекст под постом4, reply_markup=underpost_butt)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question