A
A
Andrew2021-12-05 23:46:54
Python
Andrew, 2021-12-05 23:46:54

What is the mystery of inline buttons in Telegram?

I can’t figure out why inline buttons work in one case, but not in the other.
Probably a banal question, I just understand all this, so I will be glad for a tip!

Not working version
from aiogram import types, Dispatcher
from aiogram.dispatcher.filters import Text


from keys.keying import game_keys


# @dp.message_handler(commands='game')
async def start_game(message: types.Message):
    await message.answer('Выберите!', reply_markup=game_keys)
    await message.delete()


# @dp.callback_query_handler(Text(startswith='word_'))
async def game_callback(call: types.CallbackQuery):
    await call.message.answer(f'Вы выбрали {call.data.split("_")[1]}')
    await call.message.delete()
    await call.answer()


def register_handlers_client(dp: Dispatcher):
    dp.register_message_handler(start_game, commands='game')
    dp.callback_query_handler(game_callback, Text(startswith='word_'))

working version
from aiogram import types, Dispatcher
from aiogram.dispatcher.filters import Text


from keys.keying import game_keys


@dp.message_handler(commands='game')
async def start_game(message: types.Message):
    await message.answer('Выберите!', reply_markup=game_keys)
    await message.delete()


@dp.callback_query_handler(Text(startswith='word_'))
async def game_callback(call: types.CallbackQuery):
    await call.message.answer(f'Вы выбрали {call.data.split("_")[1]}')
    await call.message.delete()
    await call.answer()


def register_handlers_client(dp: Dispatcher):
    dp.register_message_handler(start_game, commands='game')
    dp.callback_query_handler(game_callback, Text(startswith='word_'))

The difference between them is only in the decorators that I commented out in a non-working version.
Such a scheme works with ordinary buttons, but for some reason it works with inline only with decorators.
The buttons themselves appear, but when you click on them, CallbackQuery does not work.

Bot start
from logic.config import dp
from aiogram.utils import executor
from logic.game import register_handlers_client


register_handlers_client(dp)


executor.start_polling(dp, skip_updates=True)


config
from aiogram import Bot
from aiogram import Dispatcher


bot = Bot(token='TOKEN')
dp = Dispatcher(bot)</spoiler>


keying
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton


nouns = InlineKeyboardButton(text='существительное', callback_data='word_nouns')
verb = InlineKeyboardButton(text='глагол', callback_data='word_verb')
adverbs = InlineKeyboardButton(text='наречие', callback_data='word_adverbs')
adjectives = InlineKeyboardButton(text='прилагательное', callback_data='word_adjectives')


game_keys = InlineKeyboardMarkup(row_width=2)
game_keys.add(nouns).add(adverbs).add(verb).add(adjectives)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-12-06
@remberq

Inline buttons don't work because they haven't assigned any function to the click. All function bindings to events can be done in two ways
1. Using a decorator
2. Using a special function
Regular buttons work for you because you used the second option and called a special function register_*_handler, while for an inline button you called a regular function, which should be a decorator.
If you want to register a handler through a function - welcome
register_callback_query_handler()
But it makes little sense, as it seems to me, it is more convenient to use a decorator
https://docs.aiogram.dev/en/latest/dispatcher/inde...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question