M
M
motya882019-03-06 22:23:40
Python
motya88, 2019-03-06 22:23:40

How to edit message text and inline button itself in python?

Good evening! Tell me how, after clicking on the inline button, change the text of the message and the button itself. I write a bot in python and use telebot
5c801d3cdde49650658057.jpeg
When you click on the "Turn off the light" button, the text of the message and the button itself should change.
Like this:
5c801d8868c20464258690.jpeg
The code itself:

elif message.text == ' Настройки':
            keyboard = types.InlineKeyboardMarkup()
            callback_button = types.InlineKeyboardButton(text="Включить свет 1", callback_data="test")
            keyboard.add(callback_button)
            callback_button = types.InlineKeyboardButton(text="Выключить свет 2 ", callback_data="test")
            keyboard.add(callback_button)
            callback_button = types.InlineKeyboardButton(text="Выключить  свет 3", callback_data="test")
            keyboard.add(callback_button)
            callback_button = types.InlineKeyboardButton(text="Выключить свет 4", callback_data="test")
            keyboard.add(callback_button)
            bot.send_message(message.chat.id, "Включение/отключение света в помещениях:" +"\n" + "свет 1 выкл "+"\n" + "свет 2 вкл "+"\n" + "свет 3 вкл "+"\n" + "свет 4 вкл ", reply_markup=keyboard)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
motya88, 2019-03-07
@motya88

The text is clear. And how after pressing the button to change the button itself?

T
Torrua, 2019-03-07
@Torrua

А что вам понятно с текстом? Мне вот из вашего кода совсем ничего не понятно, но попробую угадать: вы хотите, чтобы при повторной отправке текста "Настройки", Телеграм учитывал предыдущие нажатия и менял текст кнопок соответственно?
Если да (как и в любом случае, в общем-то), то вам придется где-то фиксировать текущее состояние выключателей (обычно это делается в базе данных) и генерировать текст кнопок, исходя из этих состояний.
Предположим, что вы получили состояние выключателя в boolean переменную light_1_on, тогда код

text="Выключить свет 1" if light_1_on else "Включить свет 1"
callback_button = types.InlineKeyboardButton(text, callback_data="test")
будет менять текст кнопки соответственно состоянию выключателя.
Текст самого сообщения тоже должен генерироваться динамически, исходя из текущего положения выключателей.

Денис, 2019-03-07
@Sat0shi

msg = bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="ТЕКСТ", parse_mode='Markdown')
msg = bot.edit_message_reply_markup(call.from_user.id, call.message.message_id, reply_markup=ModeKeyboard)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question