@Alpharius_Prog

Как компактно написать код для многоуровневой клавиатуры меню для бота в Телеграмме?

Здравствуйте. Пишу код для меню, он используется для бота, как модуль. Работает только в виде цепочку elif.

spoiler
import telebot
from telebot import types
token = 'вдлаоплваоповдлаапдлоадлппвдалпдлвалподваолп'
bot = telebot.TeleBot(token)

@bot.message_handler(commands = ['start'])
def start(message):
    markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
    item1 = types.KeyboardButton("EN")
    item2 = types.KeyboardButton("RU")
    item3 = types.KeyboardButton("DE")
    item4 = types.KeyboardButton("UA")
    markup.add(item1,item2,item3,item4)
    bot.send_message(message.chat.id, text="Welcome to the TC-Market".format(message.from_user),reply_markup=markup)
@bot.message_handler(content_types=['text'])
def func(message):
    if (message.text == "Back"):
        markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
        item1 = types.KeyboardButton("EN")
        item2 = types.KeyboardButton("RU")
        item3 = types.KeyboardButton("DE")
        item4 = types.KeyboardButton("UA")
        markup.add(item1,item2,item3,item4)
        bot.send_message(message.chat.id, text="Welcome to the TC-Market".format(message.from_user),reply_markup=markup)
    elif (message.text == "EN"):
        markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
        item11 = types.KeyboardButton("Choose category")
        item12 = types.KeyboardButton("FAQ")
        item13 = types.KeyboardButton("About")
        item14 = types.KeyboardButton("Payment instructions")
        item15 = types.KeyboardButton("Back")
        markup.add(item11,item12,item13,item14,item15)
        bot.send_message(message.chat.id, text="EN".format(message.from_user),reply_markup=markup)
    elif (message.text == "Choose category"):
        markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
        item111 = types.KeyboardButton("History")
        item112 = types.KeyboardButton("Criminal")
        item113 = types.KeyboardButton("Back")
        markup.add(item111,item112,item113)
        bot.send_message(message.chat.id, text="Choose category".format(message.from_user),reply_markup=markup)
    elif (message.text == "FAQ"):
        markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
        item121 = types.KeyboardButton("Back")
        markup.add(item121)
        bot.send_message(message.chat.id, text="FAQ".format(message.from_user),reply_markup=markup)
    elif (message.text == "Payment instructions"):
        markup = types.InlineKeyboardMarkup()
        item131 = types.InlineKeyboardButton("Trust Wallet", url = "https://link.trustwallet.com/send?coin=0&address=bc1q83d2jeh58cv66h6kglr33dgwex6krekdg5uwsa")
        item132 = types.InlineKeyboardButton("BlockChain", url = "https://www.blockchain.com/ru/explorer")
        item133 = types.InlineKeyboardButton("Binance", url = "https://www.binance.com/ru")
        markup.add(item131, item132, item133)
        bot.send_message(message.chat.id, "Choose your wallet".format(message.from_user),reply_markup=markup)
    elif (message.text == "Zurück"):
        markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
        item1 = types.KeyboardButton("EN")
        item2 = types.KeyboardButton("RU")
        item3 = types.KeyboardButton("DE")
        item4 = types.KeyboardButton("UA")
        markup.add(item1,item2,item3,item4)
        bot.send_message(message.chat.id, text="Welcome to the TC-Market".format(message.from_user),reply_markup=markup)
    elif (message.text == "DE"):
        markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
        item21 = types.KeyboardButton("Wählen Sie eine Kategorie aus")
        item22 = types.KeyboardButton("FAQ")
        item23 = types.KeyboardButton("Über das Projekt")
        item24 = types.KeyboardButton("Zahlungsanweisungen")
        item25 = types.KeyboardButton("Zurück")
        markup.add(item21,item22,item23,item24,item25)
        bot.send_message(message.chat.id, text="DE".format(message.from_user),reply_markup=markup)
    elif (message.text == "Wählen Sie eine Kategorie aus"):
        markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
        item111 = types.KeyboardButton("Geschicht")
        item112 = types.KeyboardButton("Krimi")
        item113 = types.KeyboardButton("Zurück")
        markup.add(item111,item112,item113)
        bot.send_message(message.chat.id, text="Выберите категорию".format(message.from_user),reply_markup=markup)
    elif (message.text == "FAQ"):
        markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
        item121 = types.KeyboardButton("Zurück")
        markup.add(item121)
        bot.send_message(message.chat.id, text="FAQ".format(message.from_user),reply_markup=markup)
    elif (message.text == "Zahlungsanweisungen"):
        markup = types.InlineKeyboardMarkup()
        item131 = types.InlineKeyboardButton("Trust Wallet", url = "https://link.trustwallet.com/send?coin=0&address=bc1q83d2jeh58cv66h6kglr33dgwex6krekdg5uwsa")
        item132 = types.InlineKeyboardButton("BlockChain", url = "https://www.blockchain.com/ru/explorer")
        item133 = types.InlineKeyboardButton("Binance", url = "https://www.binance.com/ru")
        markup.add(item131, item132, item133)
        bot.send_message(message.chat.id, "Wählen Sie Ihre Geldbörse".format(message.from_user),reply_markup=markup)
    elif (message.text == "Назад"):
        markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
        item1 = types.KeyboardButton("EN")
        item2 = types.KeyboardButton("RU")
        item3 = types.KeyboardButton("DE")
        item4 = types.KeyboardButton("UA")
        markup.add(item1,item2,item3,item4)
        bot.send_message(message.chat.id, text="Welcome to the TC-Market".format(message.from_user),reply_markup=markup)
    elif (message.text == "RU"):
        markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
        item21 = types.KeyboardButton("Выберите категорию")
        item22 = types.KeyboardButton("FAQ")
        item23 = types.KeyboardButton("О проекте ")
        item24 = types.KeyboardButton("Инструкции по платежам")
        item25 = types.KeyboardButton("Назад")
        markup.add(item21,item22,item23,item24,item25)
        bot.send_message(message.chat.id, text="RU".format(message.from_user),reply_markup=markup)
    elif (message.text == "Выберите категорию"):
        markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
        item111 = types.KeyboardButton("История")
        item112 = types.KeyboardButton("Криминал")
        item113 = types.KeyboardButton("Назад")
        markup.add(item111,item112,item113)
        bot.send_message(message.chat.id, text="Выберите категорию".format(message.from_user),reply_markup=markup)
    elif (message.text == "FAQ"):
        markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
        item121 = types.KeyboardButton("Назад")
        markup.add(item121)
        bot.send_message(message.chat.id, text="FAQ".format(message.from_user),reply_markup=markup)
    elif (message.text == "Инструкции по платежам"):
        markup = types.InlineKeyboardMarkup()
        item131 = types.InlineKeyboardButton("Trust Wallet", url = "https://link.trustwallet.com/send?coin=0&address=bc1q83d2jeh58cv66h6kglr33dgwex6krekdg5uwsa")
        item132 = types.InlineKeyboardButton("BlockChain", url = "https://www.blockchain.com/ru/explorer")
        item133 = types.InlineKeyboardButton("Binance", url = "https://www.binance.com/ru")
        markup.add(item131, item132, item133)
        bot.send_message(message.chat.id, "Выберите свой кошелек".format(message.from_user),reply_markup=markup)
bot.polling(non_stop=True)


Как сделать этот код компактнее? Для уточнения: используется библиотека telebot
  • Вопрос задан
  • 229 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы