示例#1
0
def request_registration_handler(message: Message, language: str):
    chat_id = message.chat.id

    welcome_message = strings.get_string('registration.request.welcome',
                                         language)
    remove_keyboard = keyboards.get_keyboard('remove')
    telegram_bot.send_message(chat_id,
                              welcome_message,
                              reply_markup=remove_keyboard)
    telegram_bot.register_next_step_handler_by_chat_id(
        chat_id, request_registration_name_handler, language=language)
示例#2
0
 def error():
     if message.text == '/start':
         welcome(message)
         return
     error_msg = strings.get_string('registration.request.phone_number',
                                    language)
     telegram_bot.send_message(chat_id, error_msg, parse_mode='HTML')
     telegram_bot.register_next_step_handler_by_chat_id(
         chat_id,
         request_registration_phone_number_handler,
         name=name,
         language=language)
示例#3
0
 def error():
     accept_policy = types.ReplyKeyboardMarkup(resize_keyboard=True)
     item1 = types.KeyboardButton('🤝 Я согласен(сна) / Rozilik beraman')
     accept_policy.add(item1)
     error_msg = (
         'Примите пользовательское соглашение!  \n\nFoydalanuvchi shartnomasini qabul qiling!'
     )
     telegram_bot.send_message(chat_id,
                               error_msg,
                               reply_markup=accept_policy,
                               parse_mode='HTML')
     telegram_bot.register_next_step_handler_by_chat_id(chat_id, welcome)
示例#4
0
def _to_the_confirmation(chat_id, current_order, language):
    total = _total_order_sum(current_order.order_items.all())
    summary_order_message = strings.from_order(current_order, language, total)
    confirmation_keyboard = keyboards.get_keyboard('order.confirmation',
                                                   language)
    bot.send_message(chat_id,
                     summary_order_message,
                     parse_mode='HTML',
                     reply_markup=confirmation_keyboard)
    bot.register_next_step_handler_by_chat_id(chat_id,
                                              confirmation_processor,
                                              total=total)
示例#5
0
def language_handler(message: Message):
    chat_id = message.chat.id
    user_id = message.from_user.id
    language = userservice.get_user_language(user_id)

    change_language_message = strings.get_string('language.change', language)
    change_language_keyboard = keyboards.from_change_language(language)
    telegram_bot.send_message(chat_id,
                              change_language_message,
                              reply_markup=change_language_keyboard)
    telegram_bot.register_next_step_handler_by_chat_id(
        chat_id, change_language_processor)
示例#6
0
def _to_the_address(chat_id, language):
    cart = userservice.get_user_cart(chat_id)
    total = _total_cart_sum(cart)
    cart_contains_message = strings.from_cart_items(cart, language, total)
    address_message = strings.get_string(
        'order.address', language).format(cart_contains_message)
    address_keyboard = keyboards.get_keyboard('order.address', language)
    bot.send_message(chat_id,
                     address_message,
                     parse_mode='HTML',
                     reply_markup=address_keyboard)
    bot.register_next_step_handler_by_chat_id(chat_id, address_processor)
示例#7
0
def _to_audience_age(chat_id, language, include_keyboard=True, current_ad_order: AdCampaign = None):
    audience_age_msg = strings.get_string('campaign.audience_age', language)
    if include_keyboard:
        ad_campaign = current_ad_order or advertservice.get_current_campaign(chat_id)
        audience_age_keyboard = keyboards.from_ages(language, ad_campaign.age_of_audience)
        if ad_campaign.age_of_audience and ad_campaign.age_of_audience != '':
            selected_ages_template = strings.get_string('campaign.selected_ages', language)
            audience_age_msg = selected_ages_template.format(strings.format_ages(ad_campaign.age_of_audience, language))
        bot.send_message(chat_id, audience_age_msg, reply_markup=audience_age_keyboard, parse_mode='Markdown')
    else:
        bot.send_message(chat_id, audience_age_msg)
    bot.register_next_step_handler_by_chat_id(chat_id, audience_age_processor)
