inLineKeyboardButton 在用户点击时在哪里响应
Where does the inLineKeyboardButton respond when the user clicks on it
enter def start(update: Update, context: CallbackContext) -> None:
"""Sends a message with three inline buttons attached."""
keyboard = [
[
InlineKeyboardButton("Option 1", callback_data='1'),
InlineKeyboardButton("Option 2", callback_data='2'),
],
[InlineKeyboardButton("Option 3", callback_data='3')],
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.message.reply_text('Please choose:', reply_markup=reply_markup) here
我知道'Option 1'是按钮显示的名字,callback_data是回调进来的数据,但是·我不知道这个InlineKeyboardButton会在哪里响应按下事件,或者这个callback_data会在哪个函数中被传递去执行?
您需要使用处理程序来获取回调数据
updater = Updater(token, use_context=True)
dp = updater.dispatcher
dp.add_handler(CallbackQueryHandler(c_back_respons))
那么你可以使用这样的函数
def c_back_respons(update: Update, context: CallbackContext):
call_back_data = update.callback_query.data
if call_back_data in ("1"):
function(update)
enter def start(update: Update, context: CallbackContext) -> None:
"""Sends a message with three inline buttons attached."""
keyboard = [
[
InlineKeyboardButton("Option 1", callback_data='1'),
InlineKeyboardButton("Option 2", callback_data='2'),
],
[InlineKeyboardButton("Option 3", callback_data='3')],
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.message.reply_text('Please choose:', reply_markup=reply_markup) here
我知道'Option 1'是按钮显示的名字,callback_data是回调进来的数据,但是·我不知道这个InlineKeyboardButton会在哪里响应按下事件,或者这个callback_data会在哪个函数中被传递去执行?
您需要使用处理程序来获取回调数据
updater = Updater(token, use_context=True)
dp = updater.dispatcher
dp.add_handler(CallbackQueryHandler(c_back_respons))
那么你可以使用这样的函数
def c_back_respons(update: Update, context: CallbackContext):
call_back_data = update.callback_query.data
if call_back_data in ("1"):
function(update)