skip to Main Content
@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.

2

Answers


  1. 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.

    Login or Signup to reply.
  2. The problem is that you put your handlers inside the body of other functions, which is wrong. You need to move all handlers to one place. And I recommend using register_next_step_handler to register the next step in user/bot interaction. Here is a working code:

    from telebot import TeleBot, types
    
    bot = TeleBot()
    
    
    @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(namebutton)
            bot.send_message(message.from_user.id, "Name", reply_markup=begin)
    
    
    @bot.callback_query_handler(func=lambda call: True)
    def beginning(call):
        if call.data == "nm":  # callback_data from your InlineKeyboardButton
            bot.send_message(call.from_user.id, "Introduce urself")
            bot.register_next_step_handler_by_chat_id(call.from_user.id, get_name)
    
    
    def get_name(message):
        global name
        name = message.text
        bot.send_message(message.from_user.id, f"Name - {name}")
    
    
    bot.polling(none_stop=True, interval=0)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search