示例#8
0
def _to_the_confirmation(chat_id, current_order, language):
    total = _total_order_sum(current_order.order_items.all())
    summary_order_message = strings.from_order(current_order, language, total)
    confirmation_keyboard = keyboards.get_keyboard('order.confirmation',
                                                   language)
    if current_order.payment_method == Order.PaymentMethods.PAYME:
        title = strings.get_string('order.payment.title',
                                   language).format(current_order.id)
        description = strings.get_string('order.payment.description', language)
        payload = str(total)
        start_parameter = secrets.token_hex(20)
        currency = 'UZS'
        prices = strings.from_order_items_to_labeled_prices(
            current_order.order_items.all(), language)
        confirmation_keyboard = keyboards.get_keyboard(
            'order.payment_confirmation', language)
        bot.send_message(chat_id,
                         summary_order_message,
                         parse_mode='HTML',
                         reply_markup=confirmation_keyboard)
        invoice = bot.send_invoice(chat_id, title, description, payload,
                                   Config.PAYMENT_PROVIDER_TOKEN, currency,
                                   prices, start_parameter)
        bot.register_next_step_handler_by_chat_id(
            chat_id,
            confirmation_processor,
            total=total,
            message_id=invoice.message_id)
        return


#    elif current_order.payment_method == Order.PaymentMethods.CLICK:
#        title = strings.get_string('order.payment.title', language).format(current_order.id)
#        description = strings.get_string('order.payment.description', language)
#        payload = str(total)
#        start_parameter = secrets.token_hex(20)
#        currency = 'UZS'
#        prices = strings.from_order_items_to_labeled_prices(current_order.order_items.all(), language)
#        confirmation_keyboard = keyboards.get_keyboard('order.payment_confirmation', language)
#        bot.send_message(chat_id, summary_order_message, parse_mode='HTML', reply_markup=confirmation_keyboard)
#        invoice = bot.send_invoice(chat_id, title, description, payload, Config.PAYMENT_PROVIDER_TOKEN_CLICK, currency, prices,
#                         start_parameter)
#        bot.register_next_step_handler_by_chat_id(chat_id, confirmation_processor, total=total, message_id=invoice.message_id)
#        return
    else:
        bot.send_message(chat_id,
                         summary_order_message,
                         parse_mode='HTML',
                         reply_markup=confirmation_keyboard)
    bot.register_next_step_handler_by_chat_id(chat_id,
                                              confirmation_processor,
                                              total=total)
示例#9
0
def catalog(message: Message):
    chat_id = message.chat.id
    user_id = message.from_user.id
    language = userservice.get_user_language(user_id)
    dishes = Dish.query.filter(Dish.category_id == 0,
                               Dish.is_hidden == False).order_by(
                                   Dish.number.asc()).all()
    dish_message = strings.get_string('catalog.choose_dish', language)
    dishes_keyboard = keyboards.from_dishes(dishes, language)
    bot.send_message(chat_id=chat_id,
                     text=dish_message,
                     reply_markup=dishes_keyboard)
    bot.register_next_step_handler_by_chat_id(chat_id, choose_dish_processor)
示例#10
0
def package_offers_handler(message: Message):
    chat_id = message.chat.id
    user_id = message.from_user.id
    language = userservice.get_user_language(user_id)

    channels = channelservice.get_channels_only_with_package_offers()
    if len(channels) == 0:
        empty_msg = strings.get_string('package_offers.empty', language)
        bot.send_message(chat_id, empty_msg)
        return
    channel_message = strings.get_string('package_offers.channel', language)
    channels_keyboard = keyboards.from_channels(channels, language)
    bot.send_message(chat_id, channel_message, reply_markup=channels_keyboard)
    bot.register_next_step_handler_by_chat_id(chat_id, channel_processor)
示例#11
0
def catalog(message: Message):
    chat_id = message.chat.id
    user_id = message.from_user.id
    language = userservice.get_user_language(user_id)
    bot.send_chat_action(chat_id, 'typing')
    catalog_message = strings.get_string('catalog.start', language)
    categories = dishservice.get_parent_categories(sort_by_number=True)
    if len(categories) == 0:
        empty_message = strings.get_string('catalog.empty', language)
        bot.send_message(chat_id, empty_message)
        return
    category_keyboard = keyboards.from_dish_categories(categories, language)
    bot.send_message(chat_id, catalog_message, reply_markup=category_keyboard)
    bot.register_next_step_handler_by_chat_id(chat_id, catalog_processor)
