P
P
PJeys2021-02-28 22:13:36
Python
PJeys, 2021-02-28 22:13:36

Add message processing in the telegram bot button?

I can't figure out how to properly describe everything. The bottom line is that I have to process a separate message for each button (changing either the name or gender, etc.), but I only have to change one thing, and then the handler that I used for this is attached

@bot.callback_query_handler(func=lambda call: True)
def callback_worker(call):
    if call.data == "info":
        bot.send_message(chat_id, "Ім‘я: " + name + "\nСтать: " + sex + "\nВік: " + str(age))
    if call.data == "settings":
        bot.send_message(chat_id,"Налаштування: ", reply_markup=keyboard_settings)
    if call.data == "exit":
        main_menu()
    if call.data == "escape":
        bot.send_message(chat_id, "Налаштування: ", reply_markup=keyboard_settings)
    if call.data == "change_name":
        bot.send_message(chat_id, "Введіть нове ім'я, або натисніть \"Назад\":", reply_markup=keyboard_exit)
        @bot.message_handler(func=lambda message: True, content_types=['text'])
        def set_new_name(message):
            global name
            name = message.text
            if 2 <= len(name) <= 20:
                bot.send_message(chat_id, "Ваше ім‘я змінено на " + name)
                main_menu()
                return 0
            else:
                bot.send_message(message.chat.id, "Введіть ім‘я довжиною від 2 до 20 букв")
                bot.register_next_step_handler(message, set_new_name)

    if call.data == "change_sex":
        bot.send_message(chat_id, "Введіть нову стать, або натисніть \"Назад\":", reply_markup=keyboard_exit)
        @bot.message_handler(func=lambda message: True, content_types=['text'])
        def set_new_sex(message):
            global sex
            sex = message.text
            if sex.lower() == 'ч' or sex.lower() == 'ж':
                bot.send_message(chat_id, "Стать змінено на: " + sex)
                main_menu()
                return 0
            else:
                bot.send_message(chat_id, "Важіть Ч або Ж!")
                bot.register_next_step_handler(message, set_new_sex)
    if call.data == "change_age":
        bot.send_message(chat_id, "Введіть новий вік, або натисніть \"Назад\":", reply_markup=keyboard_exit)
        @bot.message_handler(func=lambda message: True, content_types=['text'])
        def set_new_age(message):
            global age
            try:
                age = int(message.text)
                if age > 0:
                    bot.send_message(chat_id, "Вік змінено на: " + str(age))
                    main_menu()
                    return 0
                else:
                    bot.send_message(message.chat.id, "Введіть коректне значення віку")
                    bot.register_next_step_handler(message, set_new_age)
            except Exception:
                bot.send_message(message.chat.id, "Введіть вік цифрами")
                bot.register_next_step_handler(message, set_new_age)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-02-28
@PJeys

Instead of creating a bunch of functions, you need to use register_next_step_handler()
https://github.com/eternnoir/pyTelegramBotAPI/blob...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question