Как сократить этот код, и сделать его лучше?
Как сократить этот код
@bot.message_handler()
def replay(message):
if message.text in ['Стрижка ??♀', 'Стрижка', 'cтрижка', 'Стрижку', 'стрижку'] or message.text in ['Мужская стрижка ??♂', 'Мужская', 'Мужская стрижка', 'Женская стрижка ??♀', 'Женская', 'Женская стрижка', 'Детская стрижка ??', 'Детская', 'Детская стрижка']:
button = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=2)
men_haircut = types.KeyboardButton(text='Мужская стрижка ??♂')
women_haircut = types.KeyboardButton(text='Женская стрижка ??♀')
kids_haircut = types.KeyboardButton(text='Детская стрижка ??')
button.add(men_haircut, women_haircut, kids_haircut)
bot.send_message(chat_id=message.chat.id, text='Выберите тип услуги', reply_markup=button)
if message.text in ['Мужская стрижка ??♂', 'Мужская', 'Мужская стрижка']:
button = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=2)
men_haircut_hair_comparison = types.KeyboardButton(text='Равнение волос')
button.add(men_haircut_hair_comparison)
bot.send_message(chat_id=message.chat.id, text='Выбирите стрижку ??♂', reply_markup=button)
elif message.text in ['Женская стрижка ??♀', 'Женская', 'Женская стрижка']:
button = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=2)
women_haircut_hair_comparison = types.KeyboardButton(text='Равнение волос')
button.add(women_haircut_hair_comparison)
bot.send_message(chat_id=message.chat.id, text='Выбирите стрижку ??♀', reply_markup=button)
elif message.text in ['Детская стрижка ??', 'Детская', 'Детская стрижка']:
button = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=2)
kids_haircut_hair_comparison = types.KeyboardButton(text='Равнение волос')
button.add(kids_haircut_hair_comparison)
bot.send_message(chat_id=message.chat.id, text='Выбирите стрижку ??', reply_markup=button)
Ответы (1 шт):
Автор решения: Edward Grachev
→ Ссылка
Можно попробовать как то так. Кнопки можно передавать распаковывая список. И не надо проверять полное совпадение, достаточно ключевого слова
@bot.message_handler()
def replay(message):
if 'стрижк' in message.text.lower():
button = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=2)
buttons_label = ['Мужская стрижка ??♂', 'Женская стрижка ??♀', 'Детская стрижка ??']
button.add(*buttons_label)
bot.send_message(chat_id=message.chat.id, text='Выберите тип услуги', reply_markup=button)
if 'мужская' in message.text.lower():
button = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=2)
button.add(types.KeyboardButton(text='Равнение волос'))
bot.send_message(chat_id=message.chat.id, text='Выбирите стрижку ??♂', reply_markup=button)
elif 'женская' in message.text.lower():
button = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=2)
button.add(types.KeyboardButton(text='Равнение волос'))
bot.send_message(chat_id=message.chat.id, text='Выбирите стрижку ??♀', reply_markup=button)
elif 'детская' in message.text.lower():
button = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=2)
button.add(types.KeyboardButton(text='Равнение волос'))
bot.send_message(chat_id=message.chat.id, text='Выбирите стрижку ??', reply_markup=button)