如何处理电报机器人的InlineKeyboardButtons的回调



如何处理内联键盘的回调?我试着在这里查看有关这个主题的文档,但没有找到任何可以帮助我解决这个问题的东西。

以下是我想要实现的,如果用户按下任何按钮,机器人就会知道按下了哪个按钮,并对其进行处理。

from django_tgbot.decorators import processor
from django_tgbot.state_manager import message_types, update_types, state_types
from django_tgbot.types.update import Update
from ..bot import state_manager
from ..models import TelegramState
from ..bot import TelegramBot

from django_tgbot.types.inlinekeyboardbutton import InlineKeyboardButton
from django_tgbot.types.inlinekeyboardmarkup import InlineKeyboardMarkup
from django_tgbot.types.keyboardbutton import KeyboardButton
from django_tgbot.types.replykeyboardremove import ReplyKeyboardRemove
from django_tgbot.types.replykeyboardmarkup import ReplyKeyboardMarkup

@processor(state_manager,success='asked_to_select_main_menu_options')
def main_manu_options(bot: TelegramBot, update: Update, state: TelegramState):
chat_id = update.get_chat().get_id()
bot.sendMessage(
chat_id,
text='What would you like to do?',
reply_markup=InlineKeyboardMarkup.a(
inline_keyboard=[
[
InlineKeyboardButton.a('Parking',callback_data='PK'),
],
[
InlineKeyboardButton.a('SBP', callback_data='SBP')
],
[
InlineKeyboardButton.a('Land Rates',callback_data='LR')
],
[
InlineKeyboardButton.a('Rent',callback_data='R')
],
[
InlineKeyboardButton.a('Bill Payment',callback_data='B')
]
]
)
)

应该用InlineKeyboardButton的结果处理Update的处理器正在调用CallbackQuery,您应该将其作为参数添加到update_type中,请参见示例:

@processor(state_manager, from_states=state_types.All, update_types=[update_types.CallbackQuery])
def handle_callback_query(bot: TelegramBot, update, state):
callback_data = update.get_callback_query().get_data()
bot.answerCallbackQuery(update.get_callback_query().get_id(), text='Callback data received: {}'.format(callback_data))

在这里,您可以找到一个使用InlineKeyboardButton并使用django-tgbot库的bot示例。

相关内容

最新更新