@bot.message_handler(content_types=['text'])
def get_text_messages(message):
if message.text == "/start":
begin = types.InlineKeyboardMarkup()
namebutton = types.InlineKeyboardButton(text="Name urself", callback_data="nm")
begin.add(namebuttonl)
@bot.callback_query_handler(func=lambda call: True)
def beginning(call):
```@bot.message_handler(content_types=['text'])
def get_name(message):
if call.data == "nm":
bot.send_message(message.from_user.id, "Introduce urself")
global name
name = message.text
bot.send_message(message.from_user.id, name)
bot.polling(none_stop=True, interval=0)```
I have problems with second message handler. When button appears, I click on it and nothing happens. After clicking on it I expect to get message from bot, where it askes about name, and then saves answer in variable. I tried to remove second handler, to change places of some lines.
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_handlerrecognizes which action to perform next. No additional reaction from the users is expected and waited for in this process. So the sequence of actionsask for the name -> wait for the name to be typed in -> reactis not what InlineKeyboard does. It's more ofask 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:
(Note that the syntax I used for
beginningis 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_handlerdocumentation. This function actually allows to wait for the next message and run your defined functions on this message.For example:
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.