Z
Z
zenondd2022-02-11 14:06:02
Python
zenondd, 2022-02-11 14:06:02

How to update SQLite table by pressing inline button in telegram bot?

I can not force the bot to access the database in any way by pressing the inline button. The function responsible for processing the inline keyboard is executed, but the lines responsible for the database are simply skipped. At the same time, if the same function is called with a regular keyboard, then everything works as it should. What and where am I missing?

Here is the part of the code in question:

from aiogram import Bot, types, executor, Dispatcher
import sqlite3
import logging

TOKEN = "_TOKEN_"
bot = Bot(token=TOKEN)
dp = Dispatcher(bot)
logging.basicConfig(level=logging.INFO)

@dp.message_handler(commands=['show']) 
async def test(message: types.Message):
    key = types.InlineKeyboardMarkup()
    b1 = types.InlineKeyboardButton(text='Test it', callback_data='testit')
    key.row(b1)
    await message.answer('Take a chance', reply_markup=key)

@dp.callback_query_handler(text='testit')
async def update(call: types.CallbackQuery): #вот эта функция не выполняется полностью
    uid = call.message.from_user.id
    conn = sqlite3.connect('db/gecko.db', check_same_thread=False)
    cursor = conn.cursor()
    cursor.execute('UPDATE table_name SET column = ? WHERE user_id = ?', ('some_text', uid))
    conn.commit()
    cursor.close()
    print('ok')

@dp.message_handler(content_types=['text']) 
async def sl(msg: types.Message): #эта функция работает прекрасно и вносит все изменения в таблицу
    if msg.text == 'sl':
        uid = msg.from_user.id
        conn = sqlite3.connect('db/gecko.db', check_same_thread=False)
        cursor = conn.cursor()
        cursor.execute('UPDATE table_name SET column = ? WHERE user_id = ?', ('some_text', uid))
        conn.commit()
        cursor.close()
        print('ok')

if __name__ == "__main__":
    executor.start_polling(dp, skip_updates=True)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Bl4ckm45k, 2022-02-12
@Bl4ckm45k

Replace
@dp.callback_query_handler(text='testit')
with

@dp.callback_query_handler(lambda c: c.data == 'testit')

Your decorator was correct for tracking KeyboardButton , but not for InlineKeyboardButton

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question