Answer the question
In order to leave comments, you need to log in
The work of the callback_query_handler decorator is looping, what is the solution?
I have a callback_query_handler decorator that is called by an inline button. I have several of these buttons. When I call one of them - the program works stably, but when I try to call another one, not the whole decorator is executed, after that the previously pressed one continues. This problem can only be solved by manually restarting the program.
from aiogram import Bot, Dispatcher, executor, types
from config import TOKEN
import keyboard as kb
import requests
from bs4 import BeautifulSoup
bot = Bot(token=TOKEN)
dp = Dispatcher(bot)
@dp.message_handler(commands=['start'])
async def send_welcome(message: types.Message):
await message.answer('выберите предмет',reply_markup=kb.selless_btn)
@dp.callback_query_handler(lambda c: c.data == 'russian_9')
async def process_callback_button1(callback_query: types.CallbackQuery):
await bot.answer_callback_query(callback_query.id)
await bot.send_message(callback_query.from_user.id, 'Введите упражнение: ')
@dp.message_handler(content_types=['text'])
async def russian(message):
number = message.text
url = 'https://gdz.ru/class-9/russkii_yazik/ribchenkova-9/'+number+'-nom/'
response = requests.get(url)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'lxml')
umage_list = []
umg = soup.find('figure')
umage = umg.find_all('img')
for i in umage:
umge = 'https:' + i.get('src')
umage_list.append(umge)
await message.answer(umage_list[0])
await message.answer(umage_list[1])
return umage_list
@dp.callback_query_handler(lambda c: c.data == 'geometry_9')
async def process_callback_button2(callback_query: types.CallbackQuery):
await bot.answer_callback_query(callback_query.id)
await bot.send_message(callback_query.from_user.id, 'Введите номер: ')
@dp.message_handler(content_types=['text'])
async def geometry(message):
number = message.text
url = 'https://gdz.ru/class-9/geometria/merzlyak-polonskij/'+number+'-nom/'
response = requests.get(url)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'lxml')
umage_list = []
umg = soup.find('figure')
umage = umg.find_all('img')
for i in umage:
umge = 'https:' + i.get('src')
umage_list.append(umge)
await message.answer(umage_list[0])
await message.answer(umage_list[1])
return umage_list
Answer the question
In order to leave comments, you need to log in
You need to use the FSM, the state machine. Enough material with examples. And you need to get rid of nested handlers
Why are you doing a message handler inside callback_query_handler?
Use bot.register_next_step_handler after "Enter number: "
await bot.send_message(callback_query.from_user.id, 'Введите номер: ')
await bot.register_next_step_handler(message, <НАЗВАНИЕ def>)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question