0
import telebot
from telebot import types

bot = telebot.TeleBot('token')

@bot.message_handler(commands=["start"])
def inline(massege):
  mainmenu = types.InlineKeyboardMarkup()
  catalog = types.InlineKeyboardButton(text="Каталог", callback_data="Catalog")
  help = types.InlineKeyboardButton(text="Поддержка", callback_data="help")
  mainmenu.add(catalog, help,)
  bot.send_message(massege.chat.id, "ВЫБЕРИТЕ КНОПКУ", reply_markup=mainmenu)


@bot.callback_query_handler(func=lambda a:True)
def inline(a):
  if a.data == 'Catalog':
    bot.send_message(a.message.chat.id, 'Это кнопка 1')
  elif a.data == 'help':
      menuhelp = types.InlineKeyboardMarkup()
      helper = types.InlineKeyboardButton(text="Связь с нами", callback_data="helper")
      backinmainmenu = types.InlineKeyboardButton(text="Назад", callback_data="backinmainmenu")
      menuhelp.add(helper, backinmainmenu)
      bot.send_message(a.message.chat.id, 'Выбирите действие1', reply_markup=menuhelp)
      @bot.callback_query_handler(func=lambda b: True)
      def inline(b):
           if b.data == 'helper':
            bot.send_message(b.message.chat.id, 'Это кнопка 2')
           elif b.data == 'backinmainmenu':
               @bot.message_handler(commands=["start"])
               def inline(massege):
                   mainmenu = types.InlineKeyboardMarkup()
                   catalog = types.InlineKeyboardButton(text="Каталог", callback_data="Catalog")
                   help = types.InlineKeyboardButton(text="Поддержка", callback_data="help")
                   mainmenu.add(catalog, help,)
                   bot.send_message(massege.chat.id, "ВЫБЕРИТЕ КНОПКУ", reply_markup=mainmenu)


bot.polling()

1 ответ 1

0

Интересно, а если Вам придется возвращаться в коде к декоратору @bot.message_handler(commands=["start"]) с функцией inline n-ое количество раз, вы будете ее тоже переписывать n-ое количество раз? Никогда не переписывайте код еще раз если в этом нет необходимости, и ознакомьтесь с правилом DRY.

Для вашего случая достаточно и одного обработчика для inline-клавиатуры.

import telebot
from telebot import types

bot = telebot.TeleBot('token')

@bot.message_handler(commands=["start"])
def inline(massege):
  mainmenu = types.InlineKeyboardMarkup()
  catalog = types.InlineKeyboardButton(text="Каталог", callback_data="Catalog")
  help = types.InlineKeyboardButton(text="Поддержка", callback_data="help")
  mainmenu.add(catalog, help,)
  bot.send_message(massege.chat.id, "ВЫБЕРИТЕ КНОПКУ", reply_markup=mainmenu)


@bot.callback_query_handler(func=lambda a:True)
def inline_a(a):
    if a.data == 'Catalog':
        bot.send_message(a.message.chat.id, 'Это кнопка 1')
    elif a.data == 'help':
        menuhelp = types.InlineKeyboardMarkup()
        helper = types.InlineKeyboardButton(text="Связь с нами", callback_data="helper")
        backinmainmenu = types.InlineKeyboardButton(text="Назад", callback_data="backinmainmenu")
        menuhelp.add(helper, backinmainmenu)
        bot.send_message(a.message.chat.id, 'Выбирите действие', reply_markup=menuhelp)
    elif a.data == "helper":
        bot.send_message(a.message.chat.id, 'Это кнопка 2')
    elif a.data == 'backinmainmenu':
        return inline(a.message)     

bot.polling()
1

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.