F
F
fabeew2021-04-08 13:05:19
Python
fabeew, 2021-04-08 13:05:19

How to implement dynamic deletion and editing of Inline button from database?

There is a table in the database that contains text and url, based on these parameters, an InlineKeyboard is formed in a cycle, through the admin panel, it is necessary to implement the ability to edit and delete these buttons. For convenience, when switching to the editing / deleting panel, InlineKeyboard should be displayed and after clicking on the desired button, it should be deleted from the database, or the user is prompted to change the text or url.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jerwright, 2021-04-08
@fabeew

Tried. Everything seems to be working correctly:

@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())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question