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 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 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 create(message): l = [message.text.split('\n')] command = "INSERT INTO `all`(`user_id`, `event_id`, `name`, `date`, `time`, `adress`, `theme`, `description`)" \ " VALUES('{}, {}, {}, {}, {}, {}, {}, {}')".format(message.from_user.id, cnt, l[0], l[1], l[2], l[3], the, l[4]) cursor.execute(command) db.commit() bot.send_message(message.chat.id, 'ГОТОВО!') bot.register_next_step_handler(message, start(False))
def print_reminds_list(id): """Функция просмотра списка напоминаний""" message = '' if id in reminds: for i, remind in enumerate(reminds[id]): message += f'№{i + 1} ({remind["time"]}) {remind["text"]}\n' else: message = 'Список пуст' bot.send_message(id, message)
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 reset(message): if db.hasUser(message.chat.id): db.deleteUser(message.chat.id) bot.send_message( message.chat.id, answers.resetSuccess, reply_markup=keyboards.removeKeyBoard() ) else: bot.send_message( message.chat.id, answers.resetFailure, reply_markup=keyboards.removeKeyBoard() )
def setFiltersStart(message: Message): # start asking of filters db.changeUserState(message.chat.id, state=states.FILTER_CATEGORY, filters=dict(), page=1) # make keyboard of categories filters = http.getFilters() reply = keyboards.listKeyboard( map(lambda item: item['name'], filters['categories'])) bot.send_message(message.chat.id, answers.filterCategory, reply_markup=reply)
def willFilter(message: Message): if message.text == 'Yes': setFiltersStart(message) elif message.text == 'No': # show all products db.changeUserState(message.chat.id, state=states.SHOW_PRODUCTS, filters=dict(), page=1) # send products getProducts(message) else: bot.send_message(message.chat.id, answers.invalidValue)
def enterFind(message: Message): if not message.text: # send error message bot.send_message(message.chat.id, answers.findInvalid) return # change state to show db.changeUserState(message.chat.id, state=states.SHOW_PRODUCTS) # find products products: dict = http.findProducts(message.text) if not len(products.get('data')): bot.send_message(message.chat.id, answers.notFound, reply_markup=keyboards.removeKeyBoard()) # show products for product in products.get('data'): helpers.sendProduct(message.chat.id, product)
def sendProduct(userID: str, product: dict): product['colors'] = ', '.join( loads(product['colors']) if isinstance(product['colors'], str ) else product['colors']) product['sizes'] = ', '.join( loads(product['sizes']) if isinstance(product['sizes'], str ) else product['sizes']) reply = keyboards.linkKeyboard( product['name'], f"{config.site_url}products/{product['slug']}") bot.send_photo(userID, f"{config.images_base_url}/{product['photo']}", reply_markup=keyboards.removeKeyBoard()) #bot.send_photo(userID, 'https://i.insider.com/5eda82ae3ad8617d4e1c0b2e?width=1100&format=jpeg&auto=webp') bot.send_message(userID, answers.productItem.format(**product), reply_markup=reply)
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 find(message: Message): db.changeUserState(message.chat.id, state=states.FIND) bot.send_message(message.chat.id, answers.findMessage, reply_markup=keyboards.removeKeyBoard())
def theme(message): global the the = message.text bot.send_message(message.chat.id, "Напишите информацию о вашем событии по шаблону \n Название \n Дата \n Время " "\n Место \n проведения \n Описание") bot.register_next_step_handler(message, create)
def products(message: Message): reply = keyboards.isFilterProducts() # send question and create user bot.send_message(message.chat.id, answers.productStart, reply_markup=reply) db.createUser(message.chat.id, filters=dict())
def site(message: Message): bot.send_message( message.chat.id, answers.site, reply_markup=keyboards.removeKeyBoard() )
def contacts(message: Message): bot.send_message( message.chat.id, answers.contacts, reply_markup=keyboards.removeKeyBoard() )
def send_message_func(remind): """Функция выдачи напоминания""" bot.send_message(remind['id'], remind['text']) reminds[remind['id']].remove(remind) return schedule.CancelJob