def back_to_the_catalog(chat_id, language, message_text=None, parent_category=None): bot.send_chat_action(chat_id, 'typing') if not message_text: catalog_message = strings.get_string('catalog.start', language) else: catalog_message = message_text if parent_category: catalog_message = strings.from_category_name(parent_category, language) categories = parent_category.get_siblings(include_self=True).all() category_keyboard = keyboards.from_dish_categories( categories, language) bot.send_message(chat_id, catalog_message, reply_markup=category_keyboard) if parent_category.parent: bot.register_next_step_handler_by_chat_id( chat_id, catalog_processor, parent_category=parent_category.parent) else: bot.register_next_step_handler_by_chat_id(chat_id, catalog_processor) return categories = dishservice.get_parent_categories(sort_by_number=True) category_keyboard = keyboards.from_dish_categories(categories, language) bot.send_message(chat_id, catalog_message, reply_markup=category_keyboard, parse_mode='HTML') bot.register_next_step_handler_by_chat_id(chat_id, catalog_processor)
def catalog_processor(message: Message, **kwargs): def send_category(category, message, keyboard): if category.image_path or category.image_id: if category.image_path and not category.image_id: try: image = open(category.image_path, 'rb') except FileNotFoundError: bot.send_message(chat_id=chat_id, text=message, reply_markup=keyboard) else: sent_message = bot.send_photo(chat_id=chat_id, photo=image, caption=message, reply_markup=keyboard) dishservice.set_category_image_id( category, sent_message.photo[-1].file_id) elif category.image_id: bot.send_photo(chat_id=chat_id, photo=category.image_id, caption=message, reply_markup=keyboard) else: bot.send_message(chat_id=chat_id, text=message, reply_markup=keyboard) chat_id = message.chat.id if message.successful_payment: bot.register_next_step_handler_by_chat_id(chat_id, catalog_processor) return user_id = message.from_user.id language = userservice.get_user_language(user_id) def error(): error_message = strings.get_string('catalog.error', language) bot.send_message(chat_id, error_message) bot.register_next_step_handler_by_chat_id(chat_id, catalog_processor) if not message.text: error() return if strings.get_string('go_back', language) in message.text: parent_category = kwargs.get('parent_category', None) if not parent_category: botutils.to_main_menu(chat_id, language) return back_to_the_catalog(chat_id, language, parent_category=None) elif strings.get_string('go_to_menu', language) in message.text: botutils.to_main_menu(chat_id, language) ##MENU elif strings.get_string('catalog.cart', language) in message.text: user_cart.cart_processor(message) elif strings.get_string('catalog.make_order', language) in message.text: orders.order_processor(message) else: category_name = message.text category = dishservice.get_category_by_name( category_name, language, kwargs.get('parent_category', None)) if not category: error() return if category.get_children().count() > 0: categories = category.get_children().all() catalog_message = strings.from_category_name(category, language) category_keyboard = keyboards.from_dish_categories( categories, language) send_category(category, catalog_message, category_keyboard) bot.register_next_step_handler_by_chat_id(chat_id, catalog_processor, parent_category=category) elif category.dishes.count() > 0: dishes = category.dishes.filter(Dish.is_hidden == False).order_by( Dish.number.asc()) dish_message = strings.get_string('catalog.choose_dish', language) dishes_keyboard = keyboards.from_dishes(dishes, language) send_category(category, dish_message, dishes_keyboard) bot.register_next_step_handler_by_chat_id(chat_id, choose_dish_processor, category=category) else: empty_message = strings.get_string('catalog.empty', language) bot.send_message(chat_id, empty_message) if category.parent: bot.register_next_step_handler_by_chat_id( chat_id, catalog_processor, parent_category=category.parent) else: bot.register_next_step_handler_by_chat_id( chat_id, catalog_processor)