A
A
AlekseiFlis2019-03-04 10:57:06
Python
AlekseiFlis, 2019-03-04 10:57:06

PyTelegramBotApi. How to implement the function of changing the keyboard callback after pressing the button?

You need to write a function. So that when entering a command in telegram, the bot sends a message with a callback keyboard and two buttons. And when you click on them, this keyboard changes. How to do it? I tried callingback_data to push a function that would change the first message, but it didn’t work that way.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Danil K., 2019-06-07
@Danya_Violet

this will send the user an inline keyboard with the /start command

@bot.message_handler(commands=['start'])
def cmd_start(message):
    start_keyboard = types.InlineKeyboardMarkup()
    start_service = types.InlineKeyboardButton(text='Службы', callback_data='service')
    start_other = types.InlineKeyboardButton(text='Другое', callback_data='other')
    start_keyboard.add(start_service, start_other)
    bot.send_message(message.chat.id, 'принята команда /start', reply_markup=start_keyboard)

this will change the keyboard sent after /start if the user has clicked on the inline "Other" button
if call.data == 'other':
        service_keyboard = types.InlineKeyboardMarkup()
        service_comproxy_k = types.InlineKeyboardButton(text='Comproxy', callback_data='comproxy')
        service_back_k = types.InlineKeyboardButton(text='Назад', callback_data='back')
        service_keyboard.add(service_comproxy_k, service_back_k)
        bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text='Выберите:',
                              reply_markup=service_keyboard)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question