Попробовал. Вроде бы всё работает правильно:
@bot.message_handler(commands=['addbutton'])
def addingbtn(message):
check_name=bot.send_message(message.from_user.id, f'Текст кнопки?')
action='adding'
bot.register_next_step_handler(check_name, adding_name, action)
@bot.message_handler(commands=['delbutton'])
def delbutn(message):
check_name=bot.send_message(message.from_user.id, f'Текст кнопки?')
action='deleting'
bot.register_next_step_handler(check_name, adding_name, action)
def adding_name(message, action):
try:
sql.execute(f"SELECT * FROM keyboards WHERE text = '{message.text}'")
found_text=sql.fetchone()
except:
found_text=None
text=message.text
check_url=bot.send_message(message.from_user.id, f'URL кнопки?')
bot.register_next_step_handler(check_url, adding_url, text, found_text, action)
def adding_url(message, text, found_text, action):
try:
sql.execute(f"SELECT * FROM keyboards WHERE url = '{message.text}'")
found_url=sql.fetchone()
except:
found_url=None
url=message.text
if found_text!=None:
if action.lower()=='adding':
sql.execute("UPDATE keyboards SET text = ?, url = ? WHERE text = ?", (text, url, text))
db.commit()
bot.reply_to(message, text=f"✅ Вы успешно изменили кнопку")
elif action.lower()=='deleting':
sql.execute(f"DELETE FROM keyboards WHERE text = '{text}'")
db.commit()
bot.reply_to(message, text=f"✅ Вы успешно удалили кнопку")
elif found_url!=None:
if action.lower()=='adding':
print(text)
sql.execute("UPDATE keyboards SET text = ?, url = ? WHERE url = ?", (text, url, url))
db.commit()
bot.reply_to(message, text=f"✅ Вы успешно изменили кнопку")
elif action.lower()=='deleting':
sql.execute(f"DELETE FROM keyboards WHERE url = '{url}'")
db.commit()
bot.reply_to(message, text=f"✅ Вы успешно удалили кнопку")
else:
if action.lower()=='adding':
sql.execute("INSERT INTO keyboards VALUES (?, ?)", (text, url))
db.commit()
bot.reply_to(message, text=f"✅ Вы успешно добавили новую кнопку")
elif action.lower()=='deleting':
bot.reply_to(message, text=f"❌ Кнопка не найдена в базе данных")
sql.execute(f"SELECT * FROM keyboards")
print(sql.fetchall())