Answer the question
In order to leave comments, you need to log in
How to pull data from the database in InlineKeyboardButton?
def generate_kb(current_amount):
current_amount = int(current_amount)
keyboard_markup = types.InlineKeyboardMarkup(row_width=3)
keyboard_markup.add(
types.InlineKeyboardButton('-', callback_data='counter_{}'.format(current_amount - 1)),
types.InlineKeyboardButton('{} шт'.format(current_amount), callback_data='empty?'),
types.InlineKeyboardButton('+', callback_data='counter_{}'.format(current_amount + 1)),
types.InlineKeyboardButton('Добавить в корзину',
callback_data='cart_add_{}'.format(current_amount)),
)
return keyboard_markup
@dp.message_handler(text=" Menu")
async def store_menu(message: types.Message):
read_db = await sql_admin.sql_read_store_menu()
kb = generate_kb(0)
for ret in read_db:
await bot.send_photo(message.from_user.id, ret[4],
f'{ret[1]}\n{ret[2]}\nЦена: {ret[3]}₽', reply_markup=kb)
@dp.callback_query_handler(filters.Regexp(regexp='^counter_'))
async def update_counter(callback: types.CallbackQuery):
current_amount = callback.data.split('_')[-1]
await bot.edit_message_reply_markup(callback.message.chat.id, callback.message.message_id,
reply_markup=generate_kb(current_amount))
Answer the question
In order to leave comments, you need to log in
As an option - put json with data there.
Collect a dictionary with the necessary data, and with the help of either json, convert
When reading callback_data, convert back to a dictionary.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question