Z
Z
zetter2018-02-13 13:55:34
Python
zetter, 2018-02-13 13:55:34

How to make a multi-level telegram bot menu (inline - callback) in python?

Good afternoon. There is a telegram bot code that displays 4 buttons and when you click on each of them, a message is displayed.

from telebot import types

import constants, os, re


bot = telebot.TeleBot(constants.token)

@bot.message_handler(commands=["start"])
def inline(message):
    key = types.InlineKeyboardMarkup()
    but_1 = types.InlineKeyboardButton(text="NumberOne", callback_data="NumberOne")
    but_2 = types.InlineKeyboardButton(text="NumberTwo", callback_data="NumberTwo")
    but_3 = types.InlineKeyboardButton(text="NumberTree", callback_data="NumberTree")
    but_4 = types.InlineKeyboardButton(text="Number4", callback_data="Number4")
    key.add(but_1, but_2, but_3, but_4)
    bot.send_message(message.chat.id, "ВЫБЕРИТЕ КНОПКУ", reply_markup=key)

@bot.callback_query_handler(func=lambda c:True)
def inlin(c):
    if c.data == 'NumberOne':
        bot.send_message(c.message.chat.id, 'Это кнопка 1')
    if c.data == 'NumberTwo':
        bot.send_message(c.message.chat.id, 'Это кнопка 2')
    if c.data == 'NumberTree':
        bot.send_message(c.message.chat.id, 'Это кнопка 3')
    if c.data == 'Number4':
        bot.send_message(c.message.chat.id, 'Это кнопка 4')




#    elif c.data == 'NumberTwo':
#        bot.send_message(c.message.chat.id, 'Это мазафака, кнопка 2')




if __name__ == "__main__":
    bot.polling(none_stop=True)

How to make it so that when you click on a button, a new menu of several buttons opens, with the ability to return to the main menu?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
SuckMyPython, 2019-10-17
@SuckMyPython

It is necessary to add an optional parameter reply_markup=key to bot.send_message (i.e., in addition to sending the text, the bot will create a menu), after specifying the necessary information.
Here's a quick example (change 3 buttons):

@bot.message_handler(commands=["start"])
def inline(message):
  key = types.InlineKeyboardMarkup()
  but_1 = types.InlineKeyboardButton(text="NumberOne", callback_data="NumberOne")
  but_2 = types.InlineKeyboardButton(text="NumberTwo", callback_data="NumberTwo")
  but_3 = types.InlineKeyboardButton(text="NumberTree", callback_data="NumberTree")
  key.add(but_1, but_2, but_3)
  bot.send_message(message.chat.id, "ВЫБЕРИТЕ КНОПКУ", reply_markup=key)

@bot.callback_query_handler(func=lambda c:True)
def inline(c):
  if c.data == 'NumberOne':
    bot.send_message(c.message.chat.id, 'Это кнопка 1')
  if c.data == 'NumberTwo':
    bot.send_message(c.message.chat.id, 'Это кнопка 2')
  if c.data == 'NumberTree':
    key = types.InlineKeyboardMarkup()
    but_1 = types.InlineKeyboardButton(text="NumberOne", callback_data="NumberOne")
    but_2 = types.InlineKeyboardButton(text="NumberTwo", callback_data="NumberTwo")
    but_3 = types.InlineKeyboardButton(text="NumberTree", callback_data="NumberTree")
    key.add(but_1, but_2, but_3)
    bot.send_message(c.message.chat.id, 'Это кнопка 3', reply_markup=key)

M
manvalit, 2019-01-08
@manvalit

This is done by simply changing the message. After that, we insert another keyboard, you can find out in detail here: https://groosha.gitbooks.io/telegram-bot-lessons/c...
If you still couldn’t understand, then write and I’ll explain.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question