示例#12
0
def _to_the_payment_method(chat_id, language, user_id: int):
    current_order = orderservice.get_current_order_by_user(user_id)
    if current_order.shipping_method == Order.ShippingMethods.PICK_UP:
        payment_message = strings.get_string('order.payment_pickup', language)
    else:
        payment_message = strings.get_string('order.payment_delivery',
                                             language)
    payment_keyboard = keyboards.get_keyboard('order.payment', language)
    bot.send_message(chat_id,
                     payment_message,
                     reply_markup=payment_keyboard,
                     parse_mode='HTML')
    bot.register_next_step_handler_by_chat_id(chat_id,
                                              payment_method_processor)
示例#13
0
def welcome(message, **kwargs):
    user_id = message.from_user.id
    chat_id = message.chat.id

    def not_allowed():
        not_allowed_message = strings.get_string('registration.not_allowed')
        remove_keyboard = keyboards.get_keyboard('remove')
        telegram_bot.send_message(chat_id, not_allowed_message, reply_markup=remove_keyboard)
    current_user = userservice.get_user_by_telegram_id(user_id)
    if current_user:
        botutils.to_main_menu(chat_id, current_user.language)
        return
    welcome_message = strings.get_string('welcome')
    language_keyboard = keyboards.get_keyboard('welcome.language')
    telegram_bot.send_message(chat_id, welcome_message, reply_markup=language_keyboard, parse_mode='HTML')
    telegram_bot.register_next_step_handler_by_chat_id(chat_id, process_user_language)
示例#14
0
def cart_action_processor(message: Message):
    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_msg = strings.get_string('cart.error', language)
        bot.send_message(chat_id, error_msg)
        bot.register_next_step_handler_by_chat_id(chat_id,
                                                  cart_action_processor)

    if not message.text:
        error()
        return
    if strings.get_string('go_back', language) in message.text:
        back_to_the_catalog(chat_id, language)
    elif strings.get_string('cart.clear', language) in message.text:
        userservice.clear_user_cart(user_id)
        back_to_the_catalog(chat_id, language)
    elif strings.get_string('catalog.make_order', language) in message.text:
        order_processor(message)
    else:
        dish_name = message.text[2:]
        removing_result = userservice.remove_dish_from_user_cart(
            user_id, dish_name, language)
        if removing_result:
            cart = userservice.get_user_cart(user_id)
            if len(cart) == 0:
                back_to_the_catalog(chat_id, language)
                return
            total = _total_cart_sum(cart)
            cart_contains_message = strings.from_cart_items(
                cart, language, total)
            cart_contains_keyboard = keyboards.from_cart_items(cart, language)
            bot.send_message(chat_id,
                             cart_contains_message,
                             parse_mode='HTML',
                             reply_markup=cart_contains_keyboard)
            bot.register_next_step_handler_by_chat_id(chat_id,
                                                      cart_action_processor)
            return
        else:
            error()
            return
示例#15
0
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)
示例#16
0
def cart_processor(message: Message, callback=None):
    chat_id = message.chat.id
    user_id = message.from_user.id
    language = userservice.get_user_language(user_id)

    cart = userservice.get_user_cart(user_id)
    if len(cart) == 0:
        cart_empty_message = strings.get_string('cart.empty', language)
        bot.send_message(chat_id, cart_empty_message)
        if callback:
            bot.register_next_step_handler_by_chat_id(chat_id, callback)
        else:
            bot.register_next_step_handler_by_chat_id(chat_id,
                                                      catalog_processor)
        return
    cart_help_message = strings.get_string('cart.help', language)
    total = _total_cart_sum(cart)
    cart_contains_message = strings.from_cart_items(cart, language, total)
    cart_items_keyboard = keyboards.from_cart_items(cart, language)
    bot.send_message(chat_id, cart_help_message, parse_mode='HTML')
    bot.send_message(chat_id,
                     cart_contains_message,
                     parse_mode='HTML',
                     reply_markup=cart_items_keyboard)
    bot.register_next_step_handler_by_chat_id(chat_id, cart_action_processor)
