S
S
szjyakgf2022-02-03 19:36:25
Python
szjyakgf, 2022-02-03 19:36:25

How to repeat code from command in button click handler?

If when you click a button, you need to do the same thing as in the command. Do I need to write it again or is there something easier?

@dp.message_handler(commands=['test'])
async def test(message):
    user_id = message.from_user.id
    keyboard = types.InlineKeyboardMarkup()
    callback_button1 = types.InlineKeyboardButton(text="повторить", callback_data="test1")
    keyboard.add(callback_button1)
    rrr = str(random.randint(1, 51))
    await message.reply(f'Твое число{rrr}', reply_markup=keyboard)

@dp.callback_query_handler(lambda call: True)
async def callback_inline1(call):
    user_id = call.from_user.id
    result = cur.execute("SELECT * FROM users WHERE user_id = ?", (user_id, )).fetchone()
    with suppress(MessageNotModified):
        if call.message:
            if call.data == "test1":
                #тут я должен повторить все как в команде /test

PS .The code is for example, in my project I need to repeat 200+ lines

Answer the question

In order to leave comments, you need to log in

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

What's stopping you from calling await test(call.message)?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question