C
C
captainJacksp232021-10-11 14:46:02
Python
captainJacksp23, 2021-10-11 14:46:02

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

2 answer(s)
M
Mikhail Krostelev, 2021-10-13
@captainJacksp23

You need to use the FSM, the state machine. Enough material with examples. And you need to get rid of nested handlers

I
InternetMaster, 2021-10-11
@InternetMaster

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>)

Already in this def put all the vomit that is below async def geometry(message):
UPD. Attached off. documentation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question