def request_registration_phone_number_handler(message: Message, **kwargs): chat_id = message.chat.id user_id = message.from_user.id name = kwargs.get('name') language = kwargs.get('language') def error(): 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) if message.contact is not None: phone_number = message.contact.phone_number else: if message.text is None: error() return else: match = re.match(r'\+*998\s*\d{2}\s*\d{3}\s*\d{2}\s*\d{2}', message.text) if match is None: error() return phone_number = match.group() userservice.register_user(user_id, message.from_user.username, name, phone_number, language) success_message = strings.get_string("welcome.registration_successfully", language) botutils.to_main_menu(chat_id, language, success_message)
def about_handler(message: Message): chat_id = message.chat.id user_id = message.from_user.id language = userservice.get_user_language(user_id) about_text = settings.get_about_text('ru') telegram_bot.send_message(chat_id, about_text) botutlis.to_main_menu(chat_id, language)
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 catalog_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('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: botutils.to_main_menu(chat_id, language) elif strings.get_string('catalog.cart', language) in message.text: cart.cart_processor(message) elif strings.get_string('catalog.make_order', language) in message.text: orders.order_processor(message) else: try: category_name = message.text dishes = dishservice.get_dishes_by_category_name( category_name, language, sort_by_number=True) except exceptions.CategoryNotFoundError: error() return 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)
def payment_method_processor(message: Message): chat_id = message.chat.id user_id = message.from_user.id language = userservice.get_user_language(user_id) current_order = orderservice.get_current_order_by_user(user_id) 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) def phone_number(): current_order = orderservice.set_phone_number(user_id) _to_the_confirmation(chat_id, current_order, language) if not message.text: error() return if strings.get_string('go_to_menu', language) in message.text: botutils.to_main_menu(chat_id, language) elif strings.get_string('go_back', language) in message.text: if current_order.shipping_method == Order.ShippingMethods.PICK_UP: back_to_the_catalog(chat_id, language) elif current_order.shipping_method == Order.ShippingMethods.DELIVERY: back_to_the_catalog(chat_id, language) elif strings.from_order_payment_method(Order.PaymentMethods.CASH, language) in message.text: orderservice.set_payment_method(user_id, Order.PaymentMethods.CASH) phone_number() 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 process_phone_number(message: Message, **kwargs): chat_id = message.chat.id user_id = message.from_user.id language = kwargs.get('language', 'ru') def error(): error_msg = strings.get_string('welcome.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, process_phone_number) if message.contact is not None: phone_number = message.contact.phone_number else: if message.text is None: error() return else: match = re.match(r'\+*998\s*\d{2}\s*\d{3}\s*\d{2}\s*\d{2}', message.text) if match is None: error() return phone_number = match.group() full_user_name = message.from_user.first_name if message.from_user.last_name: full_user_name += " " + message.from_user.last_name userservice.register_user(user_id, message.from_user.username, full_user_name, phone_number, language) success_message = strings.get_string('welcome.registration_successfully', language) botutils.to_main_menu(chat_id, language, success_message)
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)
def change_language_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('language.change', language) telegram_bot.send_message(chat_id, error_msg) telegram_bot.register_next_step_handler_by_chat_id( chat_id, change_language_processor) if not message.text: error() return if strings.get_string('go_back', language) in message.text: botutlis.to_main_menu(chat_id, language) return elif strings.get_string('language.russian') in message.text: new_language = 'ru' userservice.set_user_language(user_id, new_language) elif strings.get_string('language.uzbek') in message.text: new_language = 'uz' else: error() return userservice.set_user_language(user_id, new_language) success_message = strings.get_string('language.success', new_language) botutlis.to_main_menu(chat_id, new_language, success_message)
def welcome(message): user_id = message.from_user.id chat_id = message.chat.id current_user = userservice.get_user_by_telegram_id(user_id) if current_user: botutils.to_main_menu(chat_id, current_user.language) return process_user_language(message)
def profile_handler(message: Message): chat_id = message.chat.id user_id = message.from_user.id language = userservice.get_user_language(user_id) current_user = userservice.get_user_by_id(user_id) msg = '<b>До бесплатного кофе: {}</b>'.format( (10 - (current_user.count_orders % 10)) - 1) telegram_bot.send_message(chat_id, text=msg, parse_mode='HTML') botutlis.to_main_menu(chat_id, language)
def about_handler(message: Message): chat_id = message.chat.id user_id = message.from_user.id language = userservice.get_user_language(user_id) # about_text = settings.get_about_text('ru') # telegram_bot.send_message(chat_id, about_text) for num in range(3): photo = open((Config.CERTIFY_DIRECTORY + str(num) + '.jpg'), 'rb') telegram_bot.send_photo(chat_id, photo=photo) botutlis.to_main_menu(chat_id, language)
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)
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)
def comments_processor(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('comments.error', language) bot.send_message(chat_id, error_msg) bot.register_next_step_handler_by_chat_id(chat_id, comments_processor) if not message.text: error() return if strings.get_string('go_to_menu', language) in message.text: botutils.to_main_menu(chat_id, language) else: username = message.from_user.first_name if message.from_user.last_name: username += " {}".format(message.from_user.last_name) new_comment = commentservice.add_comment(user_id, message.text, username) notifications.notify_new_comment(new_comment) thanks_message = strings.get_string('comments.thanks', language) botutils.to_main_menu(chat_id, language, thanks_message)
def profile_handler(message: Message): global free_coffee chat_id = message.chat.id user_id = message.from_user.id language = userservice.get_user_language(user_id) current_user = userservice.get_user_by_id(user_id) print(current_user.count_orders % 10) if (current_user.count_orders % 10) == 9: free_coffee = '<b>Вы можете забрать следующий заказ бесплатно!</b>' telegram_bot.send_message(chat_id, text=(strings.get_string( 'all_coffee', language).format( current_user.count_orders, free_coffee)), parse_mode='HTML') else: free_coffee = '' telegram_bot.send_message(chat_id, text=(strings.get_string( 'all_coffee', language).format( current_user.count_orders, free_coffee)), parse_mode='HTML') botutlis.to_main_menu(chat_id, language)
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)
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)
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=None) 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, 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, 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) elif dish.image_id: bot.send_photo(chat_id, dish.image_id, caption=dish_info, reply_markup=dish_keyboard, parse_mode='HTML') else: bot.send_message(chat_id, dish_info, reply_markup=dish_keyboard, parse_mode='HTML') 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)