2

I am writing a telegram bot using aiogram. Help to implement the code to show the user a message with inline keyboard of 13 buttons and the user has an opportunity to choose 3 options and on the basis of this choice he will get a message in which will meet the selected options. In the structure of my project I put in a separate file the code with the keyboard and a separate file with handlers. keypad code:

`from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton

ikb_calc_exam = InlineKeyboardMarkup(row_width=3)
ib_calc_geography = InlineKeyboardButton(text='geography',
                                         callback_data='cd_calc_geography'
ib_calc_literature = InlineKeyboardButton(text='literature',
                                          callback_data='cd_calc_literature')
ib_calc_chemistry = InlineKeyboardButton(text='chemistry',
                                         callback_data='cd_calc_chemistry')
ib_calc_russian = InlineKeyboardButton(text='russian',
                                       callback_data='cd_calc_russian')
ib_calc_mathematics = InlineKeyboardButton(text='mathematics',
                                           callback_data='cd_calc_mathematics')
ib_calc_history = InlineKeyboardButton(text='history',
                                       callback_data='cd_calc_history')
ib_calc_physics = InlineKeyboardButton(text='physics',
                                       callback_data='cd_calc_physics')
ib_calc_socials = InlineKeyboardButton(text='socials',
                                       callback_data='cd_calc_socials')
ib_calc_biology = InlineKeyboardButton(text='biology',
                                       callback_data='cd_calc_biology')
ib_calc_international = InlineKeyboardButton(text='international',
                                             callback_data='cd_calc_international')
ib_calc_informatics = InlineKeyboardButton(text='informatics',
                                           callback_data='cd_calc_informatics')
ib_calc_professional = InlineKeyboardButton(text='professional',
                                            callback_data='cd_calc_professional')
ib_calc_creavity = InlineKeyboardButton(text='creavity',
                                        callback_data='cd_calc_creavity')
ikb_calc_exam.add(ib_calc_geography).add(ib_calc_literature).add(ib_calc_chemistry).add(ib_calc_russian).add(
    ib_calc_mathematics).add(ib_calc_history).add(ib_calc_physics).add(ib_calc_socials).add(ib_calc_biology).add(
    ib_calc_international).add(ib_calc_informatics).add(ib_calc_professional).add(ib_calc_creavity)`

handlers code:

`from aiogram import types, Dispatcher
from misc import bot, dp
from Keyboard.Keyboard_calc_exam import ikb_calc_exam
from aiogram.utils.callback_data import CallbackData


@dp.message_handler(commands=['calc_exam'])
async def calc_exam(message: types.Message):
    await bot.send_message(chat_id=message.from_user.id,
                           text='<em>Choose three options</em>',
                           parse_mode="HTML",
                           reply_markup=keyboard)
    await message.delete()

cd = CallbackData('geographi', 'literature', 'chemistry', 'russian', 'mathematics', 'history', 'physics', 'socials',
                  'biology', 'international', 'informatics', 'professional', 'creavity')


@dp.callback_query_handler(lambda callback_query: callback_query.data == 'cd_calc_geographi')
async def ikb_cb_geographi(callback: types.CallbackQuery):
    await callback.answer()`

I understand that it is necessary to use callback_data but all attempts to implement this have failed.

thp22
  • 21
  • 1

1 Answers1

0

define list_of_subjects above all handlers and:

@dp.callback_query_handler(lambda call: True)
async def choose_subject(call: types.CallbackQuery):
    list_of_subjects.append(call.data)
    subjects = ", ".join(list_of_subjects)

    if len(list_of_subjects) < 3:
        await bot.send_message(call.from_user.id, f"You've chosen {subjects}.")
    else:
        await bot.send_message(call.from_user.id, f"You've chosen {subjects}.")
        await call.message.delete()

This code adds the callbacks to the list of subjects and limits the number of answers. Message.delete does not allow to select more options, lambda call: True selects all callbacks in your bot. You can alternatively use:

@dp.callback_query_handler(lambda call: call.data.startswith("cd_calc")

to override other calls you may want to add to your program.

You may also define a value for every call.data using:

if call.data == "N":
    list_of_subjects.append("X")

Not to print exact callbacks and not to change callbacks to be neat themselves.

Anna
  • 101
  • 8