示例#17
0
def welcome(message):
    chat_id = message.chat.id

    def error():
        if message.text == '/start':
            registration.welcome(message)
            return
        accept_policy = types.ReplyKeyboardMarkup(resize_keyboard = True)
        item1 = types.KeyboardButton('🤝 Я согласен(сна) / Rozilik beraman')
        accept_policy.add(item1)
        error_msg = ('Примите пользовательское соглашение!  \n\nFoydalanuvchi shartnomasini qabul qiling!')
        telegram_bot.send_message(chat_id, error_msg, reply_markup=accept_policy, parse_mode='HTML')
        telegram_bot.register_next_step_handler_by_chat_id(chat_id, welcome)

    if message.text.startswith('🤝'):
        accept_policy = True
        welcome_message = strings.get_string('welcome')
        language_keyboard = keyboards.get_keyboard('welcome.language')
        telegram_bot.send_message(chat_id, welcome_message, reply_markup=language_keyboard, parse_mode='HTML')
        telegram_bot.register_next_step_handler_by_chat_id(chat_id, process_user_language, accept_policy=accept_policy)
    else:
        error()
        return
示例#18
0
def process_accept_policy(message):
    user_id = message.from_user.id
    chat_id = message.chat.id

    def not_allowed():
        not_allowed_message = strings.get_string('registration.not_allowed')
        remove_keyboard = keyboards.get_keyboard('remove')
        telegram_bot.send_message(chat_id, not_allowed_message, reply_markup=remove_keyboard)
    current_user = userservice.get_user_by_telegram_id(user_id)
    if current_user:
        botutils.to_main_menu(chat_id, current_user.language)
        return
    else:
        accept_text = """
        Подтвердите своё согласие с пользовательским соглашением. \nВы можете ознакомиться с пользовательским соглашением пройдя по ссылке\n
Foydalanuvchi shartnomasi bilan tanishib chiqqaningizni tasdiqlang! Foydalanuvchi shartnomasini havolani bosish orqali ko'rishingiz mumkin\n
https://delivera.uz/agreement
        """
        accept_policy = types.ReplyKeyboardMarkup(resize_keyboard = True)
        item1 = types.KeyboardButton('🤝 Я согласен(сна) / Rozilik beraman')
        accept_policy.add(item1)
        telegram_bot.send_message(chat_id, accept_text, reply_markup=accept_policy, parse_mode='HTML')
        telegram_bot.register_next_step_handler_by_chat_id(chat_id, welcome)
示例#19
0
def rating_processor(message: Message):
    chat_id = message.chat.id
    user_id = message.from_user.id
    language = userservice.get_user_language(user_id)

    def error():
        bot.register_next_step_handler_by_chat_id(chat_id, rating_processor)

    if not message.text:
        error()
        return
    if strings.get_string('main_menu', language) in message.text:
        _to_main_menu(chat_id, language)
    elif strings.get_string('rating.ratings', language) in message.text:
        rating = ratingservice.get_rating()
        if rating and rating.images.count() > 0:
            for image in rating.images.all():
                if image.image_id:
                    bot.send_photo(chat_id, image.image_id)
                else:
                    bot.send_chat_action(chat_id, 'upload_photo')
                    sent_file = bot.send_photo(chat_id, open(image.image_path, 'rb'))
                    tg_id = sent_file.photo[-1].file_id
                    ratingservice.set_rating_telegram_id(image, tg_id)
        else:
            empty_message = strings.get_string('ratings.empty', language)
            bot.send_message(chat_id, empty_message)
            bot.register_next_step_handler_by_chat_id(chat_id, rating_processor)
            return
        bot.register_next_step_handler_by_chat_id(chat_id, rating_processor)
    elif strings.get_string('rating.presentations', language) in message.text:
        presentations = channelservice.get_channel_presentations()
        if len(presentations) == 0:
            presentations_empty_message = strings.get_string('rating.presentations_empty', language)
            bot.send_message(chat_id, presentations_empty_message)
            bot.register_next_step_handler_by_chat_id(chat_id, rating_processor)
        for file in presentations:
            if file.telegram_id:
                bot.send_document(chat_id, file.telegram_id)
            else:
                bot.send_chat_action(chat_id, 'upload_document')
                sent_file = bot.send_document(chat_id, open(file.file_path, 'rb'))
                tg_id = sent_file.document.file_id
                channelservice.set_telegram_id_for_presentation_file(file.id, tg_id)
        bot.register_next_step_handler_by_chat_id(chat_id, rating_processor)
    else:
        error()
