Я пытаюсь создать бот-телеграмму с меню, используя класс Update
.Я бы объединил InlineMenuKeyboard
с ReplyKeyboardMarkup
.
. Я создал одного бота, используя аннотации, и он хорошо работает с обоими видами меню.Но мне не нравится эта структура.
Но здесь inlineKeyboard
работает хорошо, ReplyKeyboardMarkup
не отображается.Я не эксперт в создании ботов, поэтому буду рад, если кто-нибудь пришлет мне несколько примеров ботов с правильной архитектурой и структурой?
#!/usr/bin/python3
from telegram.ext import Updater
from telegram.ext import CommandHandler, CallbackQueryHandler
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telebot import types
############################### Bot ############################################
def start(bot, update):
update.message.reply_text(main_menu_message(),
reply_markup=main_menu_keyboard())
def test(bot,update):
print("TEST")
markup_settings_menu = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=2)
markup_settings_menu.add(types.KeyboardButton('Change name'),
types.KeyboardButton('Change Phone number'),
types.KeyboardButton('it`s a test')
print("test 1.5")
update.message.reply_text('hELLO',
reply_markup=markup_settings_menu)
print("TEST2")
def create_settings_menu():
markup_settings_menu = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=2)
markup_settings_menu.add(types.KeyboardButton('Change name'),
types.KeyboardButton('Change Phone number'),
types.KeyboardButton(cs.home_emoji))
return markup_settings_menu
def main_menu(bot, update):
query = update.callback_query
bot.edit_message_text(chat_id=query.message.chat_id,
message_id=query.message.message_id,
text=main_menu_message(),
reply_markup=main_menu_keyboard())
def first_menu(bot, update):
query = update.callback_query
bot.edit_message_text(chat_id=query.message.chat_id,
message_id=query.message.message_id,
text=first_menu_message(),
reply_markup=first_menu_keyboard())
def second_menu(bot, update):
query = update.callback_query
bot.edit_message_text(chat_id=query.message.chat_id,
message_id=query.message.message_id,
text=second_menu_message(),
reply_markup=second_menu_keyboard())
# and so on for every callback_data option
def first_submenu(bot, update):
pass
def second_submenu(bot, update):
pass
############################ Keyboards #########################################
def main_menu_keyboard():
keyboard = [[InlineKeyboardButton('Option 1', callback_data='m1')],
[InlineKeyboardButton('Option 2', callback_data='m2')],
[InlineKeyboardButton('Option 3', callback_data='m3')]]
return InlineKeyboardMarkup(keyboard)
def first_menu_keyboard():
keyboard = [[InlineKeyboardButton('Submenu 1-1', callback_data='m1_1')],
[InlineKeyboardButton('Submenu 1-2', callback_data='m1_2')],
[InlineKeyboardButton('Main menu', callback_data='main')]]
return InlineKeyboardMarkup(keyboard)
def second_menu_keyboard():
keyboard = [[InlineKeyboardButton('Submenu 2-1', callback_data='m2_1')],
[InlineKeyboardButton('Submenu 2-2', callback_data='m2_2')],
[InlineKeyboardButton('Main menu', callback_data='main')]]
return InlineKeyboardMarkup(keyboard)
def create_markup_menu():
markup_menu = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=3)
markup_menu.add(types.KeyboardButton('Order room'),
types.KeyboardButton('Manage order'),
types.KeyboardButton('Orders history'),
types.KeyboardButton('⚙️Settings'),
types.KeyboardButton('❓Help'))
return InlineKeyboardButton(markup_menu)
#===============================================================================
############################# Messages #########################################
def main_menu_message():
return 'Choose the option in main menu:1111'
def first_menu_message():
return 'Choose the submenu in first menu:'
def second_menu_message():
return 'Choose the submenu in second menu:'
############################# Handlers #########################################
updater = Updater('===TOKEN===')
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CommandHandler('test', test))
updater.dispatcher.add_handler(CallbackQueryHandler(main_menu, pattern='main'))
updater.dispatcher.add_handler(CallbackQueryHandler(first_menu, pattern='m1'))
updater.dispatcher.add_handler(CallbackQueryHandler(second_menu, pattern='m2'))
updater.dispatcher.add_handler(CallbackQueryHandler(first_submenu,
pattern='m1_1'))
updater.dispatcher.add_handler(CallbackQueryHandler(second_submenu,
pattern='m2_1'))
updater.start_polling()
################################################################################