def getProducts(message: Message): user = db.getUser(message.chat.id) if not user: db.createUser(message.chat.id, states.SHOW_PRODUCTS) user = db.getUser(message.chat.id) page = user[3] print(page) if page <= 0: db.changeUserState(message.chat.id, page=1) bot.send_message(message.chat.id, answers.outOfRange, reply_markup=keyboards.removeKeyBoard()) return httpProduct = http.getProducts(user[2], page) if not len(httpProduct['data']): bot.send_message(message.chat.id, answers.noData) return for product in httpProduct['data']: helpers.sendProduct(message.chat.id, product)
def filterSize(message: Message): userFilters: dict = db.getUser(message.chat.id)[2] if message.text != '.': if message.text in getSizes(): # change db data userFilters['size'] = message.text else: bot.send_message(message.chat.id, answers.filterSizeInvalid) return # change db data db.changeUserState(message.chat.id, state=states.FILTER_COLOR, filters=userFilters) # get next step data filters = http.getFilters() colors = filters['colors'] # send next step bot.send_message(message.chat.id, answers.filterColor, reply_markup=keyboards.listKeyboard(colors))
def filterCategory(message: Message): userFilters: dict = db.getUser(message.chat.id)[2] userFilters['categories'] = [] if message.text != '.': # get filters filters = http.getFilters() categories: list = list( map(lambda item: item['name'], filters['categories'])) # find category index = categories.index(message.text) if index != -1: # add filter to db catID = filters['categories'][index]['id'] userFilters['categories'] = {catID: True} else: bot.send_message(message.chat.id, answers.filterCategoryInvalid) return # set new data to db db.changeUserState(message.chat.id, state=states.FILTER_SIZE, filters=userFilters) # send next step bot.send_message(message.chat.id, answers.filterSize, reply_markup=keyboards.listKeyboard(getSizes()))
def filterColor(message: Message): userFilters: dict = db.getUser(message.chat.id)[2] if message.text != '.': filters = http.getFilters() colors = filters['colors'] if message.text in colors: # change db data userFilters['color'] = message.text else: bot.send_message(message.chat.id, answers.filterColorInvalid) return # set data to db db.changeUserState(message.chat.id, state=states.FILTER_PRICE, filters=userFilters) # send next step bot.send_message(message.chat.id, answers.filterPrice, reply_markup=keyboards.removeKeyBoard())
def getFilters(message: Message): user = db.getUser(message.chat.id) if not user: # send no filters response bot.send_message(message.chat.id, answers.noFilters) return userFilters = user[2] filtersData = parseFilters(userFilters) # send response bot.send_message(message.chat.id, answers.userFilters.format(**filtersData))
def filterPrice(message: Message): userFilters: dict = db.getUser(message.chat.id)[2] if message.text != '.': try: # parse range priceRange = parseRange(message.text) # set data to db userFilters['priceRange'] = { 'from': min(priceRange), 'to': max(priceRange) } except Exception: bot.send_message(message.chat.id, answers.filterPriceInvalid) return # get products db.changeUserState(message.chat.id, state=states.SHOW_PRODUCTS, filters=userFilters) getProducts(message)
def getPrevProducts(message: Message): user = db.getUser(message.chat.id) page = user[3] - 1 db.changeUserState(message.chat.id, page=page) getProducts(message)