如何从带有url的内联按钮获取CallbackQuery



我定义了一个键盘:

keyboard = InlineKeyboardMarkup(
inline_keyboard=[
[
InlineKeyboardButton(text=MOVIE_WATCH_BTN, callback_data=MOVIE_WATCH_CB, url=VIEWING_LINK)
]
]
)

现在我想抓住按下这个按钮的时刻:

@dp.callback_query_handler(text=[MOVIE_WATCH_CB])
async def process_movie_watch(call: types.CallbackQuery):
pass

但当我在电报中按下这个按钮时,这个方法从未被调用过。然后我尝试使用这样的方法:

@dp.callback_query_handler(lambda query: True)
async def inline_checkout(call: types.CallbackQuery):
pass

它也不叫。尽管其他没有链接的按钮工作正常。问题似乎出在链接上,也许还有其他方法可以抓住按下链接按钮的时刻?

这是整个代码:

from aiogram import Bot, Dispatcher
from aiogram import types
from aiogram import executor
from aiogram.types import (
InlineKeyboardMarkup, 
InlineKeyboardButton
)
bot = Bot(token="BotToken")
dp = Dispatcher(bot)
watch_cb = "watch_cb"
second_cb = "second_cb"
@dp.message_handler(commands=['start'])
async def cmd_start(message: types.Message):
await message.answer("The bot is started!", reply_markup=get_inline_keyboard())
def get_inline_keyboard():
return InlineKeyboardMarkup(
inline_keyboard=[
[
InlineKeyboardButton(text="Watch", callback_data=watch_cb, url="https://google.com"),
InlineKeyboardButton(text="Second Button", callback_data=second_cb)
]
]
)
@dp.callback_query_handler(text=watch_cb)
async def process_watch_btn(call: types.CallbackQuery):
print("ncall = process_watch_btn()n")

@dp.callback_query_handler(text=second_cb)
async def process_second_btn(call: types.CallbackQuery):
print("ncall = process_second_btn()n")

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

提前感谢

试试这个:

from aiogram.utils.callback_data import CallbackData
cb_inline = CallbackData("post", 'action', "data")
keyboard = InlineKeyboardMarkup(
inline_keyboard= [
InlineKeyboardButton(text='MyText', callback_data=cb_inline.new(action='myact', data='MyText')
]
)

和:

@dp.callback_query_handler(cb_inline.filter(action='myact'))
async def choice_registr_method(call: CallbackQuery, callback_data):
pass

相关内容

  • 没有找到相关文章

最新更新