Помогите упростить код телеграм бота на пайтоне. Все работает, но если добавить еще хендлеры для других дней недели и в каждом будет группа, то код получится громаднейший
@dp.message_handler(text='ПН')
async def send_text(message: types.Message):
connect = sqlite3.connect('main.db')
cursor = connect.cursor()
cursor.execute('SELECT grup FROM Users WHERE id = ?', (message.from_user.id,))
result = cursor.fetchone()
if result is None:
print('none')
for row in result:
if row == 'СВ-31':
cursor.execute("SELECT `raspisanie` FROM `Schedule` WHERE `grup` = 'SV_31__PN_n'")
row = cursor.fetchone()
await message.answer(row[0], reply_markup=il_1)
elif row == 'СП-31':
cursor.execute("SELECT `raspisanie` FROM `Schedule` WHERE `grup` = 'SP_31__PN_n'")
row = cursor.fetchone()
await message.answer(row[0], reply_markup=il_1)
elif row == 'СА-31':
cursor.execute("SELECT `raspisanie` FROM `Schedule` WHERE `grup` = 'SA_31__PN_n'")
row = cursor.fetchone()
await message.answer(row[0], reply_markup=il_1)
connect.close()
@dp.message_handler(text='ВТ')
async def send_text(message: types.Message):
connect = sqlite3.connect('main.db')
cursor = connect.cursor()
cursor.execute('SELECT grup FROM Users WHERE id = ?', (message.from_user.id,))
result = cursor.fetchone()
if result is None:
print('none')
for row in result:
if row == 'СВ-31':
cursor.execute("SELECT `raspisanie` FROM `Schedule` WHERE `grup` = 'SV_31__VT_n'")
row = cursor.fetchone()
await message.answer(row[0], reply_markup=il_3)
elif row == 'СП-31':
cursor.execute("SELECT `raspisanie` FROM `Schedule` WHERE `grup` = 'SP_31__VT_n'")
row = cursor.fetchone()
await message.answer(row[0], reply_markup=il_3)
elif row == 'СА-31':
cursor.execute("SELECT `raspisanie` FROM `Schedule` WHERE `grup` = 'SA_31__VT_n'")
row = cursor.fetchone()
await message.answer(row[0], reply_markup=il_3)
connect.close()
@dp.callback_query_handler(text='PN_p')
async def answer(call: CallbackQuery):
connect = sqlite3.connect('main.db')
cursor = connect.cursor()
cursor.execute('SELECT grup FROM Users WHERE id = ?', (call.from_user.id,))
result = cursor.fetchone()
if result is None:
print('none')
for row in result:
if row == 'СВ-31':
cursor.execute("SELECT `raspisanie` FROM `Schedule` WHERE `grup` = 'SV_31__PN_p'")
row = cursor.fetchone()
await call.message.answer(row[0], reply_markup=il_2)
@dp.callback_query_handler(text='PN_n')
async def answer(call: CallbackQuery):
connect = sqlite3.connect('main.db')
cursor = connect.cursor()
cursor.execute('SELECT grup FROM Users WHERE id = ?', (call.from_user.id,))
result = cursor.fetchone()
if result is None:
print('none')
for row in result:
if row == 'СВ-31':
cursor.execute("SELECT `raspisanie` FROM `Schedule` WHERE `grup` = 'SV_31__PN_n'")
row = cursor.fetchone()
await call.message.answer(row[0], reply_markup=il_1)
@dp.callback_query_handler(text='VT_p')
async def answer(call: CallbackQuery):
connect = sqlite3.connect('main.db')
cursor = connect.cursor()
cursor.execute('SELECT grup FROM Users WHERE id = ?', (call.from_user.id,))
result = cursor.fetchone()
if result is None:
print('none')
for row in result:
if row == 'СВ-31':
cursor.execute("SELECT `raspisanie` FROM `Schedule` WHERE `grup` = 'SV_31__VT_p'")
row = cursor.fetchone()
await call.message.answer(row[0], reply_markup=il_4)
@dp.callback_query_handler(text='VT_n')
async def answer(call: CallbackQuery):
connect = sqlite3.connect('main.db')
cursor = connect.cursor()
cursor.execute('SELECT grup FROM Users WHERE id = ?', (call.from_user.id,))
result = cursor.fetchone()
if result is None:
print('none')
for row in result:
if row == 'СВ-31':
cursor.execute("SELECT `raspisanie` FROM `Schedule` WHERE `grup` = 'SV_31__VT_n'")
row = cursor.fetchone()
await call.message.answer(row[0], reply_markup=il_3)