我正在尝试在电报机器人中实现数据库信息的通常输出。如果我通过回调来做到这一点,它就不起作用。如果我通过命令执行此操作,则一切正常。你建议我做什么?
这里我通过 /profile 命令使用输出
async def profile_info(message, reply, user_id):
userid = cur.execute("SELECT * FROM accounts WHERE tg_id = ?", (user_id,)).fetchone()
if userid:
await message.answer('👤 Profile\n\n'
f'Card: {userid[2]}\n'
'Deals: 0', reply_markup=reply)
db.commit()
@cmds_router.message(F.text, Command('profile'))
async def profileGet(message: Message):
await profile_info(message, backkb.profile_back_kb, message.from_user.id)
但是如果我通过回调得出这个结论,那就不行了
async def profile_info(callback, reply, user_id):
userid = cur.execute("SELECT * FROM accounts WHERE tg_id = ?", (user_id,)).fetchone()
if userid:
await callback.message.answer('👤 Profile\n\n'
f'Card: {userid[2]}\n'
'Deals: 0', reply_markup=reply)
db.commit()
@router_main_handler.callback_query(F.data == 'profile_menu_cb')
async def profile_menu_cb(callback: CallbackQuery):
await profile_info(callback, backkb.profile_back_kb, callback.message.from_user.id)
首先,您需要应答回调,以便电报了解您正在处理此回调。 所以代码可能如下所示:
async def profile_info(callback, reply, user_id):
await callback.answer()
userid = cur.execute("SELECT * FROM
accounts WHERE tg_id = ?", (user_id,)).fetchone()
if userid:
await callback.message.answer('👤 Profile\n\n'
f'Card: {userid[2]}\n'
'Deals: 0', reply_markup=reply)
db.commit()