B
B
Boss_Abu2022-04-21 10:18:49
Python
Boss_Abu, 2022-04-21 10:18:49

How to make an inline keyboard with message replacement on the body of the bot?

In short, I wrote the code and when you click on the "back" button, the message does not change, but remains as it is

. The code itself:

import telebot
from telebot import types

token = "Тут ваш токен"

bot=telebot.TeleBot(token)

@bot.message_handler(content_types=['text'])
def inline_key(a):
    if a.text == "/start":
        mainmenu = types.InlineKeyboardMarkup()
        key1 = types.InlineKeyboardButton(text='Игры ', callback_data='key1')
        key2 = types.InlineKeyboardButton(text='Приложения ', callback_data='key2')
        key10 = types.InlineKeyboardButton(text = ' Канал JUICY APP', url="http://t.me/Juicy_apk")
        mainmenu.add(key1, key2)
        mainmenu.add(key10)
        
        bot.send_message(a.chat.id, 'Приветствую  Ты в каталоге игр и приложений канала @Juicy_apk\nВыбери что хочешь найти:', reply_markup=mainmenu)

@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
    if call.data == "mainmenu":
        mainmenu = types.InlineKeyboardMarkup()
        key1 = types.InlineKeyboardButton(text='Игры ', callback_data='key1')
        key2 = types.InlineKeyboardButton(text='Приложения ', callback_data='key2')
        key10 = types.InlineKeyboardButton(text="Канал JUICY APP", url="http://t.me/Juicy_apk")
        mainmenu.add(key1, key2)
        mainmenu.add(key10)
        bot.edit_message_reply_markup(call.message.chat.id, call.message.message_id, reply_markup=mainmenu)
    elif call.data == "key1":
        next_menu = types.InlineKeyboardMarkup()  
        key3 = types.InlineKeyboardButton(text='Симуляторы', callback_data='key3')
        key5 = types.InlineKeyboardButton(text='Гонки', callback_data='key5')
        back = types.InlineKeyboardButton(text='Назад', callback_data='mainmenu')
        next_menu.add(key3, key5, back)
        bot.edit_message_text("Приветствую  Ты в каталоге игр и приложений канала @Juicy_apk\nВыбери что хочешь найти:", call.message.chat.id, call.message.message_id,
                              reply_markup=next_menu)                                                                         
    elif call.data == "key2":
        next_menu2 = types.InlineKeyboardMarkup()
        key4 = types.InlineKeyboardButton(text='Кнопка 4', callback_data='key4')
        back = types.InlineKeyboardButton(text='Назад', callback_data='mainmenu')
        next_menu2.add(key4, back)
        bot.edit_message_text('Приветствую  Ты в каталоге игр и приложений канала @Juicy_apk\nВыбери что хочешь найти:', call.message.chat.id, call.message.message_id,
                              reply_markup=next_menu2)

    elif call.data == "key3":
        next_menu3 = types.InlineKeyboardMarkup()
        back = types.InlineKeyboardButton(text='Назад', callback_data='mainmenu')
        next_menu3.add(back)
        bot.edit_message_text('Нажата кнопка Симуляторы', call.message.chat.id, call.message.message_id, reply_markup=next_menu3) 
        
    elif call.data == "key5":
        next_menu4 = types.InlineKeyboardMarkup()
        back = types.InlineKeyboardButton(text='Назад', callback_data='mainmenu')
        next_menu4.add(back)
        bot.edit_message_text('Нажата кнопка Гонки', call.message.chat.id, call.message.message_id, reply_markup=next_menu4)

   
bot.polling(none_stop=True)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Krostelev, 2022-04-21
@Boss_Abu

You only change the keyboard. Although in other cases you change the text of the message. Strange question, maybe not quite correctly asked.

if call.data == "mainmenu":
        ...
        text = 'Приветствую  Ты в каталоге игр и приложений канала @Juicy_apk\nВыбери что хочешь найти:'
        bot.edit_message_text(text, call.message.chat.id, call.message.message_id, reply_markup=mainmenu)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question