A
A
AlexaAioGram2021-06-16 13:41:19
Python
AlexaAioGram, 2021-06-16 13:41:19

How to make a back button on Aiogram in telegram?

I can't make the keyboard move one level back when I press the "Back" button, so if you know, please help,

handler.py

from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters import Command
from aiogram.types import Message

from handlers.users.keyboard import Choice, Katalog
from loader import dp


@dp.message_handler(Command('Start'))
async def catalog(message: Message):
    await message.answer('Вот наш каталог', reply_markup=Choice)

@dp.message_handler()
async def games(message: Message):
    if 'Старт' in message.text:
        await message.answer('Вот выберай', reply_markup=Katalog)

@dp.message_handler()
async def back(message: Message, state: FSMContext):
    text = message.text
    if 'Назад' in message.text:
        await message.answer('Ты вернулся', reply_markup=Choice)
        await Choice.Update.set()


Keyboard.py

from aiogram.types import ReplyKeyboardMarkup, KeyboardButton

Choice = ReplyKeyboardMarkup(resize_keyboard=True)
Start = KeyboardButton(text='Старт')
Choice.insert(Start)

Katalog = ReplyKeyboardMarkup(resize_keyboard=True)
game = KeyboardButton(text='Игры')
Katalog.insert(game)
back = KeyboardButton(text='Назад')
Katalog.insert(back)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Krostelev, 2021-06-16
@AlexaAioGram

Most likely, the fact is that you have 2 identical handlers @dp.message_handler()
, only the first one is checked, and the search for the occurrence of the phrase "Back" occurs in the second one. Put everything under one handler.

@dp.message_handler()
async def games(message: Message):
    if 'Старт' in message.text:
        await message.answer('Вот выберай', reply_markup=Katalog)
    elif 'Назад' in message.text:
        await message.answer('Ты вернулся', reply_markup=Choice)
        await Choice.Update.set()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question