示例#20
0
def dish_action_processor(message: Message):
    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_action_error',
                                           language)
        bot.send_message(chat_id, error_message)
        bot.register_next_step_handler_by_chat_id(chat_id,
                                                  dish_action_processor)

    if not message.text:
        error()
        return
    current_dish = userservice.get_current_user_dish(user_id)
    if strings.get_string('go_back', language) in message.text:
        dishes = dishservice.get_dishes_from_category(current_dish.category,
                                                      sort_by_number=True)
        dish_message = strings.get_string('catalog.choose_dish', language)
        dishes_keyboard = keyboards.from_dishes(dishes, language)
        bot.send_message(chat_id, dish_message, reply_markup=dishes_keyboard)
        bot.register_next_step_handler_by_chat_id(
            chat_id, choose_dish_processor, category=current_dish.category)
    elif strings.get_string('catalog.cart', language) in message.text:
        cart.cart_processor(message, dish_action_processor)
    else:
        if not message.text.isdigit():
            error()
            return
        userservice.add_dish_to_cart(user_id, current_dish, int(message.text))
        continue_message = strings.get_string('catalog.continue', language)
        back_to_the_catalog(chat_id, language, continue_message)
示例#21
0
def channel_processor(message: Message):
    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('prices.channel', language)
        bot.send_message(chat_id, error_message)
        bot.register_next_step_handler_by_chat_id(chat_id, channel_processor)

    if not message.text:
        error()
        return
    if strings.get_string('go_back', language) in message.text:
        main_menu_message = strings.get_string('main_menu.choose_option',
                                               language)
        main_menu_keyboard = keyboards.get_keyboard('main_menu', language)
        bot.send_message(chat_id,
                         main_menu_message,
                         reply_markup=main_menu_keyboard)
        return
    try:
        price_files = channelservice.get_prices_by_channel(message.text)
    except channelservice.ChannelNotFound:
        error()
        return
    for price_file in price_files:
        if price_file.telegram_id:
            bot.send_document(chat_id, price_file.telegram_id)
        else:
            bot.send_chat_action(chat_id, 'upload_document')
            file = open(price_file.file_path, 'rb')
            sent_file = bot.send_document(chat_id, file)
            tg_id = sent_file.document.file_id
            channelservice.set_telegram_id_for_price_file(price_file.id, tg_id)
    bot.register_next_step_handler_by_chat_id(chat_id, channel_processor)
示例#22
0
def coverage_processor(message: Message, **kwargs):
    chat_id = message.chat.id
    user_id = message.from_user.id
    language = userservice.get_user_language(user_id)

    def error():
        bot.register_next_step_handler_by_chat_id(chat_id, coverage_processor)

    if not message.text:
        error()
        return
    if strings.get_string('go_back', language) in message.text:
        _to_budget(chat_id, language)
    elif strings.get_string('main_menu', language) in message.text:
        _to_main_menu(chat_id, language)
    elif strings.get_string('campaign.show', language) in message.text:
        budget = kwargs.get('budget')
        current_ad_order = advertservice.set_budget(user_id, budget)
        total_order_msg = strings.total_ad_order(current_ad_order, language)
        total_order_keyboard = keyboards.get_keyboard('campaign.confirmation', language)
        bot.send_message(chat_id, total_order_msg, parse_mode='Markdown', reply_markup=total_order_keyboard)
        bot.register_next_step_handler_by_chat_id(chat_id, confirmation_processor)
    else:
        error()
