예제 #1
0
def choose_dish_processor(message: Message, **kwargs):
    chat_id = message.chat.id
    user_id = message.from_user.id
    language = userservice.get_user_language(user_id)

    def error():
        error_message = strings.get_string('catalog.dish_error', language)
        bot.send_message(chat_id, error_message)
        bot.register_next_step_handler_by_chat_id(chat_id,
                                                  choose_dish_processor)

    if not message.text:
        error()
        return
    if strings.get_string('go_back', language) in message.text:
        if 'category' in kwargs:
            category = kwargs.get('category')
            back_to_the_catalog(chat_id, language, parent_category=category)
            return
        back_to_the_catalog(chat_id, language)
    elif strings.get_string('catalog.cart', language) in message.text:
        cart.cart_processor(message, choose_dish_processor)
    else:
        dish_name = message.text
        dish = dishservice.get_dish_by_name(dish_name, language,
                                            kwargs.get('category'))
        if not dish:
            error()
            return
        userservice.set_current_user_dish(user_id, dish.id)
        dish_info = strings.from_dish(dish, language)
        dish_keyboard = keyboards.get_keyboard('catalog.dish_keyboard',
                                               language)
        if dish.image_id or dish.image_path:
            if dish.image_path and not dish.image_id:
                try:
                    image = open(dish.image_path, 'rb')
                except FileNotFoundError:
                    bot.send_message(chat_id,
                                     dish_info,
                                     reply_markup=dish_keyboard)
                else:
                    sent_message = bot.send_photo(chat_id,
                                                  image,
                                                  caption=dish_info,
                                                  reply_markup=dish_keyboard)
                    dishservice.set_dish_image_id(
                        dish, sent_message.photo[-1].file_id)
            elif dish.image_id:
                bot.send_photo(chat_id,
                               dish.image_id,
                               caption=dish_info,
                               reply_markup=dish_keyboard)
        else:
            bot.send_message(chat_id, dish_info, reply_markup=dish_keyboard)
        dish_action_helper = strings.get_string('catalog.dish_action_helper',
                                                language)
        bot.send_message(chat_id, dish_action_helper)
        bot.register_next_step_handler_by_chat_id(chat_id,
                                                  dish_action_processor)
예제 #2
0
def choose_dish_processor(message: Message, **kwargs):
    chat_id = message.chat.id
    user_id = message.from_user.id
    language = userservice.get_user_language(user_id)

    def error():
        if message.text == '/start':
            registration.welcome(message)
            return
        error_message = strings.get_string('catalog.dish_error', language)
        bot.send_message(chat_id, error_message)
        bot.register_next_step_handler_by_chat_id(chat_id,
                                                  choose_dish_processor)

    if not message.text:
        error()
        return
    if strings.get_string('go_back', language) in message.text:
        if 'category_id' in kwargs:
            category_id = kwargs.get('category_id')
            back_to_the_catalog(chat_id,
                                language,
                                parent_category_id=category_id)
            return
        back_to_the_catalog(chat_id, language)

    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, choose_dish_processor)
    else:
        dish_name = message.text
        dish = dishservice.get_dish_by_name(
            dish_name, language,
            dishservice.get_category_by_id(kwargs.get('category_id')))
        if not dish:
            error()
            return
        userservice.set_current_user_dish(user_id, dish.id)
        dish_info = strings.from_dish(dish, language)
        dish_keyboard = keyboards.get_keyboard('catalog.dish_keyboard',
                                               language)
        msg_id = None
        if dish.image_id or dish.image_path:
            if dish.image_path and not dish.image_id:
                try:
                    image = open(dish.image_path, 'rb')
                except FileNotFoundError:
                    bot.send_message(chat_id,
                                     dish_info,
                                     reply_markup=dish_keyboard,
                                     parse_mode='HTMLS')
                else:
                    sent_message = bot.send_photo(chat_id,
                                                  image,
                                                  caption=dish_info,
                                                  reply_markup=dish_keyboard,
                                                  parse_mode='HTML')
                    dishservice.set_dish_image_id(
                        dish, sent_message.photo[-1].file_id)
                    msg_id = sent_message.message_id
            elif dish.image_id:
                msg_id = bot.send_photo(chat_id,
                                        dish.image_id,
                                        caption=dish_info,
                                        reply_markup=dish_keyboard,
                                        parse_mode='HTML').message_id
        else:
            msg_id = bot.send_message(chat_id,
                                      dish_info,
                                      reply_markup=dish_keyboard,
                                      parse_mode='HTML').message_id
        bot.register_next_step_handler_by_chat_id(chat_id,
                                                  dish_action_processor,
                                                  message_id=msg_id)