Answer the question
In order to leave comments, you need to log in
How to add buttons to state (telegram aiogram)?
I have a characterAge and characterBranch state, I want to create three Inline buttons when switching from the first state, how can I create them so that I can process the output only from these buttons?
my code:
class States(StatesGroup):
characterAge = State()
characterBranch = State()
#special - работа с бд
@dp.message_handler(state=States.characterAge, content_types=ContentTypes.TEXT)
async def characterAge(message: types.Message, state: FSMContext):
try:
int(message.text)
special.add_new_character_age(message.text, message.from_user.username)
await state.update_data(message=message.text.title())
kb = InlineKeyboardMarkup()
kb.add('Маг', 'Воин', 'Вор')
await message.answer(text='Выберите одну из трех ветвей развития вашего персонажа(Влияет на диалоги)', reply_markup=kb)
await States.characterBranch.set()
except IndentationError:
await message.reply('Существа не могут жить с отрицательным возрастом')
return
except NameError as err:
print(err)
await message.reply('Возраст не может состоять из букв')
return
@dp.message_handler(state=States.characterBranch)
async def characterBranch(message: types.Message, state: FSMContext):
if message.text not in ['Маг','Воин','Вор']:
await message.answer('Для выбора ветви персонажа используйте кнопки!')
return
await state.update_data(characterBranch=message.text.lower())
if message.text.lower == 'маг':
special.add_new_character_branch('1', message.from_user.username)
elif message.text.lower == 'воин':
special.add_new_character_branch('2', message.from_user.username)
elif message.text.lower == 'вор':
special.add_new_character_branch('3', message.from_user.username)
await message.answer()
await state.finish()
Answer the question
In order to leave comments, you need to log in
Firstly:
kb = InlineKeyboardMarkup()
kb.add('Маг', 'Воин', 'Вор')
InlineKeyboardButton
, you set the text for it, and callback_data, which you then process using the decorator
. Everything you want class States(StatesGroup):
characterAge = State()
characterBranch = State()
#special - работа с бд
@dp.message_handler(state=States.characterAge, content_types=ContentTypes.TEXT)
async def characterAge(message: types.Message, state: FSMContext):
try:
int(message.text)
special.add_new_character_age(message.text, message.from_user.username)
await state.update_data(message=message.text.title())
buttons = [InlineKeyboardButton('Маг', callback_data='branch:mag'),
InlineKeyboardButton('Маг', callback_data='branch:voin'),
InlineKeyboardButton('Маг', callback_data='branch:vor'),]
kb = InlineKeyboardMarkup()
kb.add(buttons)
await message.answer(text='Выберите одну из трех ветвей развития вашего персонажа(Влияет на диалоги)', reply_markup=kb)
await States.characterBranch.set()
except IndentationError:
await message.reply('Существа не могут жить с отрицательным возрастом')
return
except NameError as err:
print(err)
await message.reply('Возраст не может состоять из букв')
return
@dp.callback_query_handler(Text(startswith='branch:'), state=States.characterBranch)
async def characterBranch(callback_query: types.CallbackQuery, state: FSMContext):
branch_dict = {'mag': 'маг',
'voin': 'воин',
'vor': 'вор'}
branch = callback_query.data.removeprefix('branch:')
await state.update_data(characterBranch=branch_dict[branch])
if branch == 'mag':
special.add_new_character_branch('1', callback_query.from_user.username)
elif branch == 'voin':
special.add_new_character_branch('2', callback_query.from_user.username)
elif branch == 'vor':
special.add_new_character_branch('3', callback_query.from_user.username)
await message.answer()
await state.finish()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question