示例#23
0
def process_choose_option(message: Message):
    chat_id = message.chat.id
    user_id = message.from_user.id
    language = userservice.get_user_language(user_id)

    def error():
        error_msg = strings.get_string('main_menu.choose_option', language)
        telegram_bot.send_message(chat_id, error_msg)
        telegram_bot.register_next_step_handler_by_chat_id(chat_id, process_choose_option)

    if not message.text:
        error()
        return
    if strings.get_string('go_back', language) in message.text:
        main_menu_message = strings.get_string('main_menu.choose_option', language)
        main_menu_keyboard = keyboards.get_keyboard('main_menu', language)
        telegram_bot.send_message(chat_id, main_menu_message, reply_markup=main_menu_keyboard)
    elif strings.get_string('settings.change_user_name', language) in message.text:
        send_name_message = strings.get_string('settings.send_name', language)
        go_back_keyboard = keyboards.get_keyboard('go_back', language)
        telegram_bot.send_message(chat_id, send_name_message, reply_markup=go_back_keyboard)
        telegram_bot.register_next_step_handler_by_chat_id(chat_id, process_change_user_name)
    elif strings.get_string('settings.change_phone_number', language) in message.text:
        send_phone_message = strings.get_string('settings.send_phone_number', language)
        phone_number_keyboard = keyboards.get_keyboard('settings.change_phone', language)
        telegram_bot.send_message(chat_id, send_phone_message, reply_markup=phone_number_keyboard, parse_mode='HTML')
        telegram_bot.register_next_step_handler_by_chat_id(chat_id, process_change_user_phone_number)
    elif strings.get_string('settings.change_company_name', language) in message.text:
        send_company_name_message = strings.get_string('settings.send_company_name', language)
        go_back_keyboard = keyboards.get_keyboard('go_back', language)
        telegram_bot.send_message(chat_id, send_company_name_message, reply_markup=go_back_keyboard)
        telegram_bot.register_next_step_handler_by_chat_id(chat_id, process_change_user_company_name)
    elif strings.get_string('settings.change_language', language) in message.text:
        choose_language_message = strings.get_string('settings.choose_language', language)
        choose_language_keyboard = keyboards.get_keyboard('settings.choose_language', language)
        telegram_bot.send_message(chat_id, choose_language_message, reply_markup=choose_language_keyboard)
        telegram_bot.register_next_step_handler_by_chat_id(chat_id, process_change_user_language)
    else:
        error()
示例#24
0
 def error():
     error_msg = strings.get_string('order.confirmation_error', language)
     bot.send_message(chat_id, error_msg)
     bot.register_next_step_handler_by_chat_id(chat_id,
                                               confirmation_processor)
示例#25
0
 def error():
     error_msg = strings.get_string('order.address_error')
     bot.send_message(chat_id, error_msg, parse_mode='HTML')
     bot.register_next_step_handler_by_chat_id(chat_id, address_processor)
示例#26
0
 def error():
     error_msg = strings.get_string('order.phone_number', language)
     bot.send_message(chat_id, error_msg, parse_mode='HTML')
     bot.register_next_step_handler_by_chat_id(chat_id,
                                               phone_number_processor)
示例#27
0
 def error():
     error_msg = strings.get_string('order.payment_error', language)
     bot.send_message(chat_id, error_msg)
     bot.register_next_step_handler_by_chat_id(chat_id,
                                               payment_method_processor)
示例#28
0
 def error():
     error_message = strings.get_string('catalog.dish_action_error',
                                        language)
     bot.send_message(chat_id, error_message)
     bot.register_next_step_handler_by_chat_id(chat_id,
                                               dish_action_processor)
示例#29
0
def dish_action_processor(message: Message):
    chat_id = message.chat.id
    user_id = message.from_user.id
    language = userservice.get_user_language(user_id)

    def _total_cart_sum(cart) -> int:
        summary_dishes_sum = [
            cart_item.dish.price * cart_item.count for cart_item in cart
        ]
        total = sum(summary_dishes_sum)
        return total

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

    if not message.text:
        error()
        return
    current_dish = userservice.get_current_user_dish(user_id)
    if strings.get_string('go_back', language) in message.text:
        dishes = dishservice.get_dishes_from_category(current_dish.category,
                                                      sort_by_number=True)
        dish_message = strings.get_string('catalog.choose_dish', language)
        dishes_keyboard = keyboards.from_dishes(dishes, language)
        bot.send_message(chat_id, dish_message, reply_markup=dishes_keyboard)
        bot.register_next_step_handler_by_chat_id(
            chat_id, choose_dish_processor, category=current_dish.category)

    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, dish_action_processor)
    else:
        if not message.text.isdigit():
            error()
            return
        # Проверка на количество товара в базе.
        selected_number = int(message.text)
        dish_to_check = Dish.query.get(current_dish.id)

        if selected_number > dish_to_check.quantity:
            not_enough_count = strings.get_string(
                'not_enough_count', language).format(dish_to_check.quantity)
            msg = bot.send_message(chat_id, text=not_enough_count)
            bot.register_next_step_handler(msg, dish_action_processor)

        else:
            userservice.add_dish_to_cart(user_id, current_dish,
                                         int(message.text))
            cart = userservice.get_user_cart(user_id)
            total = _total_cart_sum(cart)
            cart_contains_message = strings.from_cart_items(
                cart, language, total)
            continue_message = strings.get_string(
                'catalog.continue', language).format(cart_contains_message)

            back_to_the_catalog(chat_id, language, continue_message)
示例#30
0
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)