def _get_products_keyboard(self, func: Callable, products: Iterable[Product], offset: int, back_offset: int, max_offset: int, is_need_back: bool = None, *args) -> KB: if is_need_back is None: is_need_back = True kb = KB(self._serializer) for index, product in enumerate(products): if index >= IN_PAGE: break kb.button(product.name, self.open_product, (product.id, back_offset)) if is_need_back: pager = kb.pager(func, IN_PAGE, offset, max_offset, back_offset, *args) else: pager = kb.pager(func, IN_PAGE, offset, max_offset, *args) if pager is not None and is_need_back: pager.back(self.open_categories, back_offset) return kb
def open_product(self, update: Update, context, product_id: int, back_offset: int): product_id, back_offset = int(product_id), int(back_offset) image_path = self._catalog.get_png_path(product_id) desciption = self._catalog.get_description(product_id) kb = KB(self._serializer) kb.button("add to cart", self.add_to_cart, [product_id]) if isinstance(image_path, Path): photo = image_path.open('rb') msg = self.send_message_photo(update=update, context=context, photo=photo, kb=kb, caption=desciption) photo_id = msg.photo[-1].file_id self._catalog.update_photo_id(photo_id, product_id) photo.close() elif isinstance(image_path, str): self.send_message_photo(update=update, context=context, caption=desciption, kb=kb, photo=image_path)
def greeting(self, update: Update, context, name: str): kb = KeyboardBuilder() kb.button("Find anagram", "Anagram") kb.button("Find palindrome", "palindrome") markup = kb.get() msg = f"Hi, {name}! Enter '/palindrome' to find palindrome." \ f" Enter '/anagram' to find anagram, use '$add ... to add some words" self.send_message(update, context, msg, markup=markup)
def _abs_open_categories(self, func: Callable, categories: Iterable[Category], offset: int, max_offset: int) -> KB: kb = KB(self._serializer) for category in categories: kb.button(category.name, func, (0, category.id, offset)) kb.pager(self.open_categories, IN_PAGE, offset, max_offset) return kb
def greeting_callback(self, update, context): kb = KeyboardBuilder().elements_in_line(1) kb.button("1", self.callback, args=(1, 3, 4, 9))\ .button("2", self.callback, args=(2,))\ .button("3", self.callback, args=(3,))\ .button("show", self.show_callback)\ .button("delete history", self.delete_history_callback)\ update.message.reply_text('Please choose:', reply_markup=kb.get())
def add_to_cart(self, update, context, product_id: int): product_id = int(product_id) self._catalog.insert_into_cart(update._effective_user.id, product_id, 1) if len(update._effective_message.reply_markup['inline_keyboard']) == 1: kb = KB(self._serializer) kb.button("add to cart", self.add_to_cart, [product_id]).line().button("go to cart", self.get_cart_start) self.edit_message_reply_markup(update, context, kb)
def get_cart_start(self, update: Update, context, *args): max_offset, product_data = self._catalog.get_product_from_cart( update._effective_user.id, 0, 1) kb = KB(self._serializer) kb.button("delete", self.delete_product_from_cart, (0, product_data[0])).\ pager(callback=self.get_cart, in_page=1, current_offset=0, max_offset=max_offset) self.send_message_photo( update=update, context=context, photo=product_data[2], caption=f"Product: {product_data[1]}, quantity: {product_data[3]}", kb=kb)
def get_cart(self, update, context, offset: int): offset = int(offset) max_offset, product_data = self._catalog.get_product_from_cart( update._effective_user.id, offset, 1) kb = KB(self._serializer) kb.button("delete", self.delete_product_from_cart, (offset, product_data[0])). \ pager(callback=self.get_cart, in_page=1, current_offset=offset, max_offset=max_offset) input_media_photo = InputMediaPhoto( media=product_data[2], caption=f"Product: {product_data[1]}, quantity: {product_data[3]}") self.edit_message_photo(update=update, context=context, photo=input_media_photo) self.edit_message_reply_markup(update=update, context=context, kb=kb)