A
A
AlexSeley2021-12-07 23:56:16
Python
AlexSeley, 2021-12-07 23:56:16

Why doesn't my InlineKeyboardButton (Python) click response work?

@bot.message_handler(content_types=["text"])
def button(message):
keyboard = types.InlineKeyboardMarkup()
button = types.InlineKeyboardButton('First button!', callback_data='button1')
keyboard.add(button)
bot.send_message(message.chat.id, "Press, please!", reply_markup=keyboard)
bot.answer_callback_query(message.chat.id, text="Hi!")

When the button is pressed, the bot does nothing, although I specified bot.answer_callback_query(message.chat.id, text="Hi!")

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
shurshur, 2021-12-08
@shurshur

Inline buttons, unlike regular buttons, do not generate a text message. They must be caught through a separate callback_query_handler handler:

@bot.callback_query_handler(func=lambda call: call.message is not None)
def my_inline_callback(call):
  chat_id = call.message.chat.id
  user_id = call.from_user.id
  data = call.data
  ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question