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()
def count_processor(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('catalog.dish_action_error', language) bot.send_message(chat_id, error_message) bot.register_next_step_handler_by_chat_id(chat_id, count_processor) if strings.get_string('go_back', language) in message.text: botutils.to_main_menu(chat_id, language) return if not message.text.isdigit() or int(message.text) >= 11 or int( message.text) <= 0: error() return userservice.clear_user_cart(user_id) userservice.add_dish_to_cart(user_id, dishservice.get_dish_by_id(1), int(message.text)) orderservice.make_an_order(user_id) orderservice.set_shipping_method(user_id, Order.ShippingMethods.PICK_UP) orderservice.set_payment_method(user_id, Order.PaymentMethods.CASH) orderservice.set_phone_number(user_id) current_order = orderservice.get_current_order_by_user(user_id) _to_the_confirmation(chat_id, current_order, language)
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
def notification_callback_query(call): order = orderservice.get_order_by_id(call.data[6:]) if call.data[0:6] == 'accept': user_id = order.user_id current_user = userservice.get_user_by_id(user_id) current_user.count_orders += order.order_items.all()[0].count db.session.add(current_user) db.session.commit() telegram_bot.send_message(user_id, strings.get_string('notifications.accepted')) if (10 - (current_user.count_orders % 10)) - 1 == 0: free_coffee_msg = '<b>При оформлении следующего заказа, один кофе бесплатный!</b>' telegram_bot.send_message(user_id, text=free_coffee_msg, parse_mode='HTML') else: free_coffee_msg = '<b>До бесплатного кофе: {}</b>'.format((10 - (current_user.count_orders % 10)) - 1) telegram_bot.send_message(user_id, text=free_coffee_msg, parse_mode='HTML') telegram_bot.edit_message_reply_markup(call.message.chat.id, call.message.message_id, reply_markup=None) telegram_bot.answer_callback_query(call.id) else: user_id = order.user_id userservice.clear_user_cart(user_id) telegram_bot.send_message(user_id, strings.get_string('notifications.canceled')) telegram_bot.edit_message_reply_markup(call.message.chat.id, call.message.message_id, reply_markup=None) telegram_bot.answer_callback_query(call.id)
def notification_callback_query(call): order = orderservice.get_order_by_id(call.data[6:]) if call.data[0:6] == 'accept': user_id = order.user_id current_user = userservice.get_user_by_id(user_id) current_user.count_orders += order.order_items.all()[0].count db.session.add(current_user) db.session.commit() telegram_bot.send_message(user_id, strings.get_string('notifications.accepted')) telegram_bot.edit_message_reply_markup(call.message.chat.id, call.message.message_id, reply_markup=None) telegram_bot.answer_callback_query(call.id) else: user_id = order.user_id userservice.clear_user_cart(user_id) telegram_bot.send_message(user_id, strings.get_string('notifications.canceled')) telegram_bot.edit_message_reply_markup(call.message.chat.id, call.message.message_id, reply_markup=None) telegram_bot.answer_callback_query(call.id)