def show_products_or_subcategory(call): obj_id = call.data.split("_")[1] category = models.Category.objects(id=obj_id).get() if category.is_parent: keyboard = keyboards.ReplyIKB(iterable=category.subcategory, lookup_field='id', named_arg='category') #bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text='Сделайте свой выбор', reply_markup=keyboard) bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="Сделайте свой выбор", reply_markup=keyboard.generate_ikb()) else: #print(call.data) product_objects = category.get_products() for i in product_objects: photo = i.photo.read() markup = telebot.types.InlineKeyboardMarkup() button = telebot.types.InlineKeyboardButton( text='Добавить в корзину', callback_data='add-to-cart_' + str(i.id)) markup.add(button) bot.send_message(call.message.chat.id, parse_mode='HTML', text=f"<b>{i.title}</b>") bot.send_photo(call.message.chat.id, photo, parse_mode='HTML', caption=f"<b>Цена: " + str(i.price / 100) + "</b>" + f" {i.description}", reply_markup=markup)
def show_products_or_subcategory(call): obj_id = call.data.split("_")[1] category = models.Category.objects(id=obj_id).get() if category.is_parent: keyboard = keyboards.ReplyIKB(iterable=category.subcategory, lookup_field='id', named_arg='category') #bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text='Сделайте свой выбор', reply_markup=keyboard) bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="Сделайте свой выбор", reply_markup=keyboard.generate_ikb()) else: print(call.data)
def go_back(call): obj_id = call.data.split("_")[1] category = models.Category.objects(id=obj_id).get() if category.is_root: keyboard = keyboards.ReplyIKB(key = 'root', lookup_field='id', named_arg='category') keyboard.generate_ikb() else: keyboard = keyboards.ReplyIKB( iterable=category.parent.subcategory, lookup_field='id', named_arg='category' ) keyboard.generate_ikb() keyboard.add(InlineKeyboardButton(text="<<", callback_data=f"back_{category.parent.id}")) text="Категории" if not category.parent else category.parent.title bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text=text, reply_markup=keyboard)
def main_text_handler(message): if message.text.lower() == 'информация о магазине': about_str = models.Texts.objects(title='About').get().body bot.send_message(message.chat.id, about_str) main_menu(message) return elif message.text.lower() == 'последние новости': about_str = models.Texts.objects(title='Last news').get().body bot.send_message(message.chat.id, about_str) main_menu(message) return elif message.text.lower() == 'продукты': keyboard = keyboards.ReplyIKB(key='root', lookup_field='id', named_arg='category') bot.send_message(message.chat.id, "Категории" , reply_markup=keyboard.generate_ikb()) elif message.text.lower() == 'продукты со скидкой': product_objects = models.Product.objects(is_discount=True) for i in product_objects: photo = i.photo.read() markup = telebot.types.InlineKeyboardMarkup() button = telebot.types.InlineKeyboardButton(text='Добавить в корзину', callback_data='add-to-cart_' + str(i.id)) markup.add(button) bot.send_message(message.chat.id, text=f"<b>{i.title}</b>", parse_mode='HTML') bot.send_photo(message.chat.id, photo, parse_mode='HTML', caption=f"Старая цена: " + str(i.price/100) + "\n<b>Новая цена:" + str(i.new_price/100) + "</b>" + f"\n{i.description}", reply_markup=markup) elif message.text.lower() == 'корзина': if message.from_user.id in models.User.objects.distinct("user_id"): user_obj = models.User.objects.get(user_id=message.from_user.id) user_cart = models.Cart.objects(user=user_obj, active=True) if not user_cart: bot.send_message(message.chat.id, "Корзина пуста") return cart_text = "" for i in user_cart: cart_text = cart_text + i.product.title + " " + str(i.product.get_price) + " грн.\n" cart_summ = models.Cart().get_catr_summ(user_obj) cart_text = cart_text + "Общая сумма покупок: " + str(cart_summ) + " грн." markup = telebot.types.InlineKeyboardMarkup() by_button = telebot.types.InlineKeyboardButton(text='Купить', callback_data='by-cart_' + str(i.user.id)) clear_button = telebot.types.InlineKeyboardButton(text='Очистить корзину', callback_data='clear-cart_' + str(i.user.id)) markup.add(by_button, clear_button) bot.send_message(message.chat.id, cart_text, reply_markup=markup) else: bot.send_message(message.chat.id, "Корзина пуста") else: pass
def main_text_handler(message): if message.text.lower() == 'информация о магазине': about_str = models.Texts.objects(title='About').get().body bot.send_message(message.chat.id, about_str) main_menu(message) return elif message.text.lower() == 'последние новости': about_str = models.Texts.objects(title='Last news').get().body bot.send_message(message.chat.id, about_str) main_menu(message) return elif message.text.lower() == 'продукты': category_queryset = models.Category.get_root_categories() keyboard = keyboards.ReplyIKB(iterable=category_queryset, lookup_field='id', named_arg='category') bot.send_message(message.chat.id, "Выберите интересующую Вас категорию: ", reply_markup=keyboard.generate_ikb()) else: pass