Untitled
unknown
ruby
a year ago
6.4 kB
3
Indexable
cursor.execute(''' CREATE TABLE IF NOT EXISTS profiles ( id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER, city TEXT, name TEXT, age INTEGER, height INTEGER, weight INTEGER, shoe_size INTEGER, description TEXT, price_per_hour INTEGER, orientation TEXT, contacts TEXT, skills TEXT, photo_path TEXT ) ''') conn.commit() bot = TeleBot('your_token') @bot.message_handler(commands=['start']) def start(message): # Создание клавиатуры keyboard = types.ReplyKeyboardMarkup(row_width=2) create_profile_button = types.KeyboardButton(text='Создать анкету') my_profile_button = types.KeyboardButton(text='Моя анкета') info_button = types.KeyboardButton(text='Инфо') keyboard.add(create_profile_button, my_profile_button, info_button) bot.send_message(message.chat.id, 'Выберите действие:', reply_markup=keyboard) @bot.message_handler(func=lambda message: message.text == 'Создать анкету') def create_profile(message): bot.send_message(message.chat.id, 'Введите город:') bot.register_next_step_handler(message, get_city) def get_city(message): city = message.text bot.send_message(message.chat.id, 'Введите имя:') bot.register_next_step_handler(message, get_name, city) def get_name(message, city): name = message.text bot.send_message(message.chat.id, 'Введите возраст:') bot.register_next_step_handler(message, get_age, city, name) def get_age(message, city, name): age = int(message.text) bot.send_message(message.chat.id, 'Введите рост:') bot.register_next_step_handler(message, get_height, city, name, age) def get_height(message, city, name, age): height = int(message.text) bot.send_message(message.chat.id, 'Введите вес:') bot.register_next_step_handler(message, get_weight, city, name, age, height) def get_weight(message, city, name, age, height): weight = int(message.text) bot.send_message(message.chat.id, 'Введите размер чн:') bot.register_next_step_handler(message, get_shoe_size, city, name, age, height, weight) def get_shoe_size(message, city, name, age, height, weight): shoe_size = int(message.text) bot.send_message(message.chat.id, 'Введите описание себя:') bot.register_next_step_handler(message, get_description, city, name, age, height, weight, shoe_size) def get_description(message, city, name, age, height, weight, shoe_size): description = message.text bot.send_message(message.chat.id, 'Введите цену за час:') bot.register_next_step_handler(message, get_price_per_hour, city, name, age, height, weight, shoe_size, description) def get_price_per_hour(message, city, name, age, height, weight, shoe_size, description): price_per_hour = int(message.text) bot.send_message(message.chat.id, 'Введите вашу ориентацию:') bot.register_next_step_handler(message, get_orientation, city, name, age, height, weight, shoe_size, description, price_per_hour) def get_orientation(message, city, name, age, height, weight, shoe_size, description, price_per_hour): orientation = message.text bot.send_message(message.chat.id, 'Введите ваши контакты:') bot.register_next_step_handler(message, get_contacts, city, name, age, height, weight, shoe_size, description, price_per_hour, orientation) def get_contacts(message, city, name, age, height, weight, shoe_size, description, price_per_hour, orientation): contacts = message.text bot.send_message(message.chat.id, 'Введите ваши навыки:') bot.register_next_step_handler(message, get_skills, city, name, age, height, weight, shoe_size, description, price_per_hour, orientation) def get_skills(message, city, name, age, height, weight, shoe_size, description, price_per_hour, orientation): skills = message.text bot.send_message(message.chat.id, 'Загрузите фото:') bot.register_next_step_handler(message, save_profile, city, name, age, height, weight, shoe_size, description, price_per_hour, orientation, skills) def save_profile(message, city, name, age, height, weight, shoe_size, description, price_per_hour, orientation, skills): user_id = message.chat.id photo_file = message.photo[-1] # Сохранение фото в папку 'photo' photo_path = os.path.join('photo', f'{user_id}_{photo_file.file_id}.jpg') photo_file_path = bot.download_file(bot.get_file(photo_file.file_id).file_path) with open(photo_path, 'wb') as photo: photo.write(photo_file_path) data = (user_id, city, name, age, height, weight, shoe_size, description, price_per_hour, orientation, contacts, skills, photo_path) # Вставка данных анкеты в базу данных cursor.execute(''' INSERT INTO profiles (user_id, city, name, age, height, weight, shoe_size, description, price_per_hour, orientation, contacts, skills, photo_path) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ''', data) conn.commit() bot.send_message(message.chat.id, 'Анкета успешно создана') @bot.message_handler(func=lambda message: message.text == 'Моя анкета') def my_profile(message): user_id = message.chat.id profile_data = get_profile(user_id) if profile_data: profile_message = format_profile(profile_data) bot.send_message(message.chat.id, profile_message) else: bot.send_message(message.chat.id, 'Вы еще не создали анкету') def get_profile(user_id): cursor.execute("SELECT * FROM profiles WHERE user_id = ?", (user_id,)) profile_data = cursor.fetchone() return profile_data def format_profile(profile_data): photo_path = profile_data[13] caption = f'Город: {profile_data[2]}\n Имя: {profile_data[3]}\n Возраст: {profile_data[4]}\n Рост: {profile_data[5]}\n Вес: {profile_data[6]}\n Размер чн: {profile_data[7]}\n Описание: {profile_data[8]}\n Цена за час: {profile_data[9]}\n Ориентация: {profile_data[10]}\n Контакты: {profile_data[11]}\n Навыки: {profile_data[12]}' with open(photo_path, 'rb') as photo: bot.send_photo(profile_data[1], photo, caption) return caption @bot.message_handler(func=lambda message: message.text == 'Инфо') def info(message): info_message = 'Бот для создания анкет' bot.send_message(message.chat.id, info_message) bot.polling()
Editor is loading...
Leave a Comment