I am not an experienced callback query user, but here's how I understand it: each button should have a unique callback_data, by which the callback_query_handler
recognizes which action to perform next. No additional reaction from the users is expected and waited for in this process. So the sequence of actions ask for the name -> wait for the name to be typed in -> react
is not what InlineKeyboard does. It's more of ask for the name (provide choices) -> wait for the user to make a choice -> react
.
I've tried to modify your code a bit for it to start following this sequence:
@bot.message_handler(content_types=['text'])
def get_text_messages(message):
if message.text == "/start":
begin = types.InlineKeyboardMarkup()
name1 = types.InlineKeyboardButton(text="Maria", callback_data="Maria")
name2 = types.InlineKeyboardButton(text="John", callback_data="John")
begin.add(name1)
begin.add(name2)
bot.send_message(message.chat.id, "Choose your name", reply_markup=begin)
@bot.callback_query_handler(func=lambda call: True)
def beginning(call):
name = call.data
bot.send_message(call.message.chat.id, "Your name is: {}".format(name))
(Note that the syntax I used for beginning
is a bit different: there's no nested function. I guess this is why NOTHING happened when you pressed the button, instead of something strange happened)
However, in this case you should list all the possible names in the InlineKeyboard buttons. I understand, that this is probably not acceptable.
There is an alternative outside of callbacks in this - register_next_step_handler
documentation.
This function actually allows to wait for the next message and run your defined functions on this message.
For example:
@bot.message_handler(content_types=['text'])
def get_text_messages(message):
if message.text == "/start":
message = bot.send_message(message.chat.id, "Introduce yourself")
bot.register_next_step_handler(message, process_name)
def process_name(message):
name = message.text
bot.send_message(message.chat.id, "Your name is: {}".format(name))
Probably register_callback_query_handler does something similar from the user's point of view, but I haven't tried it yet, so not sure. If you end up trying it - please tell if it helps.