如何使用"返回"返回到Telegram bot中的上一个菜单按钮?

时间:2017-11-30 05:02:31

标签: python telegram telegram-bot

我真的在努力学习代码。我是编码的新手,我无法在这个问题的任何地方找到答案。我正在用Python构建一个Telegram机器人,我遇到了创建一个" back"按钮和"到主要"按钮。

import telebot

@bot.message_handler(commands=['start'])
def handle_start(message):
   main_greeting = 'Main greeting text'
   main_menu = telebot.types.ReplyKeyboardMarkup(True, False)
   main_menu.row('Answer 1')
   main_menu.row('Answer 2', 'Answer 3')

   bot.send_message(message.from_user.id, main_greeting, reply_markup=main_menu)

def handle_text(message):
    if message.text == 'Answer 1':
        answer_1_text = 'Answer 1 text'
        answer_1_menu = telebot.types.ReplyKeyboardMarkup(True, False)
        answer_1_menu.row('Option 1')
        answer_1_menu.row('Option 2')
        answer_1_menu.row('Back') #"Back" should bring user to the main_menu

        bot.send_message(message.from_user.id, answer_1_text,
                         reply_markup=main_flavor_menu)

    elif message.text == 'Option 1'
        option_1_text = 'Select from one of the categories for further help'
        option_1_menu = telebot.types.ReplyKeyboardMarkup(True, False)
        option_1_menu.row('Category 1', 'Category 2')
        option_1_menu.row('Category 3')
        option_1_menu.row('Back', 'To Main') #"Back" should bring user to 
                                             #the answer_1_menu and "To Main" 
                                             #should bring one to main_menu

        bot.send_message(message.from_user.id, option_1_text,
                         reply_markup=option_1_menu)

    elif message.text == 'Back'
        bot.send_message(message.from_user.id, '.',
                         reply_markup=answer_1_menu-1)

    elif message.text == 'Back'
        bot.send_message(message.from_user.id, '.',
                         reply_markup=option_1_menu-1)

    elif message.text == 'To Main'
        bot.send_message(message.from_user.id, option_1_text,
                         reply_markup=main_menu)

1 个答案:

答案 0 :(得分:-2)

您可以使用内联键盘及其handler

Awesome Telegram Bot