Ejemplo n.º 1
0
def confirmation_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_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)

    if not message.text:
        error()
        return
    if strings.get_string('order.confirm', language) in message.text:
        total = kwargs.get('total')
        user = userservice.get_user_by_telegram_id(user_id)
        order = orderservice.confirm_order(user_id, user.full_user_name, total)
        order_success_message = strings.get_string('order.success', language)
        back_to_the_catalog(chat_id, language, order_success_message)
        notify_new_order(order, total)
    elif strings.get_string('order.cancel', language) in message.text:
        order_canceled_message = strings.get_string('order.canceled', language)
        if 'message_id' in kwargs:
            invoice_message_id = kwargs.get('message_id')
            bot.delete_message(chat_id, invoice_message_id)
        back_to_the_catalog(chat_id, language, order_canceled_message)
    else:
        error()
Ejemplo n.º 2
0
def count_callback_query(call):
    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

    chat_id = call.message.chat.id
    user_id = call.from_user.id
    language = userservice.get_user_language(user_id)
    bot.answer_callback_query(call.id)
    bot.clear_step_handler_by_chat_id(chat_id)
    selected_number = int(call.data[6:])
    current_dish = userservice.get_current_user_dish(user_id)
    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, selected_number)
        cart = userservice.get_user_cart(user_id)
        total = _total_cart_sum(cart)
        bot.delete_message(chat_id, call.message.message_id)
        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)
        catalog_message = strings.get_string('catalog.start', language)
        bot.send_message(chat_id, catalog_message, parse_mode='HTML')
Ejemplo n.º 3
0
def confirmation_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_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)

    if not message.text:
        error()
        return
    if strings.get_string('order.confirm', language) in message.text:
        total = kwargs.get('total')
        user = userservice.get_user_by_telegram_id(user_id)
        order = orderservice.confirm_order(user_id, user.full_user_name, total)
        botutils.to_main_menu(chat_id, language,
                              strings.get_string('notifications.wait'))
        current_user = userservice.get_user_by_id(user_id)
        count_orders = current_user.count_orders
        notify_new_order(order, total, count_orders)
    elif strings.get_string('order.cancel', language) in message.text:
        userservice.clear_user_cart(user_id)
        order_canceled_message = strings.get_string('order.canceled', language)
        if 'message_id' in kwargs:
            invoice_message_id = kwargs.get('message_id')
            bot.delete_message(chat_id, invoice_message_id)
        botutils.to_main_menu(chat_id, language, order_canceled_message)
    else:
        error()
Ejemplo n.º 4
0
 def validate_channel_name_url(self, field):
     if field.data.strip() != '':
         url_re = r'https?:\/\/(t(elegram)?\.me|telegram\.org)\/([a-z0-9\_])'
         if not (re.match(url_re, field.data)
                 or field.data.startswith('@')):
             raise ValidationError(
                 'Указан неверный формат ссылки или юзернейма канала')
         if re.match(url_re, field.data):
             channel_name = '@' + field.data[field.data.rfind('/') + 1:]
         else:
             channel_name = field.data
         if channels.channel_exists(channel_name):
             raise ValidationError('Такой канал уже добавлен')
         try:
             channel_chat = telegram_bot.get_chat(channel_name)
         except ApiException:
             raise ValidationError(
                 'Указанный канал отсутствует, либо является привытаным')
         if channel_chat.type != 'channel':
             raise ValidationError('Указанный юзернейм не является каналом')
         try:
             sent_message = telegram_bot.send_message(
                 channel_name, 'Test', disable_notification=True)
             telegram_bot.delete_message(sent_message.chat.id,
                                         sent_message.message_id)
         except ApiException:
             raise ValidationError(
                 'Бот не имеет доступа к отправке и удалению сообщений')
         self.chat_id = sent_message.chat.id
         self.channel = channel_chat
Ejemplo n.º 5
0
def dish_action_processor(message: Message, **kwargs):
    chat_id = message.chat.id
    user_id = message.from_user.id
    language = userservice.get_user_language(user_id)
    message_id = kwargs.get('message_id')
    current_dish = userservice.get_current_user_dish(user_id)
    if strings.get_string('go_back', language) in message.text:
        #back_to_the_catalog(chat_id, language, parent_category=current_dish.category.parent)
        botutils.to_main_menu(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, dish_action_processor)

    else:
        choose_dish_processor(message, category=current_dish.category)

    if message_id:
        bot.delete_message(chat_id, message_id)