connect = sqlite3.connect('INFOBOT.db')
cursor = connect.cursor()
@dp.message_handler()
async def handle_message(message: types.Message):
cursor.execute("SELECT * FROM packs WHERE user_id = ?", (message.from_user.id, ))
photo_data = cursor.fetchall()
if not photo_data:
await message.answer("Фото не найдено")
return
for i in cursor.execute("SELECT * FROM packs WHERE user_id = ?", (message.from_user.id, )):
items = i
photo = items[6]
keyboard = generate_keyboard(message.from_user.id, message.text, 1, cursor.execute("SELECT COUNT(*) FROM packs WHERE user_id = ?", (message.from_user.id, )).fetchone()[0])
await message.answer_photo(photo=photo, caption = items[4],reply_markup=keyboard)
@dp.callback_query_handler()
async def navigate(call: types.CallbackQuery):
if call.data == 'ignore':
await call.answer()
return
if call.data == '1' or call.data == '2':
await start_photo_gallery(call.message, call.data)
await call.answer()
return
user_id, user_tag, photo_index = call.data.split(':')
photo_index = int(photo_index)
total_count = cursor.execute("SELECT COUNT(*) FROM packs WHERE user_id = ?", (user_id, )).fetchone()[0]
if photo_index < 0 or photo_index >= total_count:
await call.answer('No more photos')
return
cursor.execute("SELECT * FROM packs WHERE user_id = ?", (user_id, ))
photo_data = cursor.fetchone()
for i in cursor.execute("SELECT * FROM packs WHERE user_id = ?", (call.from_user.id, )):
items = i
photo = items[6]
keyboard = generate_keyboard(user_id, user_tag, photo_index, total_count)
await call.message.edit_media(
media=types.InputMediaPhoto(media=photo, caption=items[4]),
reply_markup=keyboard)
await call.answer()
def generate_keyboard(user_id, user_tag, photo_index, total_count):
buttons = []
if photo_index > 0:
buttons.append(types.InlineKeyboardButton(text='<<', callback_data=f'{user_id}:{user_tag}:0'))
buttons.append(types.InlineKeyboardButton(text='<', callback_data=f'{user_id}:{user_tag}:{max(0, photo_index - 1)}'))
else:
buttons.append(types.InlineKeyboardButton(text='-', callback_data=f'ignore'))
buttons.append(types.InlineKeyboardButton(text='-', callback_data=f'ignore'))
buttons.append(types.InlineKeyboardButton(text=f'{photo_index + 1}/{total_count}', callback_data=f'ignore'))
if photo_index < total_count - 1:
buttons.append(types.InlineKeyboardButton(text='>', callback_data=f'{user_id}:{user_tag}:{min(total_count - 1, photo_index + 1)}'))
buttons.append(types.InlineKeyboardButton(text='>>', callback_data=f'{user_id}:{user_tag}:{total_count - 1}'))
else:
buttons.append(types.InlineKeyboardButton(text='-', callback_data=f'ignore'))
buttons.append(types.InlineKeyboardButton(text='-', callback_data=f'ignore'))
keyboard = types.InlineKeyboardMarkup(
inline_keyboard=[buttons]
)
return keyboard