S
S
satway2021-11-14 14:14:54
Python
satway, 2021-11-14 14:14:54

callback_query_handler not working?

I can't figure out why callback_query_handler doesn't work. I can see from the print that it doesn’t even enter its function, although if you print callback_data=i[1], you can see that the condition is met. It's just that the numbers are transferred from the database.

@bot.message_handler(commands=['get'])
def get_from_my_list(message):

    with sqlite3.connect('users_data.db',check_same_thread=False) as db:
        cursor = db.cursor()
        user_id = message.from_user.id
        query = """SELECT wish, id FROM wishes WHERE user_id = {}""".format(user_id)
        cursor.execute(query)
        data = cursor.fetchall()
        print(data)
        if len(data) == 0:
            bot.send_message(message.chat.id, 'Ваш список пустой')

        for i in data:
            keyboard_del = types.InlineKeyboardMarkup(row_width=1)
            button = types.InlineKeyboardButton("Удалить {}".format(i[1]), callback_data=i[1])
            keyboard_del.add(button)
            bot.send_message(message.chat.id, i,reply_markup=keyboard_del)
            print(i[1])


        @bot.callback_query_handler(func=lambda call: True)
        def answer(call):
            message = call.message
            print(call.data)
            query = """DELETE FROM wishes WHERE user_id = {user_id} AND id = {key}""".format(user_id=user_id,key=int(call.data))
            cursor.execute(query)
            db.commit()
            bot.answer_callback_query(call.id)
            send = bot.send_message(call.message.chat.id, 'Удалено')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
satway, 2021-11-14
@satway

in general, comrades, the mistake was banal - I forgot that I declared a similar handler at a higher level in the code, so it worked. Yes, the code is working. I thought for a long time, but the doper only when I wrote here.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question