Answer the question
In order to leave comments, you need to log in
Aogram Why is the bot not responding to the message?
The bot does not respond to the message, I think that this is due to the fact that the handler in the code is at the very end, can I fix this somehow?
from aiogram import types, executor, Dispatcher, Bot
import sqlite3
token = ''
bot = Bot(token=token)
dp = Dispatcher(bot)
with sqlite3.connect("data.db") as db:
cursor = db.cursor()
cursor.execute("""CREATE TABLE IF NOT EXISTS users(
chat_id TEXT not null ,
username TEXT not null ,
balance BIGINT
)""")
@dp.message_handler(commands=['start'])
async def start(message: types.Message):
cursor.execute(f"SELECT chat_id FROM users WHERE chat_id = '{message.chat.id}'")
if cursor.fetchone() is None:
cursor.execute(f"INSERT INTO users VALUES(?,?,?)", (message.chat.id, message.from_user.username, 0)) #
await message.answer("База данных обновлена!")
print("Данные занесены в таблицу")
db.commit()
@dp.message_handler(content_types=['text'])
async def profile(msg: types.Message):
if msg.text.lower() == "профиль":
cursor.execute("SELECT * FROM users WHERE chat_id = '{msg.chat.id}'")
for value in cursor.execute(f"SELECT * FROM users WHERE chat_id = '{msg.chat.id}'"):
print(value[0], value[1], value[2])
await msg.answer(f"id: " + value[0] + "\n" + "Имя: " + value[1] + "\n" + "Баланс: " + str(value[2]))
@dp.message_handler(content_types=['text'])
async def balance(msg: types.Message):
if msg.text.lower() == "баланс":
for value in cursor.execute(f"SELECT balance FROM users WHERE chat_id = {msg.chat.id}"):
await msg.answer(f"Твой баланс: {value[0]}")
executor.start_polling(dp, skip_updates=True)
Answer the question
In order to leave comments, you need to log in
Yes, it's because "at the end". Handlers are sorted sequentially, and as soon as a suitable one is found, the rest are not used. And here are two handlers with the same decorator, naturally, the first one will be applied, and the second one will no longer be used.
Either process both commands in the same function, or filter the decorator so that it applies only to specific messages.
The condition in the decorator will allow the command to be used as /balance
.commands=["баланс"]
@dp.message_handler(lambda message: message.text and (message.text.lower() == "баланс"))
would do the same without the / before the word "balance". And in general, more complex checks can be done this way.
All is well, but why is the asynchronous aiogram being used, and the non-asynchronous sqlite3?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question