Esempio n. 1
0
def category_handler(call):
    category_id = ''.join(call.data.split('_')[1::])
    category = Category.objects.get(id=category_id)
    kb = InlineKeyboardMarkup(row_width=2)
    if category.subcategories:
        categories = category.subcategories
        buttons = [
            InlineKeyboardButton(text=category.title,
                                 callback_data=f'category{category.id}')
            for category in category.subcategories
        ]

        kb.add(*buttons)
        bot.edit_message_text(category.title,
                              chat_id=call.message.chat.id,
                              message_id=call.message.message_id,
                              reply_markup=kb)

    elif category.is_leaf:
        for product in category.products:
            button = InlineKeyboardButton(
                text='Добавить в карзину',
                callback_data=f'product_{product.id}')
            kb.keyboard = [[button.to_dict()]]

            bot.send_photo(call.massege.chat.id,
                           product.image.read(),
                           caption=product.description,
                           disable_notification=True,
                           reply_markup=kb)